string description = "SelfIlluminatingParticleRendering"; /************* UN-TWEAKABLES **************/ float4x4 ivM; float4x4 vM < string UIWidget="None"; >; float4x4 pM < string UIWidget="None"; >; float4x4 vP; float3 colourTint; float4 inverseParams; #include "..\\Basic_Utils.cgfx" #include "VFX_Utils.cgfx" /*********** Tweakables **********************/ ///// float3 lightColour : Diffuse < string UIWidget = "Color"; > = {1.0f, 1.0f, 1.0f}; ///// float3 lightPosition : Position < string Object = "PointLight0"; string Space = "World"; > = float3(100.0f, 100.0f, 100.0f); ////// float3 ambientColour : Ambient < string UIWidget = "Color"; > = {0.42f, 0.42f, 0.42f}; ///// float specularInt < string UIWidget = "slider"; float UIMin = 0.0; float UIMax = 5.0; float UIStep = 0.01; > = 0.6; float specularExp < string UIWidget = "slider"; float UIMin = 1.0; float UIMax = 128.0; float UIStep = 1.0; > = 48.0; ////// float reflectivity < string UIWidget = "slider"; float UIMin = 0.0; float UIMax = 1.0; float UIStep = 0.01; > = 0.4; ////// float uvTileBase < string UIWidget = "slider"; float UIMin = 0.0; float UIMax = 5.0; float UIStep = 0.01; > = 1.0; /************* Textures ******************/ texture cm : Diffuse < string ResourceType = "2D"; >; sampler2D baseColourMap = sampler_state { Texture = ; MinFilter = LinearMipMapLinear; MagFilter = Linear; }; texture nm : Normal < string ResourceType = "2D"; >; sampler2D baseLightingMap = sampler_state { Texture = ; MinFilter = LinearMipMapLinear; MagFilter = Linear; }; texture envMap : Environment < string ResourceType = "Cube"; >; samplerCUBE cubeMap= sampler_state { Texture = ; MinFilter = LinearMipMapLinear; MagFilter = Linear; WrapS = ClampToEdge; WrapT = ClampToEdge; }; texture diffEnvMap : Environment < string ResourceType = "Cube"; >; samplerCUBE diffCubeMap= sampler_state { Texture = ; MinFilter = LinearMipMapLinear; MagFilter = Linear; WrapS = ClampToEdge; WrapT = ClampToEdge; }; // Note these default values are all ignored anyway by the PS3 float3 offsets[4] = { { -0.5f, -0.5f, 0.0f }, { -0.5f, 0.5f, 0.0f }, { 0.5f, 0.5f, 0.0f }, { 0.5f, -0.5f, 0.0f } }; /************* DATA STRUCTS **************/ /* data from application vertex buffer */ struct appdata { float4 position : POSITION; float4 tangent : TEXCOORD0; float4 scale : TEXCOORD1; float4 texCoord : TEXCOORD2; }; /* data passed from vertex shader to pixel shader */ struct vertexOutput { float4 position : POSITION; float4 texCoord : TEXCOORD0; float4 colour : TEXCOORD1; float4 normal : TEXCOORD2; float2 screen : TEXCOORD3; float3 binormal : TEXCOORD4; float3 viewPos : TEXCOORD5; half3 lightDir : TEXCOORD6; }; /*********** vertex program ******/ vertexOutput simpleVP(appdata IN) { vertexOutput OUT = (vertexOutput)0; OUT.position = mul( vM, float4( IN.position.xyz, 1.f)); // Get the vertex's Index float indexf = IN.position.w / 10.0f; int index = round(indexf); // Get vertex's offsets half3 offset = offsets[index]; // Get the Opacity float opacity = GetOpacity(IN.position.w, index); // Get the Tangent (normalised in code) half3 tangent = IN.tangent.xyz; // Get the Scale half3 scale = IN.scale.xyz; // Get the Normal in view space OUT.normal.xyz = - mul(vM, float4(IN.position.xyz, 1.f)).xyz; OUT.normal.xyz = normalize(OUT.normal.xyz); // Get the binormal OUT.binormal = GetBiNormal(OUT.normal.xyz, tangent); // Calculate the offsets offset *= scale; offset = ((OUT.binormal * offset.x) + (tangent * offset.y) + (OUT.normal.xyz * offset.z)); OUT.position.xyz += offset; OUT.viewPos = OUT.position.xyz; OUT.position = mul(pM, OUT.position); OUT.texCoord.xy = IN.texCoord.xy; OUT.texCoord.z = opacity; OUT.colour.r = IN.tangent.w; OUT.colour.gb = IN.texCoord.zw; OUT.colour.rgb *= colourTint; OUT.colour.a = opacity; // we never use blue anyway... OUT.screen.xy = OUT.position.zw; // pack the tangent OUT.texCoord.zw = tangent.xy; OUT.normal.w = tangent.z; float4 lightPos = float4(lightPosition, 1.f); OUT.lightDir = normalize(mul(vM, lightPos) - OUT.viewPos.xyz); return OUT; } /********* fragment program ********/ void simpleFP(vertexOutput IN, out float4 colour : COLOR0) { // Get the pixel colour and bail if the alpha is too low float4 texColour = tex2D(baseColourMap, IN.texCoord.xy); texColour *= IN.colour; // Sample the luminance map float4 normalBlend = tex2D(baseLightingMap, IN.texCoord.xy); // get the blendmask half2 blendMask = normalBlend.xy; // get the normal half3 nM; nM.xy = normalBlend.zw; // unpack the tangent half3 tangent = half3(IN.texCoord.zw, IN.normal.w); //generate normal and world normal nM.xy -= 0.5f.xx; nM.xy *= 2.f.xx; nM = DecodeNormal(nM.xy); // nM.xy += nM.xy * 0.25f; // exagerate normals // nM = normalize(nM); nM = normalize(nM.x * IN.binormal + nM.y * tangent + nM.z * IN.normal); half3 view = normalize(IN.viewPos); half3 normal = nM; specularInt *= blendMask.r; /// *** AMBIENT *** /// // sample ambient colours half3 reflection = normalize(reflect(view, normal)); reflection = mul(ivM, half4(reflection, 0.h)).xyz; half3 envMap = texCUBE(cubeMap, reflection).xyz; half3 worldNormal = mul(ivM, half4(normal, 0.h)).xyz; half3 diffEnvMap = texCUBE(diffCubeMap, worldNormal).xyz; // ambient lighting colour.rgb = texColour * ambientColour * diffEnvMap * 2.h; colour.rgb = lerp(colour.rgb, envMap, blendMask.g * reflectivity); /// *** DIRECTIONAL *** /// half3 litVec = getLitVec(IN.lightDir, normal, view, specularExp); half3 diffuse = litVec.y * lightColour * texColour; half specColour = litVec.z * specularInt; colour += half4((diffuse + specColour.xxx), 0); colour.a = texColour.a; colour.rgb *= colour.a; } /*************/ technique basic < string layer = "Transparent" ; > { pass p0 { VertexProgram = compile arbvp1 simpleVP(); FragmentProgram = compile arbfp1 simpleFP(); DepthTestEnable = true; DepthMask = false; BlendEnable = true; BlendFuncSeparate = int4( DstAlpha , OneMinusSrcAlpha , Zero , One ) ; AlphaTestEnable = true; } } /***************************** eof ***/