Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //--------------------------------------------------------------------------------------
- // Particle.fx
- // Direct3D 10 Shader Model 4.0 Line Drawing Demo
- // Copyright (c) Stefan Petersson, 2008
- //--------------------------------------------------------------------------------------
- //-----------------------------------------------------------------------------------------
- // Input and Output Structures
- //-----------------------------------------------------------------------------------------
- //World matrices
- Matrix World;
- Matrix View;
- Matrix Projection;
- struct VSSceneIn
- {
- float3 Pos : POS;
- float3 WorldPos : POS;
- float4 Color : COL;
- float3 Normal : NORMAL;
- float2 texCo : TC;
- };
- struct PSSceneIn
- {
- float4 Pos : SV_Position; // SV_Position is a (S)ystem (V)ariable that denotes transformed position
- float3 WorldPos : POS;
- float4 Color : COLOR;
- float3 Normal : NORMAL;
- float2 texCo : TC;
- };
- struct DirectionalLight
- {
- float4 color;
- float3 dir;
- };
- struct Material
- {
- float Ka, Kd, Ks, A;
- };
- // Nonnumeric values cannot be added to a cbuffer.
- Texture2D gDiffuseMap;
- //lighting vars
- DirectionalLight light;
- Material material;
- float4 ambientLight;
- float3 CameraPos;
- //-----------------------------------------------------------------------------------------
- // Constant Buffers (where we store variables by frequency of update)
- //-----------------------------------------------------------------------------------------
- cbuffer cbEveryFrame
- {
- matrix g_mWorldViewProjection;
- };
- //Simple samplerstate
- SamplerState linearSampler
- {
- Filter = MIN_MAG_MIP_LINEAR;
- AddressU = Wrap;
- AddressV = Wrap;
- MaxAnisotropy = 16;
- };
- //RASTERIZER STATES
- //--------------------------------------------------------------------------------------
- RasterizerState rsSolid
- {
- FillMode = Solid;
- CullMode = NONE;
- FrontCounterClockwise = false;
- };
- //-----------------------------------------------------------------------------------------
- // State Structures
- //-----------------------------------------------------------------------------------------
- DepthStencilState DisableDepth
- {
- DepthEnable = FALSE;
- DepthWriteMask = ZERO;
- };
- DepthStencilState EnableDepthTestOnly
- {
- DepthEnable = TRUE;
- DepthWriteMask = ZERO;
- };
- DepthStencilState EnableDepth
- {
- DepthEnable = TRUE;
- DepthWriteMask = ALL;
- };
- RasterizerState NoCulling
- {
- CullMode = NONE;
- };
- //-----------------------------------------------------------------------------------------
- // VertexShader: VSScene
- //-----------------------------------------------------------------------------------------
- PSSceneIn VSScene(VSSceneIn input)
- {
- PSSceneIn output = (PSSceneIn)0;
- // transform the point into view space
- output.Pos = mul( float4(input.Pos,1.0), g_mWorldViewProjection );
- output.Color = input.Color;
- output.texCo = input.texCo;
- return output;
- }
- //--------------------------------------------------------------------------------------
- // Phong Lighting Reflection Model
- //--------------------------------------------------------------------------------------
- float4 calcPhongLighting( Material M, float4 LColor, float3 N, float3 L, float3 V, float3 R )
- {
- float4 Ia = M.Ka * ambientLight;
- float4 Id = M.Kd * saturate( dot(N,L) );
- float4 Is = M.Ks * pow( saturate(dot(R,V)), M.A );
- return Ia + (Id + Is) * LColor;
- }
- //--------------------------------------------------------------------------------------
- // Blinn-Phong Lighting Reflection Model
- //--------------------------------------------------------------------------------------
- float4 calcBlinnPhongLighting( Material M, float4 LColor, float3 N, float3 L, float3 H )
- {
- float4 Ia = M.Ka * ambientLight;
- float4 Id = M.Kd * saturate( dot(N,L) );
- float4 Is = M.Ks * pow( saturate(dot(N,H)), M.A );
- return Ia + (Id + Is) * LColor;
- }
- //--------------------------------------------------------------------------------------
- // PER VERTEX LIGHTING - PHONG
- //--------------------------------------------------------------------------------------
- PSSceneIn VS_VERTEX_LIGHTING_PHONG( VSSceneIn input )
- {
- PSSceneIn output;
- // transform the point into clip space
- output.Pos = mul( float4(input.Pos,1.0), g_mWorldViewProjection );
- //set texture coords
- output.texCo = input.texCo;
- //calculate lighting vectors
- float3 N = normalize( mul( input.Normal, (float3x3) World) );
- float3 V = normalize( CameraPos - (float3) input.Pos );
- //DONOT USE -light.dir since the reflection returns a ray from the surface
- float3 R = reflect( light.dir, N);
- //calculate per vertex lighting intensity and interpolate it like a color
- output.Color = calcPhongLighting( material, light.color, N, -light.dir, V, R);
- return output;
- }
- float4 PS_VERTEX_LIGHTING_PHONG( PSSceneIn input ) : SV_Target
- {
- return input.Color;
- }
- //--------------------------------------------------------------------------------------
- // PER PIXEL LIGHTING
- //--------------------------------------------------------------------------------------
- PSSceneIn VS_PIXEL_LIGHTING_PHONG( VSSceneIn input )
- {
- PSSceneIn output;
- //transform position to clip space - keep worldspace position
- output.WorldPos = mul( float4(input.Pos,1.0f), World );
- output.Pos = mul( float4(output.WorldPos,1.0f), View );
- output.Pos = mul( output.Pos, Projection);
- //set texture coords
- output.texCo = input.texCo;
- //set required lighting vectors for interpolation
- output.Normal = normalize( mul(input.Normal, (float3x3)World) );
- return output;
- }
- float4 PS_PIXEL_LIGHTING_PHONG( PSSceneIn input ) : SV_Target
- {
- //calculate lighting vectors - renormalize vectors
- input.Normal = normalize( input.Normal );
- float3 V = normalize( CameraPos - (float3) input.WorldPos );
- //DONOT USE -light.dir since the reflection returns a ray from the surface
- float3 R = reflect( light.dir, input.Normal);
- //calculate lighting
- float4 I = calcPhongLighting( material, light.color, input.Normal, -light.dir, V, R );
- return I * gDiffuseMap.Sample(linearSampler, input.texCo);
- }
- //-----------------------------------------------------------------------------------------
- // PixelShader: PSSceneMain
- //-----------------------------------------------------------------------------------------
- float4 PSScene(PSSceneIn input) : SV_Target
- {
- return gDiffuseMap.Sample(linearSampler, input.texCo);
- //return input.Color;
- //return float4(1,1,1,1);
- }
- //-----------------------------------------------------------------------------------------
- // Technique: RenderTextured
- //-----------------------------------------------------------------------------------------
- technique10 DrawLine
- {
- pass p0
- {
- // Set VS, GS, and PS
- SetVertexShader( CompileShader( vs_4_0, VS_PIXEL_LIGHTING_PHONG() ) );
- SetGeometryShader( NULL );
- SetPixelShader( CompileShader( ps_4_0, PS_PIXEL_LIGHTING_PHONG() ) );
- SetRasterizerState( NoCulling );
- SetDepthStencilState( EnableDepth, 0 );
- //SetDepthStencilState( DisableDepth, 0 );
- //SetDepthStencilState( EnableDepthTestOnly, 0 );
- }
- }
- struct SurfaceInfo
- {
- float3 pos;
- float3 normal;
- float4 diffuse;
- float4 spec;
- };
- float3 PointLight(SurfaceInfo v, Light L, float3 eyePos)
- {
- float3 litColor = float3(0.0f, 0.0f, 0.0f);
- // The vector from the surface to the light.
- float3 lightVec = L.pos - v.pos;
- // The distance from surface to light.
- float d = length(lightVec);
- if( d > L.range )
- return float3(0.0f, 0.0f, 0.0f);
- // Normalize the light vector.
- lightVec /= d;
- // Add the ambient light term.
- litColor += v.diffuse * L.ambient;
- // Add diffuse and specular term, provided the surface is in
- // the line of sight of the light.
- float diffuseFactor = dot(lightVec, v.normal);
- [branch]
- if( diffuseFactor > 0.0f )
- {
- float specPower = max(v.spec.a, 1.0f);
- float3 toEye = normalize(eyePos - v.pos);
- float3 R = reflect(-lightVec, v.normal);
- float specFactor = pow(max(dot(R, toEye), 0.0f), specPower);
- // diffuse and specular terms
- litColor += diffuseFactor * v.diffuse * L.diffuse;
- litColor += specFactor * v.spec * L.spec;
- }
- // attenuate
- return litColor / dot(L.att, float3(1.0f, d, d*d));
- }
Add Comment
Please, Sign In to add comment