The ultimate tree shader?

Posted By: Stratego

The ultimate tree shader? - 07/16/13 16:34

Okay so what I’m looking to do here is have, as the title says, the ultimate tree shader. I’m relatively new to this, but I am fairly competent when it comes to scripting / programming.

What I want the shader to do: Be affected by the sun and dynamic lights, have normal bump with specular, move verts like in the animTree.fx, and have shadow map support so I can bake occlusion.

So the tree would have 2 sets of UVs, one for color and one for shadow. It would have 3 textures, one with color in RGB and transparency in alpha, one with normal map in RGB and specular map map in alpha, and one with a shadow map.

Shadow is the most important, because baked occlusion (on trees modeled correctly) would look great. I modeled the tree below to demonstrate the affect of just occlusion (the tree isn't done yet, but you get the picture). It’s not common for a tree to be upside down, so the lighting should almost always look good.



The reason I’m posting is to see if this is possible. An expert may be out there thinking: “You can’t mix X with Y!” So I figured before I started diving in I’d ask to see what people think.

Posted By: Kartoffel

Re: The ultimate tree shader? - 07/16/13 17:02

Well... I'm not an expert but what you want to do should be possible without any problems.

Little tip: for normalmapping you only need x and y (r and g) of the normalmap, the other two can be used as specular exponent (glossiness) and factor.
(that's what I usually do)

If you need any further help feel free to ask.

EDIT: I didn't see that you want to use 2 UV sets smirk ... this shouldn't be a problem but I've never worked with multiple UVs before so this could be something I can't solve. (the coordinate stuff only)
Posted By: Stratego

Re: The ultimate tree shader? - 07/16/13 17:08

Thanks Kartoffel!

Then I'll start digging in. It'll probably take me a bit to get a handle on it, but I'll share whatever I come up with when I'm done.

If anyone else has any suggestions, please feel free!
Posted By: sivan

Re: The ultimate tree shader? - 07/17/13 06:28

hi,

I have little experience in writing shaders, but about 2 weeks ago I wrote a tree shader for shade-c that handles ambient, diffuse, specular, 8 dynamic lights, and the dynamic shadowmapping of shade-c.

it was easier than I expected. I simpply used the basic tree anim shader and the sample shaders from shader workshops, and the lines from shade-c corresponding to its shadowmapping. this way you could do it what you need, there is a normalmapping shader in shader workshops too. I'm not sure baked occlusion is good or bad when you use a proper dynamic shadowmapping that supports self shadows on alpha textures, but can be useful sometimes. maybe the 2nd UV set works like in case of level entities, that utilizes texcoord1 as input to the vertex shader, and entSkin2 for a sampler to be used in the pixel shader. somebody could verify it.

it's new to me that normalmapping requires only r and g values of the normalmap, porbably it has a reason I should read about laugh
Posted By: Kartoffel

Re: The ultimate tree shader? - 07/17/13 07:12

@sivan: normalmapping works like this (when using precomputed tangents):

Normal = normalize(In.Normal + (Normal.r * In.Tangent + Normal.g * In.Binormal) * NormalmappingStrength);

...and as you can see, the blue channes isn't needed. wink
Posted By: sivan

Re: The ultimate tree shader? - 07/17/13 07:48

thanks, it is useful for me. I worked only within shade-c with normalmapping where it is done in a compex way, but finally I can see it. moreover, in shader workshops in.Normal is not used, so another good point laugh a question: is it faster to add and normalize than lerp 2 vectors?

what I still don't really understsand is why there are more methods for specular calculations, which have different final appearance. in shader workshops (and in another tutorial at rastertek.com) a reflection vector is calculated producing imo bad results, I found better to use the halfway vector method.
Posted By: txesmi

Re: The ultimate tree shader? - 07/17/13 10:04

Originally Posted By: Kartoffel
@sivan: normalmapping works like this (when using precomputed tangents):

Normal = normalize(In.Normal + (Normal.r * In.Tangent + Normal.g * In.Binormal) * NormalmappingStrength);

...and as you can see, the blue channes isn't needed. wink


That is a valid simplification/approximation when you don't mind its inaccuracy that is quite noticeable on lowpoly models with hard edges.

Geometrically, a correct coordinate system transportation totally needs the multiply of the In.Normal vector by the blue channel. Moreover, you have to normalize the three vectors since the proportional arithmetic averages passed from the vertex shader are not unit vectors.

Normal = (Normal.r * normalize(In.Tangent)) + (Normal.g * normalize(In.Binormal)) + (Normal.b * normalize(In.Normal));

The result of this operation is a vector with same length of Normal vector so there is no need of its normalization.



sorry for the offtopic xP
Posted By: sivan

Re: The ultimate tree shader? - 07/17/13 11:51

cool, thanks, there could be a thread about a collection of similar useful things to be used in shaders, because it is not so easy to start with hlsl programming...
Posted By: Kartoffel

Re: The ultimate tree shader? - 07/17/13 13:53

Originally Posted By: sivan
what I still don't really understsand is why there are more methods for specular calculations, which have different final appearance. in shader workshops (and in another tutorial at rastertek.com) a reflection vector is calculated producing imo bad results, I found better to use the halfway vector method.

Yeah, there are different methods which have different results.

I prefer blinn-phong specular lighting (looks nice and it's not very expensive).

(in case you want a couple of spec-algorithms:)
Code:
#define Specular_Phong(normal, view, light, materialPower)     pow(saturate(dot(reflect(-light, normal), view)), materialPower) // Phong Reflection Model
#define Specular_BlinnPhong(normal, view, light, materialPower)     pow(saturate(dot(normal, normalize(light + view))), materialPower * 4) // Blinn (Phong) Reflection  Model
#define Specular_TorranceSparrow(normal, view, light, materialPower)     exp(-2 * materialPower * pow(acos(saturate(dot(normal, normalize(light + view)))), 2)) // Blinn (Torrance-Sparrow/Gauss) Reflection  Model
#define Specular_TrowbridgeReitz(normal, view, light, materialPower)     pow(1 / (1 + (1 - pow(saturate(dot(normal, normalize(light + view))), 2)) * materialPower), 2) // Blinn (Trowbridge-Reitz) Reflection  Model
#define Specular_Lyon(normal, view, light, materialPower)     pow(1 - saturate(dot(normalize(light + view) - normal, normalize(light + view) - normal) * materialPower / 2), 3) // Lyon/Blinn halfway method

normal = surface normal; view = direction vec from the fragment pos to the camera; light = direction vec from the fragment pos to the light; material power = glossiness / spec exponent (needs to be modified a bit)
(all vectors have to be normalized)

Edit: @txesmi: oops, I didn't know that smirk
However, calculating the blue channel shouldn't be a problem ;P
(I think this works: z = sqrt(1 - x * x - y * y);)
Posted By: sivan

Re: The ultimate tree shader? - 07/17/13 14:41

thanks!!
Posted By: txesmi

Re: The ultimate tree shader? - 07/17/13 15:07

thanks too! Definitively those functions go to my shader function library grin

The blue channel of a normalmap can be easily computed based in the relation between sine and cosine or Pythagorean trigonometric identity:

Originally Posted By: Pythagoras

pow ( sin A, 2 ) + pow ( cos A, 2 ) == 1
sin A == sqrt ( 1 - pow ( cos A, 2 ) )
cos A == sqrt ( 1 - pow ( sin A, 2 ) )

Code:
normal.b = sqrt ( 1 - ( normal.r * normal.r ) - ( normal.g * normal.g ) );



Of course, it will be always positive.
Posted By: Stratego

Re: The ultimate tree shader? - 07/17/13 17:59

@Malice

I didn't actually do too much in that thread, just took advice from others and tested stuff out. From now on I will post about my tree work here exclusively, and if I do come up with some good stuff, I'll share it with the community. But understand, it may take me a while because I'm just doing it for fun.
Posted By: Superku

Re: The ultimate tree shader? - 07/17/13 22:22

I like the tree, looks nice. I doubt it will look better with normal mapping.
Posted By: Stratego

Re: The ultimate tree shader? - 07/17/13 23:40

Thanks!

We'll see. I'm going to finish up the tree. I still am not happy with it, and then start applying shaders. (The tree already is looking quite a bit better)

What I'd like to get out of normal bump with specular:

1. Bark always looks good with normal mapping.
2. I'd like to get a "wet look" with the specular applied to the leaves.

First I'm going to finish the tree and all the maps, so I can test the finished product.
© 2024 lite-C Forums