A couple of beginner questions for shaders

Hi.

I’m currently trying to learn CG shader programming and I achieved some minor successes. But now I’m stuck at some point with my simple distortion effect and I don’t understand why. Now I have some questions, I hope could help me with, to understand why my shader isn’t working.

First question: I have the “_Color” property added to my shader, but I actually don’t want to change the color of my vertices. If I remove the color property, nothing is rendered at all. I don’t understand exactly why I need it, other than the color may be by default no color and completely transparent. Am I correct? I’ve not found an answer to this, but maybe this is so trivial that no one asks this :slight_smile:

Second question: I’m trying to move the vertices in the ‘vert’ function with a sinus time function. I expect to see a movement of the vertices, but nothing happens. All vertices are still at the same place (at least from what I can see in the editor). Also I tried to see a color change in the frag function, but this also doesn’t change anything.

I’m a bit helpless, so I’m thankful for every help!

Shader "DistortionTest" 
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" { }
		_Color ("Main Color", Color) = (1,1,1,1)
	}
	SubShader 
	{
		Tags 
		{ 
			"Queue" = "Opaque" 
		}

		Pass
		{
			CULL Off

			CGPROGRAM
			#pragma target 4.0
			#pragma exclude_renderers ps3 xbox360
			#pragma fragmentoption ARB_precision_hint_fastest
			#pragma vertex vert
			#pragma fragment frag

			#include "UnityCG.cginc"

			//uniform fixed4 _Color;
			uniform sampler2D _MainTex;

			struct vertexInput
			{
				float4 vertex : POSITION;
				float4 texcoord : TEXCOORD0;
			};

			struct fragmentInput
			{
				float4 pos: SV_POSITION;
				float3 color : COLOR0;
			};

			fragmentInput vert (appdata_base v) 
			{ 
			    fragmentInput o;
				o.pos.w = v.vertex.w;
				o.pos.x = _SinTime.x * v.vertex.x;
				o.pos.y = v.vertex.y;
				o.pos.z = v.vertex.z;
				o.color = float3 (1.0, 1.0, 1.0);
				return o;
			}

			half4 frag(fragmentInput i) : COLOR
			{
				return float4(_SinTime.x, 1.0, 1.0, 1.0);
			}
			ENDCG
		}
	} 
	Fallback "Transparent/Cutout/VertexLit"
}

You’re not using the _Color property in the shader you posted, so you should be able to safely remove
_Color ("Main Color", Color) = (1,1,1,1)Without a difference in behavior.

The Fragment shader isn’t using the color that is set in the vertex shader either, so
o.color = float3 (1.0, 1.0, 1.0);has no significant effect either.

You might be seeing the fallback shader “Transparent/Cutout/VertexLit” or a previous version of the shader, since there’s an error in the script you provided. “Opaque” isn’t a valid Queue value. I assume you mean

Tags{ "RenderType" = "Opaque" }

Another problem is that
o.pos.z = v.vertex.z;is causing your entire mesh to disappear.

Take a look at this page from the documentation to see basic examples of how the position is transformed using the ModelViewProjection matrix, and try modifying those examples to get the vertex displacement you’re looking for.