string description = "SelfIlluminatingParticleRendering"; /************* UN-TWEAKABLES **************/ float4x4 vM < string UIWidget="None"; >; float4x4 pM < string UIWidget="None"; >; float4x4 vP; float3 colourTint; float3 camPos; float4 inverseParams; float3 inverseOffset; float3 inverseScale; // 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 } }; /*********** Tweakables **********************/ int channelID < string UIWidget = "slider"; int UIMin = 0; int UIMax = 3; int UIStep = 1; > = 0; int2 tileDimensions = int2(4,4); 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; float depthOffset < string UIWidget = "slider"; float UIMin = -5.0; float UIMax = 5.0; float UIStep = 0.01; > = 1.0; float depthScale < string UIWidget = "slider"; float UIMin = 0.0; float UIMax = 5.0; float UIStep = 0.01; > = 0.5; /************* Textures ******************/ texture cm : Diffuse < string ResourceType = "2D"; >; sampler2D baseColourMap = sampler_state { Texture = ; MinFilter = LinearMipMapLinear; MagFilter = Linear; }; texture nm : Normal < string ResourceType = "2D"; >; sampler2D baseNormalMap = sampler_state { Texture = ; MinFilter = LinearMipMapLinear; MagFilter = Linear; }; texture dm < string ResourceType = "2D"; >; sampler2D baseDepthMap = sampler_state { Texture = ; MinFilter = LinearMipMapLinear; MagFilter = Linear; }; /************* INCLUDES *****************/ #include "..\\Basic_Utils.cgfx" #include "VFX_BasicDepth_Alpha.cgfx" /************* 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; float4 screen : TEXCOORD3; float4 binormal : TEXCOORD4; }; /*********** 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 = IN.position.w - (index * 10.0f); // 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(camPos.xyz, 1.f)).xyz*/ - mul(vM, float4(IN.position.xyz, 1.f)).xyz; //OUT.normal.xyz = camPos.xyz - IN.position.xyz; //OUT.normal.xyz = mul(vM, float4(OUT.normal.xyz, 0.f)).xyz; OUT.normal.xyz = normalize(OUT.normal.xyz); // Get the binormal OUT.binormal.xyz = GetBiNormal(OUT.normal.xyz, tangent); OUT.binormal.w = scale.x; // Calculate the offsets offset *= scale; offset = ((OUT.binormal.xyz * offset.x) + (tangent.xyz * offset.y) + (OUT.normal.xyz * offset.z)); OUT.position.xyz += offset; 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; // we never use blue anyway... OUT.screen = OUT.position; // pack the tangent OUT.texCoord.w = tangent.x; OUT.colour.w = tangent.y; OUT.normal.w = tangent.z; return OUT; } /********* fragment program ********/ void simpleFP(vertexOutput IN, out float4 colour : COLOR0, out float4 normal : COLOR1, out float depth : DEPTH) { // Cull pixels not covering the correct channel float2 texCoord = ((IN.screen.xy/IN.screen.w) + 1.f.xx) * 0.5h.xx; int2 sample = texCoord.xy * float2(1280.f, 720.f); // use floats and round to reduce error? int2 tileIndices = int2(sample.x %tileDimensions.x, sample.y %tileDimensions.y); int2 channel; channel.x = channelID %tileDimensions.x; channel.y = (channelID-channel.x) / tileDimensions.x; int2 error = tileIndices - channel; if((error.x != 0) || (error.y != 0)) discard; // Get the pixel colour and bail if the alpha is too low colour = tex2D(baseColourMap, IN.texCoord.xy); colour.rgb *= IN.colour.rgb; colour.a *= IN.texCoord.z; if(colour.a <= 0.01f) discard; // Sample the luminance map float4 normalBlend = tex2D(baseNormalMap, IN.texCoord.xy); float depthValue = tex2D(baseDepthMap, IN.texCoord.xy).r; // get the blendmask half2 blendMask = normalBlend.xy; // get the normal half3 nM; nM.xy = normalBlend.zw; // unpack the tangent half3 tangent = half3(IN.texCoord.w, IN.colour.w, 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.xyz + nM.y * tangent.xyz + nM.z * IN.normal.xyz); //nM = lerp(half3(0.f, 0.f, 1.f), nM, colour.a); //depthOffset = 0.f; //depthScale = 1.f; // Calculate the depth depthValue = (depthValue - 0.5f) ; // put into range -0.5,0.5 depthValue -= depthOffset; // offset to origin of particle depthValue *= depthScale * IN.binormal.w; // scale to size of particle float viewDepth = inverseParams.w / ((IN.screen.z/IN.screen.w) + inverseParams.z); viewDepth += depthValue; depth = (inverseParams.w / viewDepth) - inverseParams.z; depth = ((depth)/inverseScale.z)-inverseOffset.z; depth = saturate(depth); colour = float4(colour.rgb * IN.colour.rgb, blendMask.g * reflectivity); normal = half4(nM.x, nM.y, /*clamp(blendMask.r * specularInt, 0, 0.95h) + ceil(specularExp)*/ 0, sign(IN.normal.z) * viewDepth); //colour.rgb = IN.binormal; } void simple1FP(vertexOutput IN, out float4 colour : COLOR0, out float4 normal : COLOR1) { // Cull pixels not covering the correct channel float2 texCoord = ((IN.screen.xy/IN.screen.w) + 1.f.xx) * 0.5h.xx; int2 sample = texCoord.xy * float2(1280.f, 720.f); // use floats and round to reduce error? int2 tileIndices = int2(sample.x %tileDimensions.x, sample.y %tileDimensions.y); int2 channel; channel.x = channelID %tileDimensions.x; channel.y = (channelID-channel.x) / tileDimensions.x; int2 error = tileIndices - channel; if((error.x != 0) || (error.y != 0)) discard; // Get the pixel colour and bail if the alpha is too low colour = tex2D(baseColourMap, IN.texCoord.xy); colour.rgb *= IN.colour.rgb; colour.a *= IN.texCoord.z; if(colour.a <= 0.01f) discard; // Sample the luminance map float4 normalBlend = tex2D(baseNormalMap, IN.texCoord.xy); float depthValue = tex2D(baseDepthMap, IN.texCoord.xy).r; // get the blendmask half2 blendMask = normalBlend.xy; // get the normal half3 nM; nM.xy = normalBlend.zw; // unpack the tangent half3 tangent = half3(IN.texCoord.w, IN.colour.w, 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.xyz + nM.y * tangent.xyz + nM.z * IN.normal.xyz); //nM = lerp(half3(0.f, 0.f, 1.f), nM, colour.a); colour = float4(colour.rgb * IN.colour.rgb, colour.a); normal = half4(nM.x, nM.y, /*clamp(blendMask.r * specularInt, 0, 0.95h) + ceil(specularExp)*/ 0, colour.a); //colour.rgb = IN.binormal; } /*************/ technique basic < string layer = "3DScene-Alpha" ; > { pass p0 { VertexProgram = compile vp30 simpleVP(); FragmentProgram = compile fp40 simpleFP(); DepthTestEnable = true; DepthMask = true; AlphaTestEnable = false; CullFaceEnable = true; CullFace = Back ; StencilTestEnable = true; StencilFunc = int3( Always , 1 , 1 ); StencilOp = int3( Keep , Keep , Replace ); ClearStencil = 0 ; } pass p1 { VertexProgram = compile vp30 simpleVP(); FragmentProgram = compile fp40 simple1FP(); DepthTestEnable = false; DepthMask = false; BlendEnable = true; BlendFuncSeparate = int4( SrcAlpha , OneMinusSrcAlpha , Zero , One ) ; AlphaTestEnable = false; CullFaceEnable = true; CullFace = Back ; StencilTestEnable = true; StencilFunc = int3( Equal , 1 , 1 ); StencilMask = 0 ; } pass shadowPass < string layer = "Shadow" ; string excludeData = "NoShadowCast" ; > { VertexProgram = compile vp20 BasicDepthVP(); FragmentProgram = compile fp20 BasicDepthFP(); DepthTestEnable = true; DepthMask = true; ColorMask = bool4( false , false , false , false ); BlendEnable = false; AlphaTest = false; CullFaceEnable = false; } } /***************************** eof ***/