View difference between Paste ID: 3x5cfqRP and Rk6qwKrV
SHOW: | | - or go back to the newest paste.
1
//#ifndef TESSELLATION_CGINC_INCLUDED
2
//#define TESSELLATION_CGINC_INCLUDED
3
#if defined(SHADER_API_D3D11) || defined(SHADER_API_GLES3) || defined(SHADER_API_GLCORE) || defined(SHADER_API_VULKAN) || defined(SHADER_API_METAL) || defined(SHADER_API_PSSL)
4
    #define UNITY_CAN_COMPILE_TESSELLATION 1
5
    #   define UNITY_domain                 domain
6
    #   define UNITY_partitioning           partitioning
7
    #   define UNITY_outputtopology         outputtopology
8
    #   define UNITY_patchconstantfunc      patchconstantfunc
9
    #   define UNITY_outputcontrolpoints    outputcontrolpoints
10
#endif
11
12
struct Varyings
13
{       
14
    float3 worldPos : TEXCOORD1;
15
    float3 normal : NORMAL;
16
    float4 vertex : SV_POSITION;
17
    float2 uv : TEXCOORD0;
18
    float3 viewDir : TEXCOORD3;
19
    float fogFactor : TEXCOORD4;
20
};
21
22
float _Tess;
23
float _MaxTessDistance;
24
float _Fade;
25
26
struct TessellationFactors
27
{
28
    float edge[3] : SV_TessFactor;
29
    float inside : SV_InsideTessFactor;
30
};
31
32
struct Attributes
33
{
34
    float4 vertex : POSITION;
35
    float3 normal : NORMAL;
36
    float2 uv : TEXCOORD0;    
37
};
38
39
struct ControlPoint
40
{
41
    float4 vertex : INTERNALTESSPOS;
42
    float2 uv : TEXCOORD0;
43
    float3 normal : NORMAL;   
44
};
45
46
[UNITY_domain("tri")]
47
[UNITY_outputcontrolpoints(3)]
48
[UNITY_outputtopology("triangle_cw")]
49
[UNITY_partitioning("fractional_odd")]
50
[UNITY_patchconstantfunc("patchConstantFunction")]
51
ControlPoint hull(InputPatch<ControlPoint, 3> patch, uint id : SV_OutputControlPointID)
52
{
53
    return patch[id];
54
}
55
56
TessellationFactors UnityCalcTriEdgeTessFactors (float3 triVertexFactors)
57
{
58
    TessellationFactors tess;
59
    tess.edge[0] = 0.5 * (triVertexFactors.y + triVertexFactors.z);
60
    tess.edge[1] = 0.5 * (triVertexFactors.x + triVertexFactors.z);
61
    tess.edge[2] = 0.5 * (triVertexFactors.x + triVertexFactors.y);
62
    tess.inside = (triVertexFactors.x + triVertexFactors.y + triVertexFactors.z) / 3.0f;
63
    return tess;
64
}
65
66
float CalcDistanceTessFactor(float4 vertex, float minDist, float maxDist, float tess)
67
{
68
				float3 worldPosition = mul(unity_ObjectToWorld, vertex).xyz;
69
				float dist = distance(worldPosition, _WorldSpaceCameraPos);
70
				float f = clamp(1.0 - (dist - minDist) / (maxDist - minDist), 0.01, 1.0);
71
				return f * tess;
72
}
73
74
TessellationFactors DistanceBasedTess(float4 v0, float4 v1, float4 v2, float minDist, float maxDist, float tess)
75
{
76
				float3 f;
77
				f.x = CalcDistanceTessFactor(v0, minDist, maxDist, tess);
78
				f.y = CalcDistanceTessFactor(v1, minDist, maxDist, tess);
79
				f.z = CalcDistanceTessFactor(v2, minDist, maxDist, tess);
80
81
				return UnityCalcTriEdgeTessFactors(f);
82
}
83
84
uniform float3 _Position;
85
uniform sampler2D _GlobalEffectRT;
86
uniform float _OrthographicCamSize;
87
88
sampler2D  _Noise;
89
float _NoiseScale, _SnowHeight, _NoiseWeight, _SnowDepth;
90
91
TessellationFactors patchConstantFunction(InputPatch<ControlPoint, 3> patch)
92
{
93
    float minDist = 2.0;
94
    float maxDist = _MaxTessDistance;
95
    TessellationFactors f;
96
    return DistanceBasedTess(patch[0].vertex, patch[1].vertex, patch[2].vertex, minDist, maxDist, _Tess);
97
   
98
}
99
100
float4 GetShadowPositionHClip(Attributes input)
101
{
102
    float3 positionWS = TransformObjectToWorld(input.vertex.xyz);
103
    float3 normalWS = TransformObjectToWorldNormal(input.normal);
104
 
105
    float4 positionCS = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS, 0));
