// // terrain.fx // struct TextureIndices { float texIndex0; float texIndex1; float texIndex2; float texIndex3; }; StructuredBuffer TexIndices : register(t0); Texture2DArray alphaMaps : register(t1); Texture2D shaderTexture0 : register(t2); Texture2D shaderTexture1 : register(t3); Texture2D shaderTexture2 : register(t4); Texture2D shaderTexture3 : register(t5); Texture2D shaderTexture4 : register(t6); Texture2D shaderTexture5 : register(t7); Texture2D shaderTexture6 : register(t8); Texture2D shaderTexture7 : register(t9); Texture2D shaderTexture8 : register(t10); Texture2D shaderTexture9 : register(t11); Texture2D shaderTexture10 : register(t12); Texture2D shaderTexture11 : register(t13); Texture2D shaderTexture12 : register(t14); Texture2D shaderTexture13 : register(t15); Texture2D shaderTexture14 : register(t16); Texture2D shaderTexture15 : register(t17); Texture2D shaderTexture16 : register(t18); SamplerState SampleTypeWrap; SamplerState SampleTypeClamp; cbuffer MatrixBuffer { matrix worldMatrix; matrix viewMatrix; matrix projectionMatrix; }; cbuffer LightingBuffer { float4 ambient; float4 diffuse; float4 specular; float3 lightDirection; }; struct PixelInputType { float4 position : SV_POSITION; float3 normal : NORMAL; float2 tex : TEXCOORD0; float meshNum : MESHNUM; float fogFactor : FOG; }; struct VertexInputType { float4 position : POSITION; float3 normal : NORMAL; float2 tex : TEXCOORD0; float meshNum : MESHNUM; }; //////////////////////////////////////////////////////////////////////////////// // Vertex Shader //////////////////////////////////////////////////////////////////////////////// PixelInputType VS(VertexInputType input ) { PixelInputType output; input.position.w = 1.0f; output.meshNum = input.meshNum; output.position = mul(input.position, worldMatrix); output.position = mul(output.position, viewMatrix); output.position = mul(output.position, projectionMatrix); output.normal = input.normal; output.tex = input.tex; return output; } // Texture Index Helper float4 getSample( float texNum, float2 intexconv ) { if( texNum == 0.0f ) { return shaderTexture0.Sample( SampleTypeWrap, intexconv ); } else if( texNum == 1.0f ) { return shaderTexture1.Sample( SampleTypeWrap, intexconv ); } else if( texNum == 2.0f ) { return shaderTexture2.Sample( SampleTypeWrap, intexconv ); } else if( texNum == 3.0f ) { return shaderTexture3.Sample( SampleTypeWrap, intexconv ); } else if( texNum == 4.0f ) { return shaderTexture4.Sample( SampleTypeWrap, intexconv ); } else if( texNum == 5.0f ) { return shaderTexture5.Sample( SampleTypeWrap, intexconv ); } else if( texNum == 6.0f ) { return shaderTexture6.Sample( SampleTypeWrap, intexconv ); } else if( texNum == 7.0f ) { return shaderTexture7.Sample( SampleTypeWrap, intexconv ); } else if( texNum == 8.0f ) { return shaderTexture8.Sample( SampleTypeWrap, intexconv ); } else if( texNum == 9.0f ) { return shaderTexture9.Sample( SampleTypeWrap, intexconv ); } else if( texNum == 10.0f ) { return shaderTexture10.Sample( SampleTypeWrap, intexconv ); } else if( texNum == 11.0f ) { return shaderTexture11.Sample( SampleTypeWrap, intexconv ); } else if( texNum == 12.0f ) { return shaderTexture12.Sample( SampleTypeWrap, intexconv ); } else if( texNum == 13.0f ) { return shaderTexture13.Sample( SampleTypeWrap, intexconv ); } else if( texNum == 14.0f ) { return shaderTexture14.Sample( SampleTypeWrap, intexconv ); } else if( texNum == 15.0f ) { return shaderTexture15.Sample( SampleTypeWrap, intexconv ); } else { return float4( 0.0f, 0.0, 0.0f, 0.0f );//shaderTexture0.Sample( SampleTypeWrap, intexconv ); } } float4 sampleTexture( float meshNum, float texNum, float2 intexconv ) { float tex2d = 0; if( texNum == 0.0f ) { tex2d = TexIndices[int(meshNum)].texIndex0; } else if( texNum == 1.0f ) { tex2d = TexIndices[int(meshNum)].texIndex1; } else if( texNum == 2.0f ) { tex2d = TexIndices[int(meshNum)].texIndex2; } else if( texNum == 3.0f ) { tex2d = TexIndices[int(meshNum)].texIndex3; } else { tex2d = -1.0f; } return getSample( tex2d, intexconv ); } //////////////////////////////////////////////////////////////////////////////// // Pixel Shader //////////////////////////////////////////////////////////////////////////////// float4 PS(PixelInputType input) : SV_TARGET { float2 intexconv = { input.tex.x, input.tex.y }; float4 textureColor0 = sampleTexture( input.meshNum, 0, intexconv ); float4 textureColor1 = sampleTexture( input.meshNum, 1, intexconv ); float4 textureColor2 = sampleTexture( input.meshNum, 2, intexconv ); float4 textureColor3 = sampleTexture( input.meshNum, 3, intexconv ); float4 textureColorAlpha = float4( 1.0f, alphaMaps.Sample( SampleTypeClamp, float3( input.tex.x, input.tex.y, input.meshNum * 3 + 0)).a, alphaMaps.Sample( SampleTypeClamp, float3( input.tex.x, input.tex.y, input.meshNum * 3 + 1)).a, alphaMaps.Sample( SampleTypeClamp, float3( input.tex.x, input.tex.y, input.meshNum * 3 + 2)).a ); float4 oneminus1 = 1.0f - textureColorAlpha.y; float4 oneminus2 = 1.0f - textureColorAlpha.z; float4 oneminus3 = 1.0f - textureColorAlpha.w; float4 l = textureColor0; float4 m = textureColorAlpha.y * textureColor1 + oneminus1 * l; float4 n = textureColorAlpha.z * textureColor2 + oneminus2 * m; float4 o = textureColorAlpha.w * textureColor3 + oneminus3 * n; float4 diffuseColor = o; return diffuseColor; }