Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
86 lines (66 sloc) 2.4 KB
//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;
}