106
 
107
#if UNITY_REVERSED_Z
108
    positionCS.z = min(positionCS.z, positionCS.w * UNITY_NEAR_CLIP_VALUE);
109
#else
110
    positionCS.z = max(positionCS.z, positionCS.w * UNITY_NEAR_CLIP_VALUE);
111
#endif
112
    return positionCS;
113
}
114
115
Varyings vert(Attributes input)
116
{
117
    Varyings output;
118
    
119
    float3 worldPosition = mul(unity_ObjectToWorld, input.vertex).xyz;
120
    //create local uv
121
    float2 uv = worldPosition.xz - _Position.xz;
122
    uv = uv / (_OrthographicCamSize * 2);
123
    uv += 0.5;
124
    
125
    // Effects RenderTexture Reading
126
    float4 RTEffect = tex2Dlod(_GlobalEffectRT, float4(uv, 0, 0));
127
    // smoothstep to prevent bleeding
128
   	RTEffect *=  smoothstep(0.99, _Fade, uv.x) * smoothstep(0.99, _Fade,1- uv.x);
129
	RTEffect *=  smoothstep(0.99, _Fade, uv.y) * smoothstep(0.99, _Fade,1- uv.y);
130
    
131
    // worldspace noise texture
132
    float SnowNoise = tex2Dlod(_Noise, float4(worldPosition.xz * _NoiseScale, 0, 0)).r;
133
    output.viewDir = SafeNormalize(GetCameraPositionWS() - worldPosition);
134
135
	// move vertices up where snow is
136
	input.vertex.xyz += SafeNormalize(input.normal) * saturate(( _SnowHeight) + (SnowNoise * _NoiseWeight)) * saturate(1-(RTEffect.g * _SnowDepth));
137
138
    // transform to clip space
139
    #ifdef SHADERPASS_SHADOWCASTER
140
        output.vertex = GetShadowPositionHClip(input);
141
    #else
142
        output.vertex = TransformObjectToHClip(input.vertex.xyz);
143
    #endif
144
145
    //outputs
146
    output.worldPos =  mul(unity_ObjectToWorld, input.vertex).xyz;
147
    output.normal = input.normal;
148
    output.uv = input.uv;
149
    output.fogFactor = ComputeFogFactor(output.vertex.z);
150
    return output;
151
}
152
153
[UNITY_domain("tri")]
154
Varyings domain(TessellationFactors factors, OutputPatch<ControlPoint, 3> patch, float3 barycentricCoordinates : SV_DomainLocation)
155
{
156
    Attributes v;
157
    
158
    #define Interpolate(fieldName) v.fieldName = \
159
				patch[0].fieldName * barycentricCoordinates.x + \
160
				patch[1].fieldName * barycentricCoordinates.y + \
161
				patch[2].fieldName * barycentricCoordinates.z;
162
163
    Interpolate(vertex)
164
    Interpolate(uv)
165
    Interpolate(normal)
166
    
167
    return vert(v);
168
}
169