- Home /
Unexpected behavior in Vertex/Fragment shader when using saturate method (CG)
Im learning shader writing lately and now im trying to write a simple snow shader which blends 2 textures based on the snowdirection. At the moment, the snowmask looks like i want it to (black and white mask in unity viewport), but when i apply a saturation method to it, it seems all pure black pixels in the shadows turn white. I dont understand why because in my understanding, the saturation method should not make dark pixels (<= 0) to white (1). What is happening?
Shader "Simple Snow Shader2"
{
Properties
{
_MainTex("Base (RGB)", 2D) = "white" {}
_SnowDirection("Snow Direction", Vector) = (0,1,0)
_SnowFalloff("Snow Falloff", float) = 1.0
_SnowTiling("Snow Tiling", float) = 1.0
}
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
float4 _SnowDirection;
float _SnowFalloff;
float _SnowTiling;
struct vertexInput
{
float4 vertex : POSITION;
float4 tangent : TANGENT;
float4 uv : TEXCOORD0;
float3 normal : NORMAL;
fixed4 color : COLOR;
};
struct vertexOutput
{
float4 pos : SV_POSITION;
float4 col : TEXCOORD0;
float4 worldnormal : NORMAL;
float2 uv : TEXCOORD1;
};
vertexOutput vert(vertexInput input)
{
vertexOutput output;
output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
float4 worldnormal = mul(_Object2World, float4(input.normal, 0.0));
output.worldnormal = worldnormal;
output.col = input.color;
return output;
}
float4 frag(vertexOutput input) : COLOR
{
float4 diffuse = tex2D(_MainTex, input.uv);
float4 snowmask = pow(dot(input.worldnormal, _SnowDirection), _SnowFalloff);
//input.col = saturate(snowmask);
input.col = (snowmask);
return input.col;
}
ENDCG
}
}
}
I think i found it. In this line:
float4 snowmask = pow(dot(input.worldnormal, _SnowDirection), _SnowFalloff
the dot product gives a range of -1 to 1 while the power function makes all negative values positive. I guess that's the reason i get white results in the shadow areas.
Your answer
Follow this Question
Related Questions
Prevent ColorMask obscuring parts of an object's mesh 0 Answers
Stop shader values from being clamped 1 Answer
Refraction Shader. Strange Artifacts 0 Answers
Why doesn't this vertex+fragment shader work? 0 Answers
Blend textures in shader 0 Answers