I am very new to shaders but can't seem to find an answer to my problem. To put it simple I have created a flag shader, which can apply texture, animates and should also react to light. The problem is now that the shader doesn't work with both texture and light reaction at the same time. It can animate and react to light.
I created two passes, one for the color, texture and animation, and another pass with it reacting to a directional light.
I have tried out different blending options, but nothing seems to be correct.
This is a picture of it currently:
This is my code so far, I apologize for its messiness in advance :
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
Shader "Custom/FlagShader1"{ // Shader file path in inspector
Properties{ // Properties we will be working with
_Color("Main Color", Color) = (1,1,1,1) // Property for changing color
_MainTex("Main Texture" , 2D) = "white" {} // The tecture we will manipulate
_Frequency("Frequency", Float) = 1 //The frequency of the flag , which is the number of waves
_Amplitude("Amplitude",Float) = 1 // the Amplitude of the flag, which is the depth of the waves
_Speed("Speed", Float) = 1 //Speed property, used to control how fast the flag waves.
}
Subshader {
Tags{"Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" "LightMode" = "ForwardBase"} // "Queue" Determines the rendering order.//"IgnoreProjector" set to true, means that this object will not be affected by projectors. //"RenderType" Categorizes shaders into several predefined groups
Pass{ // The number of passes. We only have one pass.
Name "Animation"
CGPROGRAM// We go from shaderLab into CG language
// use "vert" function as the vertex shader
#pragma vertex vert
// use "frag" function as the pixel (fragment) shader
#pragma fragment frag
//Global variables
uniform half4 _Color;
uniform sampler2D _MainTex;
uniform float4 _MainTex_ST;
uniform float _Frequency;
uniform float _Amplitude;
uniform float _Speed;
// vertex shader inputs
struct vertexInput {
float4 vertex : POSITION;
float4 texcoord : TEXCOORD0;
};
// vertex shader outputs- these go to the fragment shader
struct vertexOutput {
float4 pos : SV_POSITION;
float4 texcoord : TEXCOORD0;
};
// vertex shader- this is where the flag is animated
float4 vertexFlagAnim(float4 vertPos, float2 uv) {
vertPos.y = vertPos.y + sin((uv.x - (_Time.y * _Speed))*_Frequency) * (_Amplitude* uv.x);
//the axis we will animate(x) and it is moved with time.y
return vertPos;
}
// vertex shader. Runs on each vertex of the 3D model.
vertexOutput vert(vertexInput v) {
vertexOutput o;
v.vertex = vertexFlagAnim(v.vertex, v.texcoord); // The animation
o.pos = UnityObjectToClipPos(v.vertex); //Transforms a point from object space to the camera’s clip space in homogeneous coordinates.
o.texcoord.xy = (v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw);// Changing the tiling and offset.
return o;
}
half4 frag(vertexOutput i) : COLOR{
float4 col = tex2D(_MainTex, i.texcoord) * _Color; // sample texture and return it
return col;
}
ENDCG
}
Pass{
Name "Light"
Blend Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc" //Global variables
// vertex shader inputs
struct vertexInput {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
// vertex shader outputs- these go to the fragment shader
struct vertexOutput {
float4 vertex : SV_POSITION; //pos
float3 normal : NORMAL;
};
// vertex shader. Runs on each vertex of the 3D model.
vertexOutput vert(vertexInput v) {
vertexOutput o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.normal = UnityObjectToWorldNormal(v.normal);
return o;
}
fixed4 frag(vertexOutput i) : SV_Target {
return saturate(dot(normalize(i.normal), _WorldSpaceLightPos0));
}
ENDCG
}
}
}
