#ifndef UNITY_COMMON_MATERIAL_INCLUDED #define UNITY_COMMON_MATERIAL_INCLUDED //----------------------------------------------------------------------------- // Helper functions for roughness //----------------------------------------------------------------------------- real PerceptualRoughnessToRoughness(real perceptualRoughness) { return perceptualRoughness * perceptualRoughness; } real RoughnessToPerceptualRoughness(real roughness) { return sqrt(roughness); } real PerceptualSmoothnessToRoughness(real perceptualSmoothness) { return (1.0 - perceptualSmoothness) * (1.0 - perceptualSmoothness); } real PerceptualSmoothnessToPerceptualRoughness(real perceptualSmoothness) { return (1.0 - perceptualSmoothness); } // Using roughness values of 0 leads to INFs and NANs. The only sensible place to use the roughness // value of 0 is IBL, so we do not modify the perceptual roughness which is used to select the MIP map level. // Note: making the constant too small results in aliasing. real ClampRoughnessForAnalyticalLights(real roughness) { return max(roughness, 1.0/1024.0); } void ConvertAnisotropyToRoughness(real perceptualRoughness, real anisotropy, out real roughnessT, out real roughnessB) { real roughness = PerceptualRoughnessToRoughness(perceptualRoughness); // Use the parametrization of Sony Imageworks. // Ref: Revisiting Physically Based Shading at Imageworks, p. 15. roughnessT = roughness * (1 + anisotropy); roughnessB = roughness * (1 - anisotropy); } // Same as ConvertAnisotropyToRoughness but // roughnessT and roughnessB are clamped, and are meant to be used with punctual and directional lights. void ConvertAnisotropyToClampRoughness(real perceptualRoughness, real anisotropy, out real roughnessT, out real roughnessB) { ConvertAnisotropyToRoughness(perceptualRoughness, anisotropy, roughnessT, roughnessB); roughnessT = ClampRoughnessForAnalyticalLights(roughnessT); roughnessB = ClampRoughnessForAnalyticalLights(roughnessB); } // Use with stack BRDF (clear coat / coat) real RoughnessToVariance(real roughness) { return 2.0 / Sq(roughness) - 2.0; } real VarianceToRoughness(real variance) { return sqrt(2.0 / (variance + 2.0)); } // ior is a value between 1.0 and 2.5 // Assume air interface for top real IORToFresnel0(real ior) { return Sq((ior - 1.0) / (ior + 1.0)); } real IORToFresnel0(real baseIor, real topIor) { return Sq((baseIor - topIor) / (baseIor + topIor)); } // Assume air interface for top // Note: Don't handle the case fresnel0 == 1 real Fresnel0ToIOR(real fresnel0) { real sqrtF0 = sqrt(fresnel0); return (1.0 + sqrtF0) / (1.0 - sqrtF0); } // This function is a coarse approximation of computing fresnel0 for a different top than air (here clear coat of IOR 1.5) when we only have fresnel0 with air interface // This function is equivalent to IORToFresnel0(Fresnel0ToIOR(fresnel0), 1.5) // mean // real sqrtF0 = sqrt(fresnel0); // return Sq(1.0 - 5.0 * sqrtF0) / Sq(5.0 - sqrtF0); // Optimization: Fit of the function (3 mad) for range [0.04 (should return 0), 1 (should return 1)] TEMPLATE_1_REAL(ConvertF0ForAirInterfaceToF0ForClearCoat15, fresnel0, return saturate(-0.0256868 + fresnel0 * (0.326846 + (0.978946 - 0.283835 * fresnel0) * fresnel0)) ) // same as regular refract except there is not the test for total internal reflection + the vector is flipped for processing real3 CoatRefract(real3 X, real3 N, real ieta) { real XdotN = saturate(dot(N, X)); return ieta * X + (sqrt(1 + ieta * ieta * (XdotN * XdotN - 1)) - ieta * XdotN) * N; } // ---------------------------------------------------------------------------- // Parallax mapping // ---------------------------------------------------------------------------- // ref https://www.gamedev.net/topic/678043-how-to-blend-world-space-normals/#entry5287707 // assume compositing in world space // Note: Using vtxNormal = real3(0, 0, 1) give the BlendNormalRNM formulation. // TODO: Untested real3 BlendNormalWorldspaceRNM(real3 n1, real3 n2, real3 vtxNormal) { // Build the shortest-arc quaternion real4 q = real4(cross(vtxNormal, n2), dot(vtxNormal, n2) + 1.0) / sqrt(2.0 * (dot(vtxNormal, n2) + 1)); // Rotate the normal return n1 * (q.w * q.w - dot(q.xyz, q.xyz)) + 2 * q.xyz * dot(q.xyz, n1) + 2 * q.w * cross(q.xyz, n1); } // ref http://blog.selfshadow.com/publications/blending-in-detail/ // ref https://gist.github.com/selfshadow/8048308 // Reoriented Normal Mapping // Blending when n1 and n2 are already 'unpacked' and normalised // assume compositing in tangent space real3 BlendNormalRNM(real3 n1, real3 n2) { real3 t = n1.xyz + real3(0.0, 0.0, 1.0); real3 u = n2.xyz * real3(-1.0, -1.0, 1.0); real3 r = (t / t.z) * dot(t, u) - u; return r; } // assume compositing in tangent space real3 BlendNormal(real3 n1, real3 n2) { return normalize(real3(n1.xy * n2.z + n2.xy * n1.z, n1.z * n2.z)); } // Ref: http://http.developer.nvidia.com/GPUGems3/gpugems3_ch01.html / http://www.slideshare.net/icastano/cascades-demo-secrets real3 ComputeTriplanarWeights(real3 normal) { // Determine the blend weights for the 3 planar projections. real3 blendWeights = abs(normal); // Tighten up the blending zone blendWeights = (blendWeights - 0.2) * 7.0; blendWeights = blendWeights * blendWeights * blendWeights; // pow(blendWeights, 3); // Force weights to sum to 1.0 (very important!) blendWeights = max(blendWeights, real3(0.0, 0.0, 0.0)); blendWeights /= dot(blendWeights, 1.0); return blendWeights; } // Planar/Triplanar convention for Unity in world space void GetTriplanarCoordinate(float3 position, out float2 uvXZ, out float2 uvXY, out float2 uvZY) { // Caution: This must follow the same rule as what is use for SurfaceGradient triplanar // TODO: Currently the normal mapping looks wrong without SURFACE_GRADIENT option because we don't handle corretly the tangent space uvXZ = float2(position.z, position.x); uvXY = float2(position.x, position.y); uvZY = float2(position.z, position.y); } real LerpWhiteTo(real b, real t) { real oneMinusT = 1.0 - t; return oneMinusT + b * t; } real3 LerpWhiteTo(real3 b, real t) { real oneMinusT = 1.0 - t; return real3(oneMinusT, oneMinusT, oneMinusT) + b * t; } #endif // UNITY_COMMON_MATERIAL_INCLUDED