Couple of questions about CG Shaders...

…that I couldn’t find answers to in documentation, or with google-fu.

  1. In my vertex structure definition:
			struct v2f 
			{
				float4	pos : SV_POSITION;
				float2	uv: TEXCOORD;
			};

How can I add variables that aren’t linked to mnemonics? And is there a usable list of mnemonics anywhere?

  1. As I understand it, the ‘Vert’ part of my shader is only run once per vertex, and the ‘fragment’ section of the shader is run for every pixel (in other words, it’s the pixel shader). That being the case, how do I calculate the current UV on a texture whilst running the fragment section of the shader? At the moment, I can only really find this information for the vertex section…

Thanks in advance for any help! :slight_smile:

SB

with dx9 you’ve generally got TEXCOORD0 to TEXCOORD7 ( so 8 float4’s )

However, if your dealing with surface shaders, it depends on lighting and what your doing in the surface function, unity uses up a good amount.

anything in the structure or uniform parameters bound to a semantics that comes off the vertex shader is interpolated per pixel in the fragment shader.

theres some info here on DirectX Semantics - Win32 apps | Microsoft Learn

ok, so if, in my fragment code, i reference the uv data that comes in via the structure, it’ll be changing for every pixel?

It will be interpolated ( mixed ) based on the 3 verts on the triangle the pixel is on yea.

So if you have a triangle where the uv is

0,0
| \
|  \
|   \
0,1_1,1

when the pixel being rendered in the fragment shader is between those vertexes, you’ll be dealing with fractions. similar to Mathf.Lerp pretty much

Cool - i just didn’t know if that was the case, or functions like ‘tex2d’ worked out what the uv should be based on the vertex input. :slight_smile:

I didnt compile this, wrote it in comment. but maybe it’ll help visualize whats going on further.

#include "UnityCG.cginc"
struct VertexOutputStructure { 
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
float2 uvWave : TEXCOORD1;
};

#pragma vertex vert

VertexOutputStructure vert( appdata_full v ) {
  VertexOutputStructure o;
  o.vertex = mul(UNITY_MATRIX_MVP,v.vertex);
  o.uv = v.texcoord;
  o.uvWave.x = sin(_Time.x + o.uv.x) * 0.1 + o.uv.x;
  o.uvWave.y = cos(_Time.y + o.uv.y) * 0.1 + o.uv.y;
  return o;
}
#pragma fragment frag
sampler2D _MainTex;
float4 frag( VertexOutputStructure i ) : COLOR
{
    return ( tex2D(_MainTex,i.uv) + tex2D(_MainTex,i.uvWave) ) * 0.5;
}