Skip to content

Instantly share code, notes, and snippets.

@matthewholliday
Last active June 4, 2022 16:41
Show Gist options
  • Save matthewholliday/ee47cfc29bb427b83ec34e2bc70f428b to your computer and use it in GitHub Desktop.
Save matthewholliday/ee47cfc29bb427b83ec34e2bc70f428b to your computer and use it in GitHub Desktop.
Simple shader that outputs the color red.
Shader "Hello World Shader"
{
Properties
{
_Value("Value",Float) = 1.0 //Defines a custom float property called "Value"
}
SubShader
{
Tags { "RenderType"="Opaque" }
Pass
{
CGPROGRAM //STARTS THE HLSL CODE
#pragma vertex vert //Telling compiler that the vertex shader is defined by the "vert()" function.
#pragma fragment frag //Telling compiler that the fragment shader is defined by the "frag()" function.
#include "UnityCG.cginc" //Imports unity-specific shader functions.
struct appdata //Better name: "TEXTURE". Contains per-vertex mesh data.
{
float4 vertex : POSITION; // Vertex position.
float4 normals : NORMAL;
//float4 color: color; //RGBA colors.
//float4 tanget
float4 uv0 : TEXCOORD0; //uv0 diffuse/normal map textures
};
//Everthing that is passed from the vertex shader to the fragment shader has to exist
//inside of this struct.
struct v2f //Better name "Interpolators"
{
//float2 uv : TEXCOORD0; //You can write anything to these channels, doesn't have to be "UV"
float4 vertex : SV_POSITION; //Clip-space position
};
float _Value; //Need a variable to match the _Value property in the properties section. Automatically sets the value of the property.
//Defines what data will be interpolated.
v2f vert (appdata v)
{
v2f o;
//Converting local space to clip space.
//If we didn't convert here, the shader image would be "stuck" to the camera.
//This should be omiatted if you're doing a postprocessing effect like a vignette.
o.vertex = UnityObjectToClipPos(v.vertex);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
return float4(1,0,0,1); //RED COLOR
}
ENDCG //ENDS THE HLSL CODE
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment