Last active
April 4, 2018 05:01
-
-
Save JohnnyBeluga/9bd5c13a2b37706852a5 to your computer and use it in GitHub Desktop.
Basic triangle Unity ShaderLab Shader; Tessellates a triangle mesh and colors it red.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Tesselation/Triangle_Tesselation" { | |
Properties { | |
_TessEdge ("Edge Tess", Range(1,64)) = 2 | |
} | |
SubShader { | |
Pass { | |
CGPROGRAM | |
#pragma target 5.0 | |
#pragma vertex VS | |
#pragma fragment PS | |
#pragma hull HS | |
#pragma domain DS | |
#pragma enable_d3d11_debug_symbols | |
//#include "UnityCG.cginc" | |
float _TessEdge; | |
struct VS_Input | |
{ | |
float3 vertex : POSITION; | |
}; | |
struct HS_Input | |
{ | |
float4 pos : POS; | |
}; | |
struct HS_ConstantOutput | |
{ | |
float TessFactor[3] : SV_TessFactor; | |
float InsideTessFactor : SV_InsideTessFactor; | |
}; | |
struct HS_ControlPointOutput | |
{ | |
float3 pos : POS; | |
}; | |
struct DS_Output | |
{ | |
float4 pos : SV_Position; | |
}; | |
struct FS_Input | |
{ | |
float4 pos : SV_Position; | |
}; | |
struct FS_Output | |
{ | |
fixed4 color : SV_Target0; | |
}; | |
HS_Input VS( VS_Input Input ) | |
{ | |
HS_Input Output; | |
Output.pos = float4(Input.vertex, 1); | |
return Output; | |
} | |
HS_ConstantOutput HSConstant( InputPatch<HS_Input, 3> Input ) | |
{ | |
HS_ConstantOutput Output = (HS_ConstantOutput)0; | |
Output.TessFactor[0] = Output.TessFactor[1] = Output.TessFactor[2] = _TessEdge; | |
Output.InsideTessFactor = _TessEdge; | |
return Output; | |
} | |
[domain("tri")] | |
[partitioning("integer")] | |
[outputtopology("triangle_cw")] | |
[patchconstantfunc("HSConstant")] | |
[outputcontrolpoints(3)] | |
HS_ControlPointOutput HS( InputPatch<HS_Input, 3> Input, uint uCPID : SV_OutputControlPointID ) | |
{ | |
HS_ControlPointOutput Output = (HS_ControlPointOutput)0; | |
Output.pos = Input[uCPID].pos.xyz; | |
return Output; | |
} | |
[domain("tri")] | |
DS_Output DS( HS_ConstantOutput HSConstantData, | |
const OutputPatch<HS_ControlPointOutput, 3> Input, | |
float3 BarycentricCoords : SV_DomainLocation) | |
{ | |
DS_Output Output = (DS_Output)0; | |
float fU = BarycentricCoords.x; | |
float fV = BarycentricCoords.y; | |
float fW = BarycentricCoords.z; | |
float3 pos = Input[0].pos * fU + Input[1].pos * fV + Input[2].pos * fW; | |
Output.pos = mul (UNITY_MATRIX_MVP, float4(pos.xyz,1.0)); | |
return Output; | |
} | |
FS_Output PS( FS_Input I ) | |
{ | |
FS_Output Output; | |
Output.color = fixed4(1, 0, 0, 1); | |
return Output; | |
} | |
ENDCG | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment