Vertex animation

I made a shader with transparency, two sided, and a separate specular texture.
And I would like to animate the mesh (for palm leaves), but it does not work …

The shader :

Shader "LC Island Pack / Leaves" {

	Properties {
		_Color ("Main Color", Color) = (1,1,1,1)
		_SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 0)
		_Shininess ("Shininess", Range (0.01, 1)) = 0.078125
		_MainTex ("Base (RGB) TransGloss (A)", 2D) = "white" {}
		_BumpMap ("Normalmap", 2D) = "bump" {}
		_SpecMap ("SpecularMap", 2D) = "white" {}
		_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
	}
	
	SubShader {
		UsePass "Transparent/Cutout/DiffuseBack/FORWARD"
	  	UsePass "Transparent/Cutout/Diffuse/FORWARD"
		Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
		LOD 400
		
		CGPROGRAM
		#pragma surface surf BlinnPhong alphatest:_Cutoff
		#pragma exclude_renderers flash
		
		sampler2D _MainTex;
		sampler2D _SpecMap;
		sampler2D _BumpMap;
		fixed4 _Color;
		half _Shininess;
		
		struct Input {
			float2 uv_MainTex;
			float2 uv_SpecMap;
			float2 uv_BumpMap;
		};
		
		
		void surf (Input IN, inout SurfaceOutput o) {
			fixed4 difTex = tex2D(_MainTex, IN.uv_MainTex);
			fixed4 specTex = tex2D(_SpecMap, IN.uv_SpecMap);
			o.Albedo = difTex.rgb * _Color.rgb;
			o.Gloss = specTex.rgb * _SpecColor.rgb;
			o.Alpha = difTex.a * _Color.a;
			o.Specular = _Shininess;
			o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
		}
		
		
		ENDCG
	
	}

	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200
		
		Pass {
			CGPROGRAM
				#pragma vertex vert
				#pragma fragment frag
				#include "UnityCG.cginc"
			
				float4 Wind;
				
				struct v2f {
					float4 pos : SV_POSITION;
					float2 uv_MainTex : TEXCOORD0;
				};
				
				inline float4 AnimateLeaves(float4 pos, float3 normal, float animParams){
					pos.xyz += animParams * Wind.xyz * Wind.w; 
					return pos;
				}
				
				float4 _MainTex_ST;
			
				v2f vert(appdata_full v) {
					v2f o;
					float4	windParams	= float(v.color.b);		
					float4 mdlPos = AnimateLeaves(v.vertex, v.normal, windParams);
					o.pos = mul(UNITY_MATRIX_MVP,mdlPos);
					o.uv_MainTex = TRANSFORM_TEX(v.texcoord, _MainTex);
					return o;
				}

				sampler2D _MainTex;
			
				float4 frag(v2f IN) : COLOR {
					half4 c = tex2D (_MainTex, IN.uv_MainTex);
					return c;
				}
			

			ENDCG
		}
	}


FallBack "Transparent/Cutout/VertexLit"
}

Script for the wind :

using UnityEngine;
using System.Collections;

public class IslandPackSetup : MonoBehaviour {
	
	public Vector4 Wind = new Vector4(0.5f,0.5f,0.5f,0.5f);
	
	
	void Start () {
		Application.targetFrameRate = 60;
		Shader.SetGlobalColor("_Wind", Wind);
	}
	
}

An idea of the problem?

You are setting the global colour “_Wind”, which is never referenced in your shader. Either use the leading underscore in both places, or remove it from both.

But I reference in the script.

public Vector4 Wind = new Vector4(0.5f,0.5f,0.5f,0.5f);
	
	
	void Start () {
		Application.targetFrameRate = 60;
		Shader.SetGlobalColor("_Wind", Wind);
	}

This is not good?

Shader "LC Island Pack / Leaves" {

	Properties {
		_Color ("Main Color", Color) = (1,1,1,1)
		_SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 0)
		_Shininess ("Shininess", Range (0.01, 1)) = 0.078125
		_MainTex ("Base (RGB) TransGloss (A)", 2D) = "white" {}
		_BumpMap ("Normalmap", 2D) = "bump" {}
		_SpecMap ("SpecularMap", 2D) = "white" {}
		_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
	}
	
	SubShader {
		UsePass "Transparent/Cutout/DiffuseBack/FORWARD"
	  	UsePass "Transparent/Cutout/Diffuse/FORWARD"
		Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
		LOD 400
		
		CGPROGRAM
		#pragma surface surf BlinnPhong alphatest:_Cutoff
		#pragma exclude_renderers flash
		
		sampler2D _MainTex;
		sampler2D _SpecMap;
		sampler2D _BumpMap;
		fixed4 _Color;
		half _Shininess;
		
		struct Input {
			float2 uv_MainTex;
			float2 uv_SpecMap;
			float2 uv_BumpMap;
		};
		
		
		void surf (Input IN, inout SurfaceOutput o) {
			fixed4 difTex = tex2D(_MainTex, IN.uv_MainTex);
			fixed4 specTex = tex2D(_SpecMap, IN.uv_SpecMap);
			o.Albedo = difTex.rgb * _Color.rgb;
			o.Gloss = specTex.rgb * _SpecColor.rgb;
			o.Alpha = difTex.a * _Color.a;
			o.Specular = _Shininess;
			o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
		}
		
		
		ENDCG
	
	}

	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200
		
		Pass {
			CGPROGRAM
				#pragma vertex vert
				#pragma fragment frag
				#include "UnityCG.cginc"
				
				float4 _Wind;
				
				struct v2f {
					float4 pos : SV_POSITION;
					float2 uv_MainTex : TEXCOORD0;
				};
				
				inline float4 AnimateLeaves(float4 pos, float3 normal, float animParams){
					pos.xyz += animParams * _Wind.xyz * _Wind.w; 
					return pos;
				}
				
				float4 _MainTex_ST;
			
				v2f vert(appdata_full v) {
					v2f o;
					float4	windParams	= float(v.color.b);		
					float4 mdlPos = AnimateLeaves(v.vertex, v.normal, windParams);
					o.pos = mul(UNITY_MATRIX_MVP,mdlPos);
					o.uv_MainTex = TRANSFORM_TEX(v.texcoord, _MainTex);
					return o;
				}

				sampler2D _MainTex;
			
				float4 frag(v2f IN) : COLOR {
					half4 c = tex2D (_MainTex, IN.uv_MainTex);
					return c;
				}
			

			ENDCG
		}
	}


FallBack "Transparent/Cutout/VertexLit"
}

Sorry, I had not seen!
But it still doesn’t work

You’ve got two different subshaders there (which itself is using two other passes from another shader before it gets to the passes your Surface Shader defines).

It’s going to use the first one because it can run on your graphics card (it will go through them from first to last/top to bottom and will use the first subshader your system can run).

So essentiall your custom shader will never actually be displayed.

So I have to put everything in one subshader?

You’ll need to integrate the vertex movement part of your shader with your main shader, yes.

See here for examples on adding your own vertex data to surface shaders; Unity - Manual: Surface Shader examples

Sorry, I really problems with the shaders: (

Shader "LC Island Pack / Leaves" {

	Properties {
		_Color ("Main Color", Color) = (1,1,1,1)
		_SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 0)
		_Shininess ("Shininess", Range (0.01, 1)) = 0.078125
		_MainTex ("Base (RGB) TransGloss (A)", 2D) = "white" {}
		_BumpMap ("Normalmap", 2D) = "bump" {}
		_SpecMap ("SpecularMap", 2D) = "white" {}
		_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
	}
	
	SubShader {
		UsePass "Transparent/Cutout/DiffuseBack/FORWARD"
	  	UsePass "Transparent/Cutout/Diffuse/FORWARD"
		Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
		LOD 400
		
		CGPROGRAM
		#pragma surface surf Lambert
		#pragma vertex vert
		#pragma fragment frag
		#include "UnityCG.cginc"
				
		sampler2D _MainTex;
		sampler2D _SpecMap;
		sampler2D _BumpMap;
		fixed4 _Color;
		half _Shininess;
		
		float4 _Wind;
		
		struct Input {
			float2 uv_MainTex;
			float2 uv_SpecMap;
			float2 uv_BumpMap;
		};
		
		void surf (Input IN, inout SurfaceOutput o) {
			fixed4 difTex = tex2D(_MainTex, IN.uv_MainTex);
			fixed4 specTex = tex2D(_SpecMap, IN.uv_SpecMap);
			o.Albedo = difTex.rgb * _Color.rgb;
			o.Gloss = specTex.rgb * _SpecColor.rgb;
			o.Alpha = difTex.a * _Color.a;
			o.Specular = _Shininess;
			o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
		}
		
		struct v2f {
					float4 pos : SV_POSITION;
					float2 uv_MainTex : TEXCOORD0;
				};
				
				inline float4 AnimateLeaves(float4 pos, float3 normal, float animParams){
					pos.xyz += animParams * _Wind.xyz * _Wind.w; 
					return pos;
				}
				
				float4 _MainTex_ST;
			
				v2f vert(appdata_full v) {
					v2f o;
					float4	windParams	= float(v.color.b);		
					float4 mdlPos = AnimateLeaves(v.vertex, v.normal, windParams);
					o.pos = mul(UNITY_MATRIX_MVP,mdlPos);
					o.uv_MainTex = TRANSFORM_TEX(v.texcoord, _MainTex);
					return o;
				}

				sampler2D _MainTex;
			
				float4 frag(v2f IN) : COLOR {
					half4 c = tex2D (_MainTex, IN.uv_MainTex);
					return c;
				}
			
			
		ENDCG
	
	}

}

I have two errors:

  • line 19 : #pragma surface does not allow specifying otehr programs at line 1ç
  • Parse error at line 81

Surface shader will generate its own passes with vertex+fragment shader,
I think It will take few days for you to understand all this in unity, good luck with you,:wink:

you can try #pragma debug in defualt surface shader

See below and trust you can see how.

http://forum.unity3d.com/threads/150482-Rotation-of-Texture-UVs-directly-from-a-shader

Just modify and shader only move vert.if you want use script to interact,just do it in script as usual.

Shader "LC Island Pack / Leaves" {
     Properties {
        _Color ("Main Color", Color) = (1,1,1,1)
        _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 0)
        _Shininess ("Shininess", Range (0.01, 1)) = 0.078125
        _MainTex ("Base (RGB) TransGloss (A)", 2D) = "white" {}
        _BumpMap ("Normalmap", 2D) = "bump" {}
        _SpecMap ("SpecularMap", 2D) = "white" {}
        _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    }
    
    SubShader 
    {
        Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
        LOD 400
        
        CGPROGRAM
         #pragma surface surf Lambert vertex:vert

        #include "UnityCG.cginc"
                
        sampler2D _MainTex;
        sampler2D _SpecMap;
        sampler2D _BumpMap;
        fixed4 _Color;
        half _Shininess;
        
        float4 _Wind;
        
        struct Input {
            float2 uv_MainTex;
            float2 uv_SpecMap;
            float2 uv_BumpMap;
        };
        
        void vert(inout appdata_full v)
        {
            v.vertex.xy += abs(sin(_Time.yy ));
        }
        
        void surf (Input IN, inout SurfaceOutput o) {
            fixed4 difTex = tex2D(_MainTex, IN.uv_MainTex);
            fixed4 specTex = tex2D(_SpecMap, IN.uv_SpecMap);
            o.Albedo = difTex.rgb * _Color.rgb;
            o.Gloss = specTex.rgb * _SpecColor.rgb;
            o.Alpha = difTex.a * _Color.a;
            o.Specular = _Shininess;
            o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
        }
            
      ENDCG
    
    }
 
}