Skip to content

Instantly share code, notes, and snippets.

@ChrisLowe-Takor
Created February 14, 2023 11:01
Show Gist options
  • Save ChrisLowe-Takor/1ddd86c2027d5921c321ad8fccfac165 to your computer and use it in GitHub Desktop.
Save ChrisLowe-Takor/1ddd86c2027d5921c321ad8fccfac165 to your computer and use it in GitHub Desktop.
The Hello World of HLSL shaders. Outputs a red pixel for every fragment
Shader "HelloWorldShader"
{
// Parameters passed in through Unity
Properties {
_Color ("Color", Color ) = (1, 1, 1, 1)
}
SubShader {
Tags { "RenderType"="Opaque" }
Pass {
CGPROGRAM
// Tell the complier what function is the vertex and fragment shader
#pragma vertex vert
#pragma fragment frag
// Include Unity specific functions
#include "UnityCG.cginc"
// Create a local variable for the property (note this matches name and type in the Properties above)
float4 _Color;
// Data structure the mesh. Automatically filled out by Unity
struct MeshData {
float4 vertex : POSITION; // vertex position
float2 uv : TEXCOORD0;
// Other data type annotations available;
// float2 someData1 : TEXCOORD1;
// float2 someData2 : TEXTCOORD2; // Increment for additional data, like diffuse, normal, lightmaps, etc
// float3 normal: NORMALS; // Normal of the vertex
// float4 color: COLOR;
// float4 tanget: TANGENT;
};
// Data structure for passing data from Vertex shader to Fragment shader
struct Interpolators {
//float2 uv : TEXCOORD0; // data to pass to through
float4 vertex : SV_POSITION; // clip space position
};
// The Vertex shader
Interpolators vert (MeshData v) {
Interpolators o;
o.vertex = UnityObjectToClipPos(v.vertex); // Converts local space to clip space
return o;
}
// The Fragment shader
float4 frag (Interpolators i) : SV_Target {
return _Color;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment