[2236] | 1 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 2 | //
|
---|
| 3 | // ## ######
|
---|
| 4 | // ###### ###
|
---|
| 5 | // ## ############### Shark 3D Engine (www.shark3d.com)
|
---|
| 6 | // ########## # # #
|
---|
| 7 | // ######## Copyright (c) 1996-2006 Spinor GmbH.
|
---|
| 8 | // ######### # # # All rights reserved.
|
---|
| 9 | // ## ##########
|
---|
| 10 | // ##
|
---|
| 11 | //
|
---|
| 12 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 13 |
|
---|
| 14 | #ifndef STDDEF_D3D9_HLSL_SC
|
---|
| 15 | #define STDDEF_D3D9_HLSL_SC
|
---|
| 16 |
|
---|
| 17 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 18 |
|
---|
| 19 | #include \
|
---|
| 20 | <levelutil/shader/prog/d3d9_hlsl/include_stdstate_d3d9_hlsl.s3d_shadercode_run>
|
---|
| 21 |
|
---|
| 22 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 23 | // Compression
|
---|
| 24 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 25 |
|
---|
| 26 | float3 s3d_uncomprNormal(
|
---|
| 27 | float3 comprNormal)
|
---|
| 28 | {
|
---|
| 29 | #ifdef S3D_COMPR_NORMAL
|
---|
| 30 | return comprNormal / 127.5 - 1.0;
|
---|
| 31 | #else
|
---|
| 32 | return comprNormal;
|
---|
| 33 | #endif
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | float4 s3d_uncomprBoneWgh(
|
---|
| 37 | float4 comprBoneWgh)
|
---|
| 38 | {
|
---|
| 39 | #ifdef S3D_COMPR_BONEWGH
|
---|
| 40 | return comprBoneWgh / 255.0f;
|
---|
| 41 | #else
|
---|
| 42 | return comprBoneWgh;
|
---|
| 43 | #endif
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 47 | // Bones
|
---|
| 48 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 49 |
|
---|
| 50 | // default to 4 for bone transformation w/o bone subscriptions
|
---|
| 51 | #ifndef S3D_MATBONE_CNT
|
---|
| 52 | #define S3D_MATBONE_CNT 4
|
---|
| 53 | #endif
|
---|
| 54 |
|
---|
| 55 | // works w/ compressed and uncompressed bone weights.
|
---|
| 56 | void s3d_calcBoneTransfSubscr(
|
---|
| 57 | out float4x4 matBoneFinal, const float4x4 matBone[S3D_MATBONE_CNT],
|
---|
| 58 | const float4 boneWgh, const int4 boneSubscr)
|
---|
| 59 | {
|
---|
| 60 | float4 uncomprBoneWgh = s3d_uncomprBoneWgh(boneWgh);
|
---|
| 61 |
|
---|
| 62 | matBoneFinal = uncomprBoneWgh.x * matBone[boneSubscr.x];
|
---|
| 63 | matBoneFinal += uncomprBoneWgh.y * matBone[boneSubscr.y];
|
---|
| 64 | matBoneFinal += uncomprBoneWgh.z * matBone[boneSubscr.z];
|
---|
| 65 | matBoneFinal += uncomprBoneWgh.w * matBone[boneSubscr.w];
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 69 |
|
---|
| 70 | #ifdef S3D_USE_BONESUBSCR
|
---|
| 71 |
|
---|
| 72 | #define S3D_BONE_DECL_STD(boneWgh, boneSubscr) \
|
---|
| 73 | float4 boneWgh: BLENDWEIGHT; \
|
---|
| 74 | int4 boneSubscr: BLENDINDICES;
|
---|
| 75 |
|
---|
| 76 | #define S3D_MATBONE_DECL_STD(matBone) \
|
---|
| 77 | const float4x4 matBone[S3D_MATBONE_CNT];
|
---|
| 78 |
|
---|
| 79 | #define S3D_BONE_TRANSF_STD(matBoneFinal, matBone, boneWgh, boneSubscr) \
|
---|
| 80 | s3d_calcBoneTransfSubscr(matBoneFinal, matBone, boneWgh, boneSubscr);
|
---|
| 81 |
|
---|
| 82 | #else
|
---|
| 83 | #ifdef S3D_USE_BONEWGH
|
---|
| 84 |
|
---|
| 85 | #define S3D_BONE_DECL_STD(boneWgh, boneSubscr) \
|
---|
| 86 | float4 boneWgh: BLENDWEIGHT;
|
---|
| 87 |
|
---|
| 88 | #define S3D_MATBONE_DECL_STD(matBone) \
|
---|
| 89 | const float4x4 matBone[S3D_MATBONE_CNT];\
|
---|
| 90 | static const int4 dummyBoneSubscr = int4(0, 1, 2, 3);
|
---|
| 91 |
|
---|
| 92 | #define S3D_BONE_TRANSF_STD(matBoneFinal, matBone, boneWgh, boneSubscr) \
|
---|
| 93 | s3d_calcBoneTransfSubscr( \
|
---|
| 94 | matBoneFinal, matBone, boneWgh, dummyBoneSubscr);
|
---|
| 95 |
|
---|
| 96 | #else
|
---|
| 97 |
|
---|
| 98 | #define S3D_BONE_DECL_STD(boneWgh, boneSubscr)
|
---|
| 99 |
|
---|
| 100 | #define S3D_MATBONE_DECL_STD(matBone)
|
---|
| 101 |
|
---|
| 102 | #define S3D_BONE_TRANSF_STD(matBoneFinal, matBone, boneWgh, boneSubscr) \
|
---|
| 103 | matBoneFinal = matView;
|
---|
| 104 |
|
---|
| 105 | #endif
|
---|
| 106 | #endif
|
---|
| 107 |
|
---|
| 108 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 109 | // Lighting
|
---|
| 110 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 111 |
|
---|
| 112 | float s3d_calcLightRangeFac(float dist, float range)
|
---|
| 113 | {
|
---|
| 114 | float lightRsqRange = 1.0 / (range * range);
|
---|
| 115 | float rangeFac = max(1.0 - dist * dist * lightRsqRange, 0.0);
|
---|
| 116 | return rangeFac;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | float s3d_calcLightRangeFac(float dist, float4 posRange)
|
---|
| 120 | {
|
---|
| 121 | return s3d_calcLightRangeFac(dist, posRange.w);
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 125 |
|
---|
| 126 | s3d_ColPair s3d_calcPointLightTerm(
|
---|
| 127 | float3 camToVertView, float3 normalView, float mtrlPower,
|
---|
| 128 | float4 lightPos, float4 lightAmbient, float4 lightDiffuse,
|
---|
| 129 | float4 lightSpecular, float4 lightAtten)
|
---|
| 130 | {
|
---|
| 131 | float3 vertToLightDirView = normalize(lightPos - camToVertView);
|
---|
| 132 | float normalDotVertToLightDir = dot(normalView, vertToLightDirView);
|
---|
| 133 | float3 camToVertDirView = normalize(camToVertView);
|
---|
| 134 | float3 HalfDirView = normalize(vertToLightDirView - camToVertDirView);
|
---|
| 135 | float normalDotHalfDir = dot(normalView, HalfDirView);
|
---|
| 136 |
|
---|
| 137 | float dist = length(lightPos - camToVertView);
|
---|
| 138 | float rangeFac = s3d_calcLightRangeFac(dist, lightAtten);
|
---|
| 139 | float4 litVec = lit(normalDotVertToLightDir,
|
---|
| 140 | normalDotHalfDir, mtrlPower) * rangeFac;
|
---|
| 141 | float ambient = litVec.x;
|
---|
| 142 | float diffuse = litVec.y;
|
---|
| 143 | float specular = litVec.z;
|
---|
| 144 |
|
---|
| 145 | s3d_ColPair result;
|
---|
| 146 | result.diffuse = lightAmbient * ambient;
|
---|
| 147 | result.diffuse += lightDiffuse * diffuse;
|
---|
| 148 | result.specular = lightSpecular * specular;
|
---|
| 149 |
|
---|
| 150 | return result;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | s3d_ColPair s3d_calcPointLightTerm(
|
---|
| 154 | float3 camToVertView, float3 normalView,
|
---|
| 155 | float mtrlPower, s3d_StdProgLight light)
|
---|
| 156 | {
|
---|
| 157 | return s3d_calcPointLightTerm(
|
---|
| 158 | camToVertView, normalView, mtrlPower, light.pos,
|
---|
| 159 | light.ambient, light.diffuse, light.specular,
|
---|
| 160 | light.atten);
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 164 |
|
---|
| 165 | void s3d_calcBump(
|
---|
| 166 | out float3 diffuseDirSurf, out float3 specularDirSurf,
|
---|
| 167 | float3 normalView, float3 vertToLightDirView, float3 halfDirView,
|
---|
| 168 | float3 tangentUView, float3 tangentVView)
|
---|
| 169 | {
|
---|
| 170 | diffuseDirSurf.x = dot(tangentUView, vertToLightDirView);
|
---|
| 171 | diffuseDirSurf.y = dot(tangentVView, vertToLightDirView);
|
---|
| 172 | diffuseDirSurf.z = dot(normalView, vertToLightDirView);
|
---|
| 173 |
|
---|
| 174 | specularDirSurf.x = dot(tangentUView, halfDirView);
|
---|
| 175 | specularDirSurf.y = dot(tangentVView, halfDirView);
|
---|
| 176 | specularDirSurf.z = dot(normalView, halfDirView);
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 180 | // Fog
|
---|
| 181 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 182 |
|
---|
| 183 | float3 s3d_calcFogExp(
|
---|
| 184 | float3 color, float3 fogColor, float fogDensity, float fogCoord)
|
---|
| 185 | {
|
---|
| 186 | float fogFactor = exp(-clamp(fogDensity, 0, 1) * fogCoord);
|
---|
| 187 | return lerp(fogColor, color, fogFactor);
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | float3 s3d_calcFogExp2(
|
---|
| 191 | float3 color, float3 fogColor, float fogDensity, float fogCoord)
|
---|
| 192 | {
|
---|
| 193 | float fogFactor = exp(-pow(clamp(fogDensity, 0, 1) * fogCoord, 2));
|
---|
| 194 | return lerp(fogColor, color, fogFactor);
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 198 | // Tex
|
---|
| 199 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 200 |
|
---|
| 201 | #endif // STDDEF_D3D9_HLSL_SC
|
---|