A game about forced loneliness, made by TACStudios
at master 57 lines 3.0 kB view raw
1real3 ADD_FUNC_SUFFIX(ADD_NORMAL_FUNC_SUFFIX(SampleUVMappingNormal))(TEXTURE2D_PARAM(textureName, samplerName), UVMapping uvMapping, real scale, real param) 2{ 3 if (uvMapping.mappingType == UV_MAPPING_TRIPLANAR) 4 { 5 real3 triplanarWeights = uvMapping.triplanarWeights; 6 7#ifdef SURFACE_GRADIENT 8 // Height map gradient. Basically, it encodes height map slopes along S and T axes. 9 real2 derivXplane; 10 real2 derivYPlane; 11 real2 derivZPlane; 12 derivXplane = derivYPlane = derivZPlane = real2(0.0, 0.0); 13 14 if (triplanarWeights.x > 0.0) 15 derivXplane = triplanarWeights.x * UNPACK_DERIVATIVE_FUNC(SAMPLE_TEXTURE_FUNC(textureName, samplerName, uvMapping.uvZY, param), scale); 16 if (triplanarWeights.y > 0.0) 17 derivYPlane = triplanarWeights.y * UNPACK_DERIVATIVE_FUNC(SAMPLE_TEXTURE_FUNC(textureName, samplerName, uvMapping.uvXZ, param), scale); 18 if (triplanarWeights.z > 0.0) 19 derivZPlane = triplanarWeights.z * UNPACK_DERIVATIVE_FUNC(SAMPLE_TEXTURE_FUNC(textureName, samplerName, uvMapping.uvXY, param), scale); 20 21 // Assume derivXplane, derivYPlane and derivZPlane sampled using (z,y), (x,z) and (x,y) respectively 22 // ie using Morten's convention http://jcgt.org/published/0009/03/04/ p80-81 23 real3 volumeGrad = real3(derivZPlane.x + derivYPlane.x, derivZPlane.y + derivXplane.y, derivXplane.x + derivYPlane.y); 24 return SurfaceGradientFromVolumeGradient(uvMapping.normalWS, volumeGrad); 25#else 26 real3 val = real3(0.0, 0.0, 0.0); 27 28 if (triplanarWeights.x > 0.0) 29 val += triplanarWeights.x * UNPACK_NORMAL_FUNC(SAMPLE_TEXTURE_FUNC(textureName, samplerName, uvMapping.uvZY, param), scale); 30 if (triplanarWeights.y > 0.0) 31 val += triplanarWeights.y * UNPACK_NORMAL_FUNC(SAMPLE_TEXTURE_FUNC(textureName, samplerName, uvMapping.uvXZ, param), scale); 32 if (triplanarWeights.z > 0.0) 33 val += triplanarWeights.z * UNPACK_NORMAL_FUNC(SAMPLE_TEXTURE_FUNC(textureName, samplerName, uvMapping.uvXY, param), scale); 34 35 return normalize(val); 36#endif 37 } 38#ifdef SURFACE_GRADIENT 39 else if (uvMapping.mappingType == UV_MAPPING_PLANAR) 40 { 41 // Note: Planar is on uv coordinate (and not uvXZ) 42 real2 derivYPlane = UNPACK_DERIVATIVE_FUNC(SAMPLE_TEXTURE_FUNC(textureName, samplerName, uvMapping.uv, param), scale); 43 // See comment above 44 real3 volumeGrad = real3(derivYPlane.x, 0.0, derivYPlane.y); 45 return SurfaceGradientFromVolumeGradient(uvMapping.normalWS, volumeGrad); 46 } 47#endif 48 else 49 { 50#ifdef SURFACE_GRADIENT 51 real2 deriv = UNPACK_DERIVATIVE_FUNC(SAMPLE_TEXTURE_FUNC(textureName, samplerName, uvMapping.uv, param), scale); 52 return SurfaceGradientFromTBN(deriv, uvMapping.tangentWS, uvMapping.bitangentWS); 53#else 54 return UNPACK_NORMAL_FUNC(SAMPLE_TEXTURE_FUNC(textureName, samplerName, uvMapping.uv, param), scale); 55#endif 56 } 57}