[12115] | 1 | ////////////////////////////// MOVING GRASS |
---|
| 2 | // Vertex program to wave some grass about |
---|
| 3 | // Assumes UV texture coords of v==0 indicates the top of the grass |
---|
| 4 | void grass_vp(float4 position : POSITION, |
---|
| 5 | float3 normal : NORMAL, |
---|
| 6 | float2 uv : TEXCOORD0, |
---|
| 7 | out float4 oPosition : POSITION, |
---|
| 8 | out float2 oUv : TEXCOORD0, |
---|
| 9 | out float4 oColour : COLOR, |
---|
| 10 | |
---|
| 11 | uniform float4x4 worldViewProj, |
---|
| 12 | //uniform float4 camObjPos, |
---|
| 13 | uniform float4 ambient, |
---|
| 14 | uniform float4 objSpaceLight, |
---|
| 15 | uniform float4 lightColour, |
---|
| 16 | uniform float4 offset) |
---|
| 17 | { |
---|
| 18 | float4 mypos = position; |
---|
| 19 | //offset = float4(0.5, 0, 0, 0); |
---|
| 20 | float4 factor = float4(1,1,1,1) - uv.yyyy; |
---|
| 21 | mypos = mypos + offset * factor; |
---|
| 22 | oPosition = mul(worldViewProj, mypos); |
---|
| 23 | |
---|
| 24 | oUv = uv; |
---|
| 25 | // Color |
---|
| 26 | // get vertex light direction (support directional and point) |
---|
| 27 | float3 light = normalize(objSpaceLight.xyz - (mypos.xyz * objSpaceLight.w)); |
---|
| 28 | // grass is just 2D quads, so if light passes underneath we need to invert the normal |
---|
| 29 | // abs() will have the same effect |
---|
| 30 | float diffuseFactor = abs(dot(normal.xyz, light)); |
---|
| 31 | oColour = ambient + diffuseFactor * lightColour; |
---|
| 32 | |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | void grass_fp( float4 position : POSITION, |
---|
| 36 | float2 uv : TEXCOORD0, |
---|
| 37 | float4 colour : COLOR, |
---|
| 38 | |
---|
| 39 | out float4 oColour : COLOR, |
---|
| 40 | |
---|
| 41 | uniform sampler2D diffuseMap |
---|
| 42 | ) |
---|
| 43 | { |
---|
| 44 | float4 texColor = tex2D(diffuseMap, uv.xy); |
---|
| 45 | oColour = float4(texColor.rgb * colour.rgb, texColor.a); |
---|
| 46 | if(oColour.a < 0.58) |
---|
| 47 | discard; |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | |
---|
| 51 | void grassTex_vp( |
---|
| 52 | float4 position : POSITION, |
---|
| 53 | float4 normal : NORMAL, |
---|
| 54 | float2 uv : TEXCOORD0, |
---|
| 55 | |
---|
| 56 | out float4 oPosition : POSITION, |
---|
| 57 | out float2 oUv : TEXCOORD0, |
---|
| 58 | //out float4 oNormal : NORMAL, |
---|
| 59 | |
---|
| 60 | uniform float4x4 worldViewProj, |
---|
| 61 | |
---|
| 62 | uniform float4 ambient, |
---|
| 63 | uniform float4 objSpaceLight, |
---|
| 64 | uniform float4 lightColour, |
---|
| 65 | |
---|
| 66 | uniform float4 offset) |
---|
| 67 | { |
---|
| 68 | // Position |
---|
| 69 | float4 mypos = position; |
---|
| 70 | float4 factor = float4(1,1,1,1) - uv.yyyy; |
---|
| 71 | mypos = mypos + offset * factor; |
---|
| 72 | oPosition = mul(worldViewProj, mypos); |
---|
| 73 | // Texture Coord |
---|
| 74 | oUv.xy = uv.xy; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | /// grass_vp ambient |
---|
| 78 | void grassAmbient_vp( |
---|
| 79 | float4 position : POSITION, |
---|
| 80 | float4 normal : NORMAL, |
---|
| 81 | float2 uv : TEXCOORD0, |
---|
| 82 | |
---|
| 83 | out float4 oPosition : POSITION, |
---|
| 84 | out float2 oUv : TEXCOORD0, |
---|
| 85 | out float4 oColour : COLOR, |
---|
| 86 | //out float4 oNormal : NORMAL, |
---|
| 87 | |
---|
| 88 | //uniform float4 camObjPos, |
---|
| 89 | uniform float4x4 worldViewProj, |
---|
| 90 | uniform float4 ambient, |
---|
| 91 | uniform float4 offset) |
---|
| 92 | { |
---|
| 93 | // Position |
---|
| 94 | float4 mypos = position; |
---|
| 95 | float4 factor = float4(1,1,1,1) - uv.yyyy; |
---|
| 96 | mypos = mypos + offset * factor; |
---|
| 97 | oPosition = mul(worldViewProj, mypos); |
---|
| 98 | // Texture Coord |
---|
| 99 | oUv.xy = uv.xy; |
---|
| 100 | /* |
---|
| 101 | // Normal |
---|
| 102 | // Make vec from vertex to camera |
---|
| 103 | float4 EyeVec = camObjPos - mypos; |
---|
| 104 | // Dot the v to eye and the normal to see if they point |
---|
| 105 | // in the same direction or opposite |
---|
| 106 | float aligned = dot(normal, EyeVec); // -1..1 |
---|
| 107 | // If aligned is negative, we need to flip the normal |
---|
| 108 | if (aligned < 0) |
---|
| 109 | normal = -normal; |
---|
| 110 | //oNormal = normal; |
---|
| 111 | */ |
---|
| 112 | // Color |
---|
| 113 | oColour = ambient; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | |
---|
| 117 | void grassAmbient_fp( |
---|
| 118 | float4 position : POSITION, |
---|
| 119 | float2 uv : TEXCOORD0, |
---|
| 120 | float4 colour : COLOR, |
---|
| 121 | |
---|
| 122 | out float4 oColour : COLOR, |
---|
| 123 | |
---|
| 124 | uniform sampler2D diffuseMap |
---|
| 125 | ) |
---|
| 126 | { |
---|
| 127 | float4 texColor = tex2D(diffuseMap, uv.xy); |
---|
| 128 | oColour = float4(colour.rgb, texColor.a); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | //////////////////////// GRASS SHADOW CASTER |
---|
| 132 | // Shadow caster vertex program. |
---|
| 133 | void grasscaster_vp(float4 position : POSITION, |
---|
| 134 | float2 uv : TEXCOORD0, |
---|
| 135 | |
---|
| 136 | out float4 oPosition : POSITION, |
---|
| 137 | out float2 oUv : TEXCOORD0, |
---|
| 138 | out float2 oDepth : TEXCOORD1, |
---|
| 139 | |
---|
| 140 | uniform float4x4 worldViewProj, |
---|
| 141 | uniform float4 offset, |
---|
| 142 | uniform float4 texelOffsets) |
---|
| 143 | { |
---|
| 144 | float4 mypos = position; |
---|
| 145 | float4 factor = float4(1,1,1,1) - uv.yyyy; |
---|
| 146 | mypos = mypos + offset * factor; |
---|
| 147 | oPosition = mul(worldViewProj, mypos); |
---|
| 148 | |
---|
| 149 | // fix pixel / texel alignment |
---|
| 150 | oPosition.xy += texelOffsets.zw * oPosition.w; |
---|
| 151 | |
---|
| 152 | oDepth.x = oPosition.z; |
---|
| 153 | oDepth.y = oPosition.w; |
---|
| 154 | |
---|
| 155 | oUv = uv; |
---|
| 156 | |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | |
---|
| 160 | void grasscaster_fp( |
---|
| 161 | float4 position : POSITION, |
---|
| 162 | float2 uv : TEXCOORD0, |
---|
| 163 | float2 depth : TEXCOORD1, |
---|
| 164 | |
---|
| 165 | out float4 result : COLOR, |
---|
| 166 | |
---|
| 167 | uniform sampler2D diffuseMap |
---|
| 168 | ) |
---|
| 169 | { |
---|
| 170 | float alpha = tex2D(diffuseMap, uv).a; |
---|
| 171 | if (alpha > 0.001) |
---|
| 172 | { |
---|
| 173 | result = float4(1.0f, 1.0f, 1.0f, 0.0f); |
---|
| 174 | } |
---|
| 175 | else |
---|
| 176 | { |
---|
| 177 | float finalDepth = depth.x / depth.y; |
---|
| 178 | // just smear across all components |
---|
| 179 | // therefore this one needs high individual channel precision |
---|
| 180 | result = float4(finalDepth.xxx, 1.0f); |
---|
| 181 | } |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | |
---|
| 185 | //////////////////////// GRASS SHADOW RECEIVER |
---|
| 186 | void grassreceiver_vp( |
---|
| 187 | float4 position : POSITION, |
---|
| 188 | float4 normal : NORMAL, |
---|
| 189 | float2 uv : TEXCOORD0, |
---|
| 190 | |
---|
| 191 | out float4 oPosition : POSITION, |
---|
| 192 | out float4 oShadowUV : TEXCOORD0, |
---|
| 193 | out float3 oUv : TEXCOORD1, |
---|
| 194 | out float4 oColour : COLOR, |
---|
| 195 | //out float4 oNormal : NORMAL, |
---|
| 196 | |
---|
| 197 | uniform float4x4 world, |
---|
| 198 | uniform float4x4 worldViewProj, |
---|
| 199 | uniform float4x4 texViewProj, |
---|
| 200 | |
---|
| 201 | uniform float4 camObjPos, |
---|
| 202 | |
---|
| 203 | uniform float4 objSpaceLight, |
---|
| 204 | uniform float4 lightColour, |
---|
| 205 | |
---|
| 206 | uniform float4 offset) |
---|
| 207 | { |
---|
| 208 | float4 mypos = position; |
---|
| 209 | float4 factor = float4(1,1,1,1) - uv.yyyy; |
---|
| 210 | mypos = mypos + offset * factor; |
---|
| 211 | oPosition = mul(worldViewProj, mypos); |
---|
| 212 | oUv.xy = uv.xy; |
---|
| 213 | // Transform position to world space |
---|
| 214 | float4 worldPos = mul(world, mypos); |
---|
| 215 | // calculate shadow map coords |
---|
| 216 | oShadowUV = mul(texViewProj, worldPos); |
---|
| 217 | |
---|
| 218 | // Make vec from vertex to camera |
---|
| 219 | float4 EyeVec = camObjPos - mypos; |
---|
| 220 | // Dot the v to eye and the normal to see if they point |
---|
| 221 | // in the same direction or opposite |
---|
| 222 | float alignedEye = dot(normal, EyeVec); // -1..1 |
---|
| 223 | // If aligned is negative, we need to flip the normal |
---|
| 224 | if (alignedEye < 0) |
---|
| 225 | normal = -normal; |
---|
| 226 | //oNormal = normal; |
---|
| 227 | |
---|
| 228 | // get vertex light direction (support directional and point) |
---|
| 229 | float3 lightVec = normalize(objSpaceLight.xyz - (mypos.xyz * objSpaceLight.w)); |
---|
| 230 | // Dot the v to light and the normal to see if they point |
---|
| 231 | // in the same direction or opposite |
---|
| 232 | float alignedLight = dot(normal.xyz, lightVec); // -1..1 |
---|
| 233 | // If aligned is negative, shadowing/lighting is not possible. |
---|
| 234 | oUv.z = (alignedLight < 0)? 0 : 1 ; |
---|
| 235 | |
---|
| 236 | float diffuseFactor = max(alignedLight, 0); |
---|
| 237 | //oColour = diffuseFactor * lightColour; |
---|
| 238 | oColour = lightColour; |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | |
---|
| 242 | void grassreceiver_fp( |
---|
| 243 | float4 position : POSITION |
---|
| 244 | , float4 shadowUV : TEXCOORD0 |
---|
| 245 | , float3 uv : TEXCOORD1 |
---|
| 246 | , float4 vertexLight : COLOR |
---|
| 247 | |
---|
| 248 | , out float4 oColour : COLOR |
---|
| 249 | |
---|
| 250 | , uniform sampler2D shadowMap : register(s0) |
---|
| 251 | , uniform sampler2D diffuseMap : register(s1) |
---|
| 252 | |
---|
| 253 | //, uniform float inverseShadowmapSize |
---|
| 254 | , uniform float fixedDepthBias |
---|
| 255 | , uniform float gradientClamp |
---|
| 256 | , uniform float gradientScaleBias |
---|
| 257 | ) |
---|
| 258 | { |
---|
| 259 | if (shadowUV.z > 0) |
---|
| 260 | { |
---|
| 261 | float4 diffuse = tex2D(diffuseMap, uv.xy); |
---|
| 262 | if (diffuse.a > 0.001) |
---|
| 263 | { |
---|
| 264 | oColour = float4(0.0f, 0.0f, 0.0f, 0.0f); |
---|
| 265 | } |
---|
| 266 | else |
---|
| 267 | { |
---|
| 268 | // point on shadowmap (no gradient as cg1.4 compiler is unable to handle that complexity) |
---|
| 269 | shadowUV = shadowUV / shadowUV.w; |
---|
| 270 | float4 shadowDepths = tex2D(shadowMap, shadowUV.xy); |
---|
| 271 | |
---|
| 272 | float gradientFactor = gradientClamp * gradientScaleBias; |
---|
| 273 | float depthAdjust = gradientFactor + fixedDepthBias * shadowDepths.x; |
---|
| 274 | float centerdepth = shadowDepths.x + depthAdjust; |
---|
| 275 | |
---|
| 276 | oColour = (centerdepth > shadowUV.z) ? float4(vertexLight.rgb, diffuse.a) : float4(0, 0, 0, diffuse.a); |
---|
| 277 | } |
---|
| 278 | } |
---|
| 279 | else |
---|
| 280 | { |
---|
| 281 | oColour = float4(0.0f, 0.0f, 0.0f, 0.0f); |
---|
| 282 | } |
---|
| 283 | } |
---|