Convert from Cg to Unity Shader

Hi, Im a newbie at shader programming, does anyone know how to convert this Cg shader to unity shader?

//Cg

void vshader(float4 vtx_position : POSITION,
             uniform float4x4 mat_modelproj,
             uniform float4 k_scale,
	     out float4 l_position : POSITION,
             out float4 l_pos : TEXCOORD0,
             out float4 l_lp : TEXCOORD1
             )
{
  float4 position = vtx_position * k_scale;
  l_pos = mul(mat_modelproj, position);
  l_position =  l_pos;
}

void fshader(in float4 l_pos : TEXCOORD0,
             out float4 o_color:COLOR)
{
  float z = (l_pos.z / l_pos.w) * 0.5 + 0.5;
  o_color = float4(z,z,z,1);
}

thanks!

oh sorry… i just realized that we can just embed cg inside unity shader… but i still dont know where to start from though…

Here is a link to the Unity manual that should give you a working starting point: http://unity3d.com/support/documentation/Manual/ShaderTut2.html

Since ShaderLab is not known to be too well documented (correct me if this recently changed), a good/necessary way to learn is to use the existing built-in shaders as reference. They can be found here: http://forum.unity3d.com/viewtopic.php?t=2150

Another good resource is Amir’s Shader programming course:

http://unity3d.com/support/resources/unite-presentations/shader-programming-course

thanks guys, I have read some of the unity shader tutorial, however, Im still lost at some parts, now I know how to define the input to vertex program as well as output from vertex to fragment program, but what is the uniform variables? Where do i get them from?

for instance, the float4x4 mat_modelproj in the codes above, I couldnt find the corresponding examples in unity shaderlab references, is it the properties that we should define in the shader?

Shader "Test/Test1" {
Properties {
	// mat_modelproj ()
	k_scale ("K Scale", Range(0, 1)) = 0.5
}

SubShader {
    Pass {

CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_fog_exp2
#include "UnityCG.cginc"

struct v2f {
    V2F_POS_FOG;
	float4 l_pos : TEXCOORD0;
	float4 l_lp : TEXCOORD1;
};

v2f vert (appdata_base v) {
    v2f o;
    
	float4 position = v.vertex * k_scale;

	
	// l_pos = mul(mat_modelproj, position);
	l_position = l_pos;
		
    return o;
}

float4 frag (v2f i) : COLOR {
	float z = (i.l_pos / i.l_pos.w) * 0.5 + 0.5;
	
	return float4(z, z, z, 1);
}
ENDCG

    }
}
Fallback "VertexLit"
}

That was a very good starter, except for the fact he decided to use grey on a black background in Unitron, making it impossible to see the code he was discussing. :cry:

I’m not entirely sure what you are trying to achieve with the shader but here’s a version with the errors corrected (I’ve commented the changes).

Shader "Test/Test1" { 
Properties { 
	k_scale ("K Scale", Range(0, 1)) = 0.5 
} 

SubShader { 
	 	Pass { 

CGPROGRAM 
#pragma vertex vert 
#pragma fragment frag 
#pragma fragmentoption ARB_fog_exp2 
#include "UnityCG.cginc" 

float k_scale;//* declare CG vars for the inspector material properties

struct v2f { 
	float4 l_fog : V2F_POS_FOG;//* declare what to bind this built-in
										//* state variable to
										//* (it's in the "UnityCG includes" file)
	float4 l_pos : TEXCOORD0; 
	float4 l_lp : TEXCOORD1; 
	float4 position : POSITION; //* include position too
}; 

v2f vert (appdata_base v) { 
	v2f o;
	o.position = v.vertex * k_scale; 
	return o; 
} 

float4 frag (v2f i) : COLOR { 
	float z = (i.l_pos / i.l_pos.w) * 0.5 + 0.5;
	return float4(z, z, z, 1); 
} 
ENDCG 
	 } 
} 
Fallback "VertexLit" 
}

I don’t think the data structure change is correct. The V2F_POS_FOG macro is defined as

float4 pos : POSITION; float fog : FOGC

It’s a macro, not a semantic, and declares both position and fog variables. gr33nl1nk was using it correctly before.

Ah yes. Something like this?

Shader "Test/Test1" { 
Properties { 
	 // mat_modelproj () 
	 k_scale ("K Scale", Range(0, 1)) = 0.5 
} 

SubShader { 
	Pass { 

CGPROGRAM 
#pragma vertex vert 
#pragma fragment frag 
#pragma fragmentoption ARB_fog_exp2 
#include "UnityCG.cginc" 

float k_scale;//* declare CG vars for the inspector material properties

struct v2f { 
	V2F_POS_FOG;// #define float4 pos : POSITION; float fog : FOGC
	float4 l_pos : TEXCOORD0; 
	float4 l_lp : TEXCOORD1; 
}; 

v2f vert (appdata_base v) { 
	v2f o; 
	o.pos = v.vertex * k_scale;
	o.l_pos = o.pos;
	return o; 
} 

float4 frag (v2f i) : COLOR {
	float z = (i.l_pos / i.l_pos.w) * 0.5 + 0.5;
	return float4(z, z, z, 1); 
}
ENDCG 

	} 
} 
Fallback "VertexLit" 
}

Yes, that works. Although I don’t really get what this shader is for yet.

thanks guys for sharing, I have some shader programs written in Cg, so, I was just trying to put that into my game in Unity, and I was wondering how I could get those uniform variables, then I find that those variables either come from the gl state e.g. mat_modelproj (Cg) → glstate.matrix.mvp (Unity Cg), or from the application, in the Unity case I think its the Shader properties which we expose in the Unity editor…