1 | texture2D elevationTexture;
|
---|
2 | sampler2D elevationSampler = sampler_state
|
---|
3 | {
|
---|
4 | texture = <elevationTexture>;
|
---|
5 | MipFilter = Linear;
|
---|
6 | MagFilter = Linear;
|
---|
7 | MinFilter = Linear;
|
---|
8 | AddressU = CLAMP;
|
---|
9 | AddressV = CLAMP;
|
---|
10 | };
|
---|
11 |
|
---|
12 | texture2D terrainTexture;
|
---|
13 | sampler2D terrainSampler = sampler_state
|
---|
14 | {
|
---|
15 | texture = <terrainTexture>;
|
---|
16 | MipFilter = Linear;
|
---|
17 | MagFilter = Linear;
|
---|
18 | MinFilter = Linear;
|
---|
19 | AddressU = WRAP;
|
---|
20 | AddressV = WRAP;
|
---|
21 | };
|
---|
22 |
|
---|
23 | float3 eyeDirection;
|
---|
24 | float fp;
|
---|
25 | float bp;
|
---|
26 |
|
---|
27 | struct vsTerrainInput
|
---|
28 | {
|
---|
29 | float4 pos : POSITION;
|
---|
30 | };
|
---|
31 |
|
---|
32 | float getTerrainHeight(float3 pos)
|
---|
33 | {
|
---|
34 | float2 d = sin(pos.xz / 100.0);
|
---|
35 | for(int ni=0; ni<4; ni++)
|
---|
36 | d = float2(d.x*d.x - d.y * d.y + 0.45, 2 * d.x * d.y + 0.3);
|
---|
37 | return 1/(dot(d, d) + 1) * 20.1 - 10.0;
|
---|
38 | }
|
---|
39 |
|
---|
40 | struct vsTerrainOutput
|
---|
41 | {
|
---|
42 | float4 pos : POSITION;
|
---|
43 | float3 normal : NORMAL;
|
---|
44 | float3 worldPos : TEXCOORD0;
|
---|
45 | float3 tangent : TEXCOORD1;
|
---|
46 | float3 binormal : TEXCOORD2;
|
---|
47 | float3 eyeRayDir : TEXCOORD4;
|
---|
48 |
|
---|
49 | float2 bumpCoord : TEXCOORD3;
|
---|
50 | };
|
---|
51 |
|
---|
52 | vsTerrainOutput vsTerrain(vsTerrainInput input)
|
---|
53 | {
|
---|
54 | vsTerrainOutput output = (vsTerrainOutput)0;
|
---|
55 | output.bumpCoord.xy = input.pos.xz*0.006;
|
---|
56 |
|
---|
57 | // input.pos.y = tex2Dlod(elevationSampler, float4(input.pos.xz, 0, 1)) - 0.5;
|
---|
58 | input.pos.y = cos(input.pos.x * 12.0) - 0.01;
|
---|
59 |
|
---|
60 | input.pos.xyz *= float3(1000.0, 10.0, 1000.0);
|
---|
61 |
|
---|
62 | output.worldPos = input.pos.xyz;
|
---|
63 |
|
---|
64 | float3 du = float3(1, 0, 0);
|
---|
65 | float3 dv = float3(0, 0, 1);
|
---|
66 |
|
---|
67 | output.pos = mul(float4(input.pos.xyz, 1), viewProjMatrix);
|
---|
68 |
|
---|
69 | output.binormal = normalize(du) * 0.3;
|
---|
70 | output.tangent = normalize(dv) * 0.3;
|
---|
71 | output.normal = normalize(cross(dv,du));
|
---|
72 |
|
---|
73 | return output;
|
---|
74 | }
|
---|
75 |
|
---|
76 | float4 psTerrain(vsTerrainOutput input) : COLOR0
|
---|
77 | {
|
---|
78 | /* input.worldPos.y = 0;
|
---|
79 | input.eyeRayDir = normalize(input.eyeRayDir);
|
---|
80 | for(int r=0; r<15; r++)
|
---|
81 | input.worldPos -= input.eyeRayDir * (getTerrainHeight(input.worldPos) - input.worldPos.y);
|
---|
82 |
|
---|
83 | input.worldPos.y = getTerrainHeight(input.worldPos);
|
---|
84 | depth = (dot(input.worldPos - eyePosition, eyeDirection) - fp)/(bp-fp);*/
|
---|
85 |
|
---|
86 | half4 t0 = tex2D(terrainSampler, input.bumpCoord) * 2 - 1;
|
---|
87 | half3 N = t0.xzy;
|
---|
88 |
|
---|
89 | half3x3 m;
|
---|
90 | m[0] = input.tangent;
|
---|
91 | m[2] = input.binormal;
|
---|
92 | m[1] = input.normal;
|
---|
93 |
|
---|
94 | half3 normalWorld = mul(m, N);
|
---|
95 | normalWorld = normalize(normalWorld);
|
---|
96 |
|
---|
97 | float3 viewDir = -normalize(eyePosition - input.worldPos);
|
---|
98 |
|
---|
99 | return float4(1, (input.worldPos.y + 5) / 20, 0, 1);
|
---|
100 | }
|
---|
101 |
|
---|
102 | technique terrain
|
---|
103 | {
|
---|
104 | pass P0
|
---|
105 | {
|
---|
106 | VertexShader = compile vs_3_0 vsTerrain();
|
---|
107 | PixelShader = compile ps_3_0 psTerrain();
|
---|
108 | }
|
---|
109 | };
|
---|