- Home /
Dynamically generated bump map for Surface Shader?
The planet surfaces in my space game are generated by a surface shader which gets fed a number of randomly chosen textures. The shader is working really well, with uv coordinates offset by random amounts and city light textures scaled and thresholded depending on the population and size of the planet. This means I get a lot of variation with a lot less textures.
But I'd like to add bump mapping, if possible. I THINK a bump map uses the 3 coordinates (colours) to describe the direction of the normal at that UV coordinate. Id like to be able to affect the normal from within the shader (not by passing a pre-generated bump map), instead using the overall resultant brightness of the current pixel to describe if that pixel is receded slightly toward the centre of the planet sphere, or raised and prominent.
At the moment I'm using the following rough and experimental lines at the end of my 'surf' function to affect the normal of the current pixel (where _surface colour is the final _float4 colour of that pixel), but it's just turning the surface black:
fixed3 adjustedNormal = o.Normal;
adjustedNormal.x = adjustedNormal.x * _surfaceColour.r;
adjustedNormal.y = adjustedNormal.y * _surfaceColour.r;
adjustedNormal.z = adjustedNormal.z * _surfaceColour.r;
o.Normal = adjustedNormal;
My question is, how should I affect the normal of a sphere, to simulate the slight variations in height of mountains and valleys on a planet surface without passing in a pre-calculated bump map?
Any help, much appreciated, thanks.
That code wont actually change the direction of the normal. It will,however, make it have a magnitude that is NOT 1.0f, (which is usually what defines a normal vector).
If you were to do that same operation to a vertex on a sphere,centered at (0,0,0), the normals of triangles that USE this vertex WOULD be effected (assu$$anonymous$$g you recompute them).
I found THIS question.. http://answers.unity3d.com/questions/561900/get-local-position-in-surface-shader.html ...but adding in the suggested code and changing the final line to o.localPos = adjustedNormal; doesn't work. Can I re-calculate the vertex position from inside my 'surf' function?
That sounds like a job for a vertex shader, so I thought the answer was "NO". But, double checking found this: http://forum.unity3d.com/threads/u-3-0-surface-shader-vertex-shader-mixing.54264/
Your answer
Follow this Question
Related Questions
How can I fix a normalmap procedurally generated? 2 Answers
How to add Bump/Normal Map to surface shader? (CG) 0 Answers
Bump Mapping 1 Answer
How to not require normals in a surface shader 1 Answer
Changing bump map opacity. 0 Answers