Hello all,
I am learning how to do shader programming, and using this video as a base:
I am completely new to this, so apologies if I made a newbie mistake.
I got to the bit where I implemented the vertex and fragment functions. In the video, he is able to have an object with a texture and a colour overlayed. I, on the other hand, am getting an error;
syntax error: unexpected token ‘v2f’
Compiling Vertex program
Platform defines: UNITY_ENABLE_REFLECTION_BUFFERS UNITY_PBS_USE_BRDF1 UNITY_SPECCUBE_BOX_PROJECTION UNITY_SPECCUBE_BLENDING SHADER_API_DESKTOP
I have made sure all varibles are written correctly, even went as far as making a new shader in Notepad++ and saving it but my shader does not compile.
My code (there’s a bunch of comments):
Shader "MyShader/Test2"{
// Unity uses ShaderLab, consider this the frontend
// CG can be considered the backend
// Properties are like public variables
// This is just a frontend, we're not applying them to anything
Properties{
_MainTexture("Main Color (RGB) Test2!", 2D) = "white"{} // Internal reference name, public name (inspector), data type, initialising value (can be nothing)
_Color("Colour", Color) = (1,1,1,1)
}
// Can have multiple SubShader to target different platforms
SubShader{
// Pass takes data and draws them on to screen, can have multiple passes; each can do something that will interate how the object is rendered onto screen
// Each pass is a draw call, so 3 pass means 3 draw calls
Pass{
// The main thing that applies shading to objects
CGPROGRAM
// Need two functions; the Vertex and Fragment
// Similar to C/C++, we tell CGPROGRAM function identifiers
#pragma vertex vertexFunction
#pragma fragment fragmentFunction
//Can also include external files to use different functions
#include "UnityCG.cginc"
// first, We need to get data from the object to use
struct appdata {
// We pass in the values we want, such as verticies, normals, colour, uv
float4 vertex : SV_POSITION; // data type to store, variable name, the type of data i'm getting
float2 uv : TEXCOORD0;
};
// We need an object from vertex to pass onto fragment, so we store this in a struct
struct v2f {
float4 position : SV_POSITION; // SV_POSITION has to do something with working with DX platforms
float2 uv : TEXCOORD0;
};
// Take the stuff from ShaderLab and import into CG
float4 _Color;
sampler2D _MainTexture
//Vertex
//Build the object
// Return type, function name, parameters (variable, input/output?)
v2f vertexFunction (appdata IN) {
v2f OUT;
OUT.position = mul(UNITY_MATRIX_MVP, IN.vertex); // multiply (nvidia cg function)
// MVP - Model View Projection - Get the model, get the view from camera, get the projection from the camera; send this to screen
// Takes into account whether the camera is in perspective or orthographic
OUT.uv = IN.uv;
return OUT;
}
//Fragment
//Colour it in
fixed4 fragmentFunction (v2f IN) : SV_Target{
// Take the colour
float textureColor = tex2D(_MainTexture, IN.uv);
// Take the texture
return textureColor * _Color;
}
ENDCG
}
}
}
I understand that the compiler is basically saying that I am using something that is not recognised, however, I have defined “v2f” as a struct, so I am confused what I am doing wrong.
Many thanks