-
Notifications
You must be signed in to change notification settings - Fork 15
/
geoClipGrass.sha
85 lines (66 loc) · 2.4 KB
/
geoClipGrass.sha
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//Cg
const float clumpWidthScalar=1.0;
const float alphaThreshold=.1;
const float tiltScalar=2.0;
const float heightScalar=30;
const float positionVariability=1.0;
const float minHeight=0.1; //(added to 0-1 height)
const float widthVariability=5.0; // multiplied by 0-1 and added to clumpWidthScalar
float2 geoGlipUV(float2 vertxy, float2 tileOffset, float heightMapRez){
float2 ftileGridPos=vertxy+tileOffset;
return (ftileGridPos+.5)/heightMapRez;
}
float unpackFloat(float3 h){
return h.x+h.y/256+h.z/(256*256);
}
float hight(sampler2D k_height,float2 l_uv){
return unpackFloat(tex2D(k_height,l_uv).xyz);
}
void vshader(
uniform float4x4 mat_modelproj,
in float4 vtx_position : POSITION,
uniform float4 k_tileOffset,
uniform float4 k_heightMapRez,
out float2 l_uv: TEXCOORD0,
in uniform sampler2D k_grassData: TEXUNIT0,
in uniform sampler2D k_grassData2: TEXUNIT3,
in uniform sampler2D k_height: TEXUNIT2,
out float l_height : TEXCOORD1,
out float2 l_uvSheet: TEXCOORD2,
out float4 l_position : POSITION,
uniform in float4 k_heightScale,
out float3 l_norm: TEXCOORD3
)
{
l_uv=geoGlipUV(vtx_position.xy.xy,k_tileOffset.xy,k_heightMapRez.x);
l_height=hight(k_height,l_uv);
float2 rPos=round(vtx_position.xy);
float4 data=tex2D(k_grassData,l_uv);
float fPos=vtx_position.x-rPos.x;
l_uvSheet=float2(fPos*2+.5,vtx_position.z);
fPos*=(clumpWidthScalar+widthVariability*frac(data.x*64));
float2 pos;
sincos(data.z*3.1415*8,pos.x,pos.y);
float height=vtx_position.z*(data.w+minHeight);
height*=tex2D(k_grassData2,l_uv).w*0.5;
float2 sway;
sincos(data.z*3.1415*15,sway.x,sway.y);
sway=height*sway*tiltScalar*frac(data.w*32);
l_norm=normalize(cross(float3(fPos*pos+sway,height),float3(-fPos*pos+sway,height)));
pos=fPos*pos+sway;
pos+=rPos+(data.xy-float2(0.5,0.5))*positionVariability;
l_position = mul(mat_modelproj, float4(pos,l_height*k_heightScale.x+height*heightScalar,1));
}
void fshader(
in float2 l_uvSheet: TEXCOORD2,
in float l_height : TEXCOORD1,
in uniform sampler2D k_grassSheet: TEXUNIT1,
in float3 l_norm: TEXCOORD3,
out float4 o_color: COLOR)
{
o_color=tex2D(k_grassSheet,l_uvSheet);
if (o_color.w<alphaThreshold){discard;}
float d=max(0,dot(l_norm,float3(1,1,1)))*.3+.7;
o_color*=float4(d,d,d,0);
//o_color.xyz=l_norm;
}