struct Mtrl{ float4 ambient; float4 diffuse; float4 spec; float specPower;};struct DirLight{ float4 ambient; float4 diffuse; float4 spec; float3 dirW; };uniform extern float4x4 gWorld;uniform extern float4x4 gWorldInv;uniform extern float4x4 gWorldInvTrans;uniform extern float4x4 gWVP;uniform extern Mtrl gMtrl;uniform extern DirLight gLight;uniform extern float3 gEyePosW;uniform extern texture gTex;uniform extern texture gEnvMap;uniform extern texture gNormalMap;uniform extern float gReflectivity;uniform extern float gNormalPower;uniform extern float gTexBlend;sampler TexS = sampler_state{ Texture = <gTex>; MinFilter = LINEAR; MagFilter = LINEAR; MipFilter = LINEAR; AddressU = WRAP; AddressV = WRAP;};sampler NormalMapS = sampler_state{ Texture = <gNormalMap>; MinFilter = ANISOTROPIC; MaxAnisotropy = 8; MagFilter = LINEAR; MipFilter = LINEAR; AddressU = WRAP; AddressV = WRAP;};sampler EnvMapS = sampler_state{ Texture = <gEnvMap>; MinFilter = LINEAR; MagFilter = LINEAR; MipFilter = LINEAR; AddressU = WRAP; AddressV = WRAP;};struct OutputVS{ float4 posH : POSITION0; float3 toEyeT : TEXCOORD0; float3 lightDirT : TEXCOORD1; float2 tex0 : TEXCOORD2; float3 normalW : TEXCOORD3; float3 toEyeW : TEXCOORD4;};OutputVS PhongEnvNMVS( float3 posL : POSITION0, float3 normalL : NORMAL0, float3 tangentL : TANGENT0, float3 binormalL : BINORMAL0, float2 tex0 : TEXCOORD0){ // Zero out the output. OutputVS outVS = (OutputVS)0; ////////////////////////// TANGENT SPACE CALCULATIONS: // Build Tangent Binormal-basis. float3x3 TBN; TBN[0] = tangentL; TBN[1] = binormalL; TBN[2] = normalL; // Transform matrix from object space to tangent space float3x3 toTangentSpace = transpose(TBN); // Transform eye position to local space. float3 eyePosL = mul(float4(gEyePosW, 1.0f), gWorldInv); // Transform to-eye vector to tangent space. float3 toEyeL = eyePosL - posL; // Transform light direction to tangent space. outVS.toEyeT = mul(toEyeL, toTangentSpace); /////////////////////////////////////////////////////// float3 lightDirL = mul(float4(gLight.dirW, 0.0f), gWorldInv).xyz; outVS.lightDirT = mul(lightDirL, toTangentSpace); // Transform to homogeneous clip space. outVS.posH = mul(float4(posL, 1.0f), gWVP); // Pass texture coordinates to be interpolated during rasterization. outVS.tex0 = tex0; outVS.normalW = mul(float4(normalL, 0.0f), gWorldInvTrans).xyz; float3 posW = mul(float4(posL, 1.0f), gWorld).xyz; outVS.toEyeW = gEyePosW - posW; return outVS;}float4 PhongEnvNMPS( float3 toEyeT : TEXCOORD0, float3 lightDirT : TEXCOORD1, float2 tex0 : TEXCOORD2, float3 normalW : TEXCOORD3, float3 toEyeW : TEXCOORD4) : COLOR { // Normalize, because interpolated normals can warp toEyeT = normalize(toEyeT); lightDirT = normalize(lightDirT); normalW = normalize(normalW); toEyeW = normalize(toEyeW); // Light vector is opposite the direction of the light. float3 lightVecT = -lightDirT; // Sample normal map. float3 normalT = tex2D(NormalMapS, tex0); // Expand from [0, 1] compressed interval to true [-1, 1] interval. normalT = (2.0f*normalT - 1.0f) * gNormalPower; // Make it a unit vector. normalT = normalize(normalT); //Compute the normal reflection vector float rN = reflect(-lightVecT, normalT); // Determine the diffuse light intensity that strikes the vertex. float tN = pow(max(dot(rN, toEyeT), 0.0f), gMtrl.specPower); float sN = max(dot(lightVecT, normalT), 0.0f); // If the diffuse light intensity is low, kill the specular lighting term. // It doesn't look right to add specular light when the surface receives // little diffuse light. if(sN <= 0.0f) tN = 0.0f; // Sample the environment map and get the reflected color + add the normal component to it float3 envMapTex = reflect(-toEyeW, normalW) + (normalT*gNormalPower); float3 reflectedColor = texCUBE(EnvMapS, envMapTex); // Light vector is opposite the direction of the light. float3 lightVecW = -gLight.dirW; // Compute the reflection vector. float3 r = reflect(-lightVecW, normalW); // Determine how much (if any) specular light makes it into the eye. float t = pow(max(dot(r, toEyeW), 0.0f), gMtrl.specPower); float s = max(dot(lightVecW, normalW), 0.0f); // Get the texture color, and multiply it by how blended it should be. float4 texColor = tex2D(TexS, tex0) * gTexBlend; // Weighted average between the reflected color, and usual // diffuse/ambient material color modulated with the texture color. float3 ambientMtrl = gReflectivity*reflectedColor + (1.0f-gReflectivity)*(gMtrl.ambient*texColor); float3 diffuseMtrl = gReflectivity*reflectedColor + (1.0f-gReflectivity)*(gMtrl.diffuse*texColor); // Compute the ambient, diffuse and specular terms separately. float3 spec = tN*(gMtrl.spec*gLight.spec).rgb; float3 diffuse = sN*(diffuseMtrl*gLight.diffuse.rgb); float3 ambient = ambientMtrl*gLight.ambient; // Compute the final color: ambient + diffuse + specular: float3 final = (ambient + diffuse)*texColor.rgb + spec; // Output the color and the alpha. return float4(final, gMtrl.diffuse.a*texColor.a);}technique PhongEnvNMTech{ pass P0 { // Specify the vertex and pixel shader associated with this pass. vertexShader = compile vs_2_0 PhongEnvNMVS(); pixelShader = compile ps_2_0 PhongEnvNMPS(); }}
That looks pretty.
This looks really neat, though I don't really know what it's about.
Quote from: Sai on April 05, 2015, 09:58:00 PMThis looks really neat, though I don't really know what it's about.It's DirectX programming. Essentially, I've been programming the stuff that lets you see "3D" objects on your screen, how your computer handles being in a 3D world (using linear algebra to figure out where something is positioned locally, globally, through your view, etc...). Since I accomplished that a few weeks ago, the code I posted is for a shader, which is basically how a graphics card colors a 3D object: before, I had it do the computations relating to handling how light and color affect the look of the model; what I posted in the OP is how the computer makes something shiny, and that warpy effect that you get from looking at something shiny from another angle.I also did normal mapping and texture mapping, which means I wrapped this around the model (kinda like applying a coat of paint in a very specific way):And then used this normal map:To make the model give off the illusion of added depth/bumpiness. The shader goes through the normal map, and the color at each pixel of the map tells the shader how light should react at that spot on the model