Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Standard shader for shading things.
- float4x4 World;
- float4x4 View;
- float4x4 Projection;
- float Alpha;
- float4 AmbientColor;
- float AmbientIntensity;
- float4 DiffuseColor;
- float DiffuseIntensity;
- float3 DiffuseLightDirection;
- float4 SpecularColor;
- float SpecularIntensity;
- float Shinniness;
- float3 CameraPosition;
- float3 CameraDirection;
- int AtlasWidth = 25;
- int SquareSize = 32;
- float TexturePercent = 0.04; //percent of the entire texture taken up by 1 pixel
- //float4 RimColor;
- //float RimIntensity;
- float FogBegin;
- float FogEnd;
- // Texturing variables
- Texture ModelTexture; // Our texture which will be mapped onto our object
- // Pass coordinates to our texture sampler to get a color for a certain pixel
- sampler TextureSampler = sampler_state {
- texture = <ModelTexture> ;
- magfilter = LINEAR;
- minfilter = LINEAR;
- mipfilter= LINEAR;
- AddressU = WRAP;
- AddressV = WRAP;};
- //-----------------------------------------------------------------------------
- // Inputs and Outputs
- //-----------------------------------------------------------------------------
- struct VSInputTx
- {
- float4 Position : POSITION0;
- float3 Normal : NORMAL0;
- short2 BlockAndDecalID : TEXCOORD0;
- };
- struct VSInputVc
- {
- float4 Position : POSITION0;
- float3 Normal : NORMAL0;
- float4 Color : COLOR0;
- };
- struct VSOutputTx
- {
- float4 Position : POSITION0;
- float3 Normal : TEXCOORD0;
- float3 CameraView : TEXCOORD1;
- short2 TexCoords : TEXCOORD2;
- short2 DecalCoords : TEXCOORD3;
- float FogAmt : TEXCOORD4;
- };
- struct VSOutputVc
- {
- float4 Position : POSITION0;
- float3 Normal : TEXCOORD0;
- float4 Color : COLOR0;
- float3 CameraView : TEXCOORD1;
- float FogAmt : TEXCOORD2;
- };
- //-----------------------------------------------------------------------------
- // Vertex shaders
- //-----------------------------------------------------------------------------
- VSOutputTx VSBasicTx( VSInputTx input )
- {
- VSOutputTx output;
- float4 worldPosition = mul( input.Position, World );
- float4 viewPosition = mul( worldPosition, View );
- output.Position = mul( viewPosition, Projection );
- output.Normal = mul( input.Normal, World );
- output.CameraView = normalize( CameraPosition - worldPosition );
- output.FogAmt = 1 - saturate((distance(worldPosition,CameraPosition)-FogBegin)/(FogEnd-FogBegin)); //This sets the fog amount (since it needs position data)
- // Convert texture coordinates to short2 from blockID
- // When they transfer to the pixel shader, they will be interpolated
- // per pixel.
- output.TexCoords = short2((short)(((input.BlockAndDecalID.x) % (AtlasWidth)) * TexturePercent), (short)(((input.BlockAndDecalID.x) / (AtlasWidth)) * TexturePercent));
- output.DecalCoords = short2((short)(((input.BlockAndDecalID.y) % (AtlasWidth)) * TexturePercent), (short)(((input.BlockAndDecalID.y) / (AtlasWidth)) * TexturePercent));
- return output;
- }
- VSOutputVc VSBasicVc( VSInputVc input )
- {
- VSOutputVc output;
- float4 worldPosition = mul( input.Position, World );
- float4 viewPosition = mul( worldPosition, View );
- output.Position = mul( viewPosition, Projection );
- output.Normal = mul( input.Normal, World );
- output.CameraView = normalize( CameraPosition - worldPosition );
- output.FogAmt = 1 - saturate((distance(worldPosition,CameraPosition)-FogBegin)/(FogEnd-FogBegin));
- // Just pass the texture coordinates to the vertex shader output.
- // When they transfer to the pixel shader, they will be interpolated
- // per pixel.
- output.Color = input.Color;
- return output;
- }
- //-----------------------------------------------------------------------------
- // Pixel shaders
- //-----------------------------------------------------------------------------
- float4 PSBasicTx( VSOutputTx input ) : COLOR0
- {
- // Sample our texture at the specified texture coordinates to get the texture color
- float4 texColor = tex2D( TextureSampler, input.TexCoords );
- float4 decColor = tex2D( TextureSampler, input.DecalCoords );
- float t_a = decColor.a + texColor.a*(1 -decColor.a) ;
- texColor.a = 0;
- decColor += texColor*(1 - decColor.a)/t_a;
- texColor = float4(decColor.r, decColor.g, decColor.b, t_a);
- float3 lightdir = normalize( DiffuseLightDirection );
- float3 norm = normalize( input.Normal );
- float3 halfAngle = normalize( lightdir + input.CameraView );
- float specular = pow( saturate( dot( norm, halfAngle ) ), Shinniness ) * SpecularColor * SpecularIntensity;
- // Calculate the rim lighting component by subtract the dot product of the normal and camera direction
- // from 1, then taking that to the 2nd power. This will give pixels at more perpendicular angles to the camera
- // a glow effect. Multiply that by rim color and intensity to adjust the color and brightness.
- // - If you take the dot product to a higher power, the glow will be thinner and closer to the edges.
- // - If you take the dot product to a lower power, the glow will be more spread out away from the edges.
- //float4 rim = pow( 1 - dot( norm, CameraDirection ), 1.5 ) * RimColor * RimIntensity;
- float4 diffuse = dot( lightdir, input.Normal ) * DiffuseIntensity * DiffuseColor; //Diffuse light
- float4 ambient = AmbientIntensity * AmbientColor; //Calculate ambient light
- float4 returnColor = texColor * (diffuse + ambient + specular); //Set the return color.
- returnColor.a = t_a * Alpha; //Manually set the alpha value.
- returnColor*=input.FogAmt; //Multiply the color by fog amount to get fog.
- return returnColor;
- }
- float4 PSBasicVc( VSOutputVc input ) : COLOR0
- {
- // Sample our texture at the specified texture coordinates to get the texture color
- float4 texColor = input.Color;
- float t_a = texColor.a;
- float3 lightdir = normalize( DiffuseLightDirection );
- float3 norm = normalize( input.Normal );
- float3 halfAngle = normalize( lightdir + input.CameraView );
- float specular = pow( saturate( dot( norm, halfAngle ) ), Shinniness ) * SpecularColor * SpecularIntensity;
- // Calculate the rim lighting component by subtract the dot product of the normal and camera direction
- // from 1, then taking that to the 2nd power. This will give pixels at more perpendicular angles to the camera
- // a glow effect. Multiply that by rim color and intensity to adjust the color and brightness.
- // - If you take the dot product to a higher power, the glow will be thinner and closer to the edges.
- // - If you take the dot product to a lower power, the glow will be more spread out away from the edges.
- //float4 rim = pow( 1 - dot( norm, CameraDirection ), 1.5 ) * RimColor * RimIntensity;
- float4 diffuse = dot( lightdir, input.Normal ) * DiffuseIntensity * DiffuseColor;
- float4 ambient = AmbientIntensity * AmbientColor;
- float4 returnColor = texColor * (diffuse + ambient + specular);
- returnColor.a = t_a * Alpha;
- returnColor*=input.FogAmt;
- return returnColor;
- }
- //int ShaderIndex = 0;
- VertexShader VSArray[2] =
- {
- compile vs_1_1 VSBasicTx(),
- compile vs_1_1 VSBasicVc()
- };
- PixelShader PSArray[2] =
- {
- compile ps_2_0 PSBasicTx(),
- compile ps_2_0 PSBasicVc()
- };
- Technique BasicEffect
- {
- Pass TexturePass
- {
- VertexShader = (VSArray[0]);
- PixelShader = (PSArray[0]);
- }
- Pass VertexColorPass
- {
- VertexShader = (VSArray[1]);
- PixelShader = (PSArray[1]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement