/////////////////////////////////////////////////////////////////////// // Utility Effects // // *** INTERACTIVE DATA VISUALIZATION (IDV) PROPRIETARY INFORMATION *** // // This software is supplied under the terms of a license agreement or // nondisclosure agreement with Interactive Data Visualization and may // not be copied or disclosed except in accordance with the terms of // that agreement. // // Copyright (c) 2003-6 IDV, Inc. // All Rights Reserved. // // IDV, Inc. // Web: http://www.idvinc.com // // *** Release Version 4.0 *** // Some generic definitions to simplify shader code - Morv //!MP! I use this in shadows.h, so I can use the same header file for C code //MP! with hierarchical z-testing, this is no longer LE of all platforms // Some max-specific definitions //////////////////////////////////////////////////////////////////////////////////////////////////// // constants in HLSL //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// // these are VERTEX SHADER constants //////////////////////////////////////////////////////////////////////////////////////////////////// // 15..26 used by generic constant table, and facial morphing weights float4 PA_StartColour : register(c9); float4 PA_EndColour : register(c10); float4 PA_StartMMEndMMSize : register(c11); float4 PA_Velocity_Alpha_Rotation_Min_AspectRatio : register(c14); float4 PA_Velocity_Alpha_Rotation_Max_Gravity : register(c15); float PA_DrawAsBillboard : register(c16); float4 SpreadNumParticlesAnimationCountLoopSystem : register(c18); float4 PA_MidMinMaxEndMinMaxVelocity : register(c19); float4 PA_EmissionArea : register(c21); float4 PA_ExpiryTime : register(c23); float4 PA_AnimationFrames : register(c24); float4 PA_Wind : register(c26); float4 ParticleFadeValue : register(c28); float4 Radiosity_YCbCr [3] : register(c36); float4 TrapezoidalShadowEpsilon : register(c39); float4 LightIndexScalar : register(c40); float4 LightIndexOffset : register(c41); float4 Foliage_Animation_Scalars : register(c42); float4 Foliage_Animation_Offsets : register(c43); float4 LeafUnitSquare [4] : register(c44); float4 FontXYScalarOffset : register(c48); float4 FontUVScalarOffset : register(c49); float4 VCMainLightDirection : register(c50); float4 CubeShadowMap_LightPosition : register(c51); float4 FarFogPlane : register(c52); float4 CharacterInertiaAndVertexScale : register(c53); float4 DualBumpLODBias : register(c54); float4 VertexBonePaletteTexture : register(c55); float4x4 ShadowMapMatrix : register(c56); float4x4 WorldViewProjection : register(c60); float4x4 WorldMatrix : register(c64); float4x4 ViewMatrix : register(c68); float4x4 ViewProj : register(c72); float4x4 TextureTransform : register(c76); float4 ScreenRes [2] : register(c80); float4 ParticleRoot : register(c88); float4 TimeIntoEffect : register(c89); float4 RadiosityInfluence : register(c90); float4 TexCoordScaleLODBlur : register(c91); // used by the cube variance shadow map blending float4x4 ProjMatrix : register(c92); float4 Bones [96] : register(c150); float4 RicsFontShaderConstants : register(c100); float4 BillboardTextureCoords [60] : register(c100); float4 CameraRightAxis : register(c160); float4 CameraUpAxis : register(c161); float4 InstancingData : register(c162); float4 TreePos : register(c163); float4 TreeRot : register(c164); float4 TreeScale : register(c165); float4x4 WindMatrices [7] : register(c180); float4 WindSettings : register(c208); float4 SpeedTreeLightSettings : register(c209); float4 CameraAngles : register(c210); float4 SelfShadowMotion : register(c211); float4 LeafAngles [20] : register(c216); float4x4 BillboardNormals : register(c236); float4x4 BillboardBinormals : register(c240); float4x4 BillboardTangents : register(c244); float4 BillboardSpecialAzimuth : register(c248); float4 FoliageSettings : register(c249); float4 PS3StaticLightData [33 * 2] : register(c352); //////////////////////////////////////////////////////////////////////////////////////////////////// // these are PIXEL SHADER constants //////////////////////////////////////////////////////////////////////////////////////////////////// float4 FogColour : register(c0); float4 SpecularPower : register(c2); float4 MainLightInvColor : register(c7); // inverse direction light color - to show off bump maps float4 DualBumpNormalErrorThreshold : register(c8); float4 ShadowBias : register(c9); float4 MainLightDirection : register(c10); float4 MainLightColor : register(c11); float4 VarianceShadowMap_Setup : register(c12); float4 VarianceShadowMap_EdgeDelta : register(c13); float4 ShadowMapDimensions : register(c14); // 15..26 used by generic constant table float4 RadiosityCSC : register(c29); float4 RayleighMieConstants : register(c30); float BodySolidity : register(c31); float4 PostProcessing_BloomScalarOffset : register(c32); float4 PostProcessing_OverlaySettings : register(c33); float4 PostProcessing_Distortion : register(c34); // xy=distortion range, z=LOD range, w=luminance range float4 PostProcessing_TintScalar : register(c35); float4 PostProcessing_TintOffset : register(c36); float4 FilterOptions : register(c37); float4 DirectionalLightingIntensities : register(c38); float4 FogEquation [2] : register(c39); float4 HDR_Constants : register(c41); float4 AmbientDiffuse : register(c42); float4 DirectLightingInfluence : register(c43); float4 DynamicLightData [12] : register(c44); // 12 used (ie 4 lights, 1 extra just in case...), followed by static data float4 DynamicLight0_Data : register(c44); float4 DynamicLight1_Data : register(c47); float4 DynamicLight2_Data : register(c50); float4 DynamicLight3_Data : register(c53); float4 StaticLightData [96] : register(c56); float4 AmbientScalar : register(c142); // used by dynamic objects to offset the ambient values float4 AmbientOffset : register(c143); // ... half4 PostProcessing_TintEq [4] : register(c150); float4 DOFFocalPointSpread : register(c154); float4 DOFReverseZPerspective : register(c155); float4 DOFKernelConstants : register(c156); float4 MotionBlurMovement : register(c157); float4 MotionBlurConstants : register(c158); float4x4 MotionBlurCurrInvViewProjection : register(c160); float4x4 MotionBlurPrevViewProjection : register(c164); float4 RayleighMieScatteringWavelengthsQuad : register(c168); // wavelengths used by rayleigh/mie to the 4th, ie "lambda^4" float4 CloudTextureDef0 : register(c169); // scrolling float4 CloudTextureDef1 : register(c170); // scrolling float4 CloudTextureDef2 : register(c171); // scrolling float4 CloudTextureDef3 : register(c172); // rotating float4 CloudBlendParams : register(c173); // blend params float4 RayleighMieSunPosition : register(c174); // blend params float4 RayleighMieRGBTranslation [3] : register(c175); // translates RGB -> whichever color float4 ShadowParameters : register(c179); // scalar, offset //////////////////////////////////////////////////////////////////////////////////////////////////// bool CubicShadowReceive : register(b0); bool RadiosityGatheringInProgress : register(b1); //////////////////////////////////////////////////////////////////////////////////////////////////// // these are VERTEX AND PIXEL SHADER constants //////////////////////////////////////////////////////////////////////////////////////////////////// float4 ConstantColour : register(c1); // !MP! do not rely on this; it's part of the old lighting system and will disappear after this milestone float4 TimeAndRainLevel : register(c3); // in .x float4 RainLevel : register(c3); // in .y float4 CameraPosition : register(c5); // 15..26 used by generic constant table //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// // Generic constants (loaded into a table at c15, but the shaders that use them don't know that) //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // radiosity backface hack //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// float lfInputFaceDirection = 0.f; // less than ideal - this will internally be 'overwritten', if a variable of this name exists locally float4 BlankReverseSideDuringRadiosityGathering( float lfFaceDirection, float4 lOutputColor) { //not necessary any longer i think return lOutputColor; return lOutputColor; } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // snow //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// float3 SnowValue (float3 input_colour, float world_space_normal_y) { return lerp (input_colour, float3 (1, 1, 1), saturate (world_space_normal_y - 0.5f) * (TimeAndRainLevel.z)); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // transformed vertex z position to pixel depth calculation : used for soft sprites //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// float VertexToPixelDepth(float PositionZ) // PositionZ = mul(float4(world_pos, 1), ViewProj).z; { return DOFReverseZPerspective.x / (PositionZ * DOFReverseZPerspective.y - DOFReverseZPerspective.z); // m32/(o*m23-m22) } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // color range tweak //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// static const float g_overbright = 32; // Ivan: experimentally shown that hdr range of 99% of the scene is 0..4 //static const float MAX_HDR_RANGE = 3.5f; // test stuff //half4 RGBHighlightRange( half4 c, float mv ) //{ // half scale = max( c.x, max( c.y, c.z ) ); // // if( scale > mv ) // { // c = half4( 1, 0, 0, 1 ); // } // // return c; //} // classic rgbs //half4 RGB_to_RGBS( half4 c ) //{ // half scale = max( c.x, max( c.y, c.z ) ); // // half3 rgb = c.xyz / scale; // half a = saturate( scale / MAX_HDR_RANGE ); // // return half4( rgb, a ); //} // classic rgbs //half4 RGBS_to_RGB( half4 c ) //{ // return half4( c.xyz * c.a * MAX_HDR_RANGE, c.a ); //} // classic exponential //half4 RGB_to_ScaledRGB( half4 c ) //{ // return half4( c.xyz / MAX_HDR_RANGE, c.a ); //} // classic exponential //half4 ScaledRGB_to_RGB( half4 c ) //{ // return half4( c.xyz * MAX_HDR_RANGE, c.a ); //} // classic exponential // TODO: use halfs throughout for better compilation //half4 RGB_to_PPP( half4 c ) //{ // return half4( sqrt( ( half3( c.xyz ) / half( MAX_HDR_RANGE ) ) ), c.a ); //} // classic exponential //half4 PPP_to_RGB( half4 c ) //{ // return half4( c.xyz * c.xyz * MAX_HDR_RANGE, c.a ); //} //float4 RGBA_to_SonyRGBS( float4 ) static const float fLinearRange = 2.f; // scales the output color float4 HDRColorCorrected(float4 lColor) { float3 corrected = lColor.xyz / fLinearRange; return float4( corrected, lColor.a ); } // scales the output color float4 HDRColorCorrected0(float4 lColor) { return float4( lColor.xyz / fLinearRange, lColor.a ); } // scales the output color half4 HDRColorCorrected(half4 lColor) { half3 corrected = lColor.xyz / (half)fLinearRange; return half4( corrected, lColor.a ); } float4 HDRColorReverted(float4 lColor) { float multiply = lColor.a == 0 ? g_overbright : 1; return float4( lColor.xyz * fLinearRange * multiply, 1.f ); } half4 HDRColorReverted(half4 lColor) { half multiply = lColor.a == 0 ? (half)g_overbright : 1; return half4( lColor.xyz * (half)fLinearRange * multiply, 1.f ); } float4 HDRForceReversion(float4 lColor) { return lColor; } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // normal = tangent X normal //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// float3 CreateBinormal( float3 lNormal, float3 lTangent) { //!MP! i actually thought it was the other way around, but this produces consistent results return cross(lTangent,lNormal); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // color space conversion //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // performs color space conversion float3 ConvertYCbCr2RGB( float3 lYCbCr) { float3x4 lNewMatrix; // Keef - Casting to float3x4 doesn't work with Cg for some reason lNewMatrix._m00_m01_m02_m03 = Radiosity_YCbCr[0]; lNewMatrix._m10_m11_m12_m13 = Radiosity_YCbCr[1]; lNewMatrix._m20_m21_m22_m23 = Radiosity_YCbCr[2]; //!MP! the PS3 should use this version as well return max(0,mul(lNewMatrix,float4(lYCbCr,1))); } // we use YCbCr rather than YUV due to YUV's limited range float3 DecompressYCbCr844( int2 lYCbCr) { // get Y:8,Cb:4,Cr:4 from the zw components (Cb,Cr are packed into the second byte) float lfY = lYCbCr.x; float lfCr = lYCbCr.y*0.0625f; //1/16 float lfCb = floor(lfCr); lfCr = lfCr-lfCb; float4 lfYCbCr1 = float4(lfY,lfCb,lfCr,1); // Keef - Casting to float3x4 doesn't work with Cg for some reason float3x4 lNewMatrix; lNewMatrix._m00_m01_m02_m03 = Radiosity_YCbCr[0]; lNewMatrix._m10_m11_m12_m13 = Radiosity_YCbCr[1]; lNewMatrix._m20_m21_m22_m23 = Radiosity_YCbCr[2]; float3 lRGB = saturate(mul(lNewMatrix,lfYCbCr1)); return lRGB; } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // character UV scalar - centralized, as editing only the standard shaders mess up the shadows //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //The 360 FXLite differs from the PC FX interfaces in one important area. //The textures can only be shared on the PC and the 360 can only share and read samplers. //So when you need to define and use a texture and sampler use the macros below. // Max MapChannels make me cry // #define _USE_INSTANCING_ // #define BRANCH_DETAIL_LAYER float3 test_ambient = float3(0.8f, 0.8f, 0.8f); /////////////////////////////////////////////////////////////////////// // Modulate_Float float Modulate_Float(float x, float y) { return x - (int(x / y) * y); } float3 WindEffect(float3 vPosition, float2 vWindInfo, float wind_matrix_offset) { // return vPosition; // decode both wind weights and matrix indices at the same time in order to save // vertex instructions float2 info = vWindInfo; // info.xy += wind_matrix_offset.xx; float2 vWeights = frac(info.xy); float2 vIndices = (info - vWeights) * 0.05f * WindSettings.x; vIndices.x += wind_matrix_offset; vIndices.y += wind_matrix_offset; vIndices = clamp(vIndices, 0, WindSettings.x - 1); // first-level wind effect - interpolate between static position and fully-blown // wind position by the wind weight value // float3 vWindEffect = lerp(vPosition.xyz, mul(vPosition, WindMatrices[int(vIndices.x)]), vWeights.x); // Changed this to compile on PS3. I suspect the behaviour might be wrong on other platforms though! float3 vWindEffect = lerp(vPosition.xyz, mul(vPosition, float3x3( WindMatrices[int(vIndices.x / 4)] ) ), vWeights.x); // second-level wind effect - interpolate between first-level wind position and // the fully-blown wind position by the second wind weight value // Changed this to compile on PS3. I suspect the behaviour might be wrong on other platforms though! vWindEffect = lerp(vWindEffect, mul(vWindEffect, float3x3( WindMatrices[int(vIndices.y / 4)] ) ).xyz, vWeights.y); return vWindEffect; } /////////////////////////////////////////////////////////////////////// // LightDiffse // // Very simple lighting equation, used by the fronds and leaves (branches // are normal mapped). /* float3 LightDiffse(float3 vVertex, float3 vNormal, float3 vLightDir, float3 vLightColor, float3 vDiffuseMaterial) { // return vLightColor * max(dot(vNormal, vLightDir), 0.0f); return vLightColor * max(dot(vNormal, vLightDir), vDiffuseMaterial); } */ float3 LightDiffuse(float3 vVertex, float3 vNormal, float3 vLightDir, float3 vLightColor, float3 vDiffuseMaterial) { // return vDiffuseMaterial * vLightColor * max(dot(vNormal, vLightDir), 0); return vDiffuseMaterial * vLightColor * dot(vLightDir, vNormal); } /////////////////////////////////////////////////////////////////////// // LightDiffse_Capped // // Very simple lighting equation, used by the fronds and leaves (branches // are normal mapped). float3 LightDiffse_Capped(float3 vVertex, float3 vNormal, float3 vLightDir, float3 vLightColor, float3 vDiffuseMaterial) { float fDotProduct = max(dot(vNormal, vLightDir), 0.0f); fDotProduct = clamp(fDotProduct, 0, 1); fDotProduct = lerp(SpeedTreeLightSettings.y, 1.0f, fDotProduct); // return vDiffuseMaterial * vLightColor * fDotProduct; return vLightColor * fDotProduct; } /////////////////////////////////////////////////////////////////////// // RotationMatrix_zAxis // // Constructs a Z-axis rotation matrix float3x3 RotationMatrix_zAxis(float fAngle) { // compute sin/cos of fAngle float2 vSinCos; sincos(fAngle, vSinCos.x, vSinCos.y); return float3x3(vSinCos.y, -vSinCos.x, 0.0f, vSinCos.x, vSinCos.y, 0.0f, 0.0f, 0.0f, 1.0f); } /////////////////////////////////////////////////////////////////////// // RotationMatrix_yAxis // // Constructs a Y-axis rotation matrix float3x3 RotationMatrix_yAxis(float fAngle) { // compute sin/cos of fAngle float2 vSinCos; sincos(fAngle, vSinCos.x, vSinCos.y); return float3x3(vSinCos.y, 0.0f, vSinCos.x, 0.0f, 1.0f, 0.0f, -vSinCos.x, 0.0f, vSinCos.y); } /////////////////////////////////////////////////////////////////////// // RotationMatrix_xAxis // // Constructs a X-axis rotation matrix float3x3 RotationMatrix_xAxis(float fAngle) { // compute sin/cos of fAngle float2 vSinCos; sincos(fAngle, vSinCos.x, vSinCos.y); return float3x3(1.0f, 0.0f, 0.0f, 0.0f, vSinCos.y, -vSinCos.x, 0.0f, vSinCos.x, vSinCos.y); } /////////////////////////////////////////////////////////////////////// // // These are here for debug purposes and the fact that the // fx replacement stuff removes global variables from shaders. // /////////////////////////////////////////////////////////////////////// int get_billboard_index(int active_pass, float adjusted_azimuth, float slice_diameter, int num_images) { int index; if(!active_pass) { index = int(adjusted_azimuth / slice_diameter + 1); } else { index = int(adjusted_azimuth / slice_diameter); } if (index > num_images - 1) index = 0; return index; } float get_billboard_vertex_alpha(int active_pass, int index, float adjusted_azimuth, float slice_diameter) { // return 0; float alpha; if(!active_pass) { alpha = 1.0f - Modulate_Float(adjusted_azimuth, slice_diameter) / slice_diameter; } else { alpha = (adjusted_azimuth - (index * slice_diameter)) / slice_diameter; } return alpha; } float get_tree_light_scale() { return 1.0f; } float4 get_tree_ambient_light() { float4 col = float4(0.5, 0.45, 0.45, 1.0f); // float4 col = ConstantColour; // float4 col = PosLightCol; return saturate(col * get_tree_light_scale()); } float4 get_tree_diffuse_light() { float4 col = float4(0.6, 0.55, 0.55, 1.0f); // float4 col = PosLightCol2; return saturate(col * get_tree_light_scale()); } float3 get_light_dir(float3 vertex_world_pos) { // return float3(-1, 0, 0); // float3 light_dir = vertex_world_pos - PosLightPos; // light_dir = normalize(light_dir); // return light_dir; return float3(-0.707f, 0, 0.707f); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // instance data gather //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// struct sInstanceData3D { float4 data0; float4 data1; float4 data2; }; struct sInstanceData4D { float4 data0; float4 data1; float4 data2; float4 data3; }; ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////