Hi, I’m trying to create a shader which changes the color of the surface based on the normal and the world up. But I have no idea how to reference to world.up from the shader. So you can always tell where “down” is.
I’m using one of the example shaders as a starting point:
Properties{
_MainTex("Texture", 2D) = "white" {}
}
SubShader{
Tags{ "RenderType" = "Opaque" }
CGPROGRAM
#pragma surface surf Lambert vertex:vert
struct Input {
float2 uv_MainTex;
float3 customColor;
};
void vert(inout appdata_full v, out Input o) {
UNITY_INITIALIZE_OUTPUT(Input, o);
/* Here I would want to do something like
float x = Angle(v.normal, world.up);
o.customColor = float3 (x, x, x);
or something, I'm completely out of my depth.
*/
o.customColor = v.normal * 0.5 + .5;
/* Now it's just setting the color based on 3D vector
So walls are different color based on where they're facing
*/
}
sampler2D _MainTex;
void surf(Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D(_MainTex, IN.uv_MainTex).rgb;
o.Albedo *= IN.customColor;
}
ENDCG
}
Fallback "Diffuse"
float x = dot(v.normal, float3(0,0,1)); // returns -1 to 1 value
x = x * 0.5 - 0.5; // now 0-1
float3 color = lerp(colorA, colorB, x); // interpolate between the two colors based on the difference between the vectors.
Yes, using float3(0, 1, 0) worked, other than that jbooth’s tip was very helpful!
Now I have another question. How could I reference the objects center? As in the center of the object that the surface is part of? Because I have a planet and I have a custom gravity system pointing to the center of the planet, so I would want the colors changing in regards to the center of the object.
The normal is always pointing outwards relative to the surface (that is, if it’s been made proper that way), so taking the reverse if your normal, the Y component should point to the center.
Vertex position in model space is probably one answer to your question. The distance of a given vertex position to the models origin divided by the radius of your planet results in a fairly usable gradient that you can map to different colors.