Skip to content

Instantly share code, notes, and snippets.

@sugi-cho
Last active August 25, 2019 17:52
Show Gist options
  • Save sugi-cho/cccfb063efdcbbaed0289b348f1b3e17 to your computer and use it in GitHub Desktop.
Save sugi-cho/cccfb063efdcbbaed0289b348f1b3e17 to your computer and use it in GitHub Desktop.
//http://esprog.hatenablog.com/entry/2016/10/17/184146
Shader "Unlit/Unlit-Tess"{
Properties{
_TessFactor("Tess Factor",Vector) = (2,2,2,2)
_DispPhong ("Disp Phong",Range(0,1)) = 0
}
SubShader{
Pass{
CGPROGRAM
#pragma vertex VS
#pragma fragment FS
#pragma hull HS
#pragma domain DS
#define INPUT_PATCH_SIZE 3
#define OUTPUT_PATCH_SIZE 3
uniform vector _TessFactor;
float _DispPhong;
struct appdata {
float4 w_vert:POSITION;
float3 normal:NORMAL;
};
struct v2h {
float4 pos:POS;
float3 normal:NORMAL;
};
struct h2d_main {
float3 pos:POS;
float3 normal:NORMAL;
};
struct h2d_const {
float tess_factor[3] : SV_TessFactor;
float InsideTessFactor : SV_InsideTessFactor;
};
struct d2f {
float4 pos:SV_Position;
float3 bary:TEXCOORD0;
};
struct f_input {
float4 vertex:SV_Position;
float3 bary:TEXCOORD0;
};
v2h VS(appdata i) {
v2h o = (v2h)0;
o.pos = i.w_vert;
o.normal = i.normal;
return o;
}
h2d_const HSConst(InputPatch<v2h, INPUT_PATCH_SIZE> i) {
h2d_const o = (h2d_const)0;
o.tess_factor[0] = _TessFactor.x;
o.tess_factor[1] = _TessFactor.y;
o.tess_factor[2] = _TessFactor.z;
o.InsideTessFactor = _TessFactor.w;
return o;
}
[domain("tri")]
[partitioning("integer")]
[outputtopology("triangle_cw")]
[outputcontrolpoints(OUTPUT_PATCH_SIZE)]
[patchconstantfunc("HSConst")]
h2d_main HS(InputPatch<v2h, INPUT_PATCH_SIZE> i, uint id:SV_OutputControlPointID) {
h2d_main o = (h2d_main)0;
o.pos = i[id].pos;
o.normal = i[id].normal;
return o;
}
[domain("tri")]
d2f DS(h2d_const hs_const_data, const OutputPatch<h2d_main, OUTPUT_PATCH_SIZE> i, float3 bary:SV_DomainLocation) {
d2f o = (d2f)0;
float3 pos = i[0].pos * bary.x + i[1].pos * bary.y + i[2].pos * bary.z;
float3 pp[3];
for(int n=0; n<3; n++)
pp[n] = pos - i[n].normal * (dot(pos, i[n].normal) - dot(i[n].pos, i[n].normal));
pos = _DispPhong * (pp[0] * bary.x + pp[1] * bary.y + pp[2] * bary.z) + (1.0 - _DispPhong) * pos;
o.pos = UnityObjectToClipPos(pos);
o.bary = bary;
return o;
}
float4 FS(f_input i) : SV_Target {
half3 d = fwidth(i.bary);
half3 a3 = smoothstep(half3(0,0,0), d*1.0, i.bary);
half w = 1.0 - min(min(a3.x,a3.y),a3.z);
return w;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment