[Log] Verge3D 3.7.0 pre5 for Blender (Trial, WebGL 1.0) (v3d.js, line 1) [Error] WebGL: ERROR: 0:2702: 'node_output_world' : no matching overloaded function found compileShader Bo (v3d.js:1:582886) $o (v3d.js:1:598579) acquireProgram (v3d.js:1:608438) Le (v3d.js:1:694602) Re (v3d.js:1:697848) (anonymous function) (v3d.js:1:690619) Ee (v3d.js:1:694050) Ce (v3d.js:1:693705) (anonymous function) (v3d.js:1:705053) (anonymous function) (v3d.js:1:147424) renderSceneToCubemap (v3d.js:1:1184542) value (v3d.js:1:1352303) (anonymous function) (v3d.js:1:1335549) (anonymous function) (v3d.js:1:1273919) (anonymous function) (v3d.js:1:1285033) promiseReactionJob [Error] v3d.WebGLProgram: shader error: – 0 – "35715" – false – "gl.getProgramInfoLog" – "" – "" – "v3d.WebGLShader: gl.getShaderInfoLog() fragment↵ERROR: 0:2702: 'node_output_world' : no matching overloaded function found1: #extensio…" "v3d.WebGLShader: gl.getShaderInfoLog() fragment ERROR: 0:2702: 'node_output_world' : no matching overloaded function found1: #extension GL_OES_standard_derivatives : enable 2: precision highp float; 3: precision highp int; 4: #define HIGH_PRECISION 5: #define SHADER_NAME MeshNodeMaterial 6: #define LIGHT_PATH_IS_CAM_RAY 1 7: #define WORLD_NODES 1 8: #define NODE_RGB_NUM 0 9: #define NODE_VALUE_NUM 0 10: #define NODE_TEX_COORD_NUM 0 11: #define NODE_BACKGROUND_BL 12: #define NODE_OUTPUT_WORLD_BL 13: #define MT_BLENDER 14: #define COMPAT_USE_SPEC_ENV_BLENDER_APPROX 15: #define GAMMA_FACTOR 2 16: #define FLIP_SIDED 17: #define PHYSICALLY_CORRECT_LIGHTS 18: #define UNITS_SCALE_FACTOR 1.0 19: uniform mat4 viewMatrix; 20: uniform vec3 cameraPosition; 21: uniform bool isOrthographic; 22: 23: vec4 LinearToLinear(in vec4 value) { 24: return value; 25: } 26: vec4 GammaToLinear(in vec4 value, in float gammaFactor) { 27: value = max(value, vec4(0.0)); 28: return vec4(pow(value.rgb, vec3(gammaFactor)), value.a); 29: } 30: vec4 LinearToGamma(in vec4 value, in float gammaFactor) { 31: value = max(value, vec4(0.0)); 32: return vec4(pow(value.rgb, vec3(1.0 / gammaFactor)), value.a); 33: } 34: vec4 sRGBToLinear(in vec4 value) { 35: value = max(value, vec4(0.0)); 36: return vec4(mix(pow(value.rgb * 0.9478672986 + vec3(0.0521327014), vec3(2.4)), value.rgb * 0.0773993808, vec3(lessThanEqual(value.rgb, vec3(0.04045)))), value.a); 37: } 38: vec4 LinearTosRGB(in vec4 value) { 39: value = max(value, vec4(0.0)); 40: return vec4(mix(pow(value.rgb, vec3(0.41666)) * 1.055 - vec3(0.055), value.rgb * 12.92, vec3(lessThanEqual(value.rgb, vec3(0.0031308)))), value.a); 41: } 42: vec4 RGBEToLinear(in vec4 value) { 43: return vec4(value.rgb * exp2(value.a * 255.0 - 128.0), 1.0); 44: } 45: vec4 LinearToRGBE(in vec4 value) { 46: float maxComponent = max(max(value.r, value.g), value.b); 47: float fExp = clamp(ceil(log2(maxComponent)), -128.0, 127.0); 48: return vec4(value.rgb / exp2(fExp), (fExp + 128.0) / 255.0); 49: } 50: vec4 RGBMToLinear(in vec4 value, in float maxRange) { 51: return vec4(value.rgb * value.a * maxRange, 1.0); 52: } 53: vec4 LinearToRGBM(in vec4 value, in float maxRange) { 54: float maxRGB = max(value.r, max(value.g, value.b)); 55: float M = clamp(maxRGB / maxRange, 0.0, 1.0); 56: M = ceil(M * 255.0) / 255.0; 57: return vec4(value.rgb / (M * maxRange), M); 58: } 59: vec4 RGBDToLinear(in vec4 value, in float maxRange) { 60: return vec4(value.rgb * ((maxRange / 255.0) / value.a), 1.0); 61: } 62: vec4 LinearToRGBD(in vec4 value, in float maxRange) { 63: float maxRGB = max(value.r, max(value.g, value.b)); 64: float D = max(maxRange / maxRGB, 1.0); 65: D = clamp(floor(D) / 255.0, 0.0, 1.0); 66: return vec4(value.rgb * (D * (255.0 / maxRange)), D); 67: } 68: const mat3 cLogLuvM = mat3(0.2209, 0.3390, 0.4184, 0.1138, 0.6780, 0.7319, 0.0102, 0.1130, 0.2969); 69: vec4 LinearToLogLuv(in vec4 value) { 70: vec3 Xp_Y_XYZp = cLogLuvM * value.rgb; 71: Xp_Y_XYZp = max(Xp_Y_XYZp, vec3(1e-6, 1e-6, 1e-6)); 72: vec4 vResult; 73: vResult.xy = Xp_Y_XYZp.xy / Xp_Y_XYZp.z; 74: float Le = 2.0 * log2(Xp_Y_XYZp.y) + 127.0; 75: vResult.w = fract(Le); 76: vResult.z = (Le - (floor(vResult.w * 255.0)) / 255.0) / 255.0; 77: return vResult; 78: } 79: const mat3 cLogLuvInverseM = mat3(6.0014, -2.7008, -1.7996, -1.3320, 3.1029, -5.7721, 0.3008, -1.0882, 5.6268); 80: vec4 LogLuvToLinear(in vec4 value) { 81: float Le = value.z * 255.0 + value.w; 82: vec3 Xp_Y_XYZp; 83: Xp_Y_XYZp.y = exp2((Le - 127.0) / 2.0); 84: Xp_Y_XYZp.z = Xp_Y_XYZp.y / value.y; 85: Xp_Y_XYZp.x = value.x * Xp_Y_XYZp.z; 86: vec3 vRGB = cLogLuvInverseM * Xp_Y_XYZp.rgb; 87: return vec4(max(vRGB, 0.0), 1.0); 88: } 89: vec4 linearToOutputTexel(vec4 value) { return LinearTosRGB(value); } 90: #define ESM_DISTANCE_SCALE 1.0 91: 92: #define NODE 93: #define PI 3.14159265359 94: #define PI2 6.28318530718 95: #define PI_HALF 1.5707963267949 96: #define RECIPROCAL_PI 0.31830988618 97: #define RECIPROCAL_PI2 0.15915494 98: #define LOG2 1.442695 99: #define EPSILON 1e-6 100: #ifndef saturate 101: #define saturate(a) clamp(a, 0.0, 1.0) 102: #endif 103: #define whiteComplement(a) (1.0 - saturate(a)) 104: #define RECIPROCAL_3 0.333333333333 105: float pow2(const in float x) { return x*x; } 106: float pow3(const in float x) { return x*x*x; } 107: float pow4(const in float x) { float x2 = x*x; return x2*x2; } 108: float average(const in vec3 color) { return dot(color, vec3(0.3333)); } 109: highp float rand(const in vec2 uv) { 110: const highp float a = 12.9898, b = 78.233, c = 43758.5453; 111: highp float dt = dot(uv.xy, vec2(a,b)), sn = mod(dt, PI); 112: return fract(sin(sn) * c); 113: } 114: #ifdef HIGH_PRECISION 115: float precisionSafeLength(vec3 v) { return length(v); } 116: #else 117: float max3(vec3 v) { return max(max(v.x, v.y), v.z); } 118: float precisionSafeLength(vec3 v) { 119: float maxComponent = max3(abs(v)); 120: return length(v / maxComponent) * maxComponent; 121: } 122: #endif 123: struct IncidentLight { 124: vec3 color; 125: vec3 direction; 126: bool visible; 127: }; 128: struct ReflectedLight { 129: vec3 directDiffuse; 130: vec3 directSpecular; 131: vec3 indirectDiffuse; 132: vec3 indirectSpecular; 133: }; 134: struct GeometricContext { 135: vec3 position; 136: vec3 normal; 137: vec3 viewDir; 138: #ifdef CLEARCOAT 139: vec3 clearcoatNormal; 140: #endif 141: }; 142: vec3 transformDirection(in vec3 dir, in mat4 matrix) { 143: return normalize((matrix * vec4(dir, 0.0)).xyz); 144: } 145: vec3 inverseTransformDirection(in vec3 dir, in mat4 matrix) { 146: return normalize((vec4(dir, 0.0) * matrix).xyz); 147: } 148: vec3 projectOnPlane(in vec3 point, in vec3 pointOnPlane, in vec3 planeNormal) { 149: float distance = dot(planeNormal, point - pointOnPlane); 150: return - distance * planeNormal + point; 151: } 152: float sideOfPlane(in vec3 point, in vec3 pointOnPlane, in vec3 planeNormal) { 153: return sign(dot(point - pointOnPlane, planeNormal)); 154: } 155: vec3 linePlaneIntersect(in vec3 pointOnLine, in vec3 lineDirection, in vec3 pointOnPlane, in vec3 planeNormal) { 156: return lineDirection * (dot(planeNormal, pointOnPlane - pointOnLine) / dot(planeNormal, lineDirection)) + pointOnLine; 157: } 158: mat3 transposeMat3(const in mat3 m) { 159: mat3 tmp; 160: tmp[0] = vec3(m[0].x, m[1].x, m[2].x); 161: tmp[1] = vec3(m[0].y, m[1].y, m[2].y); 162: tmp[2] = vec3(m[0].z, m[1].z, m[2].z); 163: return tmp; 164: } 165: float linearToRelativeLuminance(const in vec3 color) { 166: vec3 weights = vec3(0.2126, 0.7152, 0.0722); 167: return dot(weights, color.rgb); 168: } 169: bool isPerspectiveMatrix(mat4 m) { 170: return m[2][3] == - 1.0; 171: } 172: highp vec3 rand3(const in vec3 v) { 173: const highp float c = 43758.5453; 174: const highp mat3 coeffs = mat3( 175: 165.15, 253.34, 323.22, 176: 241.49, 329.07, 147.79, 177: 376.31, 143.45, 281.63 178: ); 179: highp vec3 sn = mod(coeffs * v, PI); 180: return fract(sin(sn) * c); 181: } 182: float powCompat(const in float val, const in float power) { 183: if (power == 0.0) 184: return 1.0; 185: else if (val < 0.0) { 186: if (mod(-power, 2.0) == 0.0) 187: return pow(abs(val), power); 188: else 189: return -pow(abs(val), power); 190: } else if (val == 0.0) 191: return 0.0; 192: return pow(abs(val), power); 193: } 194: float maxFromRGB(vec3 rgb) { 195: return max(max(rgb.r, rgb.g), rgb.b); 196: } 197: bool isOrtho(const in mat4 m) { 198: if (m[3][3] != 0.0) 199: return true; 200: else 201: return false; 202: } 203: vec3 swizzleUpZ(const vec3 vec) { 204: return vec3(vec[0], -vec[2], vec[1]); 205: } 206: vec3 swizzleUpY(const vec3 vec) { 207: return vec3(vec[0], vec[2], -vec[1]); 208: } 209: vec3 xyz_to_sRGB(vec3 xyz) { 210: mat3 convMat = mat3( 211: 3.2406, -0.9689, 0.0557, 212: -1.5372, 1.8758, -0.2040, 213: -0.4986, 0.0415, 1.0570 214: ); 215: return convMat * xyz; 216: } 217: vec3 xyY_to_XYZ(float x, float y, float Y) { 218: float X = 0.0; 219: float Z = 0.0; 220: if (y != 0.0) { 221: X = (Y / y) * x; 222: Z = (Y / y) * (1.0 - x - y); 223: } 224: return vec3(X, Y, Z); 225: } 226: vec3 octUVToCubeVec(vec2 octUV, vec2 texelSize) { 227: octUV = (1.0 + 2.0 * texelSize) * octUV - texelSize; 228: octUV = octUV * 2.0 - 1.0; 229: float x = octUV.x; 230: float z = -octUV.y; 231: float absX = abs(x); 232: float absZ = abs(z); 233: vec3 cubeVec = vec3(x, 1.0 - absX - absZ, z); 234: if (absX + absZ > 1.0) { 235: cubeVec.xz = -(vec2(absZ, absX) - 1.0) * sign(vec2(x, z)); 236: } 237: return cubeVec; 238: } 239: vec2 cubeVecToOctUV(vec3 cubeVec, vec2 texelSize) { 240: cubeVec /= dot(vec3(1.0), abs(cubeVec)); 241: vec2 octUV = vec2(cubeVec.x, -cubeVec.z); 242: if (cubeVec.y < 0.0) { 243: octUV = sign(octUV) * (1.0 - abs(octUV.ts)); 244: } 245: octUV = (octUV + 1.0) / 2.0; 246: octUV = (1.0 - 2.0 * texelSize) * octUV + texelSize; 247: return octUV; 248: } 249: #if __VERSION__ == 100 250: float cosh(float x) { 251: return (exp(x) + exp(-x)) / 2.0; 252: } 253: vec2 cosh(vec2 x) { 254: return (exp(x) + exp(-x)) / 2.0; 255: } 256: vec3 cosh(vec3 x) { 257: return (exp(x) + exp(-x)) / 2.0; 258: } 259: vec4 cosh(vec4 x) { 260: return (exp(x) + exp(-x)) / 2.0; 261: } 262: float sinh(float x) { 263: return (exp(x) - exp(-x)) / 2.0; 264: } 265: vec2 sinh(vec2 x) { 266: return (exp(x) - exp(-x)) / 2.0; 267: } 268: vec3 sinh(vec3 x) { 269: return (exp(x) - exp(-x)) / 2.0; 270: } 271: vec4 sinh(vec4 x) { 272: return (exp(x) - exp(-x)) / 2.0; 273: } 274: float tanh(float x) { 275: float exp2x = exp(2.0 * x); 276: return (exp2x - 1.0) / (exp2x + 1.0); 277: } 278: vec2 tanh(vec2 x) { 279: vec2 exp2x = exp(2.0 * x); 280: return (exp2x - 1.0) / (exp2x + 1.0); 281: } 282: vec3 tanh(vec3 x) { 283: vec3 exp2x = exp(2.0 * x); 284: return (exp2x - 1.0) / (exp2x + 1.0); 285: } 286: vec4 tanh(vec4 x) { 287: vec4 exp2x = exp(2.0 * x); 288: return (exp2x - 1.0) / (exp2x + 1.0); 289: } 290: float trunc(float x) { 291: return floor(abs(x)) * sign(x); 292: } 293: vec2 trunc(vec2 x) { 294: return floor(abs(x)) * sign(x); 295: } 296: vec3 trunc(vec3 x) { 297: return floor(abs(x)) * sign(x); 298: } 299: vec4 trunc(vec4 x) { 300: return floor(abs(x)) * sign(x); 301: } 302: #endif 303: float getSmoothFactor(float a, float b, float smoothness) { 304: return max(smoothness - abs(a - b), 0.0) / smoothness; 305: } 306: float smoothMin(float a, float b, float smoothness) { 307: float smoothFac = getSmoothFactor(a, b, smoothness); 308: return min(a, b) - smoothFac * smoothFac * smoothFac * smoothness * (1.0 / 6.0); 309: } 310: float smoothMax(float a, float b, float smoothness) { 311: float smoothFac = getSmoothFactor(a, b, smoothness); 312: return max(a, b) + smoothFac * smoothFac * smoothFac * smoothness * (1.0 / 6.0); 313: } 314: vec3 vec3RotateAxisAngle(vec3 vector, vec3 axis, float angle) { 315: vec3 axisNorm = normalize(axis); 316: float x = axisNorm.x, y = axisNorm.y, z = axisNorm.z; 317: float s = sin(angle), c = cos(angle); 318: return mat3( 319: x * x * (1.0 - c) + c, x * y * (1.0 - c) + z * s, x * z * (1.0 - c) - y * s, 320: x * y * (1.0 - c) - z * s, y * y * (1.0 - c) + c, y * z * (1.0 - c) + x * s, 321: x * z * (1.0 - c) + y * s, y * z * (1.0 - c) - x * s, z * z * (1.0 - c) + c 322: ) * vector; 323: } 324: mat3 mat3RotateX(float angle) { 325: float s = sin(angle), c = cos(angle); 326: return mat3(1.0, 0.0, 0.0, 327: 0.0, c, s, 328: 0.0, -s, c); 329: } 330: mat3 mat3RotateY(float angle) { 331: float s = sin(angle), c = cos(angle); 332: return mat3(c, 0.0, -s, 333: 0.0, 1.0, 0.0, 334: s, 0.0, c); 335: } 336: mat3 mat3RotateZ(float angle) { 337: float s = sin(angle), c = cos(angle); 338: return mat3(c, s, 0.0, 339: -s, c, 0.0, 340: 0.0, 0.0, 1.0); 341: } 342: vec3 vec3RotateXAngle(vec3 vector, float angle) { 343: return mat3RotateX(angle) * vector; 344: } 345: vec3 vec3RotateYAngle(vec3 vector, float angle) { 346: return mat3RotateY(angle) * vector; 347: } 348: vec3 vec3RotateZAngle(vec3 vector, float angle) { 349: return mat3RotateZ(angle) * vector; 350: } 351: vec4 eulerToAxisAngle(vec3 euler) { 352: float c1 = cos(euler.x / 2.0), c2 = cos(euler.y / 2.0), c3 = cos(euler.z / 2.0); 353: float s1 = sin(euler.x / 2.0), s2 = sin(euler.y / 2.0), s3 = sin(euler.z / 2.0); 354: vec4 axisAngle = vec4( 355: s1 * c2 * c3 - c1 * s2 * s3, 356: c1 * s2 * c3 + s1 * c2 * s3, 357: c1 * c2 * s3 - s1 * s2 * c3, 358: 2.0 * acos(c1 * c2 * c3 + s1 * s2 * s3) 359: ); 360: axisAngle.xyz = length(axisAngle.xyz) > 0.0 ? normalize(axisAngle.xyz) : vec3(1.0, 0.0, 0.0); 361: return axisAngle; 362: } 363: float mat3GetDeterminant(mat3 mat) { 364: return mat[0][0] * mat[1][1] * mat[2][2] 365: + mat[0][2] * mat[1][0] * mat[2][1] 366: + mat[0][1] * mat[1][2] * mat[2][0] 367: - mat[0][2] * mat[1][1] * mat[2][0] 368: - mat[0][0] * mat[1][2] * mat[2][1] 369: - mat[0][1] * mat[1][0] * mat[2][2]; 370: } 371: mat3 mat3GetInverseTransposed(mat3 mat) { 372: float det = mat3GetDeterminant(mat); 373: float a00 = (mat[1][1] * mat[2][2] - mat[1][2] * mat[2][1]) / det; 374: float a01 = - (mat[1][0] * mat[2][2] - mat[1][2] * mat[2][0]) / det; 375: float a02 = (mat[1][0] * mat[2][1] - mat[1][1] * mat[2][0]) / det; 376: float a10 = - (mat[0][1] * mat[2][2] - mat[0][2] * mat[2][1]) / det; 377: float a11 = (mat[0][0] * mat[2][2] - mat[0][2] * mat[2][0]) / det; 378: float a12 = - (mat[0][0] * mat[2][1] - mat[0][1] * mat[2][0]) / det; 379: float a20 = (mat[0][1] * mat[1][2] - mat[0][2] * mat[1][1]) / det; 380: float a21 = - (mat[0][0] * mat[1][2] - mat[0][2] * mat[1][0]) / det; 381: float a22 = (mat[0][0] * mat[1][1] - mat[0][1] * mat[1][0]) / det; 382: return mat3( 383: a00, a01, a02, 384: a10, a11, a12, 385: a20, a21, a22 386: ); 387: } 388: mat3 toMat3(mat4 mat) { 389: return mat3(mat[0][0], mat[0][1], mat[0][2], 390: mat[1][0], mat[1][1], mat[1][2], 391: mat[2][0], mat[2][1], mat[2][2]); 392: } 393: mat4 toMat4(mat3 mat) { 394: return mat4(mat[0][0], mat[0][1], mat[0][2], 0.0, 395: mat[1][0], mat[1][1], mat[1][2], 0.0, 396: mat[2][0], mat[2][1], mat[2][2], 0.0, 397: 0.0, 0.0, 0.0, 1.0); 398: } 399: vec3 packNormalToRGB(const in vec3 normal) { 400: return normalize(normal) * 0.5 + 0.5; 401: } 402: vec3 unpackRGBToNormal(const in vec3 rgb) { 403: return 2.0 * rgb.xyz - 1.0; 404: } 405: const float PackUpscale = 256. / 255.; 406: const float UnpackDownscale = 255. / 256.; 407: const vec3 PackFactors = vec3(256. * 256. * 256., 256. * 256., 256.); 408: const vec4 UnpackFactors = UnpackDownscale / vec4(PackFactors, 1.); 409: const float ShiftRight8 = 1. / 256.; 410: vec4 packDepthToRGBA(const in float v) { 411: vec4 r = vec4(fract(v * PackFactors), v); 412: r.yzw -= r.xyz * ShiftRight8; 413: return r * PackUpscale; 414: } 415: float unpackRGBAToDepth(const in vec4 v) { 416: return dot(v, UnpackFactors); 417: } 418: vec4 pack2HalfToRGBA(vec2 v) { 419: vec4 r = vec4(v.x, fract(v.x * 255.0), v.y, fract(v.y * 255.0)); 420: return vec4(r.x - r.y / 255.0, r.y, r.z - r.w / 255.0, r.w); 421: } 422: vec2 unpackRGBATo2Half(vec4 v) { 423: return vec2(v.x + (v.y / 255.0), v.z + (v.w / 255.0)); 424: } 425: float viewZToOrthographicDepth(const in float viewZ, const in float near, const in float far) { 426: return (viewZ + near) / (near - far); 427: } 428: float orthographicDepthToViewZ(const in float linearClipZ, const in float near, const in float far) { 429: return linearClipZ * (near - far) - near; 430: } 431: float viewZToPerspectiveDepth(const in float viewZ, const in float near, const in float far) { 432: return ((near + viewZ) * far) / ((far - near) * viewZ); 433: } 434: float perspectiveDepthToViewZ(const in float invClipZ, const in float near, const in float far) { 435: return (near * far) / ((far - near) * invClipZ - far); 436: } 437: #ifdef DITHERING 438: vec3 dithering(vec3 color) { 439: float grid_position = rand(gl_FragCoord.xy); 440: vec3 dither_shift_RGB = vec3(0.25 / 255.0, -0.25 / 255.0, 0.25 / 255.0); 441: dither_shift_RGB = mix(2.0 * dither_shift_RGB, -2.0 * dither_shift_RGB, grid_position); 442: return color + dither_shift_RGB; 443: } 444: #endif 445: 446: vec2 integrateSpecularBRDF(const in float dotNV, const in float roughness) { 447: const vec4 c0 = vec4(- 1, - 0.0275, - 0.572, 0.022); 448: const vec4 c1 = vec4(1, 0.0425, 1.04, - 0.04); 449: vec4 r = roughness * c0 + c1; 450: float a004 = min(r.x * r.x, exp2(- 9.28 * dotNV)) * r.x + r.y; 451: return vec2(-1.04, 1.04) * a004 + r.zw; 452: } 453: float punctualLightIntensityToIrradianceFactor(float lightDistance, const in float cutoffDistance, const in float decayExponent) { 454: #if defined (PHYSICALLY_CORRECT_LIGHTS) 455: lightDistance = UNITS_SCALE_FACTOR * lightDistance; 456: #ifdef MT_MAYA 457: float distanceFalloff = 1.0 / pow(lightDistance + 1.0, decayExponent); 458: #else 459: float distanceFalloff = 1.0 / max(pow(lightDistance, decayExponent), 0.01); 460: #endif 461: if(cutoffDistance > 0.0) { 462: distanceFalloff *= pow2(saturate(1.0 - pow4(lightDistance / cutoffDistance))); 463: } 464: return distanceFalloff; 465: #else 466: if(cutoffDistance > 0.0 && decayExponent > 0.0) { 467: return pow(saturate(-lightDistance / cutoffDistance + 1.0), decayExponent); 468: } 469: return 1.0; 470: #endif 471: } 472: vec3 BRDF_Diffuse_Lambert(const in vec3 diffuseColor) { 473: return RECIPROCAL_PI * diffuseColor; 474: } 475: vec3 F_Schlick(const in vec3 specularColor, const in float dotLH) { 476: float fresnel = exp2((-5.55473 * dotLH - 6.98316) * dotLH); 477: return (1.0 - specularColor) * fresnel + specularColor; 478: } 479: vec3 F_Schlick_RoughnessDependent(const in vec3 F0, const in float dotNV, const in float roughness) { 480: float fresnel = exp2((-5.55473 * dotNV - 6.98316) * dotNV); 481: vec3 Fr = max(vec3(1.0 - roughness), F0) - F0; 482: return Fr * fresnel + F0; 483: } 484: float G_GGX_Smith(const in float alpha, const in float dotNL, const in float dotNV) { 485: float a2 = pow2(alpha); 486: float gl = dotNL + sqrt(a2 + (1.0 - a2) * pow2(dotNL)); 487: float gv = dotNV + sqrt(a2 + (1.0 - a2) * pow2(dotNV)); 488: return 1.0 / (gl * gv); 489: } 490: float G_GGX_SmithCorrelated(const in float alpha, const in float dotNL, const in float dotNV) { 491: float a2 = pow2(alpha); 492: float gv = dotNL * sqrt(a2 + (1.0 - a2) * pow2(dotNV)); 493: float gl = dotNV * sqrt(a2 + (1.0 - a2) * pow2(dotNL)); 494: return 0.5 / max(gv + gl, EPSILON); 495: } 496: float D_GGX(const in float alpha, const in float dotNH) { 497: float a2 = pow2(alpha); 498: float denom = pow2(dotNH) * (a2 - 1.0) + 1.0; 499: return RECIPROCAL_PI * a2 / pow2(denom); 500: } 501: vec3 BRDF_Specular_GGX(const in IncidentLight incidentLight, const in vec3 viewDir, const in vec3 normal, const in vec3 specularColor, const in float roughness) { 502: float alpha = pow2(clamp(roughness, 0.04, 1.0)); 503: vec3 halfDir = normalize(incidentLight.direction + viewDir); 504: float dotNL = saturate(dot(normal, incidentLight.direction)); 505: float dotNV = saturate(dot(normal, viewDir)); 506: float dotNH = saturate(dot(normal, halfDir)); 507: float dotLH = saturate(dot(incidentLight.direction, halfDir)); 508: vec3 F = F_Schlick(specularColor, dotLH); 509: float G = G_GGX_SmithCorrelated(alpha, dotNL, dotNV); 510: float D = D_GGX(alpha, dotNH); 511: return F * (G * D); 512: } 513: vec2 LTC_Uv(const in vec3 N, const in vec3 V, const in float roughness) { 514: const float LUT_SIZE = 64.0; 515: const float LUT_SCALE = (LUT_SIZE - 1.0) / LUT_SIZE; 516: const float LUT_BIAS = 0.5 / LUT_SIZE; 517: float dotNV = saturate(dot(N, V)); 518: vec2 uv = vec2(roughness, sqrt(1.0 - dotNV)); 519: uv = uv * LUT_SCALE + LUT_BIAS; 520: return uv; 521: } 522: float LTC_ClippedSphereFormFactor(const in vec3 f) { 523: float l = length(f); 524: return max((l * l + f.z) / (l + 1.0), 0.0); 525: } 526: vec3 LTC_EdgeVectorFormFactor(const in vec3 v1, const in vec3 v2) { 527: float x = dot(v1, v2); 528: float y = abs(x); 529: float a = 0.8543985 + (0.4965155 + 0.0145206 * y) * y; 530: float b = 3.4175940 + (4.1616724 + y) * y; 531: float v = a / b; 532: float theta_sintheta = (x > 0.0) ? v : 0.5 * inversesqrt(max(1.0 - x * x, 1e-7)) - v; 533: return cross(v1, v2) * theta_sintheta; 534: } 535: vec3 LTC_Evaluate(const in vec3 N, const in vec3 V, const in vec3 P, const in mat3 mInv, const in vec3 rectCoords[4]) { 536: vec3 v1 = rectCoords[1] - rectCoords[0]; 537: vec3 v2 = rectCoords[3] - rectCoords[0]; 538: vec3 lightNormal = cross(v1, v2); 539: if(dot(lightNormal, P - rectCoords[0]) < 0.0) return vec3(0.0); 540: vec3 T1, T2; 541: T1 = normalize(V - N * dot(V, N)); 542: T2 = - cross(N, T1); 543: mat3 mat = mInv * transposeMat3(mat3(T1, T2, N)); 544: vec3 coords[4]; 545: coords[0] = mat * (rectCoords[0] - P); 546: coords[1] = mat * (rectCoords[1] - P); 547: coords[2] = mat * (rectCoords[2] - P); 548: coords[3] = mat * (rectCoords[3] - P); 549: coords[0] = normalize(coords[0]); 550: coords[1] = normalize(coords[1]); 551: coords[2] = normalize(coords[2]); 552: coords[3] = normalize(coords[3]); 553: vec3 vectorFormFactor = vec3(0.0); 554: vectorFormFactor += LTC_EdgeVectorFormFactor(coords[0], coords[1]); 555: vectorFormFactor += LTC_EdgeVectorFormFactor(coords[1], coords[2]); 556: vectorFormFactor += LTC_EdgeVectorFormFactor(coords[2], coords[3]); 557: vectorFormFactor += LTC_EdgeVectorFormFactor(coords[3], coords[0]); 558: float result = LTC_ClippedSphereFormFactor(vectorFormFactor); 559: return vec3(result); 560: } 561: vec2 get_BRDF_SpecCoeffsBlender(float x, float y) { 562: vec3 xyFactors0 = vec3(x * x, y * y, x * y); 563: vec3 xyFactors1 = vec3(x, y, 1); 564: vec3 a0a1a2 = vec3(0.33749372, 0.15167605, 1.09684597); 565: vec3 a3a4a5 = vec3(-1.26123466, -0.927699, 0.9199188); 566: vec3 b0b1b2 = vec3(0.41699717, 0.44675109, 0.79947684); 567: vec3 b3b4b5 = vec3(-1.19307849, -0.89813958, 0.89305222); 568: vec3 c0c1c2 = vec3(0.29920727, 0.09505591, -0.9136233); 569: vec3 c3c4c5 = vec3(0.77055201, 0.13006674, -0.23085581); 570: vec3 d0d1d2 = vec3(15.05004149, 7.98517355, 13.30473726); 571: vec3 d3d4d5 = vec3(-32.00353547, -12.97743434, 17.83646751); 572: float coeff0 = (dot(xyFactors0, a0a1a2) + dot(xyFactors1, a3a4a5)) 573: / (dot(xyFactors0, b0b1b2) + dot(xyFactors1, b3b4b5)); 574: float coeff1 = (dot(xyFactors0, c0c1c2) + dot(xyFactors1, c3c4c5)) 575: / (dot(xyFactors0, d0d1d2) + dot(xyFactors1, d3d4d5)); 576: coeff1 = clamp(coeff1, 0.0, 1.0); 577: return vec2(coeff0, coeff1); 578: } 579: vec3 BRDF_Specular_GGX_Environment(const in vec3 viewDir, const in vec3 normal, const in vec3 specularColor, const in float roughness) { 580: float dotNV = saturate(dot(normal, viewDir)); 581: vec2 brdf = integrateSpecularBRDF(dotNV, roughness); 582: return specularColor * brdf.x + brdf.y; 583: } 584: vec3 BRDF_Specular_GGX_Environment_Blender_Approx(const in GeometricContext geometry, 585: const in vec3 fresnelRefl0, const in vec3 fresnelRefl90, 586: const in float roughness, const int useCoat) { 587: vec3 normal = geometry.normal; 588: #ifdef CLEARCOAT 589: if (useCoat == 1) { 590: normal = geometry.clearcoatNormal; 591: } 592: #endif 593: float dotNV = saturate(dot(normal, geometry.viewDir)); 594: float angle = acos(abs(dotNV)) / PI_HALF; 595: vec2 specCoeffs = get_BRDF_SpecCoeffsBlender(angle, roughness); 596: vec3 specular = specCoeffs.x * fresnelRefl0 597: + specCoeffs.y * fresnelRefl90 598: * vec3(saturate(50.0 * linearToRelativeLuminance(fresnelRefl0))); 599: #if defined (COMPAT_SATURATE_SPEC_ENV_BLENDER_APPROX) 600: specular = saturate(specular); 601: #endif 602: return specular; 603: } 604: void BRDF_Specular_Multiscattering_Environment(const in GeometricContext geometry, const in vec3 specularColor, const in float roughness, inout vec3 singleScatter, inout vec3 multiScatter) { 605: float dotNV = saturate(dot(geometry.normal, geometry.viewDir)); 606: vec3 F = F_Schlick_RoughnessDependent(specularColor, dotNV, roughness); 607: vec2 brdf = integrateSpecularBRDF(dotNV, roughness); 608: vec3 FssEss = F * brdf.x + brdf.y; 609: float Ess = brdf.x + brdf.y; 610: float Ems = 1.0 - Ess; 611: vec3 Favg = specularColor + (1.0 - specularColor) * 0.047619; 612: vec3 Fms = FssEss * Favg / (1.0 - Ems * Favg); 613: singleScatter += FssEss; 614: multiScatter += Fms * Ems; 615: } 616: float G_BlinnPhong_Implicit() { 617: return 0.25; 618: } 619: float D_BlinnPhong(const in float shininess, const in float dotNH) { 620: return RECIPROCAL_PI * (shininess * 0.5 + 1.0) * pow(dotNH, shininess); 621: } 622: vec3 BRDF_Specular_BlinnPhong(const in IncidentLight incidentLight, const in GeometricContext geometry, const in vec3 specularColor, const in float shininess) { 623: vec3 halfDir = normalize(incidentLight.direction + geometry.viewDir); 624: float dotNH = saturate(dot(geometry.normal, halfDir)); 625: float dotLH = saturate(dot(incidentLight.direction, halfDir)); 626: vec3 F = F_Schlick(specularColor, dotLH); 627: float G = G_BlinnPhong_Implicit(); 628: float D = D_BlinnPhong(shininess, dotNH); 629: return F * (G * D); 630: } 631: float GGXRoughnessToBlinnExponent(const in float ggxRoughness) { 632: return (2.0 / pow2(ggxRoughness + 0.0001) - 2.0); 633: } 634: float BlinnExponentToGGXRoughness(const in float blinnExponent) { 635: return sqrt(2.0 / (blinnExponent + 2.0)); 636: } 637: #if defined(USE_SHEEN) 638: float D_Charlie(float roughness, float NoH) { 639: float invAlpha = 1.0 / roughness; 640: float cos2h = NoH * NoH; 641: float sin2h = max(1.0 - cos2h, 0.0078125); 642: return (2.0 + invAlpha) * pow(sin2h, invAlpha * 0.5) / (2.0 * PI); 643: } 644: float V_Neubelt(float NoV, float NoL) { 645: return saturate(1.0 / (4.0 * (NoL + NoV - NoL * NoV))); 646: } 647: vec3 BRDF_Specular_Sheen(const in float roughness, const in vec3 L, const in GeometricContext geometry, vec3 specularColor) { 648: vec3 N = geometry.normal; 649: vec3 V = geometry.viewDir; 650: vec3 H = normalize(V + L); 651: float dotNH = saturate(dot(N, H)); 652: return specularColor * D_Charlie(roughness, dotNH) * V_Neubelt(dot(N, V), dot(N, L)); 653: } 654: #endif 655: #ifdef ENVMAP_TYPE_CUBE_UV 656: float getFace(vec3 direction) { 657: vec3 absDirection = abs(direction); 658: float face = -1.0; 659: if (absDirection.x > absDirection.z) { 660: if (absDirection.x > absDirection.y) { 661: face = direction.x > 0.0 ? 0.0 : 3.0; 662: } else { 663: face = direction.y > 0.0 ? 1.0 : 4.0; 664: } 665: } else { 666: if (absDirection.z > absDirection.y) { 667: face = direction.z > 0.0 ? 2.0 : 5.0; 668: } else { 669: face = direction.y > 0.0 ? 1.0 : 4.0; 670: } 671: } 672: return face; 673: } 674: vec2 getUV(vec3 direction, float face) { 675: vec2 uv; 676: if (face == 0.0) { 677: uv = vec2(-direction.z, direction.y) / abs(direction.x); 678: } else if (face == 1.0) { 679: uv = vec2(direction.x, -direction.z) / abs(direction.y); 680: } else if (face == 2.0) { 681: uv = direction.xy / abs(direction.z); 682: } else if (face == 3.0) { 683: uv = vec2(direction.z, direction.y) / abs(direction.x); 684: } else if (face == 4.0) { 685: uv = direction.xz / abs(direction.y); 686: } else { 687: uv = vec2(-direction.x, direction.y) / abs(direction.z); 688: } 689: return 0.5 * (uv + 1.0); 690: } 691: #ifndef cubeUV_maxTileSize 692: #define cubeUV_maxTileSize 256.0 693: #endif 694: #define cubeUV_lodIdxMin 0.0 695: #define cubeUV_lodIdxLastDownscaled 4.0 696: #define cubeUV_lodIdxMax 10.0 697: #define cubeUV_minTileSize (cubeUV_maxTileSize / exp2(cubeUV_lodIdxLastDownscaled)) 698: float getLodTileSize(float lodIdx) { 699: return cubeUV_maxTileSize / exp2(min(lodIdx, cubeUV_lodIdxLastDownscaled)); 700: } 701: float getLodFilterLevel(float lodIdx) { 702: return max(lodIdx - cubeUV_lodIdxLastDownscaled, 0.0); 703: } 704: vec2 fixCubeUVSeams(vec2 uv, float faceSize) { 705: float BORDER_WIDTH_PX = max(cubeUV_maxTileSize / 256.0 - 1.0, 0.0); 706: float scale = (faceSize - BORDER_WIDTH_PX) / faceSize; 707: float offset = 0.5 * BORDER_WIDTH_PX / faceSize; 708: return uv * scale + offset; 709: } 710: vec2 getUVPixels(vec3 direction, float lodIdx) { 711: float face = getFace(direction); 712: float faceSize = getLodTileSize(lodIdx); 713: float filterLevel = getLodFilterLevel(lodIdx); 714: vec2 uv = getUV(direction, face); 715: uv = fixCubeUVSeams(uv, faceSize); 716: uv *= (faceSize - 1.0); 717: if (face > 2.0) { 718: uv.y += faceSize; 719: face -= 3.0; 720: } 721: uv.x += face * faceSize; 722: if (lodIdx > 0.0) { 723: uv.y += 2.0 * cubeUV_maxTileSize; 724: } 725: uv.y += filterLevel * 2.0 * cubeUV_minTileSize; 726: uv.x += 3.0 * max(0.0, cubeUV_maxTileSize - 2.0 * faceSize); 727: return uv; 728: } 729: vec3 bilinearCubeUV(sampler2D envMap, vec3 direction, float lodIdx) { 730: float texelSize = 1.0 / (3.0 * cubeUV_maxTileSize); 731: vec2 uv = getUVPixels(direction, lodIdx); 732: vec2 f = fract(uv); 733: uv += 0.5 - f; 734: uv *= texelSize; 735: vec3 tl = envMapTexelToLinear(texture2D(envMap, uv)).rgb; 736: uv.x += texelSize; 737: vec3 tr = envMapTexelToLinear(texture2D(envMap, uv)).rgb; 738: uv.y += texelSize; 739: vec3 br = envMapTexelToLinear(texture2D(envMap, uv)).rgb; 740: uv.x -= texelSize; 741: vec3 bl = envMapTexelToLinear(texture2D(envMap, uv)).rgb; 742: vec3 tm = mix(tl, tr, f.x); 743: vec3 bm = mix(bl, br, f.x); 744: return mix(tm, bm, f.y); 745: } 746: vec3 sampleCubeUV(sampler2D envMap, vec3 direction, float lodIdx) { 747: float texelSize = 1.0 / (3.0 * cubeUV_maxTileSize); 748: vec2 uv = getUVPixels(direction, lodIdx); 749: uv += 0.5; 750: uv *= texelSize; 751: return envMapTexelToLinear(texture2D(envMap, uv)).rgb; 752: } 753: #define cubeUV_r0 1.0 754: #define cubeUV_v0 0.339 755: #define cubeUV_m0 -2.0 756: #define cubeUV_r1 0.8 757: #define cubeUV_v1 0.276 758: #define cubeUV_m1 -1.0 759: #define cubeUV_r4 0.4 760: #define cubeUV_v4 0.046 761: #define cubeUV_m4 2.0 762: #define cubeUV_r5 0.305 763: #define cubeUV_v5 0.016 764: #define cubeUV_m5 3.0 765: #define cubeUV_r6 0.21 766: #define cubeUV_v6 0.0038 767: #define cubeUV_m6 4.0 768: float roughnessToMip(float roughness) { 769: float r = roughness; 770: float r2 = r * r; 771: float r3 = r2 * r; 772: roughness = -1.20278049 * r3 + 1.86860137 * r2 + 0.32478081 * r + 0.0098139; 773: return roughness * (cubeUV_lodIdxMax - cubeUV_lodIdxMin); 774: } 775: vec4 textureCubeUV(sampler2D envMap, vec3 sampleDir, float roughness) { 776: float lodIdx = clamp(roughnessToMip(roughness), cubeUV_lodIdxMin, 777: cubeUV_lodIdxMax); 778: float lodIdxF = fract(lodIdx); 779: float lodIdxI = floor(lodIdx); 780: vec3 color0 = sampleCubeUV(envMap, sampleDir, lodIdxI); 781: if (lodIdxF == 0.0) { 782: return vec4(color0, 1.0); 783: } else { 784: vec3 color1 = sampleCubeUV(envMap, sampleDir, lodIdxI + 1.0); 785: return vec4(mix(color0, color1, lodIdxF), 1.0); 786: } 787: } 788: #endif 789: #ifdef USE_ENVMAP 790: uniform float envMapIntensity; 791: uniform float flipEnvMap; 792: uniform int maxMipLevel; 793: #ifdef ENVMAP_TYPE_CUBE 794: uniform samplerCube envMap; 795: #else 796: uniform sampler2D envMap; 797: #endif 798: #if defined(ENVMAP_TYPE_CUBE) && defined(NODE) || defined(ENVMAP_TYPE_CUBE_UV) 799: float calcGeometryRoughness(vec3 geometryNormal) { 800: vec3 dxy = max(abs(dFdx(geometryNormal)), abs(dFdy(geometryNormal))); 801: return max(max(dxy.x, dxy.y), dxy.z); 802: } 803: float calcCubeUVAdjustedRoughness(float origRoughness, float geomRoughness) { 804: return min(max(origRoughness, 0.0525) + geomRoughness, 1.0); 805: } 806: #endif 807: #endif 808: #if defined(NODE_NEW_GEOMETRY_BL) || defined(NODE_VECT_TRANSFORM_BL) || defined(NODE_TEX_COORD_BL) || defined(NODE_NORMAL_MAP_BL) || defined(NODE_LAYER_WEIGHT_BL) || defined(NODE_FRESNEL_BL) || defined(NODE_BUMP_BL) || defined(NODE_BSDF_GLASS_BL) || defined(NODE_BSDF_PRINCIPLED_BL) || defined(NODE_TANGENT_BL) || defined(NODE_BITMAP_MX) || defined(NODE_GRADIENT_RAMP_MX) || defined(NODE_NOISE_MX) || defined(NODE_SAMPLER_INFO_MY) || defined(NODE_INCIDENT) || defined(NODE_POSITION) || defined(NODE_NORMAL) || defined(USE_OSL) || defined(USE_ENVMAP) || defined(SHADOWMAP_TYPE_ESM) && (defined(NODE_BSDF_DIFFUSE_BL) || defined(NODE_EEVEE_SPECULAR_BL) || defined(NODE_BSDF_GLOSSY_BL) || defined(NODE_BSDF_REFRACTION_BL) || defined(NODE_MATERIAL_MX) || defined(NODE_PHYSICAL_MX) || defined(NODE_STANDARD_SURFACE_AR) || defined(NODE_SHADOW_MATTE_AR)) 809: uniform mat4 invViewMatrix; 810: #endif 811: #if defined(USE_ENVMAP) 812: #define ENVMAP_PARALLAX_INFINITE 0 813: #define ENVMAP_PARALLAX_SPHERE 1 814: #define ENVMAP_PARALLAX_BOX 2 815: #ifdef ENVMAP_MODE_REFRACTION 816: uniform float refractionRatio; 817: #endif 818: uniform int envMapParallaxType; 819: uniform mat4 envMapParallaxMatrix; 820: uniform mat4 envMapParallaxMatrixInv; 821: vec3 correctParallax(vec3 directionVecWorld, vec3 posWorld, int parallaxType) { 822: vec3 posProbe = (envMapParallaxMatrix * vec4(posWorld, 1.0)).xyz; 823: vec3 reflectVecProbe = transformDirection(directionVecWorld, envMapParallaxMatrix); 824: if (parallaxType == ENVMAP_PARALLAX_SPHERE) { 825: float b = 2.0 * dot(reflectVecProbe, posProbe); 826: float c = dot(posProbe, posProbe) - 1.0; 827: float D = b * b - 4.0 * c; 828: if (D >= 0.0) { 829: float x = (sqrt(D) - b) / 2.0; 830: reflectVecProbe = posProbe + x * reflectVecProbe; 831: } 832: } else if (parallaxType == ENVMAP_PARALLAX_BOX) { 833: vec3 scalePos = (vec3(1.0) - posProbe) / reflectVecProbe; 834: vec3 scaleNeg = (vec3(-1.0) - posProbe) / reflectVecProbe; 835: vec3 scalePosNeg = mix(scaleNeg, scalePos, step(vec3(0.0), reflectVecProbe)); 836: float x = min(scalePosNeg.x, min(scalePosNeg.y, scalePosNeg.z)); 837: reflectVecProbe = posProbe + x * reflectVecProbe; 838: } 839: vec3 directionVecWorldCorrected = transformDirection(reflectVecProbe, 840: envMapParallaxMatrixInv); 841: return directionVecWorldCorrected; 842: } 843: vec3 getLightProbeIndirectIrradiance( const in GeometricContext geometry, const in int maxMIPLevel) { 844: vec3 worldNormal = inverseTransformDirection(geometry.normal, viewMatrix); 845: #ifdef ENVMAP_TYPE_CUBE 846: vec3 queryVec = vec3(flipEnvMap * worldNormal.x, worldNormal.yz); 847: #ifdef TEXTURE_LOD_EXT 848: vec4 envMapColor = textureCubeLodEXT(envMap, queryVec, float(maxMIPLevel)); 849: #else 850: vec4 envMapColor = textureCube(envMap, queryVec, float(maxMIPLevel)); 851: #endif 852: envMapColor.rgb = envMapTexelToLinear(envMapColor).rgb; 853: #elif defined(ENVMAP_TYPE_CUBE_UV) 854: vec3 queryVec = vec3(flipEnvMap * worldNormal.x, worldNormal.yz); 855: vec4 envMapColor = textureCubeUV(envMap, queryVec, 1.0); 856: #else 857: vec4 envMapColor = vec4(0.0); 858: #endif 859: return PI * envMapColor.rgb * envMapIntensity; 860: } 861: float getSpecularMIPLevel(const in float blinnShininessExponent, const in int maxMIPLevel) { 862: float maxMIPLevelScalar = float(maxMIPLevel); 863: float clapmedBlinnShininessExponent = min(blinnShininessExponent, 30000.0); 864: float desiredMIPLevel = maxMIPLevelScalar + 0.79248 865: - 0.5 * log2(pow2(clapmedBlinnShininessExponent) + 1.0); 866: return clamp(desiredMIPLevel, 0.0, maxMIPLevelScalar); 867: } 868: vec3 _getLightProbeIndirect(const float blinnShininessExponent, 869: const int maxMIPLevel, vec3 directionVec) { 870: float specularMIPLevel = getSpecularMIPLevel(blinnShininessExponent, maxMIPLevel); 871: #ifdef ENVMAP_TYPE_CUBE 872: vec3 queryVec = vec3(flipEnvMap * directionVec.x, directionVec.yz); 873: #ifdef TEXTURE_LOD_EXT 874: vec4 envMapColor = textureCubeLodEXT(envMap, queryVec, specularMIPLevel); 875: #else 876: vec4 envMapColor = textureCube(envMap, queryVec, specularMIPLevel); 877: #endif 878: envMapColor.rgb = envMapTexelToLinear(envMapColor).rgb; 879: #elif defined(ENVMAP_TYPE_CUBE_UV) 880: vec3 queryVec = vec3(flipEnvMap * directionVec.x, directionVec.yz); 881: vec4 envMapColor = textureCubeUV(envMap, queryVec, 882: BlinnExponentToGGXRoughness(blinnShininessExponent)); 883: #elif defined(ENVMAP_TYPE_EQUIREC) 884: vec2 sampleUV; 885: sampleUV.y = asin(clamp(directionVec.y, - 1.0, 1.0)) * RECIPROCAL_PI + 0.5; 886: sampleUV.x = atan(directionVec.z, directionVec.x) * RECIPROCAL_PI2 + 0.5; 887: #ifdef TEXTURE_LOD_EXT 888: vec4 envMapColor = texture2DLodEXT(envMap, sampleUV, specularMIPLevel); 889: #else 890: vec4 envMapColor = texture2D(envMap, sampleUV, specularMIPLevel); 891: #endif 892: envMapColor.rgb = envMapTexelToLinear(envMapColor).rgb; 893: #endif 894: return envMapColor.rgb * envMapIntensity; 895: } 896: vec3 getLightProbeIndirectRadiance( 897: const GeometricContext geometry, const float blinnShininessExponent, 898: const int maxMIPLevel, const int useCoat) { 899: vec3 normal = geometry.normal; 900: #ifdef CLEARCOAT 901: if (useCoat == 1) { 902: normal = geometry.clearcoatNormal; 903: } 904: #endif 905: #ifdef ENVMAP_MODE_REFLECTION 906: vec3 directionVec = reflect(-geometry.viewDir, normal); 907: #else 908: vec3 directionVec = refract(-geometry.viewDir, normal, refractionRatio); 909: #endif 910: directionVec = inverseTransformDirection(directionVec, viewMatrix); 911: vec3 posWorld = (invViewMatrix * vec4(geometry.position, 1.0)).xyz; 912: if (envMapParallaxType != ENVMAP_PARALLAX_INFINITE) { 913: directionVec = correctParallax(directionVec, posWorld, envMapParallaxType); 914: } 915: return _getLightProbeIndirect(blinnShininessExponent, maxMIPLevel, 916: directionVec); 917: } 918: vec3 getLightProbeIndirectRefraction( 919: const GeometricContext geometry, const float blinnShininessExponent, 920: const int maxMIPLevel, const float refrRatio) { 921: vec3 directionVec = refract(-geometry.viewDir, geometry.normal, refrRatio); 922: directionVec = inverseTransformDirection(directionVec, viewMatrix); 923: vec3 posWorld = (invViewMatrix * vec4(geometry.position, 1.0)).xyz; 924: if (envMapParallaxType != ENVMAP_PARALLAX_INFINITE) { 925: directionVec = correctParallax(directionVec, posWorld, envMapParallaxType); 926: } 927: return _getLightProbeIndirect(blinnShininessExponent, maxMIPLevel, 928: directionVec); 929: } 930: #endif 931: #ifdef USE_FOG 932: uniform vec3 fogColor; 933: varying float fogDepth; 934: #ifdef FOG_EXP2 935: uniform float fogDensity; 936: #else 937: uniform float fogNear; 938: uniform float fogFar; 939: #endif 940: #endif 941: uniform bool receiveShadow; 942: uniform vec3 ambientLightColor; 943: uniform vec3 lightProbe[9]; 944: vec3 shGetIrradianceAt(in vec3 normal, in vec3 shCoefficients[9]) { 945: float x = normal.x, y = normal.y, z = normal.z; 946: vec3 result = shCoefficients[0] * 0.886227; 947: result += shCoefficients[1] * 2.0 * 0.511664 * y; 948: result += shCoefficients[2] * 2.0 * 0.511664 * z; 949: result += shCoefficients[3] * 2.0 * 0.511664 * x; 950: result += shCoefficients[4] * 2.0 * 0.429043 * x * y; 951: result += shCoefficients[5] * 2.0 * 0.429043 * y * z; 952: result += shCoefficients[6] * (0.743125 * z * z - 0.247708); 953: result += shCoefficients[7] * 2.0 * 0.429043 * x * z; 954: result += shCoefficients[8] * 0.429043 * (x * x - y * y); 955: return result; 956: } 957: vec3 getLightProbeIrradiance(const in vec3 lightProbe[9], const in GeometricContext geometry) { 958: vec3 worldNormal = inverseTransformDirection(geometry.normal, viewMatrix); 959: vec3 irradiance = shGetIrradianceAt(worldNormal, lightProbe); 960: return irradiance; 961: } 962: vec3 getAmbientLightIrradiance(const in vec3 ambientLightColor) { 963: vec3 irradiance = ambientLightColor; 964: #ifndef PHYSICALLY_CORRECT_LIGHTS 965: irradiance *= PI; 966: #endif 967: return irradiance; 968: } 969: #if 0 > 0 970: struct DirectionalLight { 971: vec3 direction; 972: vec3 color; 973: }; 974: uniform DirectionalLight directionalLights[0]; 975: void getDirectionalDirectLightIrradiance(const in DirectionalLight directionalLight, const in GeometricContext geometry, out IncidentLight directLight) { 976: directLight.color = directionalLight.color; 977: directLight.direction = directionalLight.direction; 978: directLight.visible = true; 979: } 980: #endif 981: #if 0 > 0 982: struct PointLight { 983: vec3 position; 984: vec3 color; 985: float distance; 986: float decay; 987: }; 988: uniform PointLight pointLights[0]; 989: void getPointDirectLightIrradiance(const in PointLight pointLight, const in GeometricContext geometry, out IncidentLight directLight) { 990: vec3 lVector = pointLight.position - geometry.position; 991: directLight.direction = normalize(lVector); 992: float lightDistance = length(lVector); 993: directLight.color = pointLight.color; 994: directLight.color *= punctualLightIntensityToIrradianceFactor(lightDistance, pointLight.distance, pointLight.decay); 995: directLight.visible = (directLight.color != vec3(0.0)); 996: } 997: #endif 998: #if 0 > 0 999: struct SpotLight { 1000: vec3 position; 1001: vec3 direction; 1002: vec3 color; 1003: float distance; 1004: float decay; 1005: float coneCos; 1006: float penumbraCos; 1007: }; 1008: uniform SpotLight spotLights[0]; 1009: void getSpotDirectLightIrradiance(const in SpotLight spotLight, const in GeometricContext geometry, out IncidentLight directLight) { 1010: vec3 lVector = spotLight.position - geometry.position; 1011: directLight.direction = normalize(lVector); 1012: float lightDistance = length(lVector); 1013: float angleCos = dot(directLight.direction, spotLight.direction); 1014: #if defined(MT_MAX) && defined(PHYSICALLY_CORRECT_LIGHTS) 1015: float coneCosDecayed = 2.0 * spotLight.coneCos - spotLight.penumbraCos; 1016: if (angleCos > coneCosDecayed) { 1017: float spotEffect = pow(max(angleCos, 0.0), log(0.5) / log(spotLight.penumbraCos) - 1.0); 1018: if (angleCos < spotLight.coneCos) { 1019: float decayFac = 1.0 + (spotLight.coneCos - angleCos) 1020: / (spotLight.coneCos - spotLight.penumbraCos); 1021: spotEffect *= pow2(decayFac) * (3.0 - 2.0 * decayFac); 1022: } 1023: directLight.color = spotLight.color * spotEffect 1024: * punctualLightIntensityToIrradianceFactor(lightDistance, 1025: spotLight.distance, spotLight.decay); 1026: directLight.visible = true; 1027: } else { 1028: directLight.color = vec3(0.0); 1029: directLight.visible = false; 1030: } 1031: #else 1032: if (angleCos > spotLight.coneCos) { 1033: float spotEffect = smoothstep(spotLight.coneCos, spotLight.penumbraCos, angleCos); 1034: directLight.color = spotLight.color * spotEffect 1035: * punctualLightIntensityToIrradianceFactor(lightDistance, 1036: spotLight.distance, spotLight.decay); 1037: directLight.visible = true; 1038: } else { 1039: directLight.color = vec3(0.0); 1040: directLight.visible = false; 1041: } 1042: #endif 1043: } 1044: #endif 1045: #if 0 > 0 1046: struct RectAreaLight { 1047: vec3 color; 1048: vec3 position; 1049: vec3 halfWidth; 1050: vec3 halfHeight; 1051: }; 1052: uniform sampler2D ltc_1; 1053: uniform sampler2D ltc_2; 1054: uniform RectAreaLight rectAreaLights[0]; 1055: #endif 1056: #if 0 > 0 1057: struct HemisphereLight { 1058: vec3 direction; 1059: vec3 skyColor; 1060: vec3 groundColor; 1061: }; 1062: uniform HemisphereLight hemisphereLights[0]; 1063: vec3 getHemisphereLightIrradiance(const in HemisphereLight hemiLight, const in GeometricContext geometry) { 1064: float dotNL = dot(geometry.normal, hemiLight.direction); 1065: float hemiDiffuseWeight = 0.5 * dotNL + 0.5; 1066: vec3 irradiance = mix(hemiLight.groundColor, hemiLight.skyColor, hemiDiffuseWeight); 1067: #ifndef PHYSICALLY_CORRECT_LIGHTS 1068: irradiance *= PI; 1069: #endif 1070: return irradiance; 1071: } 1072: #endif 1073: 1074: varying vec3 vViewPosition; 1075: #ifndef FLAT_SHADED 1076: varying vec3 vNormal; 1077: #endif 1078: struct NodeMaterial { 1079: vec3 diffuseColor; 1080: vec3 specularColor; 1081: vec3 refractionColor; 1082: float refractionRoughness; 1083: float refractionIOR; 1084: #ifdef CLEARCOAT 1085: float clearcoat; 1086: float clearcoatRoughness; 1087: #endif 1088: #ifdef USE_SHEEN 1089: vec3 sheenColor; 1090: float sheenRoughness; 1091: #endif 1092: #ifdef PHYSICAL 1093: vec3 fresnelRefl90; 1094: float specularRoughness; 1095: #else 1096: float diffuseIntensity; 1097: float specularIntensity; 1098: float specularHardness; 1099: #endif 1100: }; 1101: #define MAXIMUM_SPECULAR_COEFFICIENT 0.16 1102: #define DEFAULT_SPECULAR_COEFFICIENT 0.04 1103: #define BLENDER_SPECULAR_COEFFICIENT 0.08 1104: float shadeSpecPhong(vec3 normal, vec3 lightDir, vec3 viewDir, float hard) 1105: { 1106: float specFac; 1107: vec3 h = normalize(lightDir + viewDir); 1108: float rslt = max(dot(h, normal), 0.0); 1109: specFac = pow(rslt, hard); 1110: return specFac; 1111: } 1112: float clearcoatDHRApprox(const in float roughness, const in float dotNL) { 1113: return DEFAULT_SPECULAR_COEFFICIENT + (1.0 - DEFAULT_SPECULAR_COEFFICIENT) * (pow(1.0 - dotNL, 5.0) * pow(1.0 - roughness, 2.0)); 1114: } 1115: #if 0 > 0 1116: void RE_Direct_RectArea_Node(const in RectAreaLight rectAreaLight, const in GeometricContext geometry, const in NodeMaterial material, inout ReflectedLight reflectedLight) { 1117: vec3 normal = geometry.normal; 1118: vec3 viewDir = geometry.viewDir; 1119: vec3 position = geometry.position; 1120: vec3 lightPos = rectAreaLight.position; 1121: vec3 halfWidth = rectAreaLight.halfWidth; 1122: vec3 halfHeight = rectAreaLight.halfHeight; 1123: vec3 lightColor = rectAreaLight.color; 1124: float roughness = material.specularRoughness; 1125: vec3 rectCoords[4]; 1126: rectCoords[0] = lightPos + halfWidth - halfHeight; 1127: rectCoords[1] = lightPos - halfWidth - halfHeight; 1128: rectCoords[2] = lightPos - halfWidth + halfHeight; 1129: rectCoords[3] = lightPos + halfWidth + halfHeight; 1130: vec2 uv = LTC_Uv(normal, viewDir, roughness); 1131: vec4 t1 = texture2D(ltc_1, uv); 1132: vec4 t2 = texture2D(ltc_2, uv); 1133: mat3 mInv = mat3( 1134: vec3(t1.x, 0, t1.y), 1135: vec3( 0, 1, 0), 1136: vec3(t1.z, 0, t1.w) 1137: ); 1138: vec3 fresnel = (material.specularColor * t2.x + (vec3(1.0) - material.specularColor) * t2.y); 1139: reflectedLight.directSpecular += lightColor * fresnel * LTC_Evaluate(normal, viewDir, position, mInv, rectCoords); 1140: reflectedLight.directDiffuse += lightColor * material.diffuseColor * LTC_Evaluate(normal, viewDir, position, mat3(1.0), rectCoords); 1141: } 1142: #endif 1143: void RE_DirectDiffuse_Node(const in IncidentLight directLight, 1144: const in GeometricContext geometry, const in NodeMaterial material, 1145: inout ReflectedLight reflectedLight) { 1146: float dotNL = saturate(dot(geometry.normal, directLight.direction)); 1147: vec3 irradiance = dotNL * directLight.color; 1148: #ifndef PHYSICALLY_CORRECT_LIGHTS 1149: irradiance *= PI; 1150: #endif 1151: #ifdef PHYSICAL 1152: #ifdef CLEARCOAT 1153: float ccDotNL = saturate(dot(geometry.clearcoatNormal, directLight.direction)); 1154: float clearcoatDHR = material.clearcoat * clearcoatDHRApprox(material.clearcoatRoughness, ccDotNL); 1155: #else 1156: float clearcoatDHR = 0.0; 1157: #endif 1158: reflectedLight.directDiffuse += (1.0 - clearcoatDHR) * irradiance 1159: * BRDF_Diffuse_Lambert(material.diffuseColor); 1160: #else 1161: reflectedLight.directDiffuse += irradiance * material.diffuseIntensity 1162: * BRDF_Diffuse_Lambert(material.diffuseColor); 1163: #endif 1164: } 1165: void RE_DirectSpecular_Node(const in IncidentLight directLight, 1166: const in GeometricContext geometry, const in NodeMaterial material, 1167: inout ReflectedLight reflectedLight) { 1168: float dotNL = saturate(dot(geometry.normal, directLight.direction)); 1169: vec3 irradiance = dotNL * directLight.color; 1170: #ifndef PHYSICALLY_CORRECT_LIGHTS 1171: irradiance *= PI; 1172: #endif 1173: #ifdef PHYSICAL 1174: #ifdef CLEARCOAT 1175: float ccDotNL = saturate(dot(geometry.clearcoatNormal, directLight.direction)); 1176: vec3 ccIrradiance = ccDotNL * directLight.color; 1177: #ifndef PHYSICALLY_CORRECT_LIGHTS 1178: ccIrradiance *= PI; 1179: #endif 1180: #endif 1181: #ifdef CLEARCOAT 1182: float clearcoatDHR = material.clearcoat * clearcoatDHRApprox(material.clearcoatRoughness, ccDotNL); 1183: #else 1184: float clearcoatDHR = 0.0; 1185: #endif 1186: reflectedLight.directSpecular += (1.0 - clearcoatDHR) * irradiance 1187: * BRDF_Specular_GGX(directLight, geometry.viewDir, geometry.normal, 1188: material.specularColor, material.specularRoughness); 1189: #ifdef CLEARCOAT 1190: reflectedLight.directSpecular += ccIrradiance * material.clearcoat 1191: * BRDF_Specular_GGX(directLight, geometry.viewDir, geometry.clearcoatNormal, 1192: vec3(DEFAULT_SPECULAR_COEFFICIENT), material.clearcoatRoughness); 1193: #endif 1194: #ifdef USE_SHEEN 1195: reflectedLight.directSpecular += (1.0 - clearcoatDHR) * irradiance * BRDF_Specular_Sheen( 1196: material.sheenRoughness, 1197: directLight.direction, 1198: geometry, 1199: material.sheenColor 1200: ); 1201: #endif 1202: #else 1203: reflectedLight.directSpecular += material.specularIntensity * 1204: shadeSpecPhong(geometry.normal, directLight.direction, 1205: geometry.viewDir, material.specularHardness) * 1206: directLight.color * material.specularColor; 1207: #endif 1208: } 1209: void RE_IndirectDiffuse_Node(const in vec3 irradiance, const in vec3 iblIrradiance, 1210: const in GeometricContext geometry, const in NodeMaterial material, 1211: inout ReflectedLight reflectedLight) { 1212: reflectedLight.indirectDiffuse += (irradiance + iblIrradiance) 1213: * BRDF_Diffuse_Lambert(material.diffuseColor); 1214: } 1215: void RE_IndirectSpecular_Node(const in vec3 radiance, const in vec3 irradiance, 1216: const in vec3 clearcoatRadiance, const in GeometricContext geometry, 1217: const in NodeMaterial material, inout ReflectedLight reflectedLight) { 1218: #ifdef PHYSICAL 1219: #ifdef CLEARCOAT 1220: float ccDotNL = saturate(dot(geometry.clearcoatNormal, geometry.viewDir)); 1221: float clearcoatDHR = material.clearcoat * clearcoatDHRApprox( 1222: material.clearcoatRoughness, ccDotNL); 1223: #else 1224: float clearcoatDHR = 0.0; 1225: #endif 1226: #ifdef MT_BLENDER 1227: #ifdef COMPAT_USE_SPEC_ENV_BLENDER_APPROX 1228: vec3 specEnv = BRDF_Specular_GGX_Environment_Blender_Approx(geometry, 1229: material.specularColor, material.fresnelRefl90, 1230: material.specularRoughness, 0); 1231: #else 1232: vec3 specEnv = BRDF_Specular_GGX_Environment(geometry.viewDir, 1233: geometry.normal, material.specularColor, 1234: material.specularRoughness); 1235: #endif 1236: #elif defined(MT_MAX) 1237: float alphaEnv = pow2(pow2(material.specularRoughness)); 1238: vec3 specEnv = material.specularColor / (1.0 - alphaEnv + PI * alphaEnv); 1239: #elif defined(MT_MAYA) 1240: vec3 specEnv = BRDF_Specular_GGX_Environment(geometry.viewDir, geometry.normal, material.specularColor, material.specularRoughness); 1241: #else 1242: vec3 specEnv = vec3(1.0); 1243: #endif 1244: reflectedLight.indirectSpecular += (1.0 - clearcoatDHR) * radiance * specEnv; 1245: #ifdef CLEARCOAT 1246: #ifdef MT_BLENDER 1247: vec3 specEnvCC = BRDF_Specular_GGX_Environment(geometry.viewDir, geometry.clearcoatNormal, vec3(DEFAULT_SPECULAR_COEFFICIENT), material.clearcoatRoughness); 1248: #elif defined(MT_MAX) || defined(MT_MAYA) 1249: vec3 specEnvCC = BRDF_Specular_GGX_Environment(geometry.viewDir, geometry.clearcoatNormal, vec3(DEFAULT_SPECULAR_COEFFICIENT), material.clearcoatRoughness); 1250: #else 1251: vec3 specEnvCC = vec3(1.0); 1252: #endif 1253: reflectedLight.indirectSpecular += clearcoatRadiance * material.clearcoat * specEnvCC; 1254: #endif 1255: #endif 1256: } 1257: void RE_Refraction_Node(const vec3 refraction, const NodeMaterial material, 1258: inout vec3 refractedLight) { 1259: refractedLight += refraction * material.refractionColor; 1260: } 1261: #ifdef MT_BLENDER 1262: #define Material_BlinnShininessExponent(material) GGXRoughnessToBlinnExponent(material.specularRoughness) 1263: #define Material_Refraction_BlinnShininessExponent(material) GGXRoughnessToBlinnExponent(material.refractionRoughness) 1264: #elif defined(MT_MAX) 1265: #define Material_BlinnShininessExponent(material) GGXRoughnessToBlinnExponent(material.specularRoughness * exp(0.35*(1.0-pow2(material.specularRoughness)))) 1266: #define Material_Refraction_BlinnShininessExponent(material) GGXRoughnessToBlinnExponent(material.refractionRoughness * exp(0.35*(1.0-pow2(material.refractionRoughness)))) 1267: #else 1268: #define Material_BlinnShininessExponent(material) GGXRoughnessToBlinnExponent(material.specularRoughness) 1269: #define Material_Refraction_BlinnShininessExponent(material) GGXRoughnessToBlinnExponent(material.refractionRoughness) 1270: #endif 1271: #define Material_ClearCoat_BlinnShininessExponent(material) GGXRoughnessToBlinnExponent(material.clearcoatRoughness) 1272: float computeSpecularOcclusion(const in float dotNV, const in float ambientOcclusion, const in float roughness) { 1273: return saturate(pow(abs(dotNV + ambientOcclusion), exp2(- 16.0 * roughness - 1.0)) - 1.0 + ambientOcclusion); 1274: } 1275: #define RE_Direct_RectArea RE_Direct_RectArea_Node 1276: 1277: #define BIAS_FRUSTUM_SCALE_COEFF 30.0 1278: #define ESM_SPOT_SINGLE_BLUR_COEFF 0.25 1279: #define PCF_POISSON_SPOT_OMNI_BLUR_COEFF 4.0 1280: #define PCF_POISSON_POINT_BLUR_COEFF 2.5 1281: #define PCF_PCF_SOFT_DIR_SPOT_BLUR_COEFF 0.5 1282: #define ESM_BIAS_COEFF 100.0 1283: #ifndef ESM_DISTANCE_SCALE 1284: #define ESM_DISTANCE_SCALE 1.0 1285: #endif 1286: #ifdef USE_SHADOWMAP 1287: #if 0 > 0 1288: uniform sampler2D directionalShadowMap[0]; 1289: varying vec4 vDirectionalShadowCoord[0]; 1290: struct DirectionalLightShadow { 1291: float shadowBias; 1292: float shadowNormalBias; 1293: float shadowRadius; 1294: vec2 shadowMapSize; 1295: vec3 position; 1296: float shadowCameraNear; 1297: float maxDistance; 1298: float expBias; 1299: }; 1300: uniform DirectionalLightShadow directionalLightShadows[0]; 1301: #endif 1302: #if 0 > 0 1303: uniform sampler2D spotShadowMap[0]; 1304: varying vec4 vSpotShadowCoord[0]; 1305: struct SpotLightShadow { 1306: float shadowBias; 1307: float shadowNormalBias; 1308: float shadowRadius; 1309: vec2 shadowMapSize; 1310: int shadow; 1311: float shadowCameraNear; 1312: float shadowCameraFar; 1313: float expBias; 1314: }; 1315: uniform SpotLightShadow spotLightShadows[0]; 1316: #endif 1317: #if 0 > 0 1318: uniform sampler2D pointShadowMap[0]; 1319: varying vec4 vPointShadowCoord[0]; 1320: struct PointLightShadow { 1321: float shadowBias; 1322: float shadowNormalBias; 1323: float shadowRadius; 1324: vec2 shadowMapSize; 1325: float shadowCameraNear; 1326: float shadowCameraFar; 1327: float expBias; 1328: }; 1329: uniform PointLightShadow pointLightShadows[0]; 1330: #endif 1331: #if 0 > 0 1332: uniform sampler2D rectAreaShadowMap[0]; 1333: varying vec4 vRectAreaShadowCoord[0]; 1334: struct RectAreaLightShadow { 1335: float shadowBias; 1336: float shadowNormalBias; 1337: float shadowRadius; 1338: vec2 shadowMapSize; 1339: float shadowCameraNear; 1340: float shadowCameraFar; 1341: float expBias; 1342: }; 1343: uniform RectAreaLightShadow rectAreaLightShadows[0]; 1344: #endif 1345: const vec3 PERMUTE_DIR_X = vec3(1.0, 0.0, 0.0); 1346: const vec3 PERMUTE_DIR_Y = vec3(0.0, 1.0, 0.0); 1347: const vec3 PERMUTE_DIR_Z = vec3(0.0, 0.0, 1.0); 1348: const mat4 POISSON_DISK_0 = mat4( 1349: 0.954845, 0.242214, -0.623893, -0.235473, 1350: -0.173288, 0.799228, 0.605969, -0.548050, 1351: -0.560406, 0.327647, -0.448307, -0.774344, 1352: 0.308258, 0.417332, -0.125623, -0.056098 1353: ); 1354: const mat4 POISSON_DISK_1 = mat4( 1355: 0.145585, -0.305634, 0.264060, -0.661648, 1356: 0.617942, 0.652121, -0.041412, -0.893582, 1357: 0.463911, 0.039752, 0.212664, 0.810727, 1358: -0.955989, -0.014390, -0.652588, 0.671204 1359: ); 1360: float texture2DCompare(sampler2D depths, vec2 uv, float compare) { 1361: return step(compare, unpackRGBAToDepth(texture2D(depths, uv))); 1362: } 1363: vec2 texture2DDistribution(sampler2D shadow, vec2 uv) { 1364: return unpackRGBATo2Half(texture2D(shadow, uv)); 1365: } 1366: float VSMShadow (sampler2D shadow, vec2 uv, float compare){ 1367: float occlusion = 1.0; 1368: vec2 distribution = texture2DDistribution(shadow, uv); 1369: float hard_shadow = step(compare , distribution.x); 1370: if (hard_shadow != 1.0) { 1371: float distance = compare - distribution.x ; 1372: float variance = max(0.00000, distribution.y * distribution.y); 1373: float softness_probability = variance / (variance + distance * distance); 1374: softness_probability = clamp((softness_probability - 0.3) / (0.95 - 0.3), 0.0, 1.0); 1375: occlusion = clamp(max(hard_shadow, softness_probability), 0.0, 1.0); 1376: } 1377: return occlusion; 1378: } 1379: float texture2DShadowLerp(sampler2D depths, vec2 size, vec2 uv, float compare) { 1380: const vec2 offset = vec2(0.0, 1.0); 1381: vec2 texelSize = vec2(1.0) / size; 1382: vec2 centroidUV = floor(uv * size + 0.5) / size; 1383: float lb = texture2DCompare(depths, centroidUV + texelSize * offset.xx, compare); 1384: float lt = texture2DCompare(depths, centroidUV + texelSize * offset.xy, compare); 1385: float rb = texture2DCompare(depths, centroidUV + texelSize * offset.yx, compare); 1386: float rt = texture2DCompare(depths, centroidUV + texelSize * offset.yy, compare); 1387: vec2 f = fract(uv * size + 0.5); 1388: float a = mix(lb, lt, f.y); 1389: float b = mix(rb, rt, f.y); 1390: float c = mix(a, b, f.x); 1391: return c; 1392: } 1393: vec2 cubeToUV(vec3 v, float texelSizeY) { 1394: vec3 absV = abs(v); 1395: float scaleToCube = 1.0 / max(absV.x, max(absV.y, absV.z)); 1396: absV *= scaleToCube; 1397: v *= scaleToCube * (1.0 - 2.0 * texelSizeY); 1398: vec2 planar = v.xy; 1399: float almostATexel = 1.5 * texelSizeY; 1400: float almostOne = 1.0 - almostATexel; 1401: if (absV.z >= almostOne) { 1402: if (v.z > 0.0) 1403: planar.x = 4.0 - v.x; 1404: } else if (absV.x >= almostOne) { 1405: float signX = sign(v.x); 1406: planar.x = v.z * signX + 2.0 * signX; 1407: } else if (absV.y >= almostOne) { 1408: float signY = sign(v.y); 1409: planar.x = v.x + 2.0 * signY + 2.0; 1410: planar.y = v.z * signY - 2.0; 1411: } 1412: return vec2(0.125, 0.25) * planar + vec2(0.375, 0.75); 1413: } 1414: float texture2DShadowAvgCube(sampler2D depths, vec2 size, vec3 bd3D, float compare) { 1415: vec2 texelSize = vec2(1.0) / size; 1416: vec3 dirX = normalize(abs(bd3D.y) < 0.99999 ? vec3(bd3D.z, 0.0, -bd3D.x) 1417: : vec3(0.0, -bd3D.z, bd3D.y)); 1418: vec3 dirY = cross(bd3D, dirX); 1419: float theta = PI_HALF * texelSize.y; 1420: vec3 sX = sin(theta) * dirX; 1421: vec3 sY = sin(theta) * dirY; 1422: float cosT = cos(theta); 1423: vec3 sampleVec[4]; 1424: sampleVec[0] = bd3D; 1425: sampleVec[1] = bd3D * cosT + sY; 1426: sampleVec[2] = bd3D * cosT + sX; 1427: sampleVec[3] = sampleVec[2] * cosT + sY; 1428: float avg = 0.0; 1429: for (int i = 0; i < 4; i++) { 1430: avg += texture2DCompare(depths, cubeToUV(sampleVec[i], texelSize.y), compare); 1431: } 1432: avg /= 4.0; 1433: return avg; 1434: } 1435: float getShadow(sampler2D shadowMap, vec2 shadowMapSize, float shadowBias, float shadowRadius, vec4 shadowCoord, 1436: float expBias, float distWorld) { 1437: float shadow = 1.0; 1438: shadowCoord.xyz /= shadowCoord.w; 1439: bvec4 inFrustumVec = bvec4 (shadowCoord.x >= 0.0, shadowCoord.x <= 1.0, shadowCoord.y >= 0.0, shadowCoord.y <= 1.0); 1440: bool inFrustum = all(inFrustumVec); 1441: bvec3 frustumTestVec = bvec3(inFrustum, shadowCoord.z <= 1.0, shadowCoord.z >= 0.0); 1442: bool frustumTest = all(frustumTestVec); 1443: if (frustumTest) { 1444: #if defined(SHADOWMAP_TYPE_BILINEAR) 1445: shadowCoord.z += shadowBias; 1446: shadow = texture2DShadowLerp(shadowMap, shadowMapSize, 1447: shadowCoord.xy, shadowCoord.z); 1448: #elif defined(SHADOWMAP_TYPE_PCF) 1449: shadowCoord.z += shadowBias; 1450: vec2 texelSize = vec2(1.0) / shadowMapSize; 1451: float dx0 = - texelSize.x * shadowRadius; 1452: float dy0 = - texelSize.y * shadowRadius; 1453: float dx1 = + texelSize.x * shadowRadius; 1454: float dy1 = + texelSize.y * shadowRadius; 1455: vec2 offsetVec[9]; 1456: offsetVec[0] = vec2(dx0, dy0); 1457: offsetVec[1] = vec2(0.0, dy0); 1458: offsetVec[2] = vec2(dx1, dy0); 1459: offsetVec[3] = vec2(dx0, 0.0); 1460: offsetVec[4] = vec2(0.0); 1461: offsetVec[5] = vec2(dx1, 0.0); 1462: offsetVec[6] = vec2(dx0, dy1); 1463: offsetVec[7] = vec2(0.0, dy1); 1464: offsetVec[8] = vec2(dx1, dy1); 1465: shadow = 0.0; 1466: for (int i = 0; i < 9; i++) { 1467: shadow += texture2DCompare(shadowMap, shadowCoord.xy + offsetVec[i], shadowCoord.z); 1468: } 1469: shadow /= 9.0; 1470: #elif defined(SHADOWMAP_TYPE_PCF_SOFT) 1471: shadowCoord.z += shadowBias; 1472: vec2 texelSize = vec2(1.0) / shadowMapSize; 1473: float dx0 = - texelSize.x * shadowRadius; 1474: float dy0 = - texelSize.y * shadowRadius; 1475: float dx1 = + texelSize.x * shadowRadius; 1476: float dy1 = + texelSize.y * shadowRadius; 1477: vec2 offsetVec[9]; 1478: offsetVec[0] = vec2(dx0, dy0); 1479: offsetVec[1] = vec2(0.0, dy0); 1480: offsetVec[2] = vec2(dx1, dy0); 1481: offsetVec[3] = vec2(dx0, 0.0); 1482: offsetVec[4] = vec2(0.0); 1483: offsetVec[5] = vec2(dx1, 0.0); 1484: offsetVec[6] = vec2(dx0, dy1); 1485: offsetVec[7] = vec2(0.0, dy1); 1486: offsetVec[8] = vec2(dx1, dy1); 1487: shadow = 0.0; 1488: for (int i = 0; i < 9; i++) { 1489: shadow += texture2DShadowLerp(shadowMap, shadowMapSize, 1490: shadowCoord.xy + offsetVec[i], shadowCoord.z); 1491: } 1492: shadow /= 9.0; 1493: #elif defined(SHADOWMAP_TYPE_PCF_POISSON_DISK) 1494: shadowCoord.z += shadowBias; 1495: vec2 texelSize = vec2(1.0) / shadowMapSize; 1496: float randAngle = rand(gl_FragCoord.xy) * PI2; 1497: float c = cos(randAngle), s = sin(randAngle); 1498: mat2 sampleMat = mat2(c, s, -s, c) 1499: * mat2(shadowRadius * texelSize.x, 0.0, 0.0, shadowRadius * texelSize.y); 1500: vec2 sampleVec[16]; 1501: sampleVec[0] = POISSON_DISK_0[0].xy; 1502: sampleVec[1] = POISSON_DISK_0[0].zw; 1503: sampleVec[2] = POISSON_DISK_0[1].xy; 1504: sampleVec[3] = POISSON_DISK_0[1].zw; 1505: sampleVec[4] = POISSON_DISK_0[2].xy; 1506: sampleVec[5] = POISSON_DISK_0[2].zw; 1507: sampleVec[6] = POISSON_DISK_0[3].xy; 1508: sampleVec[7] = POISSON_DISK_0[3].zw; 1509: sampleVec[8] = POISSON_DISK_1[0].xy; 1510: sampleVec[9] = POISSON_DISK_1[0].zw; 1511: sampleVec[10] = POISSON_DISK_1[1].xy; 1512: sampleVec[11] = POISSON_DISK_1[1].zw; 1513: sampleVec[12] = POISSON_DISK_1[2].xy; 1514: sampleVec[13] = POISSON_DISK_1[2].zw; 1515: sampleVec[14] = POISSON_DISK_1[3].xy; 1516: sampleVec[15] = POISSON_DISK_1[3].zw; 1517: shadow = 0.0; 1518: for (int i = 0; i < 16; i++) { 1519: shadow += texture2DCompare(shadowMap, shadowCoord.xy 1520: + sampleMat * sampleVec[i], shadowCoord.z); 1521: } 1522: shadow /= 16.0; 1523: #elif defined(SHADOWMAP_TYPE_VSM) 1524: shadow = VSMShadow(shadowMap, shadowCoord.xy, shadowCoord.z); 1525: #elif defined(SHADOWMAP_TYPE_ESM) 1526: shadow = saturate(exp(expBias * (texture2D(shadowMap, shadowCoord.xy).x 1527: - length(distWorld) * ESM_DISTANCE_SCALE 1528: - ESM_BIAS_COEFF * shadowBias))); 1529: #else 1530: shadowCoord.z += shadowBias; 1531: shadow = texture2DCompare(shadowMap, shadowCoord.xy, shadowCoord.z); 1532: #endif 1533: } 1534: return shadow; 1535: } 1536: float getShadow(sampler2D shadowMap, vec2 shadowMapSize, float shadowBias, float shadowRadius, vec4 shadowCoord) { 1537: return getShadow(shadowMap, shadowMapSize, shadowBias, shadowRadius, shadowCoord, 0.0, 0.0); 1538: } 1539: float getOmniShadow(sampler2D shadowMap, vec2 shadowMapSize, float shadowBias, 1540: float expBias, float shadowRadius, vec4 shadowCoord, 1541: float shadowCameraNear, float shadowCameraFar) { 1542: float shadow = 1.0; 1543: vec3 lightToPosition = shadowCoord.xyz; 1544: float dp = (length(lightToPosition) - shadowCameraNear) 1545: / (shadowCameraFar - shadowCameraNear); 1546: bvec2 frustumTestVec = bvec2(dp <= 1.0, dp >= 0.0); 1547: bool frustumTest = all(frustumTestVec); 1548: if (frustumTest) { 1549: float biasScaleCoeff = BIAS_FRUSTUM_SCALE_COEFF 1550: / (shadowCameraFar - shadowCameraNear); 1551: dp += shadowBias * biasScaleCoeff; 1552: vec3 bd3D = normalize(lightToPosition); 1553: vec2 texelSize = 1.0 / shadowMapSize; 1554: #if defined(SHADOWMAP_TYPE_BILINEAR) 1555: shadow = texture2DShadowAvgCube(shadowMap, shadowMapSize, bd3D, dp); 1556: #elif defined(SHADOWMAP_TYPE_ESM) 1557: shadow = saturate(exp(expBias * (texture2D(shadowMap, 1558: cubeVecToOctUV(bd3D, texelSize)).x 1559: - length(lightToPosition) * ESM_DISTANCE_SCALE 1560: - ESM_BIAS_COEFF * shadowBias))); 1561: #elif defined(SHADOWMAP_TYPE_PCF) || defined(SHADOWMAP_TYPE_PCF_SOFT) 1562: vec2 offset = vec2(-1, 1) * shadowRadius * texelSize.y; 1563: vec3 offsetVec[9]; 1564: offsetVec[0] = offset.xyy; 1565: offsetVec[1] = offset.yyy; 1566: offsetVec[2] = offset.xyx; 1567: offsetVec[3] = offset.yyx; 1568: offsetVec[4] = vec3(0.0); 1569: offsetVec[5] = offset.xxy; 1570: offsetVec[6] = offset.yxy; 1571: offsetVec[7] = offset.xxx; 1572: offsetVec[8] = offset.yxx; 1573: shadow = 0.0; 1574: for (int i = 0; i < 9; i++) { 1575: shadow += texture2DCompare(shadowMap, cubeToUV(bd3D + offsetVec[i], texelSize.y), dp); 1576: } 1577: shadow /= 9.0; 1578: #elif defined(SHADOWMAP_TYPE_PCF_POISSON_DISK) 1579: float randAngle = rand(gl_FragCoord.xy) * PI2; 1580: float c = cos(randAngle), s = sin(randAngle); 1581: mat2 sampleMat = mat2(c, s, -s, c) 1582: * mat2(shadowRadius * texelSize.y, 0.0, 0.0, shadowRadius * texelSize.y); 1583: vec3 absBd3D = abs(bd3D); 1584: absBd3D /= max(absBd3D.x, max(absBd3D.y, absBd3D.z)); 1585: bvec2 isPointingCubeFace = greaterThan(absBd3D.xy, vec2(0.999)); 1586: mat3 permuteMat = mat3( 1587: isPointingCubeFace.x ? PERMUTE_DIR_Y : PERMUTE_DIR_X, 1588: isPointingCubeFace.x || isPointingCubeFace.y ? PERMUTE_DIR_Z : PERMUTE_DIR_Y, 1589: isPointingCubeFace.x ? PERMUTE_DIR_X : isPointingCubeFace.y ? PERMUTE_DIR_Y : PERMUTE_DIR_Z 1590: ); 1591: vec2 sampleVec[16]; 1592: sampleVec[0] = POISSON_DISK_0[0].xy; 1593: sampleVec[1] = POISSON_DISK_0[0].zw; 1594: sampleVec[2] = POISSON_DISK_0[1].xy; 1595: sampleVec[3] = POISSON_DISK_0[1].zw; 1596: sampleVec[4] = POISSON_DISK_0[2].xy; 1597: sampleVec[5] = POISSON_DISK_0[2].zw; 1598: sampleVec[6] = POISSON_DISK_0[3].xy; 1599: sampleVec[7] = POISSON_DISK_0[3].zw; 1600: sampleVec[8] = POISSON_DISK_1[0].xy; 1601: sampleVec[9] = POISSON_DISK_1[0].zw; 1602: sampleVec[10] = POISSON_DISK_1[1].xy; 1603: sampleVec[11] = POISSON_DISK_1[1].zw; 1604: sampleVec[12] = POISSON_DISK_1[2].xy; 1605: sampleVec[13] = POISSON_DISK_1[2].zw; 1606: sampleVec[14] = POISSON_DISK_1[3].xy; 1607: sampleVec[15] = POISSON_DISK_1[3].zw; 1608: shadow = 0.0; 1609: for (int i = 0; i < 16; i++) { 1610: shadow += texture2DCompare(shadowMap, cubeToUV(bd3D + permuteMat * vec3(sampleMat * sampleVec[i], 0.0), texelSize.y), dp); 1611: } 1612: shadow /= 16.0; 1613: #else 1614: shadow = texture2DCompare(shadowMap, cubeToUV(bd3D, texelSize.y), dp); 1615: #endif 1616: } 1617: return shadow; 1618: } 1619: #if 0 > 0 1620: float getDirShadow(DirectionalLightShadow light, sampler2D shadowMap, 1621: vec4 shadowCoord, float distWorld) { 1622: float shadowRadius = light.shadowRadius; 1623: #if defined(SHADOWMAP_TYPE_PCF) || defined(SHADOWMAP_TYPE_PCF_SOFT) 1624: shadowRadius *= PCF_PCF_SOFT_DIR_SPOT_BLUR_COEFF; 1625: #endif 1626: return getShadow(shadowMap, light.shadowMapSize, light.shadowBias, shadowRadius, shadowCoord, 1627: light.expBias, distWorld); 1628: } 1629: #endif 1630: #if 0 > 0 1631: float getPointShadow(PointLightShadow light, sampler2D shadowMap, vec4 shadowCoord) { 1632: float shadowRadius = light.shadowRadius; 1633: vec2 mapSize = light.shadowMapSize; 1634: #if defined(SHADOWMAP_TYPE_ESM) 1635: mapSize *= 2.0; 1636: #else 1637: mapSize *= vec2(4.0, 2.0); 1638: #if defined(SHADOWMAP_TYPE_PCF_POISSON_DISK) 1639: shadowRadius *= PCF_POISSON_POINT_BLUR_COEFF; 1640: #endif 1641: #endif 1642: return getOmniShadow(shadowMap, mapSize, light.shadowBias, light.expBias, 1643: shadowRadius, shadowCoord, light.shadowCameraNear, 1644: light.shadowCameraFar); 1645: } 1646: #endif 1647: #if 0 > 0 1648: float getRectAreaShadow(RectAreaLightShadow light, sampler2D shadowMap, vec4 shadowCoord) { 1649: float shadowRadius = light.shadowRadius; 1650: vec2 mapSize = light.shadowMapSize; 1651: #if defined(SHADOWMAP_TYPE_ESM) 1652: mapSize *= 2.0; 1653: #else 1654: mapSize *= vec2(4.0, 2.0); 1655: #if defined(SHADOWMAP_TYPE_PCF_POISSON_DISK) 1656: shadowRadius *= PCF_POISSON_POINT_BLUR_COEFF; 1657: #endif 1658: #endif 1659: return getOmniShadow(shadowMap, mapSize, light.shadowBias, light.expBias, 1660: shadowRadius, shadowCoord, light.shadowCameraNear, 1661: light.shadowCameraFar); 1662: } 1663: #endif 1664: #if 0 > 0 1665: float getSpotOmniShadow(SpotLightShadow light, sampler2D shadowMap, vec4 shadowCoord) { 1666: float shadowRadius = light.shadowRadius; 1667: vec2 mapSize = light.shadowMapSize; 1668: #if defined(SHADOWMAP_TYPE_ESM) 1669: #else 1670: mapSize *= vec2(4.0, 2.0); 1671: #if defined(SHADOWMAP_TYPE_PCF_POISSON_DISK) 1672: shadowRadius *= PCF_POISSON_SPOT_OMNI_BLUR_COEFF; 1673: #endif 1674: #endif 1675: return getOmniShadow(shadowMap, mapSize, light.shadowBias, light.expBias, 1676: shadowRadius, shadowCoord, light.shadowCameraNear, 1677: light.shadowCameraFar); 1678: } 1679: float biasLinearNormalizedToNonlinear(float bias, float near, float far, 1680: float projZ, float projW) { 1681: return (bias * (far + near) + 2.0 * projZ) / (bias * (far - near) + 2.0 * projW) 1682: - projZ / projW; 1683: } 1684: float getSpotShadow(SpotLightShadow light, sampler2D shadowMap, vec4 shadowCoord, 1685: float distWorld) { 1686: float shadowRadius = light.shadowRadius; 1687: float shadowBias = light.shadowBias; 1688: #if defined(SHADOWMAP_TYPE_PCF) || defined(SHADOWMAP_TYPE_PCF_SOFT) 1689: shadowRadius *= PCF_PCF_SOFT_DIR_SPOT_BLUR_COEFF; 1690: #elif defined(SHADOWMAP_TYPE_ESM) 1691: shadowRadius *= ESM_SPOT_SINGLE_BLUR_COEFF; 1692: #endif 1693: shadowBias *= BIAS_FRUSTUM_SCALE_COEFF 1694: / (light.shadowCameraFar - light.shadowCameraNear); 1695: #if defined(SHADOWMAP_TYPE_BASIC) || defined(SHADOWMAP_TYPE_BILINEAR) || defined(SHADOWMAP_TYPE_PCF) || defined(SHADOWMAP_TYPE_PCF_SOFT) || defined(SHADOWMAP_TYPE_PCF_POISSON_DISK) 1696: shadowBias = biasLinearNormalizedToNonlinear(shadowBias, 1697: light.shadowCameraNear, light.shadowCameraFar, shadowCoord.z, 1698: shadowCoord.w); 1699: #endif 1700: return getShadow(shadowMap, light.shadowMapSize, shadowBias, shadowRadius, shadowCoord, 1701: light.expBias, distWorld); 1702: } 1703: #endif 1704: #endif 1705: #if defined(USE_LOGDEPTHBUF) && defined(USE_LOGDEPTHBUF_EXT) 1706: uniform float logDepthBufFC; 1707: varying float vFragDepth; 1708: varying float vIsPerspective; 1709: #endif 1710: #if 0 > 0 1711: varying vec3 vClipPosition; 1712: uniform vec4 clippingPlanes[0]; 1713: #endif 1714: #if defined(NODE_NEW_GEOMETRY_BL) || defined(NODE_LAYER_WEIGHT_BL) || defined(NODE_FRESNEL_BL) || defined(NODE_BSDF_GLASS_BL) || defined(NODE_BSDF_PRINCIPLED_BL) || defined(USE_SSR) || (defined(NODE_NORMAL) && defined(MT_BLENDER)) || defined(USE_OSL) 1715: uniform mat4 projectionMatrix; 1716: #endif 1717: #if defined(NODE_VECT_TRANSFORM_BL) || defined(NODE_TEX_COORD_BL) || defined(NODE_NEW_GEOMETRY_BL) || defined(NODE_TANGENT_BL) || defined(NODE_BITMAP_MX) || defined(NODE_GRADIENT_RAMP_MX) || defined(NODE_NOISE_MX) || defined(NODE_SAMPLER_INFO_MY) || defined(NODE_TRANSFORM_MY) || defined(USE_OSL) 1718: uniform mat4 modelMatrix; 1719: uniform mat4 invModelMatrix; 1720: #endif 1721: #if defined(NODE_VECT_TRANSFORM_BL) || defined(NODE_NORMAL_MAP_BL) || defined(NODE_NORMAL_BUMP_MX) || defined(NODE_BUMP_2D_MY) || defined(NODE_SAMPLER_INFO_MY) 1722: uniform mat4 modelViewMatrix; 1723: #endif 1724: #if defined(NODE_TEX_IMAGE_BL) 1725: uniform mat3 normalMatrix; 1726: #endif 1727: #if defined(NODE_TEX_COORD_BL) || defined(NODE_NEW_GEOMETRY_BL) || defined(NODE_TANGENT_BL) 1728: uniform vec3 boundingBoxMin; 1729: uniform vec3 boundingBoxMax; 1730: #endif 1731: #if defined(NODE_REFLECT_REFRACT_MX) || defined(NODE_BITMAP_ENV_MX) || defined(NODE_BUMP_BL) || defined(NODE_PHY_SUN_SKY_ENV_MX) || defined(NODE_SKYDOME_LIGHT_AR) || defined(NODE_ENV_SPHERE_MY) 1732: varying vec3 vWorldPosition; 1733: #endif 1734: #if defined(NODE_TEX_COORD_BL) 1735: uniform vec2 viewWidthHeight; 1736: #endif 1737: #ifdef USE_SSR 1738: uniform sampler2D ssrSourceBuffer; 1739: uniform sampler2D ssrDepthBuffer; 1740: uniform sampler2D ssrBackfaceDepthBuffer; 1741: uniform mat4 invProjectionMatrix; 1742: uniform vec2 ssrResolution; 1743: uniform float ssrThickness; 1744: uniform float ssrStride; 1745: uniform float ssrJitter; 1746: uniform float ssrMaxDistance; 1747: #ifdef USE_SSR_REFRACT 1748: #define STEPS_FADE_AMOUNT 0.1 1749: #define SCREEN_FADE_THRESHOLD 0.6 1750: #else 1751: #define STEPS_FADE_AMOUNT 1.0 1752: #define SCREEN_FADE_THRESHOLD 0.4 1753: #endif 1754: vec3 deproject(vec3 p) { 1755: vec4 res = invProjectionMatrix * vec4(p, 1); 1756: return res.xyz / res.w; 1757: } 1758: bool doesIntersect(float rayzmax, float rayzmin, vec2 uv) { 1759: float sceneZMin = texture2D(ssrDepthBuffer, uv).r; 1760: #ifdef USE_SSR_REFRACT 1761: return rayzmin >= (sceneZMin-ssrThickness) && rayzmax <= sceneZMin; 1762: #else 1763: float sceneZMax = texture2D(ssrBackfaceDepthBuffer, uv).r; 1764: return rayzmin >= sceneZMax && rayzmax <= sceneZMin; 1765: #endif 1766: } 1767: float distanceSquared(vec2 a, vec2 b) { a -= b; return dot(a, a); } 1768: void swapIfBigger(inout float a, inout float b) { 1769: if (a > b) { 1770: float t = a; 1771: a = b; 1772: b = t; 1773: } 1774: } 1775: bool isOutsideUvBounds(float x) { return x < 0.0 || x > 1.0; } 1776: bool isOutsideUvBounds(vec2 uv) { return isOutsideUvBounds(uv.x) || isOutsideUvBounds(uv.y); } 1777: vec3 computeSSR(vec3 color, vec3 normal, float ior) { 1778: vec2 uv = gl_FragCoord.xy / ssrResolution; 1779: vec2 screenCoord = uv * 2.0 - vec2(1, 1); 1780: float nearClip = deproject(vec3(0, 0, -1)).z; 1781: vec3 ray = deproject(vec3(screenCoord, -1)); 1782: ray /= ray.z; 1783: float depthSample = -vViewPosition.z; 1784: vec3 vpos = depthSample * ray; 1785: #ifdef USE_SSR_REFRACT 1786: vec3 dir = normalize(refract(normalize(vpos), normalize(normal), 1.0/ior)); 1787: #else 1788: vec3 dir = normalize(reflect(normalize(vpos), normalize(normal))); 1789: #endif 1790: float maxDist = ssrMaxDistance; 1791: float rayLength = (vpos.z + dir.z * maxDist) > nearClip ? (nearClip - vpos.z) / dir.z : maxDist; 1792: vec3 csOrig = vpos; 1793: vec3 csEndPoint = csOrig + dir * rayLength; 1794: vec4 H0 = projectionMatrix * vec4(csOrig, 1.0); 1795: vec4 H1 = projectionMatrix * vec4(csEndPoint, 1.0); 1796: float k0 = 1.0 / H0.w, k1 = 1.0 / H1.w; 1797: vec3 Q0 = csOrig.xyz * k0, Q1 = csEndPoint.xyz * k1; 1798: vec2 P0 = H0.xy * k0, P1 = H1.xy * k1; 1799: P0 = P0 * 0.5 + vec2(0.5), P1 = P1 * 0.5 + vec2(0.5); 1800: #ifndef SSR_SIMPLE_REFRACT 1801: P0 *= ssrResolution, P1 *= ssrResolution; 1802: P1 += vec2((distanceSquared(P0, P1) < 0.0001) ? 0.01 : 0.0); 1803: vec2 delta = P1 - P0; 1804: bool permute = false; 1805: if (abs(delta.x) < abs(delta.y)) { 1806: permute = true; delta = delta.yx; P0 = P0.yx; P1 = P1.yx; 1807: } 1808: float stepDir = sign(delta.x); 1809: float invdx = stepDir / delta.x; 1810: vec3 dQ = (Q1 - Q0) * invdx; 1811: float dk = (k1 - k0) * invdx; 1812: vec2 dP = vec2(stepDir, delta.y * invdx); 1813: float pixelStride = ssrStride; 1814: float jitterMod = (gl_FragCoord.x + gl_FragCoord.y) * 0.25; 1815: vec4 PQK = vec4(P0, Q0.z, k0); 1816: vec4 dPQK = vec4(dP, dQ.z, dk); 1817: dPQK *= pixelStride; 1818: PQK += dPQK * mod(jitterMod, 1.0) * ssrJitter; 1819: float end = P1.x * stepDir; 1820: float prevZMaxEstimate = PQK.z / PQK.w; 1821: float rayZMin = prevZMaxEstimate, rayZMax = prevZMaxEstimate; 1822: float stepped = 0.0; 1823: vec2 hitUV; 1824: bool intersected = false; 1825: for (float stepCount = 1.0; stepCount <= float(MAX_STEPS); stepCount ++) { 1826: PQK += dPQK; 1827: rayZMin = prevZMaxEstimate; 1828: rayZMax = (dPQK.z * 0.5 + PQK.z) / (dPQK.w * 0.5 + PQK.w); 1829: prevZMaxEstimate = rayZMax; 1830: swapIfBigger(rayZMax, rayZMin); 1831: 1832: stepped = stepCount; 1833: hitUV = (permute ? PQK.yx: PQK.xy) / ssrResolution; 1834: if (isOutsideUvBounds(hitUV)) break; 1835: intersected = doesIntersect(rayZMax, rayZMin, hitUV); 1836: if (intersected || (P0.x * stepDir) > end) break; 1837: } 1838: if (intersected && pixelStride > 1.0) { 1839: PQK -= dPQK; 1840: dPQK /= ssrStride; 1841: float ogStride = pixelStride * 0.5; 1842: float currStride = pixelStride; 1843: prevZMaxEstimate = PQK.z / PQK.w; 1844: rayZMin = prevZMaxEstimate, rayZMax = prevZMaxEstimate; 1845: for(int j = 0; j < int(BINARY_SEARCH_ITERATIONS); j ++) { 1846: PQK += dPQK * currStride; 1847: rayZMin = prevZMaxEstimate; 1848: rayZMax = (dPQK.z * 0.5 + PQK.z) / (dPQK.w * 0.5 + PQK.w); 1849: prevZMaxEstimate = rayZMax; 1850: swapIfBigger(rayZMax, rayZMin); 1851: vec2 newUV = (permute ? PQK.yx: PQK.xy) / ssrResolution; 1852: ogStride *= 0.5; 1853: if (doesIntersect(rayZMax, rayZMin, newUV)) { 1854: hitUV = newUV; 1855: currStride = -ogStride; 1856: } else { 1857: currStride = ogStride; 1858: } 1859: } 1860: } 1861: vec3 result = color; 1862: #ifdef USE_SSR_REFRACT 1863: #endif 1864: if (intersected) { 1865: vec4 col = sRGBToLinear(texture2D(ssrSourceBuffer, hitUV)); 1866: vec2 ndc = abs(hitUV * 2.0 - 1.0); 1867: float maxndc = max(ndc.x, ndc.y); 1868: float fadeVal = 1869: (1.0 - (max(0.0, maxndc - SCREEN_FADE_THRESHOLD) / (1.0 - SCREEN_FADE_THRESHOLD))) * 1870: (1.0 - STEPS_FADE_AMOUNT * (stepped / float(MAX_STEPS))); 1871: col.a = fadeVal; 1872: result = mix(result, col.rgb, col.a); 1873: } 1874: #else 1875: vec3 result = sRGBToLinear(texture2D(ssrSourceBuffer, P1)).xyz; 1876: #endif 1877: return result; 1878: } 1879: #endif 1880: #define LUMENS_PER_WATT 683.0 1881: #define MAX_ENV_COORDS_DIR 0 1882: #define MAX_ENV_COORDS_REFLECT 1 1883: #define MAX_ENV_COORDS_REFRACT 2 1884: #define MAYA_LUM_VECTOR vec3(0.3, 0.59, 0.11) 1885: vec4 nodeTexelToLinear(in vec4 color, in int sourceType) { 1886: if (sourceType == 1) 1887: return sRGBToLinear(color); 1888: else if (sourceType == 2) 1889: return RGBEToLinear(color); 1890: else 1891: return color; 1892: } 1893: #if defined(NODE_COMBHSV_BL) || defined(NODE_SEPHSV_BL) || defined(NODE_HUE_SAT_BL) || defined(NODE_COLOR_CORRECTION_MX) || defined(NODE_COMPOSITE_LAYER_MX) || defined(USE_OSL) 1894: #define HSV_NODES 1895: #endif 1896: #if defined HSV_NODES 1897: void hsvToRGB(vec4 hsv, out vec4 outCol) 1898: { 1899: float i, f, p, q, t, h, s, v; 1900: vec3 rgb; 1901: h = hsv[0]; 1902: s = hsv[1]; 1903: v = hsv[2]; 1904: if (s == 0.0) 1905: rgb = vec3(v, v, v); 1906: else { 1907: if (h == 1.0) 1908: h = 0.0; 1909: h *= 6.0; 1910: i = floor(h); 1911: f = h - i; 1912: rgb = vec3(f, f, f); 1913: p = v * (1.0 - s); 1914: q = v * (1.0 - (s * f)); 1915: t = v * (1.0 - (s * (1.0 - f))); 1916: if (i == 0.0) 1917: rgb = vec3(v, t, p); 1918: else if (i == 1.0) 1919: rgb = vec3(q, v, p); 1920: else if (i == 2.0) 1921: rgb = vec3(p, v, t); 1922: else if (i == 3.0) 1923: rgb = vec3(p, q, v); 1924: else if (i == 4.0) 1925: rgb = vec3(t, p, v); 1926: else 1927: rgb = vec3(v, p, q); 1928: } 1929: outCol = vec4(rgb, hsv.w); 1930: } 1931: void rgbToHSV(vec4 rgb, out vec4 outCol) 1932: { 1933: float cmax, cmin, h, s, v, cdelta; 1934: vec3 c; 1935: cmax = max(rgb[0], max(rgb[1], rgb[2])); 1936: cmin = min(rgb[0], min(rgb[1], rgb[2])); 1937: cdelta = cmax - cmin; 1938: v = cmax; 1939: if (cmax != 0.0) 1940: s = cdelta / cmax; 1941: else { 1942: s = 0.0; 1943: h = 0.0; 1944: } 1945: if (s == 0.0) 1946: h = 0.0; 1947: else { 1948: c = (vec3(cmax, cmax, cmax) - rgb.xyz) / cdelta; 1949: if (rgb.x == cmax) h = c[2] - c[1]; 1950: else if (rgb.y == cmax) h = 2.0 + c[0] - c[2]; 1951: else h = 4.0 + c[1] - c[0]; 1952: h /= 6.0; 1953: if (h < 0.0) 1954: h += 1.0; 1955: } 1956: outCol = vec4(h, s, v, rgb.w); 1957: } 1958: #endif 1959: bool isPerspective(const mat4 projMatrix) 1960: { 1961: return (projMatrix[3][3] == 0.0); 1962: } 1963: #if defined(NODE_REFLECT_REFRACT_MX) || defined(NODE_BITMAP_ENV_MX) || defined(NODE_SKYDOME_LIGHT_AR) || defined(NODE_ENV_SPHERE_MY) 1964: vec4 sampleEquirectangular(sampler2D map, vec3 reflectVec, mat3 uvTransform, int encoding) 1965: { 1966: reflectVec = normalize(reflectVec); 1967: vec2 sampleUV; 1968: sampleUV.y = asin(clamp(reflectVec.y, - 1.0, 1.0)) * RECIPROCAL_PI + 0.5; 1969: sampleUV.x = atan(reflectVec.x, reflectVec.z) * RECIPROCAL_PI2 + 0.5; 1970: sampleUV.y *= -1.0; 1971: const float seamWidth = 0.15; 1972: const float seamBiasFactor = -10.0; 1973: float seam = max(0.0, 1.0 - abs (reflectVec.x) / seamWidth) * 1974: clamp (1.0 - reflectVec.z / seamWidth, 0.0, 1.0); 1975: sampleUV = (uvTransform * vec3(sampleUV, 1.0)).xy; 1976: vec4 color = texture2D(map, sampleUV, seamBiasFactor * seam); 1977: color = nodeTexelToLinear(color, encoding); 1978: return color; 1979: } 1980: #endif 1981: #if defined(NODE_FRESNEL_BL) || defined(NODE_LAYER_WEIGHT_BL) || defined(NODE_FALLOFF_MX) || defined(NODE_BSDF_GLASS_BL) || defined(NODE_BSDF_PRINCIPLED_BL) 1982: float fresnelReflection(const vec3 dir, const vec3 normal, const float ior) { 1983: float cosTheta = clamp(abs(dot(dir, normal)), -1.0, 1.0); 1984: float gSquared = pow2(ior) + pow2(cosTheta) - 1.0; 1985: if (gSquared < 0.0) return 1.0; 1986: float g = sqrt(gSquared); 1987: return 0.5 * pow2((g - cosTheta) / (g + cosTheta)) 1988: * (1.0 + pow2( 1989: ((g + cosTheta) * cosTheta - 1.0) / 1990: ((g - cosTheta) * cosTheta + 1.0) 1991: )); 1992: } 1993: #endif 1994: #if defined(NODE_BITMAP_MX) || defined(NODE_BITMAP_ENV_MX) || defined(NODE_GRADIENT_RAMP_MX) 1995: #define MAPPING_EXPLICIT_MAP_CHANNEL 1 1996: #define MAPPING_VERTEX_COLOR_CHANNEL 2 1997: #define MAPPING_PLANAR_OBJECT_XYZ 3 1998: #define MAPPING_PLANAR_WORLD_XYZ 4 1999: #define AXIS_XY 1 2000: #define AXIS_YZ 2 2001: #define AXIS_ZX 3 2002: #endif 2003: #if defined(NODE_BITMAP_MX) || defined(NODE_BITMAP_ENV_MX) || defined(NODE_BUMP_MX) || defined(NODE_GRADIENT_RAMP_MX) || defined(NODE_PLACE_2D_TEXTURE_MY) 2004: mat3 calcUvTransform(float uOffset, float vOffset, float uTiling, float vTiling, float wAngle) 2005: { 2006: if (abs(uOffset) < EPSILON && abs(vOffset) < EPSILON && 2007: (abs(uTiling - 1.0)) < EPSILON && (abs(vTiling - 1.0)) < EPSILON && 2008: abs(wAngle) < EPSILON) 2009: return mat3(1.0); 2010: float sx = uTiling; 2011: float sy = vTiling; 2012: float c = cos(-wAngle); 2013: float s = sin(-wAngle); 2014: #if defined(NODE_PLACE_2D_TEXTURE_MY) 2015: float tx = uOffset; 2016: float ty = vOffset; 2017: float cx = 0.5; 2018: float cy = 0.5; 2019: return mat3(c*sx, s*sx, 0.0, 2020: -s*sy, c*sy, 0.0, 2021: s*(ty+sy-cy)+c*(tx-cx)+cx, -c*(ty+sy-cy)+s*(tx-cx)-cy+1.0, 1.0); 2022: #else 2023: float tx = -uOffset; 2024: float ty = -vOffset; 2025: float cx = uOffset + 0.5; 2026: float cy = vOffset + 0.5; 2027: return mat3(sx * c, -sy * s, 0.0, 2028: sx * s, sy * c, 0.0, 2029: -sx * (c * cx + s * cy) + cx + tx, -sy * (- s * cx + c * cy) + cy + ty, 1.0); 2030: #endif 2031: } 2032: #endif 2033: #if defined(NODE_NOISE_MX) 2034: mat4 calcXYZTransform(vec3 offset, vec3 tiling, vec3 angle) { 2035: mat4 rot = mat4( 2036: cos(angle.y)*cos(angle.z), cos(angle.x)*sin(angle.z)+sin(angle.x)*sin(angle.y)*cos(angle.z), sin(angle.x)*sin(angle.z)-cos(angle.x)*sin(angle.y)*cos(angle.z), 0.0, 2037: -cos(angle.y)*sin(angle.z), cos(angle.x)*cos(angle.z)-sin(angle.x)*sin(angle.y)*sin(angle.z), cos(angle.x)*sin(angle.y)*sin(angle.z)+sin(angle.x)*cos(angle.z), 0.0, 2038: sin(angle.y), -sin(angle.x)*cos(angle.y), cos(angle.x)*cos(angle.y), 0.0, 2039: 0.0, 0.0, 0.0, 1.0 2040: ); 2041: mat4 til = mat4( 2042: tiling.x, 0.0, 0.0, 0.0, 2043: 0.0, tiling.y, 0.0, 0.0, 2044: 0.0, 0.0, tiling.z, 0.0, 2045: 0.0, 0.0, 0.0, 1.0 2046: ); 2047: mat4 off = mat4( 2048: 1.0, 0.0, 0.0, 0.0, 2049: 0.0, 1.0, 0.0, 0.0, 2050: 0.0, 0.0, 1.0, 0.0, 2051: offset.x, offset.y, offset.z, 1.0 2052: ); 2053: return (til * rot * off); 2054: } 2055: #endif 2056: #if defined(NODE_TEX_NOISE_BL) || defined(NODE_TEX_WAVE_BL) || defined(NODE_NOISE_MX) || defined(USE_OSL) || defined(NODE_NOISE_MY) 2057: #define NOISE_AMP_HACK 0.75 2058: #define NOISE_BLENDER_MEAN 0.78 2059: #define NOISE_SCALE_HACK 0.5 2060: #define noiseModulo(x) (x - floor(x * (1.0 / 289.0)) * 289.0) 2061: vec4 noisePermute(vec4 x) { 2062: return noiseModulo(((x * 34.0) + 1.0) * x); 2063: } 2064: vec4 taylorInvSqrt(vec4 r) { 2065: return 1.79284291400159 - 0.85373472095314 * r; 2066: } 2067: float taylorInvSqrt(float r) { 2068: return 1.79284291400159 - 0.85373472095314 * r; 2069: } 2070: float noisePerlin(vec3 v) { 2071: const vec2 C = vec2(1.0 / 6.0, 1.0 / 3.0); 2072: const vec4 D = vec4(0.0, 0.5, 1.0, 2.0); 2073: vec3 i = floor(v + dot(v, C.yyy)); 2074: vec3 x0 = v - i + dot(i, C.xxx); 2075: vec3 g = step(x0.yzx, x0.xyz); 2076: vec3 l = 1.0 - g; 2077: vec3 i1 = min(g.xyz, l.zxy); 2078: vec3 i2 = max(g.xyz, l.zxy); 2079: vec3 x1 = x0 - i1 + C.xxx; 2080: vec3 x2 = x0 - i2 + C.yyy; 2081: vec3 x3 = x0 - D.yyy; 2082: i = noiseModulo(i); 2083: vec4 p = noisePermute(noisePermute(noisePermute(i.z + vec4(0.0, i1.z, i2.z, 1.0)) 2084: + i.y + vec4(0.0, i1.y, i2.y, 1.0)) + i.x + vec4(0.0, i1.x, i2.x, 1.0)); 2085: float n_ = 0.142857142857; 2086: vec3 ns = n_ * D.wyz - D.xzx; 2087: vec4 j = p - 49.0 * floor(p * ns.z * ns.z); 2088: vec4 x_ = floor(j * ns.z); 2089: vec4 y_ = floor(j - 7.0 * x_); 2090: vec4 x = x_ * ns.x + ns.yyyy; 2091: vec4 y = y_ * ns.x + ns.yyyy; 2092: vec4 h = 1.0 - abs(x) - abs(y); 2093: vec4 b0 = vec4(x.xy, y.xy); 2094: vec4 b1 = vec4(x.zw, y.zw); 2095: vec4 s0 = floor(b0) * 2.0 + 1.0; 2096: vec4 s1 = floor(b1) * 2.0 + 1.0; 2097: vec4 sh = -step(h, vec4(0.0)); 2098: vec4 a0 = b0.xzyw + s0.xzyw * sh.xxyy; 2099: vec4 a1 = b1.xzyw + s1.xzyw * sh.zzww; 2100: vec3 p0 = vec3(a0.xy, h.x); 2101: vec3 p1 = vec3(a0.zw, h.y); 2102: vec3 p2 = vec3(a1.xy, h.z); 2103: vec3 p3 = vec3(a1.zw, h.w); 2104: vec4 norm = taylorInvSqrt(vec4(dot(p0, p0), dot(p1, p1), dot(p2, p2), dot(p3, p3))); 2105: p0 *= norm.x; 2106: p1 *= norm.y; 2107: p2 *= norm.z; 2108: p3 *= norm.w; 2109: vec4 m = max(0.6 - vec4(dot(x0, x0), dot(x1, x1), dot(x2, x2), dot(x3, x3)), 0.0); 2110: m = m * m; 2111: return 42.0 * dot(m * m, vec4(dot(p0, x0), dot(p1, x1), 2112: dot(p2, x2), dot(p3, x3))); 2113: } 2114: vec4 permute(vec4 x) { 2115: return mod(((x*34.0)+1.0)*x, 289.0); 2116: } 2117: float permute(float x) { 2118: return floor(mod(((x*34.0)+1.0)*x, 289.0)); 2119: } 2120: vec4 grad4(float j, vec4 ip) { 2121: const vec4 ones = vec4(1.0, 1.0, 1.0, -1.0); 2122: vec4 p,s; 2123: p.xyz = floor( fract (vec3(j) * ip.xyz) * 7.0) * ip.z - 1.0; 2124: p.w = 1.5 - dot(abs(p.xyz), ones.xyz); 2125: s = vec4(lessThan(p, vec4(0.0))); 2126: p.xyz = p.xyz + (s.xyz*2.0 - 1.0) * s.www; 2127: return p; 2128: } 2129: float snoise(vec4 v) { 2130: const vec2 C = vec2(0.138196601125010504, 2131: 0.309016994374947451); 2132: vec4 i = floor(v + dot(v, C.yyyy) ); 2133: vec4 x0 = v - i + dot(i, C.xxxx); 2134: vec4 i0; 2135: vec3 isX = step( x0.yzw, x0.xxx ); 2136: vec3 isYZ = step( x0.zww, x0.yyz ); 2137: i0.x = isX.x + isX.y + isX.z; 2138: i0.yzw = 1.0 - isX; 2139: i0.y += isYZ.x + isYZ.y; 2140: i0.zw += 1.0 - isYZ.xy; 2141: i0.z += isYZ.z; 2142: i0.w += 1.0 - isYZ.z; 2143: vec4 i3 = clamp( i0, 0.0, 1.0 ); 2144: vec4 i2 = clamp( i0-1.0, 0.0, 1.0 ); 2145: vec4 i1 = clamp( i0-2.0, 0.0, 1.0 ); 2146: vec4 x1 = x0 - i1 + 1.0 * C.xxxx; 2147: vec4 x2 = x0 - i2 + 2.0 * C.xxxx; 2148: vec4 x3 = x0 - i3 + 3.0 * C.xxxx; 2149: vec4 x4 = x0 - 1.0 + 4.0 * C.xxxx; 2150: i = mod(i, 289.0); 2151: float j0 = permute( permute( permute( permute(i.w) + i.z) + i.y) + i.x); 2152: vec4 j1 = permute( permute( permute( permute ( 2153: i.w + vec4(i1.w, i2.w, i3.w, 1.0 )) 2154: + i.z + vec4(i1.z, i2.z, i3.z, 1.0 )) 2155: + i.y + vec4(i1.y, i2.y, i3.y, 1.0 )) 2156: + i.x + vec4(i1.x, i2.x, i3.x, 1.0 )); 2157: vec4 ip = vec4(1.0/294.0, 1.0/49.0, 1.0/7.0, 0.0) ; 2158: vec4 p0 = grad4(j0, ip); 2159: vec4 p1 = grad4(j1.x, ip); 2160: vec4 p2 = grad4(j1.y, ip); 2161: vec4 p3 = grad4(j1.z, ip); 2162: vec4 p4 = grad4(j1.w, ip); 2163: vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3))); 2164: p0 *= norm.x; 2165: p1 *= norm.y; 2166: p2 *= norm.z; 2167: p3 *= norm.w; 2168: p4 *= taylorInvSqrt(dot(p4,p4)); 2169: vec3 m0 = max(0.6 - vec3(dot(x0,x0), dot(x1,x1), dot(x2,x2)), 0.0); 2170: vec2 m1 = max(0.6 - vec2(dot(x3,x3), dot(x4,x4) ), 0.0); 2171: m0 = m0 * m0; 2172: m1 = m1 * m1; 2173: return 49.0 * ( dot(m0*m0, vec3( dot( p0, x0 ), dot( p1, x1 ), dot( p2, x2 ))) 2174: + dot(m1*m1, vec2( dot( p3, x3 ), dot( p4, x4 ) ) ) ) ; 2175: } 2176: float noiseBlender(vec3 p) { 2177: return 0.5 * NOISE_AMP_HACK * (noisePerlin(NOISE_SCALE_HACK * vec3(p.x, p.y, p.z))) + 0.5; 2178: } 2179: float noiseSmooth(vec3 p, float octaveLenPerPixel, float falloffFactor, 2180: float dispersionFactor) { 2181: float mixFac = mix(1.0, smoothstep(0.0, 1.0, octaveLenPerPixel) * falloffFactor, 2182: dispersionFactor); 2183: return mix(noiseBlender(p), NOISE_BLENDER_MEAN, mixFac); 2184: } 2185: #define MAX_OCTAVES_NUM 16 2186: float noiseTurbulence(vec3 p, float octaves, float octaveLenPerPixel, 2187: float falloffFactor, float dispersionFactor) { 2188: float fscale = 1.0; 2189: float amp = 1.0; 2190: float sum = 0.0; 2191: octaves = clamp(octaves, 0.0, 16.0); 2192: int octavesInt = int(octaves); 2193: for (int i = 0; i <= MAX_OCTAVES_NUM; i++) { 2194: #if __VERSION__ == 300 2195: if (i <= octavesInt) { 2196: float t = noiseSmooth(fscale * p, octaveLenPerPixel, falloffFactor, 2197: dispersionFactor); 2198: sum += t * amp; 2199: amp *= 0.5; 2200: fscale *= 2.0; 2201: octaveLenPerPixel *= 2.0; 2202: } else { 2203: i = MAX_OCTAVES_NUM; 2204: } 2205: #else 2206: if (i > octavesInt) break; 2207: float t = noiseSmooth(fscale * p, octaveLenPerPixel, falloffFactor, 2208: dispersionFactor); 2209: sum += t * amp; 2210: amp *= 0.5; 2211: fscale *= 2.0; 2212: octaveLenPerPixel *= 2.0; 2213: #endif 2214: } 2215: float octavesFrac = fract(octaves); 2216: float octavesCoeff = pow(2.0, float(octavesInt)); 2217: if (octavesFrac != 0.0) { 2218: float t = noiseSmooth(fscale * p, octaveLenPerPixel, falloffFactor, 2219: dispersionFactor); 2220: float sum2 = sum + t * amp; 2221: sum *= octavesCoeff / (2.0 * octavesCoeff - 1.0); 2222: sum2 *= 2.0 * octavesCoeff / (4.0 * octavesCoeff - 1.0); 2223: return mix(sum, sum2, octavesFrac); 2224: } else { 2225: return sum * octavesCoeff / (2.0 * octavesCoeff - 1.0); 2226: } 2227: } 2228: #endif 2229: #if (defined(USE_OSL) || defined(NODE_WAVELENGTH_BL)) && __VERSION__ == 300 2230: vec3 cieColorMatch[81] = vec3[]( 2231: vec3(0.0014,0.0000,0.0065), vec3(0.0022,0.0001,0.0105), vec3(0.0042,0.0001,0.0201), 2232: vec3(0.0076,0.0002,0.0362), vec3(0.0143,0.0004,0.0679), vec3(0.0232,0.0006,0.1102), 2233: vec3(0.0435,0.0012,0.2074), vec3(0.0776,0.0022,0.3713), vec3(0.1344,0.0040,0.6456), 2234: vec3(0.2148,0.0073,1.0391), vec3(0.2839,0.0116,1.3856), vec3(0.3285,0.0168,1.6230), 2235: vec3(0.3483,0.0230,1.7471), vec3(0.3481,0.0298,1.7826), vec3(0.3362,0.0380,1.7721), 2236: vec3(0.3187,0.0480,1.7441), vec3(0.2908,0.0600,1.6692), vec3(0.2511,0.0739,1.5281), 2237: vec3(0.1954,0.0910,1.2876), vec3(0.1421,0.1126,1.0419), vec3(0.0956,0.1390,0.8130), 2238: vec3(0.0580,0.1693,0.6162), vec3(0.0320,0.2080,0.4652), vec3(0.0147,0.2586,0.3533), 2239: vec3(0.0049,0.3230,0.2720), vec3(0.0024,0.4073,0.2123), vec3(0.0093,0.5030,0.1582), 2240: vec3(0.0291,0.6082,0.1117), vec3(0.0633,0.7100,0.0782), vec3(0.1096,0.7932,0.0573), 2241: vec3(0.1655,0.8620,0.0422), vec3(0.2257,0.9149,0.0298), vec3(0.2904,0.9540,0.0203), 2242: vec3(0.3597,0.9803,0.0134), vec3(0.4334,0.9950,0.0087), vec3(0.5121,1.0000,0.0057), 2243: vec3(0.5945,0.9950,0.0039), vec3(0.6784,0.9786,0.0027), vec3(0.7621,0.9520,0.0021), 2244: vec3(0.8425,0.9154,0.0018), vec3(0.9163,0.8700,0.0017), vec3(0.9786,0.8163,0.0014), 2245: vec3(1.0263,0.7570,0.0011), vec3(1.0567,0.6949,0.0010), vec3(1.0622,0.6310,0.0008), 2246: vec3(1.0456,0.5668,0.0006), vec3(1.0026,0.5030,0.0003), vec3(0.9384,0.4412,0.0002), 2247: vec3(0.8544,0.3810,0.0002), vec3(0.7514,0.3210,0.0001), vec3(0.6424,0.2650,0.0000), 2248: vec3(0.5419,0.2170,0.0000), vec3(0.4479,0.1750,0.0000), vec3(0.3608,0.1382,0.0000), 2249: vec3(0.2835,0.1070,0.0000), vec3(0.2187,0.0816,0.0000), vec3(0.1649,0.0610,0.0000), 2250: vec3(0.1212,0.0446,0.0000), vec3(0.0874,0.0320,0.0000), vec3(0.0636,0.0232,0.0000), 2251: vec3(0.0468,0.0170,0.0000), vec3(0.0329,0.0119,0.0000), vec3(0.0227,0.0082,0.0000), 2252: vec3(0.0158,0.0057,0.0000), vec3(0.0114,0.0041,0.0000), vec3(0.0081,0.0029,0.0000), 2253: vec3(0.0058,0.0021,0.0000), vec3(0.0041,0.0015,0.0000), vec3(0.0029,0.0010,0.0000), 2254: vec3(0.0020,0.0007,0.0000), vec3(0.0014,0.0005,0.0000), vec3(0.0010,0.0004,0.0000), 2255: vec3(0.0007,0.0002,0.0000), vec3(0.0005,0.0002,0.0000), vec3(0.0003,0.0001,0.0000), 2256: vec3(0.0002,0.0001,0.0000), vec3(0.0002,0.0001,0.0000), vec3(0.0001,0.0000,0.0000), 2257: vec3(0.0001,0.0000,0.0000), vec3(0.0001,0.0000,0.0000), vec3(0.0000,0.0000,0.0000) 2258: ); 2259: #endif 2260: #if defined(USE_OSL) || defined(NODE_BLACKBODY_BL) 2261: vec3 colorTempToRGB(float temp) 2262: { 2263: vec3 retColor; 2264: temp = clamp(temp, 100.0, 40000.0) / 100.0; 2265: if (temp <= 66.0) { 2266: retColor.r = 1.0; 2267: retColor.g = saturate(0.390081578 * log(temp) - 0.631841443); 2268: } else { 2269: float t = temp - 60.0; 2270: retColor.r = saturate(1.292936186 * pow(t, -0.133204759)); 2271: retColor.g = saturate(1.129890860 * pow(t, -0.075514849)); 2272: } 2273: if (temp >= 66.0) 2274: retColor.b = 1.0; 2275: else if (temp <= 19.0) 2276: retColor.b = 0.0; 2277: else 2278: retColor.b = saturate(0.543206789 * log(temp - 10.0) - 1.196254089); 2279: return retColor; 2280: } 2281: #endif 2282: #ifdef USE_OSL 2283: #define M_PI PI 2284: #define M_PI_2 PI / 2.0 2285: #define M_PI_4 PI / 4.0 2286: #define M_2_PI 2.0 / PI 2287: #define M_2PI 2.0 * PI 2288: #define M_4PI 4.0 * PI 2289: #define M_2_SQRTPI 2.0 / sqrt(PI) 2290: #define M_E 2.718281828459 2291: #define M_LN2 0.69314718056 2292: #define M_LN10 2.30258509299 2293: #define M_LOG2E 1.4426950409 2294: #define M_LOG10E 0.43429448190 2295: #define M_SQRT2 sqrt(2.0) 2296: #define M_SQRT1_2 sqrt(0.5) 2297: #define OSL_ALPHA 33633 2298: #define OSL_ANISOTROPIC 40205 2299: #define OSL_AVERAGEALPHA 57701 2300: #define OSL_AVERAGECOLOR 46077 2301: #define OSL_BANDWIDTH 37485 2302: #define OSL_BEZIER 27645 2303: #define OSL_BLACK 62409 2304: #define OSL_BSPLINE 16959 2305: #define OSL_CAMERA 8198 2306: #define OSL_CAMERA_CLIP 34380 2307: #define OSL_CAMERA_CLIP_FAR 31933 2308: #define OSL_CAMERA_CLIP_NEAR 21485 2309: #define OSL_CAMERA_FOV 60706 2310: #define OSL_CAMERA_PIXELASPECT 4950 2311: #define OSL_CAMERA_PROJECTION 29369 2312: #define OSL_CAMERA_RESOLUTION 39679 2313: #define OSL_CAMERA_SCREEN_WINDOW 47009 2314: #define OSL_CAMERA_SHUTTER 7107 2315: #define OSL_CAMERA_SHUTTER_CLOSE 34406 2316: #define OSL_CAMERA_SHUTTER_OPEN 40085 2317: #define OSL_CATMULL_ROM 31642 2318: #define OSL_CELL 20984 2319: #define OSL_CHANNELS 8726 2320: #define OSL_CLAMP 20052 2321: #define OSL_COLOR 53753 2322: #define OSL_COMMON 49871 2323: #define OSL_CONSTANT 25144 2324: #define OSL_DATAWINDOW 54276 2325: #define OSL_DEFAULT 54870 2326: #define OSL_DIFFUSE 40389 2327: #define OSL_DIRECTION 42220 2328: #define OSL_DISPLAYWINDOW 30728 2329: #define OSL_DISTANCE 51337 2330: #define OSL_DO_FILTER 35765 2331: #define OSL_EMPTY 9314 2332: #define OSL_ERRORMESSAGE 38305 2333: #define OSL_EXISTS 41510 2334: #define OSL_FILL 39132 2335: #define OSL_FIRSTCHANNEL 48155 2336: #define OSL_GABOR 57764 2337: #define OSL_GEOM_NAME 63686 2338: #define OSL_GLOSSY 47998 2339: #define OSL_HASH 49390 2340: #define OSL_HERMITE 63643 2341: #define OSL_HIT 48491 2342: #define OSL_HITDIST 22029 2343: #define OSL_HSL 49898 2344: #define OSL_HSV 29073 2345: #define OSL_IMPULSES 56191 2346: #define OSL_INDEX 1731 2347: #define OSL_INTERP 10557 2348: #define OSL_LINEAR 2182 2349: #define OSL_MIRROR 64591 2350: #define OSL_MISSINGALPHA 39755 2351: #define OSL_MISSINGCOLOR 51667 2352: #define OSL_NDC 48899 2353: #define OSL_NORMAL 16520 2354: #define OSL_OBJECT 59084 2355: #define OSL_OSL_VERSION 47920 2356: #define OSL_PERIODIC 8749 2357: #define OSL_PERLIN 730 2358: #define OSL_POSITION 43041 2359: #define OSL_RASTER 2618 2360: #define OSL_REFLECTION 37621 2361: #define OSL_REFRACTION 37287 2362: #define OSL_RESOLUTION 48704 2363: #define OSL_RGB 26673 2364: #define OSL_RWRAP 47801 2365: #define OSL_SCREEN 55875 2366: #define OSL_SHADER 21066 2367: #define OSL_SHADER_GROUPNAME 62327 2368: #define OSL_SHADER_LAYERNAME 51796 2369: #define OSL_SHADER_SHADERNAME 65123 2370: #define OSL_SHADOW 60708 2371: #define OSL_SIMPLEX 61636 2372: #define OSL_SUBIMAGE 33526 2373: #define OSL_SUBIMAGES 2366 2374: #define OSL_SWRAP 4328 2375: #define OSL_TEXTUREFORMAT 17851 2376: #define OSL_TIME 52235 2377: #define OSL_TRACE 62908 2378: #define OSL_TWRAP 30524 2379: #define OSL_TYPE 64071 2380: #define OSL_UPERLIN 65308 2381: #define OSL_USIMPLEX 11314 2382: #define OSL_WIDTH 48751 2383: #define OSL_WORLD 9059 2384: #define OSL_WORLDTOCAMERA 32273 2385: #define OSL_WORLDTOSCREEN 33876 2386: #define OSL_WRAP 58300 2387: #define OSL_XYY 2228 2388: #define OSL_XYZ 47351 2389: #define OSL_YIQ 15839 2390: vec3 oslGetP(vec3 viewPos) { 2391: #if WORLD_NODES == 1 2392: return swizzleUpZ((invViewMatrix * vec4(-viewPos, 0.0)).xyz); 2393: #else 2394: return swizzleUpZ((invViewMatrix * vec4(-viewPos, 1.0)).xyz); 2395: #endif 2396: } 2397: vec3 oslGetI(vec3 viewPos) { 2398: #if WORLD_NODES == 1 2399: return swizzleUpZ((invViewMatrix * vec4(normalize(-viewPos), 0.0)).xyz); 2400: #else 2401: return swizzleUpZ((invViewMatrix * vec4(normalize(-viewPos), 0.0)).xyz); 2402: #endif 2403: } 2404: vec3 oslGetN(vec3 viewNorm) { 2405: return swizzleUpZ(normalize(invViewMatrix * vec4(viewNorm, 0.0)).xyz); 2406: } 2407: vec3 oslBlackbody(float temperatureK) { 2408: return sRGBToLinear(vec4(colorTempToRGB(temperatureK), 1.0)).rgb; 2409: } 2410: float oslDistance(vec3 p0, vec3 p1) { 2411: return distance(p0, p1); 2412: } 2413: float oslDistance(vec3 p0, vec3 p1, vec3 q) { 2414: vec3 d = p1 - p0; 2415: float dd = dot(d, d); 2416: if (dd == 0.0) 2417: return distance(q, p0); 2418: float t = dot(q - p0, d) / dd; 2419: return distance(q, p0 + clamp(t, 0.0, 1.0) * d); 2420: } 2421: int oslEndsWith(int name1, int name2) { 2422: return int(name1 == name2); 2423: } 2424: int oslFormat(int name1, int name2) { 2425: return name2; 2426: } 2427: int oslGetAttribute(int name, out int value) { 2428: value = 0; 2429: return 0; 2430: } 2431: int oslGetAttribute(int name, out float value) { 2432: value = 0.0; 2433: return 0; 2434: } 2435: int oslGetAttribute(int name, out vec3 vec) { 2436: vec = vec3(0.0, 0.0, 0.0); 2437: return 0; 2438: } 2439: void oslGetTextureInfo(int filename, int name, out int value) { 2440: value = 4; 2441: } 2442: void oslGetTextureInfo(int filename, int name, out int value[2]) { 2443: value[0] = 1024; 2444: value[1] = 1024; 2445: } 2446: vec3 oslHSV(float h, float s, float v) { 2447: vec4 outCol; 2448: hsvToRGB(vec4(h, s, v, 1.0), outCol); 2449: return outCol.rgb; 2450: } 2451: float oslHypot(float x, float y) { 2452: return sqrt(x*x + y*y); 2453: } 2454: float oslHypot(float x, float y, float z) { 2455: return sqrt(x*x + y*y + z*z); 2456: } 2457: float oslLog2(float x, float y) { 2458: return log(x) / log(y); 2459: } 2460: float oslLuminance(vec3 color) { 2461: return (abs(color.r) * 0.263 + abs(color.g) * 0.655 + abs(color.b) * 0.082); 2462: } 2463: float oslNoise(int type, vec3 vec, float phase) { 2464: float n = snoise(vec4(vec, phase)); 2465: if (type == OSL_UPERLIN) 2466: n = n * 0.5 + 0.5; 2467: return n; 2468: } 2469: float oslNoise(int type, float value, float phase) { 2470: return oslNoise(type, vec3(value), phase); 2471: } 2472: float oslNoise(int type, vec3 vec) { 2473: return oslNoise(type, vec, 0.0); 2474: } 2475: float oslNoise(int type, float value) { 2476: return oslNoise(type, vec3(value), 0.0); 2477: } 2478: vec3 oslNoise3D(int type, vec3 vec, float phase) { 2479: float x = snoise(vec4(vec, phase)); 2480: float y = snoise(vec4(vec.y, vec.x, vec.z, phase)); 2481: float z = snoise(vec4(vec.y, vec.z, vec.x, phase)); 2482: vec3 n = vec3(x, y, z); 2483: if (type == OSL_UPERLIN) 2484: n = n * 0.5 + 0.5; 2485: return n; 2486: } 2487: vec3 oslNoise3D(int type, float value, float phase) { 2488: return oslNoise3D(type, vec3(value), phase); 2489: } 2490: vec3 oslNoise3D(int type, vec3 vec) { 2491: return oslNoise3D(type, vec, 0.0); 2492: } 2493: vec3 oslNoise3D(int type, float value) { 2494: return oslNoise3D(type, vec3(value), 0.0); 2495: } 2496: float oslPow(float a, float b) { 2497: return pow(a, b); 2498: } 2499: vec3 oslPow(vec3 a, float b) { 2500: return pow(a, vec3(b)); 2501: } 2502: int oslRayType(int name) { 2503: if (name == OSL_CAMERA) 2504: #if LIGHT_PATH_IS_CAM_RAY 2505: return 1; 2506: #else 2507: return 0; 2508: #endif 2509: else 2510: return 0; 2511: } 2512: vec3 oslRotate(vec3 vec, float angle, vec3 p0, vec3 p1) { 2513: vec3 axis = normalize(p1 - p0); 2514: float c = cos(angle); 2515: float s = sin(angle); 2516: float x = axis[0]; 2517: float y = axis[1]; 2518: float z = axis[2]; 2519: mat4 mat = mat4( 2520: x * x + (1.0 - x * x) * c, x * y * (1.0 - c) + z * s, x * z * (1.0 - c) - y * s, 0.0, 2521: x * y * (1.0 - c) - z * s, y * y + (1.0 - y * y) * c, y * z * (1.0 - c) + x * s, 0.0, 2522: x * z * (1.0 - c) + y * s, y * z * (1.0 - c) - x * s, z * z + (1.0 - z * z) * c, 0.0, 2523: 0.0, 0.0, 0.0, 1.0 2524: ); 2525: return (mat * vec4((vec - p0), 1.0) + vec4(p0, 1.0)).xyz; 2526: } 2527: vec3 oslRotate(vec3 vec, float angle, vec3 axis) { 2528: return oslRotate(vec, angle, vec3(0.0), axis); 2529: } 2530: int oslStartsWith(int name1, int name2) { 2531: return int(name1 == name2); 2532: } 2533: int oslStrLen(int name) { 2534: if (name == OSL_EMPTY) 2535: return 0; 2536: else 2537: return 1; 2538: } 2539: int oslSubStr(int s, int start, int len) { 2540: return s; 2541: } 2542: int oslSubStr(int s, int start) { 2543: return s; 2544: } 2545: vec3 oslTexture(sampler2D image, float u, float v, int wrapModeFlag, int wrapMode, int alphaFlag, out float alpha) { 2546: if (wrapMode == OSL_DEFAULT || wrapMode == OSL_BLACK) { 2547: if (u < 0.0 || u > 1.0 || v < 0.0 || v > 1.0) 2548: return vec3(0.0); 2549: } else if (wrapMode == OSL_CLAMP) { 2550: u = clamp(u, 0.0, 1.0); 2551: v = clamp(v, 0.0, 1.0); 2552: } else if (wrapMode == OSL_PERIODIC) { 2553: u = mod(u, 1.0); 2554: v = mod(v, 1.0); 2555: } else if (wrapMode == OSL_MIRROR) { 2556: if (mod(floor(u), 2.0) == 0.0) 2557: u = u - floor(u); 2558: else 2559: u = 1.0 - (u - floor(u)); 2560: if (mod(floor(v), 2.0) == 0.0) 2561: v = v - floor(v); 2562: else 2563: v = 1.0 - (v - floor(v)); 2564: } 2565: vec4 colAlpha = texture2D(image, vec2(u, v)); 2566: alpha = colAlpha.a; 2567: return colAlpha.rgb; 2568: } 2569: vec3 oslTexture(sampler2D image, float u, float v, int alphaFlag, out float alpha, int wrapModeFlag, int wrapMode) { 2570: return oslTexture(image, u, v, wrapModeFlag, wrapMode, alphaFlag, alpha); 2571: } 2572: vec3 oslTexture(sampler2D image, float u, float v, int alphaFlag, out float alpha) { 2573: return oslTexture(image, u, v, OSL_WRAP, OSL_DEFAULT, alphaFlag, alpha); 2574: } 2575: vec3 oslTexture(sampler2D image, float u, float v, int wrapModeFlag, int wrapMode) { 2576: float alpha; 2577: return oslTexture(image, u, v, wrapModeFlag, wrapMode, OSL_ALPHA, alpha); 2578: } 2579: vec3 oslTexture(sampler2D image, float u, float v) { 2580: float alpha; 2581: return oslTexture(image, u, v, OSL_WRAP, OSL_DEFAULT, OSL_ALPHA, alpha); 2582: } 2583: vec3 oslTransform(int fromSpace, int toSpace, vec4 vec) { 2584: if (toSpace == OSL_WORLD || toSpace == OSL_SHADER || toSpace == OSL_COMMON) { 2585: return vec.xyz; 2586: } else if (toSpace == OSL_OBJECT) { 2587: vec = vec4(swizzleUpY(vec.xyz), vec.w); 2588: vec = invModelMatrix * vec; 2589: return swizzleUpZ(vec.xyz); 2590: } else if (toSpace == OSL_CAMERA) { 2591: vec = vec4(swizzleUpY(vec.xyz), vec.w); 2592: return (viewMatrix * vec).xyz; 2593: } else if (toSpace == OSL_SCREEN) { 2594: return vec.xyz; 2595: } else if (toSpace == OSL_RASTER) { 2596: return gl_FragCoord.xyz; 2597: } else if (toSpace == OSL_NDC) { 2598: return vec.xyz; 2599: } else { 2600: return vec.xyz; 2601: } 2602: } 2603: vec3 oslTransform(int fromSpace, int toSpace, vec3 vec) { 2604: return oslTransform(fromSpace, toSpace, vec4(vec, 1.0)); 2605: } 2606: vec3 oslTransform(int toSpace, vec3 vec) { 2607: return oslTransform(OSL_COMMON, toSpace, vec4(vec, 1.0)); 2608: } 2609: vec3 oslTransformDir(int fromSpace, int toSpace, vec3 vec) { 2610: return oslTransform(fromSpace, toSpace, vec4(vec, 0.0)); 2611: } 2612: vec3 oslTransformDir(int toSpace, vec3 vec) { 2613: return oslTransform(OSL_COMMON, toSpace, vec4(vec, 0.0)); 2614: } 2615: vec3 oslTransformC(int fromSpace, int toSpace, vec3 vec) { 2616: vec4 outVec = vec4(vec, 1.0); 2617: if (fromSpace == OSL_HSV && toSpace == OSL_RGB) 2618: hsvToRGB(vec4(vec, 1.0), outVec); 2619: else if (fromSpace == OSL_RGB && toSpace == OSL_HSV) 2620: rgbToHSV(vec4(vec, 1.0), outVec); 2621: return outVec.rgb; 2622: } 2623: vec3 oslTransformC(int toSpace, vec3 vec) { 2624: return oslTransformC(OSL_RGB, toSpace, vec); 2625: } 2626: void oslError() {} 2627: void oslFPrintf() {} 2628: void oslPrintf() {} 2629: void oslWarning() {} 2630: vec3 oslWaveLengthColor(float lambdaNM) { 2631: #if __VERSION__ == 300 2632: vec3 xyz = vec3(0.0); 2633: float ii = (lambdaNM - 380.0) / 5.0; 2634: int i = int(ii); 2635: if (i < 0 || i >= 80) 2636: return xyz; 2637: ii -= float(i); 2638: vec3 c1 = cieColorMatch[i]; 2639: vec3 c2 = cieColorMatch[i+1]; 2640: xyz = mix(c1, c2, ii); 2641: return xyz_to_sRGB(xyz); 2642: #else 2643: return vec3(0.0); 2644: #endif 2645: } 2646: #endif 2647: void node_background(vec4 color, float strength, out vec4 outColor) 2648: { 2649: outColor = strength * color; 2650: } 2651: void node_output_world(vec4 surface, vec4 volume, out vec4 outgoingLight) 2652: { 2653: outgoingLight = surface; 2654: } 2655: void main() { 2656: bool _frontFacingValue = gl_FrontFacing; 2657: #define FRONT_FACING_VALUE _frontFacingValue 2658: #if 0 > 0 2659: vec4 plane; 2660: 2661: 2662: #if 0 < 0 2663: bool clipped = true; 2664: 2665: #pragma unroll_loop_end 2666: if (clipped) discard; 2667: #endif 2668: #endif 2669: #if defined(USE_LOGDEPTHBUF) && defined(USE_LOGDEPTHBUF_EXT) 2670: gl_FragDepthEXT = vIsPerspective == 0.0 ? gl_FragCoord.z : log2(vFragDepth) * logDepthBufFC * 0.5; 2671: #endif 2672: #ifdef FLAT_SHADED 2673: vec3 fdx = vec3(dFdx(vViewPosition.x), dFdx(vViewPosition.y), dFdx(vViewPosition.z)); 2674: vec3 fdy = vec3(dFdy(vViewPosition.x), dFdy(vViewPosition.y), dFdy(vViewPosition.z)); 2675: vec3 normal = normalize(cross(fdx, fdy)); 2676: #else 2677: vec3 normal = normalize(vNormal); 2678: #ifdef DOUBLE_SIDED 2679: #ifdef FRONT_FACING_VALUE 2680: bool frontFacing = FRONT_FACING_VALUE; 2681: #else 2682: bool frontFacing = gl_FrontFacing; 2683: #endif 2684: normal = normal * (float(frontFacing) * 2.0 - 1.0); 2685: #endif 2686: #ifdef USE_TANGENT 2687: vec3 tangent = normalize(vTangent); 2688: vec3 bitangent = normalize(vBitangent); 2689: #ifdef DOUBLE_SIDED 2690: tangent = tangent * (float(gl_FrontFacing) * 2.0 - 1.0); 2691: bitangent = bitangent * (float(gl_FrontFacing) * 2.0 - 1.0); 2692: #endif 2693: #if defined(TANGENTSPACE_NORMALMAP) || defined(USE_CLEARCOAT_NORMALMAP) 2694: mat3 vTBN = mat3(tangent, bitangent, normal); 2695: #endif 2696: #endif 2697: #endif 2698: vec3 geometryNormal = normal; 2699: vec4 outgoingLight = vec4(0.0); 2700: vec4 background_bl_out0_n1; 2701: node_background(vec4(0.05087608844041824,0.05087608844041824,0.05087608844041824,1.0),1.0,background_bl_out0_n1); 2702: node_output_world(background_bl_out0_n1,vec4(0.0,0.0,0.0,0.0),vec4(0.0,0.0,0.0,0.0),vec4(0.0,0.0,0.0,0.0),outgoingLight); 2703: #if WORLD_NODES == 1 2704: outgoingLight.a = 1.0; 2705: #endif 2706: #ifdef ALPHATEST 2707: if (outgoingLight.a < ALPHATEST) 2708: discard; 2709: else 2710: outgoingLight.a = 1.0; 2711: #endif 2712: gl_FragColor = vec4(outgoingLight); 2713: #if defined(TONE_MAPPING) 2714: gl_FragColor.rgb = toneMapping(gl_FragColor.rgb); 2715: #endif 2716: gl_FragColor = linearToOutputTexel(gl_FragColor); 2717: #ifdef USE_FOG 2718: #ifdef FOG_EXP2 2719: float fogFactor = 1.0 - exp(- fogDensity * fogDensity * fogDepth * fogDepth); 2720: #else 2721: float fogFactor = smoothstep(fogNear, fogFar, fogDepth); 2722: #endif 2723: gl_FragColor.rgb = mix(gl_FragColor.rgb, fogColor, fogFactor); 2724: #endif 2725: #ifdef PREMULTIPLIED_ALPHA 2726: gl_FragColor.rgb *= gl_FragColor.a; 2727: #endif 2728: #ifdef DITHERING 2729: gl_FragColor.rgb = dithering(gl_FragColor.rgb); 2730: #endif 2731: }" S (v3d.js:1:599058) (anonymous function) (v3d.js:1:599469) Re (v3d.js:1:698017) (anonymous function) (v3d.js:1:690619) Ee (v3d.js:1:694050) Ce (v3d.js:1:693705) (anonymous function) (v3d.js:1:705053) (anonymous function) (v3d.js:1:147424) renderSceneToCubemap (v3d.js:1:1184542) value (v3d.js:1:1352303) (anonymous function) (v3d.js:1:1335549) (anonymous function) (v3d.js:1:1273919) (anonymous function) (v3d.js:1:1285033) promiseReactionJob [Error] WebGL: INVALID_OPERATION: useProgram: program not valid useProgram useProgram (v3d.js:1:642188) Re (v3d.js:1:698048) (anonymous function) (v3d.js:1:690619) Ee (v3d.js:1:694050) Ce (v3d.js:1:693705) (anonymous function) (v3d.js:1:705053) (anonymous function) (v3d.js:1:147424) renderSceneToCubemap (v3d.js:1:1184542) value (v3d.js:1:1352303) (anonymous function) (v3d.js:1:1335549) (anonymous function) (v3d.js:1:1273919) (anonymous function) (v3d.js:1:1285033) promiseReactionJob [Error] WebGL: ERROR: 0:3177: 'node_output_material' : no matching overloaded function found compileShader Bo (v3d.js:1:582886) $o (v3d.js:1:598579) acquireProgram (v3d.js:1:608438) Le (v3d.js:1:694602) (anonymous function) (v3d.js:1:692954) (anonymous function) (v3d.js:1:120763) (anonymous function) promiseReactionJob [Error] v3d.WebGLProgram: shader error: – 1282 – "35715" – false – "gl.getProgramInfoLog" – "" – "" – "v3d.WebGLShader: gl.getShaderInfoLog() fragment↵ERROR: 0:3177: 'node_output_material' : no matching overloaded function found1: #extensio…" "v3d.WebGLShader: gl.getShaderInfoLog() fragment ERROR: 0:3177: 'node_output_material' : no matching overloaded function found1: #extension GL_OES_standard_derivatives : enable 2: #extension GL_EXT_shader_texture_lod : enable 3: precision highp float; 4: precision highp int; 5: #define HIGH_PRECISION 6: #define SHADER_NAME MeshNodeMaterial 7: #define LIGHT_PATH_IS_CAM_RAY 1 8: #define WORLD_NODES 0 9: #define NODE_RGB_NUM 1 10: #define NODE_VALUE_NUM 0 11: #define NODE_TEX_COORD_NUM 0 12: #define NODE_RGB_BL 13: #define NODE_NORMAL 14: #define NODE_BSDF_PRINCIPLED_BL 15: #define NODE_OUTPUT_MATERIAL_BL 16: #define STANDARD 17: #define PHYSICAL 18: #define MT_BLENDER 19: #define COMPAT_USE_SPEC_ENV_BLENDER_APPROX 20: #define GAMMA_FACTOR 2 21: #define USE_ENVMAP 22: #define ENVMAP_TYPE_CUBE_UV 23: #define ENVMAP_MODE_REFLECTION 24: #define ENVMAP_BLENDING_NONE 25: #define cubeUV_maxTileSize 512.0 26: #define USE_SHADOWMAP 27: #define SHADOWMAP_TYPE_PCF_POISSON_DISK 28: #define PHYSICALLY_CORRECT_LIGHTS 29: #define TEXTURE_LOD_EXT 30: #define UNITS_SCALE_FACTOR 1.0 31: uniform mat4 viewMatrix; 32: uniform vec3 cameraPosition; 33: uniform bool isOrthographic; 34: #define TONE_MAPPING 35: #ifndef saturate 36: #define saturate(a) clamp(a, 0.0, 1.0) 37: #endif 38: #ifndef PI 39: #define PI 3.14159265359 40: #endif 41: uniform float toneMappingExposure; 42: uniform float toneMappingMidTones; 43: uniform float toneMappingPhysicalScale; 44: uniform float toneMappingBrightness; 45: uniform float toneMappingContrast; 46: uniform bool toneMappingChromaticAdaptation; 47: uniform vec3 toneMappingWhiteColor; 48: uniform bool toneMappingColorDifferentiation; 49: uniform bool toneMappingExteriorDaylight; 50: uniform vec3 toneMappingWhiteBalance; 51: uniform float toneMappingHighlights; 52: uniform float toneMappingShadows; 53: uniform float toneMappingSaturation; 54: uniform float toneMappingAperture; 55: uniform float toneMappingShutter; 56: uniform float toneMappingISO; 57: uniform float toneMappingVignetting; 58: uniform vec2 toneMappingResolution; 59: const float FILMIC_BLENDER_LOG_MIN = -12.473931188; 60: const float FILMIC_BLENDER_LOG_MAX = 12.526068812; 61: const float FILMIC_BLENDER_EXPOSURE_LATITUDE_RATIO = 0.66; 62: vec3 LinearToneMapping(vec3 color) { 63: return toneMappingExposure * color; 64: } 65: vec3 ReinhardToneMapping(vec3 color) { 66: color *= toneMappingExposure; 67: return saturate(color / (vec3(1.0) + color)); 68: } 69: vec3 OptimizedCineonToneMapping(vec3 color) { 70: color *= toneMappingExposure; 71: color = max(vec3(0.0), color - 0.004); 72: return pow((color * (6.2 * color + 0.5)) / (color * (6.2 * color + 1.7) + 0.06), vec3(2.2)); 73: } 74: vec3 RRTAndODTFit(vec3 v) { 75: vec3 a = v * (v + 0.0245786) - 0.000090537; 76: vec3 b = v * (0.983729 * v + 0.4329510) + 0.238081; 77: return a / b; 78: } 79: vec3 ACESFilmicToneMapping(vec3 color) { 80: const mat3 ACESInputMat = mat3( 81: vec3(0.59719, 0.07600, 0.02840), 82: vec3(0.35458, 0.90834, 0.13383), 83: vec3(0.04823, 0.01566, 0.83777) 84: ); 85: const mat3 ACESOutputMat = mat3( 86: vec3( 1.60475, -0.10208, -0.00327), 87: vec3(-0.53108, 1.10813, -0.07276), 88: vec3(-0.07367, -0.00605, 1.07602) 89: ); 90: color *= toneMappingExposure / 0.6; 91: color = ACESInputMat * color; 92: color = RRTAndODTFit(color); 93: color = ACESOutputMat * color; 94: return saturate(color); 95: } 96: vec3 CustomToneMapping(vec3 color) { return color; } 97: #define ORDERS_OF_MAG 5.0 98: float toneCalcBrightness(in vec3 color) 99: { 100: return (abs(color.r) * 0.263 + abs(color.g) * 0.655 + abs(color.b) * 0.082); 101: } 102: float toneApproximateScotopicLuminance(vec3 color) 103: { 104: return (0.062 * color.r + 0.608 * color.g + 0.330 * color.b); 105: } 106: vec3 LogarithmicMaxToneMapping(vec3 color) { 107: float inputScaleFactor = toneMappingPhysicalScale / PI; 108: float brightness = (toneMappingBrightness < 0.0) ? 0.0 : (toneMappingBrightness * 0.7); 109: float powerBot = toneMappingExteriorDaylight ? 4.0 : 2.0; 110: float res = 100.0 / ORDERS_OF_MAG; 111: float mag = floor((50.0 / res)); 112: float power = ((brightness / 20.0 - ORDERS_OF_MAG) - powerBot) + mag; 113: float stepsize = 9.0 / res; 114: float step = 50.0 - (mag * res); 115: float param_c = (0.02 * toneMappingContrast) * 2.0; 116: float param_b = pow(10.0, power) * (1.0 + (stepsize * step)); 117: float param_a = param_b * (1.0 + param_c); 118: param_c /= pow(2.0, toneMappingMidTones - 1.0); 119: param_b *= PI; 120: vec3 whiteConstancyFactor = toneMappingWhiteColor; 121: if (toneMappingChromaticAdaptation) { 122: float luminance = toneCalcBrightness(whiteConstancyFactor); 123: whiteConstancyFactor.r = (whiteConstancyFactor.r > 0.001) ? luminance / whiteConstancyFactor.r : luminance / 0.001; 124: whiteConstancyFactor.g = (whiteConstancyFactor.g > 0.001) ? luminance / whiteConstancyFactor.g : luminance / 0.001; 125: whiteConstancyFactor.b = (whiteConstancyFactor.b > 0.001) ? luminance / whiteConstancyFactor.b : luminance / 0.001; 126: } 127: vec3 outColor = inputScaleFactor * color; 128: if (toneMappingChromaticAdaptation) { 129: outColor *= whiteConstancyFactor.rgb; 130: } 131: float luminance = toneCalcBrightness(outColor); 132: if (toneMappingColorDifferentiation && (luminance < 5.62)) { 133: float sLuminance = toneApproximateScotopicLuminance(outColor); 134: if (luminance <= 5.62e-3) { 135: outColor = vec3(sLuminance); 136: } else { 137: float w = (luminance - 5.62e-3) / 5.61438; 138: outColor = outColor * w + sLuminance * (1.0 - w); 139: } 140: } 141: outColor = outColor * (param_a / (param_b * outColor + param_c)); 142: return outColor; 143: } 144: float maxExposurePhotographic(in vec4 color1, in vec4 color2) 145: { 146: return ((color1.r * color2.r) + (color1.g * color2.g)) + (color1.b * color2.b); 147: } 148: vec3 PhysicalMaxToneMapping(vec3 color) { 149: float vignettingInfluence = 1.0; 150: if (toneMappingVignetting > 0.0) { 151: vec3 vignettingCoords = vec3(0.0, 0.0, 0.0); 152: float aspect = toneMappingResolution.x / toneMappingResolution.y; 153: vignettingCoords.x = gl_FragCoord.x / toneMappingResolution.x - 0.5; 154: vignettingCoords.y = (gl_FragCoord.y / toneMappingResolution.y - 0.5) / aspect; 155: vignettingCoords.z = 1.0; 156: vignettingCoords = normalize(vignettingCoords); 157: vignettingInfluence = pow(vignettingCoords.z, toneMappingVignetting); 158: } 159: float inputScaleFactor = toneMappingPhysicalScale / PI; 160: float filmISO = toneMappingISO; 161: float camShutter = 1.0 / toneMappingShutter; 162: float fNumber = toneMappingAperture; 163: float cm2 = 1.0; 164: float burnHighlights = toneMappingHighlights; 165: float crushBlacks = toneMappingShadows; 166: float saturation = toneMappingSaturation; 167: vec3 whitePointInfluence = toneMappingWhiteBalance; 168: if (whitePointInfluence.r > 0.0) { 169: whitePointInfluence.r = 1.0 / whitePointInfluence.r; 170: } else { 171: whitePointInfluence.r = 1.0; 172: } 173: if (whitePointInfluence.g > 0.0) { 174: whitePointInfluence.g = 1.0 / whitePointInfluence.g; 175: } else { 176: whitePointInfluence.g = 1.0; 177: } 178: if (whitePointInfluence.b > 0.0) { 179: whitePointInfluence.b = 1.0 / whitePointInfluence.b; 180: } else { 181: whitePointInfluence.b = 1.0; 182: } 183: vec4 lumFactor = vec4(0.212671, 0.715160, 0.072169, 0.0); 184: float whiteLumFactor = maxExposurePhotographic(lumFactor, vec4(whitePointInfluence, 0.0)); 185: whitePointInfluence.r /= whiteLumFactor; 186: whitePointInfluence.g /= whiteLumFactor; 187: whitePointInfluence.b /= whiteLumFactor; 188: float isoInfluence = 0.0; 189: float camShutterInv = 1.0 / camShutter; 190: if (filmISO > 0.0) { 191: isoInfluence = ((cm2 * 0.169811) * (filmISO * camShutterInv)) / ((15.4 * fNumber) * fNumber); 192: } else { 193: isoInfluence = cm2; 194: } 195: vec3 outColor = inputScaleFactor * color; 196: outColor.r = outColor.r * whitePointInfluence.r * isoInfluence * vignettingInfluence; 197: outColor.g = outColor.g * whitePointInfluence.g * isoInfluence * vignettingInfluence; 198: outColor.b = outColor.b * whitePointInfluence.b * isoInfluence * vignettingInfluence; 199: outColor.r = (outColor.r * (1.0 + (outColor.r * burnHighlights))) / (1.0 + outColor.r); 200: outColor.g = (outColor.g * (1.0 + (outColor.g * burnHighlights))) / (1.0 + outColor.g); 201: outColor.b = (outColor.b * (1.0 + (outColor.b * burnHighlights))) / (1.0 + outColor.b); 202: float lumFactor2 = maxExposurePhotographic(lumFactor, vec4(outColor, 0.0)); 203: float tmpFloat = 1.0 - saturation; 204: outColor.r = outColor.r * saturation + lumFactor2 * tmpFloat; 205: outColor.g = outColor.g * saturation + lumFactor2 * tmpFloat; 206: outColor.b = outColor.b * saturation + lumFactor2 * tmpFloat; 207: outColor = max(vec3(0.0), outColor); 208: float crushBlacksFac = crushBlacks * 2.0 + 1.0; 209: float crushBlacksFac2 = pow(maxExposurePhotographic(lumFactor, vec4(outColor, 0.0)), 0.5); 210: tmpFloat = (1.0 - crushBlacksFac2); 211: if (crushBlacksFac2 < 1.0) { 212: outColor.r = outColor.r * crushBlacksFac2 + pow(outColor.r, crushBlacksFac) * tmpFloat; 213: outColor.g = outColor.g * crushBlacksFac2 + pow(outColor.g, crushBlacksFac) * tmpFloat; 214: outColor.b = outColor.b * crushBlacksFac2 + pow(outColor.b, crushBlacksFac) * tmpFloat; 215: } 216: return outColor; 217: } 218: float filmicBlenderDesaturationMinIntensity(vec3 color) { 219: float maxChannel = max(color.r, max(color.g, color.b)); 220: float x = max(maxChannel, 0.6251); 221: return (1.2192868 * x - 0.63221059) 222: * ((x - 0.65069831) / (abs(x - 0.65069831) + 0.00952982) + 0.73015231); 223: } 224: vec3 filmicBlenderDesaturationTransform(vec3 color) { 225: const float CURVE_SMOOTHNESS = 0.03; 226: float minIntensity = filmicBlenderDesaturationMinIntensity(color); 227: vec4 x = vec4(color, 1.0) - minIntensity; 228: x = pow(x, vec4(2.0)) / (abs(x) + CURVE_SMOOTHNESS); 229: return (x.rgb - x.a + color + 1.0) / 2.0; 230: } 231: vec3 filmicBlenderDynamicRangeTransform(vec3 color) { 232: return pow( 233: (0.28882259 * color - 0.15880336) 234: / (pow(color - 0.6229693, vec3(2.0)) + 0.16965022) 235: + 0.20453365 * color + 0.37847142, 236: vec3(3.0) 237: ); 238: } 239: vec3 FilmicBlenderToneMapping(vec3 color) { 240: color *= toneMappingExposure; 241: color = max(color, 0.000175); 242: color = clamp((log2(color) - FILMIC_BLENDER_LOG_MIN) 243: / (FILMIC_BLENDER_LOG_MAX - FILMIC_BLENDER_LOG_MIN), 0.0, 1.0); 244: color = filmicBlenderDesaturationTransform(color); 245: color = clamp(color / FILMIC_BLENDER_EXPOSURE_LATITUDE_RATIO, 0.0, 1.0); 246: color = filmicBlenderDynamicRangeTransform(color); 247: return color; 248: } 249: vec3 toneMapping(vec3 color) { return FilmicBlenderToneMapping(color); } 250: 251: vec4 LinearToLinear(in vec4 value) { 252: return value; 253: } 254: vec4 GammaToLinear(in vec4 value, in float gammaFactor) { 255: value = max(value, vec4(0.0)); 256: return vec4(pow(value.rgb, vec3(gammaFactor)), value.a); 257: } 258: vec4 LinearToGamma(in vec4 value, in float gammaFactor) { 259: value = max(value, vec4(0.0)); 260: return vec4(pow(value.rgb, vec3(1.0 / gammaFactor)), value.a); 261: } 262: vec4 sRGBToLinear(in vec4 value) { 263: value = max(value, vec4(0.0)); 264: return vec4(mix(pow(value.rgb * 0.9478672986 + vec3(0.0521327014), vec3(2.4)), value.rgb * 0.0773993808, vec3(lessThanEqual(value.rgb, vec3(0.04045)))), value.a); 265: } 266: vec4 LinearTosRGB(in vec4 value) { 267: value = max(value, vec4(0.0)); 268: return vec4(mix(pow(value.rgb, vec3(0.41666)) * 1.055 - vec3(0.055), value.rgb * 12.92, vec3(lessThanEqual(value.rgb, vec3(0.0031308)))), value.a); 269: } 270: vec4 RGBEToLinear(in vec4 value) { 271: return vec4(value.rgb * exp2(value.a * 255.0 - 128.0), 1.0); 272: } 273: vec4 LinearToRGBE(in vec4 value) { 274: float maxComponent = max(max(value.r, value.g), value.b); 275: float fExp = clamp(ceil(log2(maxComponent)), -128.0, 127.0); 276: return vec4(value.rgb / exp2(fExp), (fExp + 128.0) / 255.0); 277: } 278: vec4 RGBMToLinear(in vec4 value, in float maxRange) { 279: return vec4(value.rgb * value.a * maxRange, 1.0); 280: } 281: vec4 LinearToRGBM(in vec4 value, in float maxRange) { 282: float maxRGB = max(value.r, max(value.g, value.b)); 283: float M = clamp(maxRGB / maxRange, 0.0, 1.0); 284: M = ceil(M * 255.0) / 255.0; 285: return vec4(value.rgb / (M * maxRange), M); 286: } 287: vec4 RGBDToLinear(in vec4 value, in float maxRange) { 288: return vec4(value.rgb * ((maxRange / 255.0) / value.a), 1.0); 289: } 290: vec4 LinearToRGBD(in vec4 value, in float maxRange) { 291: float maxRGB = max(value.r, max(value.g, value.b)); 292: float D = max(maxRange / maxRGB, 1.0); 293: D = clamp(floor(D) / 255.0, 0.0, 1.0); 294: return vec4(value.rgb * (D * (255.0 / maxRange)), D); 295: } 296: const mat3 cLogLuvM = mat3(0.2209, 0.3390, 0.4184, 0.1138, 0.6780, 0.7319, 0.0102, 0.1130, 0.2969); 297: vec4 LinearToLogLuv(in vec4 value) { 298: vec3 Xp_Y_XYZp = cLogLuvM * value.rgb; 299: Xp_Y_XYZp = max(Xp_Y_XYZp, vec3(1e-6, 1e-6, 1e-6)); 300: vec4 vResult; 301: vResult.xy = Xp_Y_XYZp.xy / Xp_Y_XYZp.z; 302: float Le = 2.0 * log2(Xp_Y_XYZp.y) + 127.0; 303: vResult.w = fract(Le); 304: vResult.z = (Le - (floor(vResult.w * 255.0)) / 255.0) / 255.0; 305: return vResult; 306: } 307: const mat3 cLogLuvInverseM = mat3(6.0014, -2.7008, -1.7996, -1.3320, 3.1029, -5.7721, 0.3008, -1.0882, 5.6268); 308: vec4 LogLuvToLinear(in vec4 value) { 309: float Le = value.z * 255.0 + value.w; 310: vec3 Xp_Y_XYZp; 311: Xp_Y_XYZp.y = exp2((Le - 127.0) / 2.0); 312: Xp_Y_XYZp.z = Xp_Y_XYZp.y / value.y; 313: Xp_Y_XYZp.x = value.x * Xp_Y_XYZp.z; 314: vec3 vRGB = cLogLuvInverseM * Xp_Y_XYZp.rgb; 315: return vec4(max(vRGB, 0.0), 1.0); 316: } 317: vec4 envMapTexelToLinear(vec4 value) { return LinearToLinear(value); } 318: vec4 linearToOutputTexel(vec4 value) { return LinearTosRGB(value); } 319: #define ESM_DISTANCE_SCALE 1.0 320: 321: #define NODE 322: #define PI 3.14159265359 323: #define PI2 6.28318530718 324: #define PI_HALF 1.5707963267949 325: #define RECIPROCAL_PI 0.31830988618 326: #define RECIPROCAL_PI2 0.15915494 327: #define LOG2 1.442695 328: #define EPSILON 1e-6 329: #ifndef saturate 330: #define saturate(a) clamp(a, 0.0, 1.0) 331: #endif 332: #define whiteComplement(a) (1.0 - saturate(a)) 333: #define RECIPROCAL_3 0.333333333333 334: float pow2(const in float x) { return x*x; } 335: float pow3(const in float x) { return x*x*x; } 336: float pow4(const in float x) { float x2 = x*x; return x2*x2; } 337: float average(const in vec3 color) { return dot(color, vec3(0.3333)); } 338: highp float rand(const in vec2 uv) { 339: const highp float a = 12.9898, b = 78.233, c = 43758.5453; 340: highp float dt = dot(uv.xy, vec2(a,b)), sn = mod(dt, PI); 341: return fract(sin(sn) * c); 342: } 343: #ifdef HIGH_PRECISION 344: float precisionSafeLength(vec3 v) { return length(v); } 345: #else 346: float max3(vec3 v) { return max(max(v.x, v.y), v.z); } 347: float precisionSafeLength(vec3 v) { 348: float maxComponent = max3(abs(v)); 349: return length(v / maxComponent) * maxComponent; 350: } 351: #endif 352: struct IncidentLight { 353: vec3 color; 354: vec3 direction; 355: bool visible; 356: }; 357: struct ReflectedLight { 358: vec3 directDiffuse; 359: vec3 directSpecular; 360: vec3 indirectDiffuse; 361: vec3 indirectSpecular; 362: }; 363: struct GeometricContext { 364: vec3 position; 365: vec3 normal; 366: vec3 viewDir; 367: #ifdef CLEARCOAT 368: vec3 clearcoatNormal; 369: #endif 370: }; 371: vec3 transformDirection(in vec3 dir, in mat4 matrix) { 372: return normalize((matrix * vec4(dir, 0.0)).xyz); 373: } 374: vec3 inverseTransformDirection(in vec3 dir, in mat4 matrix) { 375: return normalize((vec4(dir, 0.0) * matrix).xyz); 376: } 377: vec3 projectOnPlane(in vec3 point, in vec3 pointOnPlane, in vec3 planeNormal) { 378: float distance = dot(planeNormal, point - pointOnPlane); 379: return - distance * planeNormal + point; 380: } 381: float sideOfPlane(in vec3 point, in vec3 pointOnPlane, in vec3 planeNormal) { 382: return sign(dot(point - pointOnPlane, planeNormal)); 383: } 384: vec3 linePlaneIntersect(in vec3 pointOnLine, in vec3 lineDirection, in vec3 pointOnPlane, in vec3 planeNormal) { 385: return lineDirection * (dot(planeNormal, pointOnPlane - pointOnLine) / dot(planeNormal, lineDirection)) + pointOnLine; 386: } 387: mat3 transposeMat3(const in mat3 m) { 388: mat3 tmp; 389: tmp[0] = vec3(m[0].x, m[1].x, m[2].x); 390: tmp[1] = vec3(m[0].y, m[1].y, m[2].y); 391: tmp[2] = vec3(m[0].z, m[1].z, m[2].z); 392: return tmp; 393: } 394: float linearToRelativeLuminance(const in vec3 color) { 395: vec3 weights = vec3(0.2126, 0.7152, 0.0722); 396: return dot(weights, color.rgb); 397: } 398: bool isPerspectiveMatrix(mat4 m) { 399: return m[2][3] == - 1.0; 400: } 401: highp vec3 rand3(const in vec3 v) { 402: const highp float c = 43758.5453; 403: const highp mat3 coeffs = mat3( 404: 165.15, 253.34, 323.22, 405: 241.49, 329.07, 147.79, 406: 376.31, 143.45, 281.63 407: ); 408: highp vec3 sn = mod(coeffs * v, PI); 409: return fract(sin(sn) * c); 410: } 411: float powCompat(const in float val, const in float power) { 412: if (power == 0.0) 413: return 1.0; 414: else if (val < 0.0) { 415: if (mod(-power, 2.0) == 0.0) 416: return pow(abs(val), power); 417: else 418: return -pow(abs(val), power); 419: } else if (val == 0.0) 420: return 0.0; 421: return pow(abs(val), power); 422: } 423: float maxFromRGB(vec3 rgb) { 424: return max(max(rgb.r, rgb.g), rgb.b); 425: } 426: bool isOrtho(const in mat4 m) { 427: if (m[3][3] != 0.0) 428: return true; 429: else 430: return false; 431: } 432: vec3 swizzleUpZ(const vec3 vec) { 433: return vec3(vec[0], -vec[2], vec[1]); 434: } 435: vec3 swizzleUpY(const vec3 vec) { 436: return vec3(vec[0], vec[2], -vec[1]); 437: } 438: vec3 xyz_to_sRGB(vec3 xyz) { 439: mat3 convMat = mat3( 440: 3.2406, -0.9689, 0.0557, 441: -1.5372, 1.8758, -0.2040, 442: -0.4986, 0.0415, 1.0570 443: ); 444: return convMat * xyz; 445: } 446: vec3 xyY_to_XYZ(float x, float y, float Y) { 447: float X = 0.0; 448: float Z = 0.0; 449: if (y != 0.0) { 450: X = (Y / y) * x; 451: Z = (Y / y) * (1.0 - x - y); 452: } 453: return vec3(X, Y, Z); 454: } 455: vec3 octUVToCubeVec(vec2 octUV, vec2 texelSize) { 456: octUV = (1.0 + 2.0 * texelSize) * octUV - texelSize; 457: octUV = octUV * 2.0 - 1.0; 458: float x = octUV.x; 459: float z = -octUV.y; 460: float absX = abs(x); 461: float absZ = abs(z); 462: vec3 cubeVec = vec3(x, 1.0 - absX - absZ, z); 463: if (absX + absZ > 1.0) { 464: cubeVec.xz = -(vec2(absZ, absX) - 1.0) * sign(vec2(x, z)); 465: } 466: return cubeVec; 467: } 468: vec2 cubeVecToOctUV(vec3 cubeVec, vec2 texelSize) { 469: cubeVec /= dot(vec3(1.0), abs(cubeVec)); 470: vec2 octUV = vec2(cubeVec.x, -cubeVec.z); 471: if (cubeVec.y < 0.0) { 472: octUV = sign(octUV) * (1.0 - abs(octUV.ts)); 473: } 474: octUV = (octUV + 1.0) / 2.0; 475: octUV = (1.0 - 2.0 * texelSize) * octUV + texelSize; 476: return octUV; 477: } 478: #if __VERSION__ == 100 479: float cosh(float x) { 480: return (exp(x) + exp(-x)) / 2.0; 481: } 482: vec2 cosh(vec2 x) { 483: return (exp(x) + exp(-x)) / 2.0; 484: } 485: vec3 cosh(vec3 x) { 486: return (exp(x) + exp(-x)) / 2.0; 487: } 488: vec4 cosh(vec4 x) { 489: return (exp(x) + exp(-x)) / 2.0; 490: } 491: float sinh(float x) { 492: return (exp(x) - exp(-x)) / 2.0; 493: } 494: vec2 sinh(vec2 x) { 495: return (exp(x) - exp(-x)) / 2.0; 496: } 497: vec3 sinh(vec3 x) { 498: return (exp(x) - exp(-x)) / 2.0; 499: } 500: vec4 sinh(vec4 x) { 501: return (exp(x) - exp(-x)) / 2.0; 502: } 503: float tanh(float x) { 504: float exp2x = exp(2.0 * x); 505: return (exp2x - 1.0) / (exp2x + 1.0); 506: } 507: vec2 tanh(vec2 x) { 508: vec2 exp2x = exp(2.0 * x); 509: return (exp2x - 1.0) / (exp2x + 1.0); 510: } 511: vec3 tanh(vec3 x) { 512: vec3 exp2x = exp(2.0 * x); 513: return (exp2x - 1.0) / (exp2x + 1.0); 514: } 515: vec4 tanh(vec4 x) { 516: vec4 exp2x = exp(2.0 * x); 517: return (exp2x - 1.0) / (exp2x + 1.0); 518: } 519: float trunc(float x) { 520: return floor(abs(x)) * sign(x); 521: } 522: vec2 trunc(vec2 x) { 523: return floor(abs(x)) * sign(x); 524: } 525: vec3 trunc(vec3 x) { 526: return floor(abs(x)) * sign(x); 527: } 528: vec4 trunc(vec4 x) { 529: return floor(abs(x)) * sign(x); 530: } 531: #endif 532: float getSmoothFactor(float a, float b, float smoothness) { 533: return max(smoothness - abs(a - b), 0.0) / smoothness; 534: } 535: float smoothMin(float a, float b, float smoothness) { 536: float smoothFac = getSmoothFactor(a, b, smoothness); 537: return min(a, b) - smoothFac * smoothFac * smoothFac * smoothness * (1.0 / 6.0); 538: } 539: float smoothMax(float a, float b, float smoothness) { 540: float smoothFac = getSmoothFactor(a, b, smoothness); 541: return max(a, b) + smoothFac * smoothFac * smoothFac * smoothness * (1.0 / 6.0); 542: } 543: vec3 vec3RotateAxisAngle(vec3 vector, vec3 axis, float angle) { 544: vec3 axisNorm = normalize(axis); 545: float x = axisNorm.x, y = axisNorm.y, z = axisNorm.z; 546: float s = sin(angle), c = cos(angle); 547: return mat3( 548: x * x * (1.0 - c) + c, x * y * (1.0 - c) + z * s, x * z * (1.0 - c) - y * s, 549: x * y * (1.0 - c) - z * s, y * y * (1.0 - c) + c, y * z * (1.0 - c) + x * s, 550: x * z * (1.0 - c) + y * s, y * z * (1.0 - c) - x * s, z * z * (1.0 - c) + c 551: ) * vector; 552: } 553: mat3 mat3RotateX(float angle) { 554: float s = sin(angle), c = cos(angle); 555: return mat3(1.0, 0.0, 0.0, 556: 0.0, c, s, 557: 0.0, -s, c); 558: } 559: mat3 mat3RotateY(float angle) { 560: float s = sin(angle), c = cos(angle); 561: return mat3(c, 0.0, -s, 562: 0.0, 1.0, 0.0, 563: s, 0.0, c); 564: } 565: mat3 mat3RotateZ(float angle) { 566: float s = sin(angle), c = cos(angle); 567: return mat3(c, s, 0.0, 568: -s, c, 0.0, 569: 0.0, 0.0, 1.0); 570: } 571: vec3 vec3RotateXAngle(vec3 vector, float angle) { 572: return mat3RotateX(angle) * vector; 573: } 574: vec3 vec3RotateYAngle(vec3 vector, float angle) { 575: return mat3RotateY(angle) * vector; 576: } 577: vec3 vec3RotateZAngle(vec3 vector, float angle) { 578: return mat3RotateZ(angle) * vector; 579: } 580: vec4 eulerToAxisAngle(vec3 euler) { 581: float c1 = cos(euler.x / 2.0), c2 = cos(euler.y / 2.0), c3 = cos(euler.z / 2.0); 582: float s1 = sin(euler.x / 2.0), s2 = sin(euler.y / 2.0), s3 = sin(euler.z / 2.0); 583: vec4 axisAngle = vec4( 584: s1 * c2 * c3 - c1 * s2 * s3, 585: c1 * s2 * c3 + s1 * c2 * s3, 586: c1 * c2 * s3 - s1 * s2 * c3, 587: 2.0 * acos(c1 * c2 * c3 + s1 * s2 * s3) 588: ); 589: axisAngle.xyz = length(axisAngle.xyz) > 0.0 ? normalize(axisAngle.xyz) : vec3(1.0, 0.0, 0.0); 590: return axisAngle; 591: } 592: float mat3GetDeterminant(mat3 mat) { 593: return mat[0][0] * mat[1][1] * mat[2][2] 594: + mat[0][2] * mat[1][0] * mat[2][1] 595: + mat[0][1] * mat[1][2] * mat[2][0] 596: - mat[0][2] * mat[1][1] * mat[2][0] 597: - mat[0][0] * mat[1][2] * mat[2][1] 598: - mat[0][1] * mat[1][0] * mat[2][2]; 599: } 600: mat3 mat3GetInverseTransposed(mat3 mat) { 601: float det = mat3GetDeterminant(mat); 602: float a00 = (mat[1][1] * mat[2][2] - mat[1][2] * mat[2][1]) / det; 603: float a01 = - (mat[1][0] * mat[2][2] - mat[1][2] * mat[2][0]) / det; 604: float a02 = (mat[1][0] * mat[2][1] - mat[1][1] * mat[2][0]) / det; 605: float a10 = - (mat[0][1] * mat[2][2] - mat[0][2] * mat[2][1]) / det; 606: float a11 = (mat[0][0] * mat[2][2] - mat[0][2] * mat[2][0]) / det; 607: float a12 = - (mat[0][0] * mat[2][1] - mat[0][1] * mat[2][0]) / det; 608: float a20 = (mat[0][1] * mat[1][2] - mat[0][2] * mat[1][1]) / det; 609: float a21 = - (mat[0][0] * mat[1][2] - mat[0][2] * mat[1][0]) / det; 610: float a22 = (mat[0][0] * mat[1][1] - mat[0][1] * mat[1][0]) / det; 611: return mat3( 612: a00, a01, a02, 613: a10, a11, a12, 614: a20, a21, a22 615: ); 616: } 617: mat3 toMat3(mat4 mat) { 618: return mat3(mat[0][0], mat[0][1], mat[0][2], 619: mat[1][0], mat[1][1], mat[1][2], 620: mat[2][0], mat[2][1], mat[2][2]); 621: } 622: mat4 toMat4(mat3 mat) { 623: return mat4(mat[0][0], mat[0][1], mat[0][2], 0.0, 624: mat[1][0], mat[1][1], mat[1][2], 0.0, 625: mat[2][0], mat[2][1], mat[2][2], 0.0, 626: 0.0, 0.0, 0.0, 1.0); 627: } 628: vec3 packNormalToRGB(const in vec3 normal) { 629: return normalize(normal) * 0.5 + 0.5; 630: } 631: vec3 unpackRGBToNormal(const in vec3 rgb) { 632: return 2.0 * rgb.xyz - 1.0; 633: } 634: const float PackUpscale = 256. / 255.; 635: const float UnpackDownscale = 255. / 256.; 636: const vec3 PackFactors = vec3(256. * 256. * 256., 256. * 256., 256.); 637: const vec4 UnpackFactors = UnpackDownscale / vec4(PackFactors, 1.); 638: const float ShiftRight8 = 1. / 256.; 639: vec4 packDepthToRGBA(const in float v) { 640: vec4 r = vec4(fract(v * PackFactors), v); 641: r.yzw -= r.xyz * ShiftRight8; 642: return r * PackUpscale; 643: } 644: float unpackRGBAToDepth(const in vec4 v) { 645: return dot(v, UnpackFactors); 646: } 647: vec4 pack2HalfToRGBA(vec2 v) { 648: vec4 r = vec4(v.x, fract(v.x * 255.0), v.y, fract(v.y * 255.0)); 649: return vec4(r.x - r.y / 255.0, r.y, r.z - r.w / 255.0, r.w); 650: } 651: vec2 unpackRGBATo2Half(vec4 v) { 652: return vec2(v.x + (v.y / 255.0), v.z + (v.w / 255.0)); 653: } 654: float viewZToOrthographicDepth(const in float viewZ, const in float near, const in float far) { 655: return (viewZ + near) / (near - far); 656: } 657: float orthographicDepthToViewZ(const in float linearClipZ, const in float near, const in float far) { 658: return linearClipZ * (near - far) - near; 659: } 660: float viewZToPerspectiveDepth(const in float viewZ, const in float near, const in float far) { 661: return ((near + viewZ) * far) / ((far - near) * viewZ); 662: } 663: float perspectiveDepthToViewZ(const in float invClipZ, const in float near, const in float far) { 664: return (near * far) / ((far - near) * invClipZ - far); 665: } 666: #ifdef DITHERING 667: vec3 dithering(vec3 color) { 668: float grid_position = rand(gl_FragCoord.xy); 669: vec3 dither_shift_RGB = vec3(0.25 / 255.0, -0.25 / 255.0, 0.25 / 255.0); 670: dither_shift_RGB = mix(2.0 * dither_shift_RGB, -2.0 * dither_shift_RGB, grid_position); 671: return color + dither_shift_RGB; 672: } 673: #endif 674: 675: vec2 integrateSpecularBRDF(const in float dotNV, const in float roughness) { 676: const vec4 c0 = vec4(- 1, - 0.0275, - 0.572, 0.022); 677: const vec4 c1 = vec4(1, 0.0425, 1.04, - 0.04); 678: vec4 r = roughness * c0 + c1; 679: float a004 = min(r.x * r.x, exp2(- 9.28 * dotNV)) * r.x + r.y; 680: return vec2(-1.04, 1.04) * a004 + r.zw; 681: } 682: float punctualLightIntensityToIrradianceFactor(float lightDistance, const in float cutoffDistance, const in float decayExponent) { 683: #if defined (PHYSICALLY_CORRECT_LIGHTS) 684: lightDistance = UNITS_SCALE_FACTOR * lightDistance; 685: #ifdef MT_MAYA 686: float distanceFalloff = 1.0 / pow(lightDistance + 1.0, decayExponent); 687: #else 688: float distanceFalloff = 1.0 / max(pow(lightDistance, decayExponent), 0.01); 689: #endif 690: if(cutoffDistance > 0.0) { 691: distanceFalloff *= pow2(saturate(1.0 - pow4(lightDistance / cutoffDistance))); 692: } 693: return distanceFalloff; 694: #else 695: if(cutoffDistance > 0.0 && decayExponent > 0.0) { 696: return pow(saturate(-lightDistance / cutoffDistance + 1.0), decayExponent); 697: } 698: return 1.0; 699: #endif 700: } 701: vec3 BRDF_Diffuse_Lambert(const in vec3 diffuseColor) { 702: return RECIPROCAL_PI * diffuseColor; 703: } 704: vec3 F_Schlick(const in vec3 specularColor, const in float dotLH) { 705: float fresnel = exp2((-5.55473 * dotLH - 6.98316) * dotLH); 706: return (1.0 - specularColor) * fresnel + specularColor; 707: } 708: vec3 F_Schlick_RoughnessDependent(const in vec3 F0, const in float dotNV, const in float roughness) { 709: float fresnel = exp2((-5.55473 * dotNV - 6.98316) * dotNV); 710: vec3 Fr = max(vec3(1.0 - roughness), F0) - F0; 711: return Fr * fresnel + F0; 712: } 713: float G_GGX_Smith(const in float alpha, const in float dotNL, const in float dotNV) { 714: float a2 = pow2(alpha); 715: float gl = dotNL + sqrt(a2 + (1.0 - a2) * pow2(dotNL)); 716: float gv = dotNV + sqrt(a2 + (1.0 - a2) * pow2(dotNV)); 717: return 1.0 / (gl * gv); 718: } 719: float G_GGX_SmithCorrelated(const in float alpha, const in float dotNL, const in float dotNV) { 720: float a2 = pow2(alpha); 721: float gv = dotNL * sqrt(a2 + (1.0 - a2) * pow2(dotNV)); 722: float gl = dotNV * sqrt(a2 + (1.0 - a2) * pow2(dotNL)); 723: return 0.5 / max(gv + gl, EPSILON); 724: } 725: float D_GGX(const in float alpha, const in float dotNH) { 726: float a2 = pow2(alpha); 727: float denom = pow2(dotNH) * (a2 - 1.0) + 1.0; 728: return RECIPROCAL_PI * a2 / pow2(denom); 729: } 730: vec3 BRDF_Specular_GGX(const in IncidentLight incidentLight, const in vec3 viewDir, const in vec3 normal, const in vec3 specularColor, const in float roughness) { 731: float alpha = pow2(clamp(roughness, 0.04, 1.0)); 732: vec3 halfDir = normalize(incidentLight.direction + viewDir); 733: float dotNL = saturate(dot(normal, incidentLight.direction)); 734: float dotNV = saturate(dot(normal, viewDir)); 735: float dotNH = saturate(dot(normal, halfDir)); 736: float dotLH = saturate(dot(incidentLight.direction, halfDir)); 737: vec3 F = F_Schlick(specularColor, dotLH); 738: float G = G_GGX_SmithCorrelated(alpha, dotNL, dotNV); 739: float D = D_GGX(alpha, dotNH); 740: return F * (G * D); 741: } 742: vec2 LTC_Uv(const in vec3 N, const in vec3 V, const in float roughness) { 743: const float LUT_SIZE = 64.0; 744: const float LUT_SCALE = (LUT_SIZE - 1.0) / LUT_SIZE; 745: const float LUT_BIAS = 0.5 / LUT_SIZE; 746: float dotNV = saturate(dot(N, V)); 747: vec2 uv = vec2(roughness, sqrt(1.0 - dotNV)); 748: uv = uv * LUT_SCALE + LUT_BIAS; 749: return uv; 750: } 751: float LTC_ClippedSphereFormFactor(const in vec3 f) { 752: float l = length(f); 753: return max((l * l + f.z) / (l + 1.0), 0.0); 754: } 755: vec3 LTC_EdgeVectorFormFactor(const in vec3 v1, const in vec3 v2) { 756: float x = dot(v1, v2); 757: float y = abs(x); 758: float a = 0.8543985 + (0.4965155 + 0.0145206 * y) * y; 759: float b = 3.4175940 + (4.1616724 + y) * y; 760: float v = a / b; 761: float theta_sintheta = (x > 0.0) ? v : 0.5 * inversesqrt(max(1.0 - x * x, 1e-7)) - v; 762: return cross(v1, v2) * theta_sintheta; 763: } 764: vec3 LTC_Evaluate(const in vec3 N, const in vec3 V, const in vec3 P, const in mat3 mInv, const in vec3 rectCoords[4]) { 765: vec3 v1 = rectCoords[1] - rectCoords[0]; 766: vec3 v2 = rectCoords[3] - rectCoords[0]; 767: vec3 lightNormal = cross(v1, v2); 768: if(dot(lightNormal, P - rectCoords[0]) < 0.0) return vec3(0.0); 769: vec3 T1, T2; 770: T1 = normalize(V - N * dot(V, N)); 771: T2 = - cross(N, T1); 772: mat3 mat = mInv * transposeMat3(mat3(T1, T2, N)); 773: vec3 coords[4]; 774: coords[0] = mat * (rectCoords[0] - P); 775: coords[1] = mat * (rectCoords[1] - P); 776: coords[2] = mat * (rectCoords[2] - P); 777: coords[3] = mat * (rectCoords[3] - P); 778: coords[0] = normalize(coords[0]); 779: coords[1] = normalize(coords[1]); 780: coords[2] = normalize(coords[2]); 781: coords[3] = normalize(coords[3]); 782: vec3 vectorFormFactor = vec3(0.0); 783: vectorFormFactor += LTC_EdgeVectorFormFactor(coords[0], coords[1]); 784: vectorFormFactor += LTC_EdgeVectorFormFactor(coords[1], coords[2]); 785: vectorFormFactor += LTC_EdgeVectorFormFactor(coords[2], coords[3]); 786: vectorFormFactor += LTC_EdgeVectorFormFactor(coords[3], coords[0]); 787: float result = LTC_ClippedSphereFormFactor(vectorFormFactor); 788: return vec3(result); 789: } 790: vec2 get_BRDF_SpecCoeffsBlender(float x, float y) { 791: vec3 xyFactors0 = vec3(x * x, y * y, x * y); 792: vec3 xyFactors1 = vec3(x, y, 1); 793: vec3 a0a1a2 = vec3(0.33749372, 0.15167605, 1.09684597); 794: vec3 a3a4a5 = vec3(-1.26123466, -0.927699, 0.9199188); 795: vec3 b0b1b2 = vec3(0.41699717, 0.44675109, 0.79947684); 796: vec3 b3b4b5 = vec3(-1.19307849, -0.89813958, 0.89305222); 797: vec3 c0c1c2 = vec3(0.29920727, 0.09505591, -0.9136233); 798: vec3 c3c4c5 = vec3(0.77055201, 0.13006674, -0.23085581); 799: vec3 d0d1d2 = vec3(15.05004149, 7.98517355, 13.30473726); 800: vec3 d3d4d5 = vec3(-32.00353547, -12.97743434, 17.83646751); 801: float coeff0 = (dot(xyFactors0, a0a1a2) + dot(xyFactors1, a3a4a5)) 802: / (dot(xyFactors0, b0b1b2) + dot(xyFactors1, b3b4b5)); 803: float coeff1 = (dot(xyFactors0, c0c1c2) + dot(xyFactors1, c3c4c5)) 804: / (dot(xyFactors0, d0d1d2) + dot(xyFactors1, d3d4d5)); 805: coeff1 = clamp(coeff1, 0.0, 1.0); 806: return vec2(coeff0, coeff1); 807: } 808: vec3 BRDF_Specular_GGX_Environment(const in vec3 viewDir, const in vec3 normal, const in vec3 specularColor, const in float roughness) { 809: float dotNV = saturate(dot(normal, viewDir)); 810: vec2 brdf = integrateSpecularBRDF(dotNV, roughness); 811: return specularColor * brdf.x + brdf.y; 812: } 813: vec3 BRDF_Specular_GGX_Environment_Blender_Approx(const in GeometricContext geometry, 814: const in vec3 fresnelRefl0, const in vec3 fresnelRefl90, 815: const in float roughness, const int useCoat) { 816: vec3 normal = geometry.normal; 817: #ifdef CLEARCOAT 818: if (useCoat == 1) { 819: normal = geometry.clearcoatNormal; 820: } 821: #endif 822: float dotNV = saturate(dot(normal, geometry.viewDir)); 823: float angle = acos(abs(dotNV)) / PI_HALF; 824: vec2 specCoeffs = get_BRDF_SpecCoeffsBlender(angle, roughness); 825: vec3 specular = specCoeffs.x * fresnelRefl0 826: + specCoeffs.y * fresnelRefl90 827: * vec3(saturate(50.0 * linearToRelativeLuminance(fresnelRefl0))); 828: #if defined (COMPAT_SATURATE_SPEC_ENV_BLENDER_APPROX) 829: specular = saturate(specular); 830: #endif 831: return specular; 832: } 833: void BRDF_Specular_Multiscattering_Environment(const in GeometricContext geometry, const in vec3 specularColor, const in float roughness, inout vec3 singleScatter, inout vec3 multiScatter) { 834: float dotNV = saturate(dot(geometry.normal, geometry.viewDir)); 835: vec3 F = F_Schlick_RoughnessDependent(specularColor, dotNV, roughness); 836: vec2 brdf = integrateSpecularBRDF(dotNV, roughness); 837: vec3 FssEss = F * brdf.x + brdf.y; 838: float Ess = brdf.x + brdf.y; 839: float Ems = 1.0 - Ess; 840: vec3 Favg = specularColor + (1.0 - specularColor) * 0.047619; 841: vec3 Fms = FssEss * Favg / (1.0 - Ems * Favg); 842: singleScatter += FssEss; 843: multiScatter += Fms * Ems; 844: } 845: float G_BlinnPhong_Implicit() { 846: return 0.25; 847: } 848: float D_BlinnPhong(const in float shininess, const in float dotNH) { 849: return RECIPROCAL_PI * (shininess * 0.5 + 1.0) * pow(dotNH, shininess); 850: } 851: vec3 BRDF_Specular_BlinnPhong(const in IncidentLight incidentLight, const in GeometricContext geometry, const in vec3 specularColor, const in float shininess) { 852: vec3 halfDir = normalize(incidentLight.direction + geometry.viewDir); 853: float dotNH = saturate(dot(geometry.normal, halfDir)); 854: float dotLH = saturate(dot(incidentLight.direction, halfDir)); 855: vec3 F = F_Schlick(specularColor, dotLH); 856: float G = G_BlinnPhong_Implicit(); 857: float D = D_BlinnPhong(shininess, dotNH); 858: return F * (G * D); 859: } 860: float GGXRoughnessToBlinnExponent(const in float ggxRoughness) { 861: return (2.0 / pow2(ggxRoughness + 0.0001) - 2.0); 862: } 863: float BlinnExponentToGGXRoughness(const in float blinnExponent) { 864: return sqrt(2.0 / (blinnExponent + 2.0)); 865: } 866: #if defined(USE_SHEEN) 867: float D_Charlie(float roughness, float NoH) { 868: float invAlpha = 1.0 / roughness; 869: float cos2h = NoH * NoH; 870: float sin2h = max(1.0 - cos2h, 0.0078125); 871: return (2.0 + invAlpha) * pow(sin2h, invAlpha * 0.5) / (2.0 * PI); 872: } 873: float V_Neubelt(float NoV, float NoL) { 874: return saturate(1.0 / (4.0 * (NoL + NoV - NoL * NoV))); 875: } 876: vec3 BRDF_Specular_Sheen(const in float roughness, const in vec3 L, const in GeometricContext geometry, vec3 specularColor) { 877: vec3 N = geometry.normal; 878: vec3 V = geometry.viewDir; 879: vec3 H = normalize(V + L); 880: float dotNH = saturate(dot(N, H)); 881: return specularColor * D_Charlie(roughness, dotNH) * V_Neubelt(dot(N, V), dot(N, L)); 882: } 883: #endif 884: #ifdef ENVMAP_TYPE_CUBE_UV 885: float getFace(vec3 direction) { 886: vec3 absDirection = abs(direction); 887: float face = -1.0; 888: if (absDirection.x > absDirection.z) { 889: if (absDirection.x > absDirection.y) { 890: face = direction.x > 0.0 ? 0.0 : 3.0; 891: } else { 892: face = direction.y > 0.0 ? 1.0 : 4.0; 893: } 894: } else { 895: if (absDirection.z > absDirection.y) { 896: face = direction.z > 0.0 ? 2.0 : 5.0; 897: } else { 898: face = direction.y > 0.0 ? 1.0 : 4.0; 899: } 900: } 901: return face; 902: } 903: vec2 getUV(vec3 direction, float face) { 904: vec2 uv; 905: if (face == 0.0) { 906: uv = vec2(-direction.z, direction.y) / abs(direction.x); 907: } else if (face == 1.0) { 908: uv = vec2(direction.x, -direction.z) / abs(direction.y); 909: } else if (face == 2.0) { 910: uv = direction.xy / abs(direction.z); 911: } else if (face == 3.0) { 912: uv = vec2(direction.z, direction.y) / abs(direction.x); 913: } else if (face == 4.0) { 914: uv = direction.xz / abs(direction.y); 915: } else { 916: uv = vec2(-direction.x, direction.y) / abs(direction.z); 917: } 918: return 0.5 * (uv + 1.0); 919: } 920: #ifndef cubeUV_maxTileSize 921: #define cubeUV_maxTileSize 256.0 922: #endif 923: #define cubeUV_lodIdxMin 0.0 924: #define cubeUV_lodIdxLastDownscaled 4.0 925: #define cubeUV_lodIdxMax 10.0 926: #define cubeUV_minTileSize (cubeUV_maxTileSize / exp2(cubeUV_lodIdxLastDownscaled)) 927: float getLodTileSize(float lodIdx) { 928: return cubeUV_maxTileSize / exp2(min(lodIdx, cubeUV_lodIdxLastDownscaled)); 929: } 930: float getLodFilterLevel(float lodIdx) { 931: return max(lodIdx - cubeUV_lodIdxLastDownscaled, 0.0); 932: } 933: vec2 fixCubeUVSeams(vec2 uv, float faceSize) { 934: float BORDER_WIDTH_PX = max(cubeUV_maxTileSize / 256.0 - 1.0, 0.0); 935: float scale = (faceSize - BORDER_WIDTH_PX) / faceSize; 936: float offset = 0.5 * BORDER_WIDTH_PX / faceSize; 937: return uv * scale + offset; 938: } 939: vec2 getUVPixels(vec3 direction, float lodIdx) { 940: float face = getFace(direction); 941: float faceSize = getLodTileSize(lodIdx); 942: float filterLevel = getLodFilterLevel(lodIdx); 943: vec2 uv = getUV(direction, face); 944: uv = fixCubeUVSeams(uv, faceSize); 945: uv *= (faceSize - 1.0); 946: if (face > 2.0) { 947: uv.y += faceSize; 948: face -= 3.0; 949: } 950: uv.x += face * faceSize; 951: if (lodIdx > 0.0) { 952: uv.y += 2.0 * cubeUV_maxTileSize; 953: } 954: uv.y += filterLevel * 2.0 * cubeUV_minTileSize; 955: uv.x += 3.0 * max(0.0, cubeUV_maxTileSize - 2.0 * faceSize); 956: return uv; 957: } 958: vec3 bilinearCubeUV(sampler2D envMap, vec3 direction, float lodIdx) { 959: float texelSize = 1.0 / (3.0 * cubeUV_maxTileSize); 960: vec2 uv = getUVPixels(direction, lodIdx); 961: vec2 f = fract(uv); 962: uv += 0.5 - f; 963: uv *= texelSize; 964: vec3 tl = envMapTexelToLinear(texture2D(envMap, uv)).rgb; 965: uv.x += texelSize; 966: vec3 tr = envMapTexelToLinear(texture2D(envMap, uv)).rgb; 967: uv.y += texelSize; 968: vec3 br = envMapTexelToLinear(texture2D(envMap, uv)).rgb; 969: uv.x -= texelSize; 970: vec3 bl = envMapTexelToLinear(texture2D(envMap, uv)).rgb; 971: vec3 tm = mix(tl, tr, f.x); 972: vec3 bm = mix(bl, br, f.x); 973: return mix(tm, bm, f.y); 974: } 975: vec3 sampleCubeUV(sampler2D envMap, vec3 direction, float lodIdx) { 976: float texelSize = 1.0 / (3.0 * cubeUV_maxTileSize); 977: vec2 uv = getUVPixels(direction, lodIdx); 978: uv += 0.5; 979: uv *= texelSize; 980: return envMapTexelToLinear(texture2D(envMap, uv)).rgb; 981: } 982: #define cubeUV_r0 1.0 983: #define cubeUV_v0 0.339 984: #define cubeUV_m0 -2.0 985: #define cubeUV_r1 0.8 986: #define cubeUV_v1 0.276 987: #define cubeUV_m1 -1.0 988: #define cubeUV_r4 0.4 989: #define cubeUV_v4 0.046 990: #define cubeUV_m4 2.0 991: #define cubeUV_r5 0.305 992: #define cubeUV_v5 0.016 993: #define cubeUV_m5 3.0 994: #define cubeUV_r6 0.21 995: #define cubeUV_v6 0.0038 996: #define cubeUV_m6 4.0 997: float roughnessToMip(float roughness) { 998: float r = roughness; 999: float r2 = r * r; 1000: float r3 = r2 * r; 1001: roughness = -1.20278049 * r3 + 1.86860137 * r2 + 0.32478081 * r + 0.0098139; 1002: return roughness * (cubeUV_lodIdxMax - cubeUV_lodIdxMin); 1003: } 1004: vec4 textureCubeUV(sampler2D envMap, vec3 sampleDir, float roughness) { 1005: float lodIdx = clamp(roughnessToMip(roughness), cubeUV_lodIdxMin, 1006: cubeUV_lodIdxMax); 1007: float lodIdxF = fract(lodIdx); 1008: float lodIdxI = floor(lodIdx); 1009: vec3 color0 = sampleCubeUV(envMap, sampleDir, lodIdxI); 1010: if (lodIdxF == 0.0) { 1011: return vec4(color0, 1.0); 1012: } else { 1013: vec3 color1 = sampleCubeUV(envMap, sampleDir, lodIdxI + 1.0); 1014: return vec4(mix(color0, color1, lodIdxF), 1.0); 1015: } 1016: } 1017: #endif 1018: #ifdef USE_ENVMAP 1019: uniform float envMapIntensity; 1020: uniform float flipEnvMap; 1021: uniform int maxMipLevel; 1022: #ifdef ENVMAP_TYPE_CUBE 1023: uniform samplerCube envMap; 1024: #else 1025: uniform sampler2D envMap; 1026: #endif 1027: #if defined(ENVMAP_TYPE_CUBE) && defined(NODE) || defined(ENVMAP_TYPE_CUBE_UV) 1028: float calcGeometryRoughness(vec3 geometryNormal) { 1029: vec3 dxy = max(abs(dFdx(geometryNormal)), abs(dFdy(geometryNormal))); 1030: return max(max(dxy.x, dxy.y), dxy.z); 1031: } 1032: float calcCubeUVAdjustedRoughness(float origRoughness, float geomRoughness) { 1033: return min(max(origRoughness, 0.0525) + geomRoughness, 1.0); 1034: } 1035: #endif 1036: #endif 1037: #if defined(NODE_NEW_GEOMETRY_BL) || defined(NODE_VECT_TRANSFORM_BL) || defined(NODE_TEX_COORD_BL) || defined(NODE_NORMAL_MAP_BL) || defined(NODE_LAYER_WEIGHT_BL) || defined(NODE_FRESNEL_BL) || defined(NODE_BUMP_BL) || defined(NODE_BSDF_GLASS_BL) || defined(NODE_BSDF_PRINCIPLED_BL) || defined(NODE_TANGENT_BL) || defined(NODE_BITMAP_MX) || defined(NODE_GRADIENT_RAMP_MX) || defined(NODE_NOISE_MX) || defined(NODE_SAMPLER_INFO_MY) || defined(NODE_INCIDENT) || defined(NODE_POSITION) || defined(NODE_NORMAL) || defined(USE_OSL) || defined(USE_ENVMAP) || defined(SHADOWMAP_TYPE_ESM) && (defined(NODE_BSDF_DIFFUSE_BL) || defined(NODE_EEVEE_SPECULAR_BL) || defined(NODE_BSDF_GLOSSY_BL) || defined(NODE_BSDF_REFRACTION_BL) || defined(NODE_MATERIAL_MX) || defined(NODE_PHYSICAL_MX) || defined(NODE_STANDARD_SURFACE_AR) || defined(NODE_SHADOW_MATTE_AR)) 1038: uniform mat4 invViewMatrix; 1039: #endif 1040: #if defined(USE_ENVMAP) 1041: #define ENVMAP_PARALLAX_INFINITE 0 1042: #define ENVMAP_PARALLAX_SPHERE 1 1043: #define ENVMAP_PARALLAX_BOX 2 1044: #ifdef ENVMAP_MODE_REFRACTION 1045: uniform float refractionRatio; 1046: #endif 1047: uniform int envMapParallaxType; 1048: uniform mat4 envMapParallaxMatrix; 1049: uniform mat4 envMapParallaxMatrixInv; 1050: vec3 correctParallax(vec3 directionVecWorld, vec3 posWorld, int parallaxType) { 1051: vec3 posProbe = (envMapParallaxMatrix * vec4(posWorld, 1.0)).xyz; 1052: vec3 reflectVecProbe = transformDirection(directionVecWorld, envMapParallaxMatrix); 1053: if (parallaxType == ENVMAP_PARALLAX_SPHERE) { 1054: float b = 2.0 * dot(reflectVecProbe, posProbe); 1055: float c = dot(posProbe, posProbe) - 1.0; 1056: float D = b * b - 4.0 * c; 1057: if (D >= 0.0) { 1058: float x = (sqrt(D) - b) / 2.0; 1059: reflectVecProbe = posProbe + x * reflectVecProbe; 1060: } 1061: } else if (parallaxType == ENVMAP_PARALLAX_BOX) { 1062: vec3 scalePos = (vec3(1.0) - posProbe) / reflectVecProbe; 1063: vec3 scaleNeg = (vec3(-1.0) - posProbe) / reflectVecProbe; 1064: vec3 scalePosNeg = mix(scaleNeg, scalePos, step(vec3(0.0), reflectVecProbe)); 1065: float x = min(scalePosNeg.x, min(scalePosNeg.y, scalePosNeg.z)); 1066: reflectVecProbe = posProbe + x * reflectVecProbe; 1067: } 1068: vec3 directionVecWorldCorrected = transformDirection(reflectVecProbe, 1069: envMapParallaxMatrixInv); 1070: return directionVecWorldCorrected; 1071: } 1072: vec3 getLightProbeIndirectIrradiance( const in GeometricContext geometry, const in int maxMIPLevel) { 1073: vec3 worldNormal = inverseTransformDirection(geometry.normal, viewMatrix); 1074: #ifdef ENVMAP_TYPE_CUBE 1075: vec3 queryVec = vec3(flipEnvMap * worldNormal.x, worldNormal.yz); 1076: #ifdef TEXTURE_LOD_EXT 1077: vec4 envMapColor = textureCubeLodEXT(envMap, queryVec, float(maxMIPLevel)); 1078: #else 1079: vec4 envMapColor = textureCube(envMap, queryVec, float(maxMIPLevel)); 1080: #endif 1081: envMapColor.rgb = envMapTexelToLinear(envMapColor).rgb; 1082: #elif defined(ENVMAP_TYPE_CUBE_UV) 1083: vec3 queryVec = vec3(flipEnvMap * worldNormal.x, worldNormal.yz); 1084: vec4 envMapColor = textureCubeUV(envMap, queryVec, 1.0); 1085: #else 1086: vec4 envMapColor = vec4(0.0); 1087: #endif 1088: return PI * envMapColor.rgb * envMapIntensity; 1089: } 1090: float getSpecularMIPLevel(const in float blinnShininessExponent, const in int maxMIPLevel) { 1091: float maxMIPLevelScalar = float(maxMIPLevel); 1092: float clapmedBlinnShininessExponent = min(blinnShininessExponent, 30000.0); 1093: float desiredMIPLevel = maxMIPLevelScalar + 0.79248 1094: - 0.5 * log2(pow2(clapmedBlinnShininessExponent) + 1.0); 1095: return clamp(desiredMIPLevel, 0.0, maxMIPLevelScalar); 1096: } 1097: vec3 _getLightProbeIndirect(const float blinnShininessExponent, 1098: const int maxMIPLevel, vec3 directionVec) { 1099: float specularMIPLevel = getSpecularMIPLevel(blinnShininessExponent, maxMIPLevel); 1100: #ifdef ENVMAP_TYPE_CUBE 1101: vec3 queryVec = vec3(flipEnvMap * directionVec.x, directionVec.yz); 1102: #ifdef TEXTURE_LOD_EXT 1103: vec4 envMapColor = textureCubeLodEXT(envMap, queryVec, specularMIPLevel); 1104: #else 1105: vec4 envMapColor = textureCube(envMap, queryVec, specularMIPLevel); 1106: #endif 1107: envMapColor.rgb = envMapTexelToLinear(envMapColor).rgb; 1108: #elif defined(ENVMAP_TYPE_CUBE_UV) 1109: vec3 queryVec = vec3(flipEnvMap * directionVec.x, directionVec.yz); 1110: vec4 envMapColor = textureCubeUV(envMap, queryVec, 1111: BlinnExponentToGGXRoughness(blinnShininessExponent)); 1112: #elif defined(ENVMAP_TYPE_EQUIREC) 1113: vec2 sampleUV; 1114: sampleUV.y = asin(clamp(directionVec.y, - 1.0, 1.0)) * RECIPROCAL_PI + 0.5; 1115: sampleUV.x = atan(directionVec.z, directionVec.x) * RECIPROCAL_PI2 + 0.5; 1116: #ifdef TEXTURE_LOD_EXT 1117: vec4 envMapColor = texture2DLodEXT(envMap, sampleUV, specularMIPLevel); 1118: #else 1119: vec4 envMapColor = texture2D(envMap, sampleUV, specularMIPLevel); 1120: #endif 1121: envMapColor.rgb = envMapTexelToLinear(envMapColor).rgb; 1122: #endif 1123: return envMapColor.rgb * envMapIntensity; 1124: } 1125: vec3 getLightProbeIndirectRadiance( 1126: const GeometricContext geometry, const float blinnShininessExponent, 1127: const int maxMIPLevel, const int useCoat) { 1128: vec3 normal = geometry.normal; 1129: #ifdef CLEARCOAT 1130: if (useCoat == 1) { 1131: normal = geometry.clearcoatNormal; 1132: } 1133: #endif 1134: #ifdef ENVMAP_MODE_REFLECTION 1135: vec3 directionVec = reflect(-geometry.viewDir, normal); 1136: #else 1137: vec3 directionVec = refract(-geometry.viewDir, normal, refractionRatio); 1138: #endif 1139: directionVec = inverseTransformDirection(directionVec, viewMatrix); 1140: vec3 posWorld = (invViewMatrix * vec4(geometry.position, 1.0)).xyz; 1141: if (envMapParallaxType != ENVMAP_PARALLAX_INFINITE) { 1142: directionVec = correctParallax(directionVec, posWorld, envMapParallaxType); 1143: } 1144: return _getLightProbeIndirect(blinnShininessExponent, maxMIPLevel, 1145: directionVec); 1146: } 1147: vec3 getLightProbeIndirectRefraction( 1148: const GeometricContext geometry, const float blinnShininessExponent, 1149: const int maxMIPLevel, const float refrRatio) { 1150: vec3 directionVec = refract(-geometry.viewDir, geometry.normal, refrRatio); 1151: directionVec = inverseTransformDirection(directionVec, viewMatrix); 1152: vec3 posWorld = (invViewMatrix * vec4(geometry.position, 1.0)).xyz; 1153: if (envMapParallaxType != ENVMAP_PARALLAX_INFINITE) { 1154: directionVec = correctParallax(directionVec, posWorld, envMapParallaxType); 1155: } 1156: return _getLightProbeIndirect(blinnShininessExponent, maxMIPLevel, 1157: directionVec); 1158: } 1159: #endif 1160: #ifdef USE_FOG 1161: uniform vec3 fogColor; 1162: varying float fogDepth; 1163: #ifdef FOG_EXP2 1164: uniform float fogDensity; 1165: #else 1166: uniform float fogNear; 1167: uniform float fogFar; 1168: #endif 1169: #endif 1170: uniform bool receiveShadow; 1171: uniform vec3 ambientLightColor; 1172: uniform vec3 lightProbe[9]; 1173: vec3 shGetIrradianceAt(in vec3 normal, in vec3 shCoefficients[9]) { 1174: float x = normal.x, y = normal.y, z = normal.z; 1175: vec3 result = shCoefficients[0] * 0.886227; 1176: result += shCoefficients[1] * 2.0 * 0.511664 * y; 1177: result += shCoefficients[2] * 2.0 * 0.511664 * z; 1178: result += shCoefficients[3] * 2.0 * 0.511664 * x; 1179: result += shCoefficients[4] * 2.0 * 0.429043 * x * y; 1180: result += shCoefficients[5] * 2.0 * 0.429043 * y * z; 1181: result += shCoefficients[6] * (0.743125 * z * z - 0.247708); 1182: result += shCoefficients[7] * 2.0 * 0.429043 * x * z; 1183: result += shCoefficients[8] * 0.429043 * (x * x - y * y); 1184: return result; 1185: } 1186: vec3 getLightProbeIrradiance(const in vec3 lightProbe[9], const in GeometricContext geometry) { 1187: vec3 worldNormal = inverseTransformDirection(geometry.normal, viewMatrix); 1188: vec3 irradiance = shGetIrradianceAt(worldNormal, lightProbe); 1189: return irradiance; 1190: } 1191: vec3 getAmbientLightIrradiance(const in vec3 ambientLightColor) { 1192: vec3 irradiance = ambientLightColor; 1193: #ifndef PHYSICALLY_CORRECT_LIGHTS 1194: irradiance *= PI; 1195: #endif 1196: return irradiance; 1197: } 1198: #if 0 > 0 1199: struct DirectionalLight { 1200: vec3 direction; 1201: vec3 color; 1202: }; 1203: uniform DirectionalLight directionalLights[0]; 1204: void getDirectionalDirectLightIrradiance(const in DirectionalLight directionalLight, const in GeometricContext geometry, out IncidentLight directLight) { 1205: directLight.color = directionalLight.color; 1206: directLight.direction = directionalLight.direction; 1207: directLight.visible = true; 1208: } 1209: #endif 1210: #if 1 > 0 1211: struct PointLight { 1212: vec3 position; 1213: vec3 color; 1214: float distance; 1215: float decay; 1216: }; 1217: uniform PointLight pointLights[1]; 1218: void getPointDirectLightIrradiance(const in PointLight pointLight, const in GeometricContext geometry, out IncidentLight directLight) { 1219: vec3 lVector = pointLight.position - geometry.position; 1220: directLight.direction = normalize(lVector); 1221: float lightDistance = length(lVector); 1222: directLight.color = pointLight.color; 1223: directLight.color *= punctualLightIntensityToIrradianceFactor(lightDistance, pointLight.distance, pointLight.decay); 1224: directLight.visible = (directLight.color != vec3(0.0)); 1225: } 1226: #endif 1227: #if 0 > 0 1228: struct SpotLight { 1229: vec3 position; 1230: vec3 direction; 1231: vec3 color; 1232: float distance; 1233: float decay; 1234: float coneCos; 1235: float penumbraCos; 1236: }; 1237: uniform SpotLight spotLights[0]; 1238: void getSpotDirectLightIrradiance(const in SpotLight spotLight, const in GeometricContext geometry, out IncidentLight directLight) { 1239: vec3 lVector = spotLight.position - geometry.position; 1240: directLight.direction = normalize(lVector); 1241: float lightDistance = length(lVector); 1242: float angleCos = dot(directLight.direction, spotLight.direction); 1243: #if defined(MT_MAX) && defined(PHYSICALLY_CORRECT_LIGHTS) 1244: float coneCosDecayed = 2.0 * spotLight.coneCos - spotLight.penumbraCos; 1245: if (angleCos > coneCosDecayed) { 1246: float spotEffect = pow(max(angleCos, 0.0), log(0.5) / log(spotLight.penumbraCos) - 1.0); 1247: if (angleCos < spotLight.coneCos) { 1248: float decayFac = 1.0 + (spotLight.coneCos - angleCos) 1249: / (spotLight.coneCos - spotLight.penumbraCos); 1250: spotEffect *= pow2(decayFac) * (3.0 - 2.0 * decayFac); 1251: } 1252: directLight.color = spotLight.color * spotEffect 1253: * punctualLightIntensityToIrradianceFactor(lightDistance, 1254: spotLight.distance, spotLight.decay); 1255: directLight.visible = true; 1256: } else { 1257: directLight.color = vec3(0.0); 1258: directLight.visible = false; 1259: } 1260: #else 1261: if (angleCos > spotLight.coneCos) { 1262: float spotEffect = smoothstep(spotLight.coneCos, spotLight.penumbraCos, angleCos); 1263: directLight.color = spotLight.color * spotEffect 1264: * punctualLightIntensityToIrradianceFactor(lightDistance, 1265: spotLight.distance, spotLight.decay); 1266: directLight.visible = true; 1267: } else { 1268: directLight.color = vec3(0.0); 1269: directLight.visible = false; 1270: } 1271: #endif 1272: } 1273: #endif 1274: #if 0 > 0 1275: struct RectAreaLight { 1276: vec3 color; 1277: vec3 position; 1278: vec3 halfWidth; 1279: vec3 halfHeight; 1280: }; 1281: uniform sampler2D ltc_1; 1282: uniform sampler2D ltc_2; 1283: uniform RectAreaLight rectAreaLights[0]; 1284: #endif 1285: #if 0 > 0 1286: struct HemisphereLight { 1287: vec3 direction; 1288: vec3 skyColor; 1289: vec3 groundColor; 1290: }; 1291: uniform HemisphereLight hemisphereLights[0]; 1292: vec3 getHemisphereLightIrradiance(const in HemisphereLight hemiLight, const in GeometricContext geometry) { 1293: float dotNL = dot(geometry.normal, hemiLight.direction); 1294: float hemiDiffuseWeight = 0.5 * dotNL + 0.5; 1295: vec3 irradiance = mix(hemiLight.groundColor, hemiLight.skyColor, hemiDiffuseWeight); 1296: #ifndef PHYSICALLY_CORRECT_LIGHTS 1297: irradiance *= PI; 1298: #endif 1299: return irradiance; 1300: } 1301: #endif 1302: 1303: varying vec3 vViewPosition; 1304: #ifndef FLAT_SHADED 1305: varying vec3 vNormal; 1306: #endif 1307: struct NodeMaterial { 1308: vec3 diffuseColor; 1309: vec3 specularColor; 1310: vec3 refractionColor; 1311: float refractionRoughness; 1312: float refractionIOR; 1313: #ifdef CLEARCOAT 1314: float clearcoat; 1315: float clearcoatRoughness; 1316: #endif 1317: #ifdef USE_SHEEN 1318: vec3 sheenColor; 1319: float sheenRoughness; 1320: #endif 1321: #ifdef PHYSICAL 1322: vec3 fresnelRefl90; 1323: float specularRoughness; 1324: #else 1325: float diffuseIntensity; 1326: float specularIntensity; 1327: float specularHardness; 1328: #endif 1329: }; 1330: #define MAXIMUM_SPECULAR_COEFFICIENT 0.16 1331: #define DEFAULT_SPECULAR_COEFFICIENT 0.04 1332: #define BLENDER_SPECULAR_COEFFICIENT 0.08 1333: float shadeSpecPhong(vec3 normal, vec3 lightDir, vec3 viewDir, float hard) 1334: { 1335: float specFac; 1336: vec3 h = normalize(lightDir + viewDir); 1337: float rslt = max(dot(h, normal), 0.0); 1338: specFac = pow(rslt, hard); 1339: return specFac; 1340: } 1341: float clearcoatDHRApprox(const in float roughness, const in float dotNL) { 1342: return DEFAULT_SPECULAR_COEFFICIENT + (1.0 - DEFAULT_SPECULAR_COEFFICIENT) * (pow(1.0 - dotNL, 5.0) * pow(1.0 - roughness, 2.0)); 1343: } 1344: #if 0 > 0 1345: void RE_Direct_RectArea_Node(const in RectAreaLight rectAreaLight, const in GeometricContext geometry, const in NodeMaterial material, inout ReflectedLight reflectedLight) { 1346: vec3 normal = geometry.normal; 1347: vec3 viewDir = geometry.viewDir; 1348: vec3 position = geometry.position; 1349: vec3 lightPos = rectAreaLight.position; 1350: vec3 halfWidth = rectAreaLight.halfWidth; 1351: vec3 halfHeight = rectAreaLight.halfHeight; 1352: vec3 lightColor = rectAreaLight.color; 1353: float roughness = material.specularRoughness; 1354: vec3 rectCoords[4]; 1355: rectCoords[0] = lightPos + halfWidth - halfHeight; 1356: rectCoords[1] = lightPos - halfWidth - halfHeight; 1357: rectCoords[2] = lightPos - halfWidth + halfHeight; 1358: rectCoords[3] = lightPos + halfWidth + halfHeight; 1359: vec2 uv = LTC_Uv(normal, viewDir, roughness); 1360: vec4 t1 = texture2D(ltc_1, uv); 1361: vec4 t2 = texture2D(ltc_2, uv); 1362: mat3 mInv = mat3( 1363: vec3(t1.x, 0, t1.y), 1364: vec3( 0, 1, 0), 1365: vec3(t1.z, 0, t1.w) 1366: ); 1367: vec3 fresnel = (material.specularColor * t2.x + (vec3(1.0) - material.specularColor) * t2.y); 1368: reflectedLight.directSpecular += lightColor * fresnel * LTC_Evaluate(normal, viewDir, position, mInv, rectCoords); 1369: reflectedLight.directDiffuse += lightColor * material.diffuseColor * LTC_Evaluate(normal, viewDir, position, mat3(1.0), rectCoords); 1370: } 1371: #endif 1372: void RE_DirectDiffuse_Node(const in IncidentLight directLight, 1373: const in GeometricContext geometry, const in NodeMaterial material, 1374: inout ReflectedLight reflectedLight) { 1375: float dotNL = saturate(dot(geometry.normal, directLight.direction)); 1376: vec3 irradiance = dotNL * directLight.color; 1377: #ifndef PHYSICALLY_CORRECT_LIGHTS 1378: irradiance *= PI; 1379: #endif 1380: #ifdef PHYSICAL 1381: #ifdef CLEARCOAT 1382: float ccDotNL = saturate(dot(geometry.clearcoatNormal, directLight.direction)); 1383: float clearcoatDHR = material.clearcoat * clearcoatDHRApprox(material.clearcoatRoughness, ccDotNL); 1384: #else 1385: float clearcoatDHR = 0.0; 1386: #endif 1387: reflectedLight.directDiffuse += (1.0 - clearcoatDHR) * irradiance 1388: * BRDF_Diffuse_Lambert(material.diffuseColor); 1389: #else 1390: reflectedLight.directDiffuse += irradiance * material.diffuseIntensity 1391: * BRDF_Diffuse_Lambert(material.diffuseColor); 1392: #endif 1393: } 1394: void RE_DirectSpecular_Node(const in IncidentLight directLight, 1395: const in GeometricContext geometry, const in NodeMaterial material, 1396: inout ReflectedLight reflectedLight) { 1397: float dotNL = saturate(dot(geometry.normal, directLight.direction)); 1398: vec3 irradiance = dotNL * directLight.color; 1399: #ifndef PHYSICALLY_CORRECT_LIGHTS 1400: irradiance *= PI; 1401: #endif 1402: #ifdef PHYSICAL 1403: #ifdef CLEARCOAT 1404: float ccDotNL = saturate(dot(geometry.clearcoatNormal, directLight.direction)); 1405: vec3 ccIrradiance = ccDotNL * directLight.color; 1406: #ifndef PHYSICALLY_CORRECT_LIGHTS 1407: ccIrradiance *= PI; 1408: #endif 1409: #endif 1410: #ifdef CLEARCOAT 1411: float clearcoatDHR = material.clearcoat * clearcoatDHRApprox(material.clearcoatRoughness, ccDotNL); 1412: #else 1413: float clearcoatDHR = 0.0; 1414: #endif 1415: reflectedLight.directSpecular += (1.0 - clearcoatDHR) * irradiance 1416: * BRDF_Specular_GGX(directLight, geometry.viewDir, geometry.normal, 1417: material.specularColor, material.specularRoughness); 1418: #ifdef CLEARCOAT 1419: reflectedLight.directSpecular += ccIrradiance * material.clearcoat 1420: * BRDF_Specular_GGX(directLight, geometry.viewDir, geometry.clearcoatNormal, 1421: vec3(DEFAULT_SPECULAR_COEFFICIENT), material.clearcoatRoughness); 1422: #endif 1423: #ifdef USE_SHEEN 1424: reflectedLight.directSpecular += (1.0 - clearcoatDHR) * irradiance * BRDF_Specular_Sheen( 1425: material.sheenRoughness, 1426: directLight.direction, 1427: geometry, 1428: material.sheenColor 1429: ); 1430: #endif 1431: #else 1432: reflectedLight.directSpecular += material.specularIntensity * 1433: shadeSpecPhong(geometry.normal, directLight.direction, 1434: geometry.viewDir, material.specularHardness) * 1435: directLight.color * material.specularColor; 1436: #endif 1437: } 1438: void RE_IndirectDiffuse_Node(const in vec3 irradiance, const in vec3 iblIrradiance, 1439: const in GeometricContext geometry, const in NodeMaterial material, 1440: inout ReflectedLight reflectedLight) { 1441: reflectedLight.indirectDiffuse += (irradiance + iblIrradiance) 1442: * BRDF_Diffuse_Lambert(material.diffuseColor); 1443: } 1444: void RE_IndirectSpecular_Node(const in vec3 radiance, const in vec3 irradiance, 1445: const in vec3 clearcoatRadiance, const in GeometricContext geometry, 1446: const in NodeMaterial material, inout ReflectedLight reflectedLight) { 1447: #ifdef PHYSICAL 1448: #ifdef CLEARCOAT 1449: float ccDotNL = saturate(dot(geometry.clearcoatNormal, geometry.viewDir)); 1450: float clearcoatDHR = material.clearcoat * clearcoatDHRApprox( 1451: material.clearcoatRoughness, ccDotNL); 1452: #else 1453: float clearcoatDHR = 0.0; 1454: #endif 1455: #ifdef MT_BLENDER 1456: #ifdef COMPAT_USE_SPEC_ENV_BLENDER_APPROX 1457: vec3 specEnv = BRDF_Specular_GGX_Environment_Blender_Approx(geometry, 1458: material.specularColor, material.fresnelRefl90, 1459: material.specularRoughness, 0); 1460: #else 1461: vec3 specEnv = BRDF_Specular_GGX_Environment(geometry.viewDir, 1462: geometry.normal, material.specularColor, 1463: material.specularRoughness); 1464: #endif 1465: #elif defined(MT_MAX) 1466: float alphaEnv = pow2(pow2(material.specularRoughness)); 1467: vec3 specEnv = material.specularColor / (1.0 - alphaEnv + PI * alphaEnv); 1468: #elif defined(MT_MAYA) 1469: vec3 specEnv = BRDF_Specular_GGX_Environment(geometry.viewDir, geometry.normal, material.specularColor, material.specularRoughness); 1470: #else 1471: vec3 specEnv = vec3(1.0); 1472: #endif 1473: reflectedLight.indirectSpecular += (1.0 - clearcoatDHR) * radiance * specEnv; 1474: #ifdef CLEARCOAT 1475: #ifdef MT_BLENDER 1476: vec3 specEnvCC = BRDF_Specular_GGX_Environment(geometry.viewDir, geometry.clearcoatNormal, vec3(DEFAULT_SPECULAR_COEFFICIENT), material.clearcoatRoughness); 1477: #elif defined(MT_MAX) || defined(MT_MAYA) 1478: vec3 specEnvCC = BRDF_Specular_GGX_Environment(geometry.viewDir, geometry.clearcoatNormal, vec3(DEFAULT_SPECULAR_COEFFICIENT), material.clearcoatRoughness); 1479: #else 1480: vec3 specEnvCC = vec3(1.0); 1481: #endif 1482: reflectedLight.indirectSpecular += clearcoatRadiance * material.clearcoat * specEnvCC; 1483: #endif 1484: #endif 1485: } 1486: void RE_Refraction_Node(const vec3 refraction, const NodeMaterial material, 1487: inout vec3 refractedLight) { 1488: refractedLight += refraction * material.refractionColor; 1489: } 1490: #ifdef MT_BLENDER 1491: #define Material_BlinnShininessExponent(material) GGXRoughnessToBlinnExponent(material.specularRoughness) 1492: #define Material_Refraction_BlinnShininessExponent(material) GGXRoughnessToBlinnExponent(material.refractionRoughness) 1493: #elif defined(MT_MAX) 1494: #define Material_BlinnShininessExponent(material) GGXRoughnessToBlinnExponent(material.specularRoughness * exp(0.35*(1.0-pow2(material.specularRoughness)))) 1495: #define Material_Refraction_BlinnShininessExponent(material) GGXRoughnessToBlinnExponent(material.refractionRoughness * exp(0.35*(1.0-pow2(material.refractionRoughness)))) 1496: #else 1497: #define Material_BlinnShininessExponent(material) GGXRoughnessToBlinnExponent(material.specularRoughness) 1498: #define Material_Refraction_BlinnShininessExponent(material) GGXRoughnessToBlinnExponent(material.refractionRoughness) 1499: #endif 1500: #define Material_ClearCoat_BlinnShininessExponent(material) GGXRoughnessToBlinnExponent(material.clearcoatRoughness) 1501: float computeSpecularOcclusion(const in float dotNV, const in float ambientOcclusion, const in float roughness) { 1502: return saturate(pow(abs(dotNV + ambientOcclusion), exp2(- 16.0 * roughness - 1.0)) - 1.0 + ambientOcclusion); 1503: } 1504: #define RE_Direct_RectArea RE_Direct_RectArea_Node 1505: 1506: #define BIAS_FRUSTUM_SCALE_COEFF 30.0 1507: #define ESM_SPOT_SINGLE_BLUR_COEFF 0.25 1508: #define PCF_POISSON_SPOT_OMNI_BLUR_COEFF 4.0 1509: #define PCF_POISSON_POINT_BLUR_COEFF 2.5 1510: #define PCF_PCF_SOFT_DIR_SPOT_BLUR_COEFF 0.5 1511: #define ESM_BIAS_COEFF 100.0 1512: #ifndef ESM_DISTANCE_SCALE 1513: #define ESM_DISTANCE_SCALE 1.0 1514: #endif 1515: #ifdef USE_SHADOWMAP 1516: #if 0 > 0 1517: uniform sampler2D directionalShadowMap[0]; 1518: varying vec4 vDirectionalShadowCoord[0]; 1519: struct DirectionalLightShadow { 1520: float shadowBias; 1521: float shadowNormalBias; 1522: float shadowRadius; 1523: vec2 shadowMapSize; 1524: vec3 position; 1525: float shadowCameraNear; 1526: float maxDistance; 1527: float expBias; 1528: }; 1529: uniform DirectionalLightShadow directionalLightShadows[0]; 1530: #endif 1531: #if 0 > 0 1532: uniform sampler2D spotShadowMap[0]; 1533: varying vec4 vSpotShadowCoord[0]; 1534: struct SpotLightShadow { 1535: float shadowBias; 1536: float shadowNormalBias; 1537: float shadowRadius; 1538: vec2 shadowMapSize; 1539: int shadow; 1540: float shadowCameraNear; 1541: float shadowCameraFar; 1542: float expBias; 1543: }; 1544: uniform SpotLightShadow spotLightShadows[0]; 1545: #endif 1546: #if 1 > 0 1547: uniform sampler2D pointShadowMap[1]; 1548: varying vec4 vPointShadowCoord[1]; 1549: struct PointLightShadow { 1550: float shadowBias; 1551: float shadowNormalBias; 1552: float shadowRadius; 1553: vec2 shadowMapSize; 1554: float shadowCameraNear; 1555: float shadowCameraFar; 1556: float expBias; 1557: }; 1558: uniform PointLightShadow pointLightShadows[1]; 1559: #endif 1560: #if 0 > 0 1561: uniform sampler2D rectAreaShadowMap[0]; 1562: varying vec4 vRectAreaShadowCoord[0]; 1563: struct RectAreaLightShadow { 1564: float shadowBias; 1565: float shadowNormalBias; 1566: float shadowRadius; 1567: vec2 shadowMapSize; 1568: float shadowCameraNear; 1569: float shadowCameraFar; 1570: float expBias; 1571: }; 1572: uniform RectAreaLightShadow rectAreaLightShadows[0]; 1573: #endif 1574: const vec3 PERMUTE_DIR_X = vec3(1.0, 0.0, 0.0); 1575: const vec3 PERMUTE_DIR_Y = vec3(0.0, 1.0, 0.0); 1576: const vec3 PERMUTE_DIR_Z = vec3(0.0, 0.0, 1.0); 1577: const mat4 POISSON_DISK_0 = mat4( 1578: 0.954845, 0.242214, -0.623893, -0.235473, 1579: -0.173288, 0.799228, 0.605969, -0.548050, 1580: -0.560406, 0.327647, -0.448307, -0.774344, 1581: 0.308258, 0.417332, -0.125623, -0.056098 1582: ); 1583: const mat4 POISSON_DISK_1 = mat4( 1584: 0.145585, -0.305634, 0.264060, -0.661648, 1585: 0.617942, 0.652121, -0.041412, -0.893582, 1586: 0.463911, 0.039752, 0.212664, 0.810727, 1587: -0.955989, -0.014390, -0.652588, 0.671204 1588: ); 1589: float texture2DCompare(sampler2D depths, vec2 uv, float compare) { 1590: return step(compare, unpackRGBAToDepth(texture2D(depths, uv))); 1591: } 1592: vec2 texture2DDistribution(sampler2D shadow, vec2 uv) { 1593: return unpackRGBATo2Half(texture2D(shadow, uv)); 1594: } 1595: float VSMShadow (sampler2D shadow, vec2 uv, float compare){ 1596: float occlusion = 1.0; 1597: vec2 distribution = texture2DDistribution(shadow, uv); 1598: float hard_shadow = step(compare , distribution.x); 1599: if (hard_shadow != 1.0) { 1600: float distance = compare - distribution.x ; 1601: float variance = max(0.00000, distribution.y * distribution.y); 1602: float softness_probability = variance / (variance + distance * distance); 1603: softness_probability = clamp((softness_probability - 0.3) / (0.95 - 0.3), 0.0, 1.0); 1604: occlusion = clamp(max(hard_shadow, softness_probability), 0.0, 1.0); 1605: } 1606: return occlusion; 1607: } 1608: float texture2DShadowLerp(sampler2D depths, vec2 size, vec2 uv, float compare) { 1609: const vec2 offset = vec2(0.0, 1.0); 1610: vec2 texelSize = vec2(1.0) / size; 1611: vec2 centroidUV = floor(uv * size + 0.5) / size; 1612: float lb = texture2DCompare(depths, centroidUV + texelSize * offset.xx, compare); 1613: float lt = texture2DCompare(depths, centroidUV + texelSize * offset.xy, compare); 1614: float rb = texture2DCompare(depths, centroidUV + texelSize * offset.yx, compare); 1615: float rt = texture2DCompare(depths, centroidUV + texelSize * offset.yy, compare); 1616: vec2 f = fract(uv * size + 0.5); 1617: float a = mix(lb, lt, f.y); 1618: float b = mix(rb, rt, f.y); 1619: float c = mix(a, b, f.x); 1620: return c; 1621: } 1622: vec2 cubeToUV(vec3 v, float texelSizeY) { 1623: vec3 absV = abs(v); 1624: float scaleToCube = 1.0 / max(absV.x, max(absV.y, absV.z)); 1625: absV *= scaleToCube; 1626: v *= scaleToCube * (1.0 - 2.0 * texelSizeY); 1627: vec2 planar = v.xy; 1628: float almostATexel = 1.5 * texelSizeY; 1629: float almostOne = 1.0 - almostATexel; 1630: if (absV.z >= almostOne) { 1631: if (v.z > 0.0) 1632: planar.x = 4.0 - v.x; 1633: } else if (absV.x >= almostOne) { 1634: float signX = sign(v.x); 1635: planar.x = v.z * signX + 2.0 * signX; 1636: } else if (absV.y >= almostOne) { 1637: float signY = sign(v.y); 1638: planar.x = v.x + 2.0 * signY + 2.0; 1639: planar.y = v.z * signY - 2.0; 1640: } 1641: return vec2(0.125, 0.25) * planar + vec2(0.375, 0.75); 1642: } 1643: float texture2DShadowAvgCube(sampler2D depths, vec2 size, vec3 bd3D, float compare) { 1644: vec2 texelSize = vec2(1.0) / size; 1645: vec3 dirX = normalize(abs(bd3D.y) < 0.99999 ? vec3(bd3D.z, 0.0, -bd3D.x) 1646: : vec3(0.0, -bd3D.z, bd3D.y)); 1647: vec3 dirY = cross(bd3D, dirX); 1648: float theta = PI_HALF * texelSize.y; 1649: vec3 sX = sin(theta) * dirX; 1650: vec3 sY = sin(theta) * dirY; 1651: float cosT = cos(theta); 1652: vec3 sampleVec[4]; 1653: sampleVec[0] = bd3D; 1654: sampleVec[1] = bd3D * cosT + sY; 1655: sampleVec[2] = bd3D * cosT + sX; 1656: sampleVec[3] = sampleVec[2] * cosT + sY; 1657: float avg = 0.0; 1658: for (int i = 0; i < 4; i++) { 1659: avg += texture2DCompare(depths, cubeToUV(sampleVec[i], texelSize.y), compare); 1660: } 1661: avg /= 4.0; 1662: return avg; 1663: } 1664: float getShadow(sampler2D shadowMap, vec2 shadowMapSize, float shadowBias, float shadowRadius, vec4 shadowCoord, 1665: float expBias, float distWorld) { 1666: float shadow = 1.0; 1667: shadowCoord.xyz /= shadowCoord.w; 1668: bvec4 inFrustumVec = bvec4 (shadowCoord.x >= 0.0, shadowCoord.x <= 1.0, shadowCoord.y >= 0.0, shadowCoord.y <= 1.0); 1669: bool inFrustum = all(inFrustumVec); 1670: bvec3 frustumTestVec = bvec3(inFrustum, shadowCoord.z <= 1.0, shadowCoord.z >= 0.0); 1671: bool frustumTest = all(frustumTestVec); 1672: if (frustumTest) { 1673: #if defined(SHADOWMAP_TYPE_BILINEAR) 1674: shadowCoord.z += shadowBias; 1675: shadow = texture2DShadowLerp(shadowMap, shadowMapSize, 1676: shadowCoord.xy, shadowCoord.z); 1677: #elif defined(SHADOWMAP_TYPE_PCF) 1678: shadowCoord.z += shadowBias; 1679: vec2 texelSize = vec2(1.0) / shadowMapSize; 1680: float dx0 = - texelSize.x * shadowRadius; 1681: float dy0 = - texelSize.y * shadowRadius; 1682: float dx1 = + texelSize.x * shadowRadius; 1683: float dy1 = + texelSize.y * shadowRadius; 1684: vec2 offsetVec[9]; 1685: offsetVec[0] = vec2(dx0, dy0); 1686: offsetVec[1] = vec2(0.0, dy0); 1687: offsetVec[2] = vec2(dx1, dy0); 1688: offsetVec[3] = vec2(dx0, 0.0); 1689: offsetVec[4] = vec2(0.0); 1690: offsetVec[5] = vec2(dx1, 0.0); 1691: offsetVec[6] = vec2(dx0, dy1); 1692: offsetVec[7] = vec2(0.0, dy1); 1693: offsetVec[8] = vec2(dx1, dy1); 1694: shadow = 0.0; 1695: for (int i = 0; i < 9; i++) { 1696: shadow += texture2DCompare(shadowMap, shadowCoord.xy + offsetVec[i], shadowCoord.z); 1697: } 1698: shadow /= 9.0; 1699: #elif defined(SHADOWMAP_TYPE_PCF_SOFT) 1700: shadowCoord.z += shadowBias; 1701: vec2 texelSize = vec2(1.0) / shadowMapSize; 1702: float dx0 = - texelSize.x * shadowRadius; 1703: float dy0 = - texelSize.y * shadowRadius; 1704: float dx1 = + texelSize.x * shadowRadius; 1705: float dy1 = + texelSize.y * shadowRadius; 1706: vec2 offsetVec[9]; 1707: offsetVec[0] = vec2(dx0, dy0); 1708: offsetVec[1] = vec2(0.0, dy0); 1709: offsetVec[2] = vec2(dx1, dy0); 1710: offsetVec[3] = vec2(dx0, 0.0); 1711: offsetVec[4] = vec2(0.0); 1712: offsetVec[5] = vec2(dx1, 0.0); 1713: offsetVec[6] = vec2(dx0, dy1); 1714: offsetVec[7] = vec2(0.0, dy1); 1715: offsetVec[8] = vec2(dx1, dy1); 1716: shadow = 0.0; 1717: for (int i = 0; i < 9; i++) { 1718: shadow += texture2DShadowLerp(shadowMap, shadowMapSize, 1719: shadowCoord.xy + offsetVec[i], shadowCoord.z); 1720: } 1721: shadow /= 9.0; 1722: #elif defined(SHADOWMAP_TYPE_PCF_POISSON_DISK) 1723: shadowCoord.z += shadowBias; 1724: vec2 texelSize = vec2(1.0) / shadowMapSize; 1725: float randAngle = rand(gl_FragCoord.xy) * PI2; 1726: float c = cos(randAngle), s = sin(randAngle); 1727: mat2 sampleMat = mat2(c, s, -s, c) 1728: * mat2(shadowRadius * texelSize.x, 0.0, 0.0, shadowRadius * texelSize.y); 1729: vec2 sampleVec[16]; 1730: sampleVec[0] = POISSON_DISK_0[0].xy; 1731: sampleVec[1] = POISSON_DISK_0[0].zw; 1732: sampleVec[2] = POISSON_DISK_0[1].xy; 1733: sampleVec[3] = POISSON_DISK_0[1].zw; 1734: sampleVec[4] = POISSON_DISK_0[2].xy; 1735: sampleVec[5] = POISSON_DISK_0[2].zw; 1736: sampleVec[6] = POISSON_DISK_0[3].xy; 1737: sampleVec[7] = POISSON_DISK_0[3].zw; 1738: sampleVec[8] = POISSON_DISK_1[0].xy; 1739: sampleVec[9] = POISSON_DISK_1[0].zw; 1740: sampleVec[10] = POISSON_DISK_1[1].xy; 1741: sampleVec[11] = POISSON_DISK_1[1].zw; 1742: sampleVec[12] = POISSON_DISK_1[2].xy; 1743: sampleVec[13] = POISSON_DISK_1[2].zw; 1744: sampleVec[14] = POISSON_DISK_1[3].xy; 1745: sampleVec[15] = POISSON_DISK_1[3].zw; 1746: shadow = 0.0; 1747: for (int i = 0; i < 16; i++) { 1748: shadow += texture2DCompare(shadowMap, shadowCoord.xy 1749: + sampleMat * sampleVec[i], shadowCoord.z); 1750: } 1751: shadow /= 16.0; 1752: #elif defined(SHADOWMAP_TYPE_VSM) 1753: shadow = VSMShadow(shadowMap, shadowCoord.xy, shadowCoord.z); 1754: #elif defined(SHADOWMAP_TYPE_ESM) 1755: shadow = saturate(exp(expBias * (texture2D(shadowMap, shadowCoord.xy).x 1756: - length(distWorld) * ESM_DISTANCE_SCALE 1757: - ESM_BIAS_COEFF * shadowBias))); 1758: #else 1759: shadowCoord.z += shadowBias; 1760: shadow = texture2DCompare(shadowMap, shadowCoord.xy, shadowCoord.z); 1761: #endif 1762: } 1763: return shadow; 1764: } 1765: float getShadow(sampler2D shadowMap, vec2 shadowMapSize, float shadowBias, float shadowRadius, vec4 shadowCoord) { 1766: return getShadow(shadowMap, shadowMapSize, shadowBias, shadowRadius, shadowCoord, 0.0, 0.0); 1767: } 1768: float getOmniShadow(sampler2D shadowMap, vec2 shadowMapSize, float shadowBias, 1769: float expBias, float shadowRadius, vec4 shadowCoord, 1770: float shadowCameraNear, float shadowCameraFar) { 1771: float shadow = 1.0; 1772: vec3 lightToPosition = shadowCoord.xyz; 1773: float dp = (length(lightToPosition) - shadowCameraNear) 1774: / (shadowCameraFar - shadowCameraNear); 1775: bvec2 frustumTestVec = bvec2(dp <= 1.0, dp >= 0.0); 1776: bool frustumTest = all(frustumTestVec); 1777: if (frustumTest) { 1778: float biasScaleCoeff = BIAS_FRUSTUM_SCALE_COEFF 1779: / (shadowCameraFar - shadowCameraNear); 1780: dp += shadowBias * biasScaleCoeff; 1781: vec3 bd3D = normalize(lightToPosition); 1782: vec2 texelSize = 1.0 / shadowMapSize; 1783: #if defined(SHADOWMAP_TYPE_BILINEAR) 1784: shadow = texture2DShadowAvgCube(shadowMap, shadowMapSize, bd3D, dp); 1785: #elif defined(SHADOWMAP_TYPE_ESM) 1786: shadow = saturate(exp(expBias * (texture2D(shadowMap, 1787: cubeVecToOctUV(bd3D, texelSize)).x 1788: - length(lightToPosition) * ESM_DISTANCE_SCALE 1789: - ESM_BIAS_COEFF * shadowBias))); 1790: #elif defined(SHADOWMAP_TYPE_PCF) || defined(SHADOWMAP_TYPE_PCF_SOFT) 1791: vec2 offset = vec2(-1, 1) * shadowRadius * texelSize.y; 1792: vec3 offsetVec[9]; 1793: offsetVec[0] = offset.xyy; 1794: offsetVec[1] = offset.yyy; 1795: offsetVec[2] = offset.xyx; 1796: offsetVec[3] = offset.yyx; 1797: offsetVec[4] = vec3(0.0); 1798: offsetVec[5] = offset.xxy; 1799: offsetVec[6] = offset.yxy; 1800: offsetVec[7] = offset.xxx; 1801: offsetVec[8] = offset.yxx; 1802: shadow = 0.0; 1803: for (int i = 0; i < 9; i++) { 1804: shadow += texture2DCompare(shadowMap, cubeToUV(bd3D + offsetVec[i], texelSize.y), dp); 1805: } 1806: shadow /= 9.0; 1807: #elif defined(SHADOWMAP_TYPE_PCF_POISSON_DISK) 1808: float randAngle = rand(gl_FragCoord.xy) * PI2; 1809: float c = cos(randAngle), s = sin(randAngle); 1810: mat2 sampleMat = mat2(c, s, -s, c) 1811: * mat2(shadowRadius * texelSize.y, 0.0, 0.0, shadowRadius * texelSize.y); 1812: vec3 absBd3D = abs(bd3D); 1813: absBd3D /= max(absBd3D.x, max(absBd3D.y, absBd3D.z)); 1814: bvec2 isPointingCubeFace = greaterThan(absBd3D.xy, vec2(0.999)); 1815: mat3 permuteMat = mat3( 1816: isPointingCubeFace.x ? PERMUTE_DIR_Y : PERMUTE_DIR_X, 1817: isPointingCubeFace.x || isPointingCubeFace.y ? PERMUTE_DIR_Z : PERMUTE_DIR_Y, 1818: isPointingCubeFace.x ? PERMUTE_DIR_X : isPointingCubeFace.y ? PERMUTE_DIR_Y : PERMUTE_DIR_Z 1819: ); 1820: vec2 sampleVec[16]; 1821: sampleVec[0] = POISSON_DISK_0[0].xy; 1822: sampleVec[1] = POISSON_DISK_0[0].zw; 1823: sampleVec[2] = POISSON_DISK_0[1].xy; 1824: sampleVec[3] = POISSON_DISK_0[1].zw; 1825: sampleVec[4] = POISSON_DISK_0[2].xy; 1826: sampleVec[5] = POISSON_DISK_0[2].zw; 1827: sampleVec[6] = POISSON_DISK_0[3].xy; 1828: sampleVec[7] = POISSON_DISK_0[3].zw; 1829: sampleVec[8] = POISSON_DISK_1[0].xy; 1830: sampleVec[9] = POISSON_DISK_1[0].zw; 1831: sampleVec[10] = POISSON_DISK_1[1].xy; 1832: sampleVec[11] = POISSON_DISK_1[1].zw; 1833: sampleVec[12] = POISSON_DISK_1[2].xy; 1834: sampleVec[13] = POISSON_DISK_1[2].zw; 1835: sampleVec[14] = POISSON_DISK_1[3].xy; 1836: sampleVec[15] = POISSON_DISK_1[3].zw; 1837: shadow = 0.0; 1838: for (int i = 0; i < 16; i++) { 1839: shadow += texture2DCompare(shadowMap, cubeToUV(bd3D + permuteMat * vec3(sampleMat * sampleVec[i], 0.0), texelSize.y), dp); 1840: } 1841: shadow /= 16.0; 1842: #else 1843: shadow = texture2DCompare(shadowMap, cubeToUV(bd3D, texelSize.y), dp); 1844: #endif 1845: } 1846: return shadow; 1847: } 1848: #if 0 > 0 1849: float getDirShadow(DirectionalLightShadow light, sampler2D shadowMap, 1850: vec4 shadowCoord, float distWorld) { 1851: float shadowRadius = light.shadowRadius; 1852: #if defined(SHADOWMAP_TYPE_PCF) || defined(SHADOWMAP_TYPE_PCF_SOFT) 1853: shadowRadius *= PCF_PCF_SOFT_DIR_SPOT_BLUR_COEFF; 1854: #endif 1855: return getShadow(shadowMap, light.shadowMapSize, light.shadowBias, shadowRadius, shadowCoord, 1856: light.expBias, distWorld); 1857: } 1858: #endif 1859: #if 1 > 0 1860: float getPointShadow(PointLightShadow light, sampler2D shadowMap, vec4 shadowCoord) { 1861: float shadowRadius = light.shadowRadius; 1862: vec2 mapSize = light.shadowMapSize; 1863: #if defined(SHADOWMAP_TYPE_ESM) 1864: mapSize *= 2.0; 1865: #else 1866: mapSize *= vec2(4.0, 2.0); 1867: #if defined(SHADOWMAP_TYPE_PCF_POISSON_DISK) 1868: shadowRadius *= PCF_POISSON_POINT_BLUR_COEFF; 1869: #endif 1870: #endif 1871: return getOmniShadow(shadowMap, mapSize, light.shadowBias, light.expBias, 1872: shadowRadius, shadowCoord, light.shadowCameraNear, 1873: light.shadowCameraFar); 1874: } 1875: #endif 1876: #if 0 > 0 1877: float getRectAreaShadow(RectAreaLightShadow light, sampler2D shadowMap, vec4 shadowCoord) { 1878: float shadowRadius = light.shadowRadius; 1879: vec2 mapSize = light.shadowMapSize; 1880: #if defined(SHADOWMAP_TYPE_ESM) 1881: mapSize *= 2.0; 1882: #else 1883: mapSize *= vec2(4.0, 2.0); 1884: #if defined(SHADOWMAP_TYPE_PCF_POISSON_DISK) 1885: shadowRadius *= PCF_POISSON_POINT_BLUR_COEFF; 1886: #endif 1887: #endif 1888: return getOmniShadow(shadowMap, mapSize, light.shadowBias, light.expBias, 1889: shadowRadius, shadowCoord, light.shadowCameraNear, 1890: light.shadowCameraFar); 1891: } 1892: #endif 1893: #if 0 > 0 1894: float getSpotOmniShadow(SpotLightShadow light, sampler2D shadowMap, vec4 shadowCoord) { 1895: float shadowRadius = light.shadowRadius; 1896: vec2 mapSize = light.shadowMapSize; 1897: #if defined(SHADOWMAP_TYPE_ESM) 1898: #else 1899: mapSize *= vec2(4.0, 2.0); 1900: #if defined(SHADOWMAP_TYPE_PCF_POISSON_DISK) 1901: shadowRadius *= PCF_POISSON_SPOT_OMNI_BLUR_COEFF; 1902: #endif 1903: #endif 1904: return getOmniShadow(shadowMap, mapSize, light.shadowBias, light.expBias, 1905: shadowRadius, shadowCoord, light.shadowCameraNear, 1906: light.shadowCameraFar); 1907: } 1908: float biasLinearNormalizedToNonlinear(float bias, float near, float far, 1909: float projZ, float projW) { 1910: return (bias * (far + near) + 2.0 * projZ) / (bias * (far - near) + 2.0 * projW) 1911: - projZ / projW; 1912: } 1913: float getSpotShadow(SpotLightShadow light, sampler2D shadowMap, vec4 shadowCoord, 1914: float distWorld) { 1915: float shadowRadius = light.shadowRadius; 1916: float shadowBias = light.shadowBias; 1917: #if defined(SHADOWMAP_TYPE_PCF) || defined(SHADOWMAP_TYPE_PCF_SOFT) 1918: shadowRadius *= PCF_PCF_SOFT_DIR_SPOT_BLUR_COEFF; 1919: #elif defined(SHADOWMAP_TYPE_ESM) 1920: shadowRadius *= ESM_SPOT_SINGLE_BLUR_COEFF; 1921: #endif 1922: shadowBias *= BIAS_FRUSTUM_SCALE_COEFF 1923: / (light.shadowCameraFar - light.shadowCameraNear); 1924: #if defined(SHADOWMAP_TYPE_BASIC) || defined(SHADOWMAP_TYPE_BILINEAR) || defined(SHADOWMAP_TYPE_PCF) || defined(SHADOWMAP_TYPE_PCF_SOFT) || defined(SHADOWMAP_TYPE_PCF_POISSON_DISK) 1925: shadowBias = biasLinearNormalizedToNonlinear(shadowBias, 1926: light.shadowCameraNear, light.shadowCameraFar, shadowCoord.z, 1927: shadowCoord.w); 1928: #endif 1929: return getShadow(shadowMap, light.shadowMapSize, shadowBias, shadowRadius, shadowCoord, 1930: light.expBias, distWorld); 1931: } 1932: #endif 1933: #endif 1934: #if defined(USE_LOGDEPTHBUF) && defined(USE_LOGDEPTHBUF_EXT) 1935: uniform float logDepthBufFC; 1936: varying float vFragDepth; 1937: varying float vIsPerspective; 1938: #endif 1939: #if 0 > 0 1940: varying vec3 vClipPosition; 1941: uniform vec4 clippingPlanes[0]; 1942: #endif 1943: #if defined(NODE_NEW_GEOMETRY_BL) || defined(NODE_LAYER_WEIGHT_BL) || defined(NODE_FRESNEL_BL) || defined(NODE_BSDF_GLASS_BL) || defined(NODE_BSDF_PRINCIPLED_BL) || defined(USE_SSR) || (defined(NODE_NORMAL) && defined(MT_BLENDER)) || defined(USE_OSL) 1944: uniform mat4 projectionMatrix; 1945: #endif 1946: #if defined(NODE_VECT_TRANSFORM_BL) || defined(NODE_TEX_COORD_BL) || defined(NODE_NEW_GEOMETRY_BL) || defined(NODE_TANGENT_BL) || defined(NODE_BITMAP_MX) || defined(NODE_GRADIENT_RAMP_MX) || defined(NODE_NOISE_MX) || defined(NODE_SAMPLER_INFO_MY) || defined(NODE_TRANSFORM_MY) || defined(USE_OSL) 1947: uniform mat4 modelMatrix; 1948: uniform mat4 invModelMatrix; 1949: #endif 1950: #if defined(NODE_VECT_TRANSFORM_BL) || defined(NODE_NORMAL_MAP_BL) || defined(NODE_NORMAL_BUMP_MX) || defined(NODE_BUMP_2D_MY) || defined(NODE_SAMPLER_INFO_MY) 1951: uniform mat4 modelViewMatrix; 1952: #endif 1953: #if defined(NODE_TEX_IMAGE_BL) 1954: uniform mat3 normalMatrix; 1955: #endif 1956: #if defined(NODE_TEX_COORD_BL) || defined(NODE_NEW_GEOMETRY_BL) || defined(NODE_TANGENT_BL) 1957: uniform vec3 boundingBoxMin; 1958: uniform vec3 boundingBoxMax; 1959: #endif 1960: #if defined(NODE_REFLECT_REFRACT_MX) || defined(NODE_BITMAP_ENV_MX) || defined(NODE_BUMP_BL) || defined(NODE_PHY_SUN_SKY_ENV_MX) || defined(NODE_SKYDOME_LIGHT_AR) || defined(NODE_ENV_SPHERE_MY) 1961: varying vec3 vWorldPosition; 1962: #endif 1963: #if defined(NODE_TEX_COORD_BL) 1964: uniform vec2 viewWidthHeight; 1965: #endif 1966: #ifdef USE_SSR 1967: uniform sampler2D ssrSourceBuffer; 1968: uniform sampler2D ssrDepthBuffer; 1969: uniform sampler2D ssrBackfaceDepthBuffer; 1970: uniform mat4 invProjectionMatrix; 1971: uniform vec2 ssrResolution; 1972: uniform float ssrThickness; 1973: uniform float ssrStride; 1974: uniform float ssrJitter; 1975: uniform float ssrMaxDistance; 1976: #ifdef USE_SSR_REFRACT 1977: #define STEPS_FADE_AMOUNT 0.1 1978: #define SCREEN_FADE_THRESHOLD 0.6 1979: #else 1980: #define STEPS_FADE_AMOUNT 1.0 1981: #define SCREEN_FADE_THRESHOLD 0.4 1982: #endif 1983: vec3 deproject(vec3 p) { 1984: vec4 res = invProjectionMatrix * vec4(p, 1); 1985: return res.xyz / res.w; 1986: } 1987: bool doesIntersect(float rayzmax, float rayzmin, vec2 uv) { 1988: float sceneZMin = texture2D(ssrDepthBuffer, uv).r; 1989: #ifdef USE_SSR_REFRACT 1990: return rayzmin >= (sceneZMin-ssrThickness) && rayzmax <= sceneZMin; 1991: #else 1992: float sceneZMax = texture2D(ssrBackfaceDepthBuffer, uv).r; 1993: return rayzmin >= sceneZMax && rayzmax <= sceneZMin; 1994: #endif 1995: } 1996: float distanceSquared(vec2 a, vec2 b) { a -= b; return dot(a, a); } 1997: void swapIfBigger(inout float a, inout float b) { 1998: if (a > b) { 1999: float t = a; 2000: a = b; 2001: b = t; 2002: } 2003: } 2004: bool isOutsideUvBounds(float x) { return x < 0.0 || x > 1.0; } 2005: bool isOutsideUvBounds(vec2 uv) { return isOutsideUvBounds(uv.x) || isOutsideUvBounds(uv.y); } 2006: vec3 computeSSR(vec3 color, vec3 normal, float ior) { 2007: vec2 uv = gl_FragCoord.xy / ssrResolution; 2008: vec2 screenCoord = uv * 2.0 - vec2(1, 1); 2009: float nearClip = deproject(vec3(0, 0, -1)).z; 2010: vec3 ray = deproject(vec3(screenCoord, -1)); 2011: ray /= ray.z; 2012: float depthSample = -vViewPosition.z; 2013: vec3 vpos = depthSample * ray; 2014: #ifdef USE_SSR_REFRACT 2015: vec3 dir = normalize(refract(normalize(vpos), normalize(normal), 1.0/ior)); 2016: #else 2017: vec3 dir = normalize(reflect(normalize(vpos), normalize(normal))); 2018: #endif 2019: float maxDist = ssrMaxDistance; 2020: float rayLength = (vpos.z + dir.z * maxDist) > nearClip ? (nearClip - vpos.z) / dir.z : maxDist; 2021: vec3 csOrig = vpos; 2022: vec3 csEndPoint = csOrig + dir * rayLength; 2023: vec4 H0 = projectionMatrix * vec4(csOrig, 1.0); 2024: vec4 H1 = projectionMatrix * vec4(csEndPoint, 1.0); 2025: float k0 = 1.0 / H0.w, k1 = 1.0 / H1.w; 2026: vec3 Q0 = csOrig.xyz * k0, Q1 = csEndPoint.xyz * k1; 2027: vec2 P0 = H0.xy * k0, P1 = H1.xy * k1; 2028: P0 = P0 * 0.5 + vec2(0.5), P1 = P1 * 0.5 + vec2(0.5); 2029: #ifndef SSR_SIMPLE_REFRACT 2030: P0 *= ssrResolution, P1 *= ssrResolution; 2031: P1 += vec2((distanceSquared(P0, P1) < 0.0001) ? 0.01 : 0.0); 2032: vec2 delta = P1 - P0; 2033: bool permute = false; 2034: if (abs(delta.x) < abs(delta.y)) { 2035: permute = true; delta = delta.yx; P0 = P0.yx; P1 = P1.yx; 2036: } 2037: float stepDir = sign(delta.x); 2038: float invdx = stepDir / delta.x; 2039: vec3 dQ = (Q1 - Q0) * invdx; 2040: float dk = (k1 - k0) * invdx; 2041: vec2 dP = vec2(stepDir, delta.y * invdx); 2042: float pixelStride = ssrStride; 2043: float jitterMod = (gl_FragCoord.x + gl_FragCoord.y) * 0.25; 2044: vec4 PQK = vec4(P0, Q0.z, k0); 2045: vec4 dPQK = vec4(dP, dQ.z, dk); 2046: dPQK *= pixelStride; 2047: PQK += dPQK * mod(jitterMod, 1.0) * ssrJitter; 2048: float end = P1.x * stepDir; 2049: float prevZMaxEstimate = PQK.z / PQK.w; 2050: float rayZMin = prevZMaxEstimate, rayZMax = prevZMaxEstimate; 2051: float stepped = 0.0; 2052: vec2 hitUV; 2053: bool intersected = false; 2054: for (float stepCount = 1.0; stepCount <= float(MAX_STEPS); stepCount ++) { 2055: PQK += dPQK; 2056: rayZMin = prevZMaxEstimate; 2057: rayZMax = (dPQK.z * 0.5 + PQK.z) / (dPQK.w * 0.5 + PQK.w); 2058: prevZMaxEstimate = rayZMax; 2059: swapIfBigger(rayZMax, rayZMin); 2060: 2061: stepped = stepCount; 2062: hitUV = (permute ? PQK.yx: PQK.xy) / ssrResolution; 2063: if (isOutsideUvBounds(hitUV)) break; 2064: intersected = doesIntersect(rayZMax, rayZMin, hitUV); 2065: if (intersected || (P0.x * stepDir) > end) break; 2066: } 2067: if (intersected && pixelStride > 1.0) { 2068: PQK -= dPQK; 2069: dPQK /= ssrStride; 2070: float ogStride = pixelStride * 0.5; 2071: float currStride = pixelStride; 2072: prevZMaxEstimate = PQK.z / PQK.w; 2073: rayZMin = prevZMaxEstimate, rayZMax = prevZMaxEstimate; 2074: for(int j = 0; j < int(BINARY_SEARCH_ITERATIONS); j ++) { 2075: PQK += dPQK * currStride; 2076: rayZMin = prevZMaxEstimate; 2077: rayZMax = (dPQK.z * 0.5 + PQK.z) / (dPQK.w * 0.5 + PQK.w); 2078: prevZMaxEstimate = rayZMax; 2079: swapIfBigger(rayZMax, rayZMin); 2080: vec2 newUV = (permute ? PQK.yx: PQK.xy) / ssrResolution; 2081: ogStride *= 0.5; 2082: if (doesIntersect(rayZMax, rayZMin, newUV)) { 2083: hitUV = newUV; 2084: currStride = -ogStride; 2085: } else { 2086: currStride = ogStride; 2087: } 2088: } 2089: } 2090: vec3 result = color; 2091: #ifdef USE_SSR_REFRACT 2092: #endif 2093: if (intersected) { 2094: vec4 col = sRGBToLinear(texture2D(ssrSourceBuffer, hitUV)); 2095: vec2 ndc = abs(hitUV * 2.0 - 1.0); 2096: float maxndc = max(ndc.x, ndc.y); 2097: float fadeVal = 2098: (1.0 - (max(0.0, maxndc - SCREEN_FADE_THRESHOLD) / (1.0 - SCREEN_FADE_THRESHOLD))) * 2099: (1.0 - STEPS_FADE_AMOUNT * (stepped / float(MAX_STEPS))); 2100: col.a = fadeVal; 2101: result = mix(result, col.rgb, col.a); 2102: } 2103: #else 2104: vec3 result = sRGBToLinear(texture2D(ssrSourceBuffer, P1)).xyz; 2105: #endif 2106: return result; 2107: } 2108: #endif 2109: #define LUMENS_PER_WATT 683.0 2110: #define MAX_ENV_COORDS_DIR 0 2111: #define MAX_ENV_COORDS_REFLECT 1 2112: #define MAX_ENV_COORDS_REFRACT 2 2113: #define MAYA_LUM_VECTOR vec3(0.3, 0.59, 0.11) 2114: vec4 nodeTexelToLinear(in vec4 color, in int sourceType) { 2115: if (sourceType == 1) 2116: return sRGBToLinear(color); 2117: else if (sourceType == 2) 2118: return RGBEToLinear(color); 2119: else 2120: return color; 2121: } 2122: #if defined(NODE_COMBHSV_BL) || defined(NODE_SEPHSV_BL) || defined(NODE_HUE_SAT_BL) || defined(NODE_COLOR_CORRECTION_MX) || defined(NODE_COMPOSITE_LAYER_MX) || defined(USE_OSL) 2123: #define HSV_NODES 2124: #endif 2125: #if defined HSV_NODES 2126: void hsvToRGB(vec4 hsv, out vec4 outCol) 2127: { 2128: float i, f, p, q, t, h, s, v; 2129: vec3 rgb; 2130: h = hsv[0]; 2131: s = hsv[1]; 2132: v = hsv[2]; 2133: if (s == 0.0) 2134: rgb = vec3(v, v, v); 2135: else { 2136: if (h == 1.0) 2137: h = 0.0; 2138: h *= 6.0; 2139: i = floor(h); 2140: f = h - i; 2141: rgb = vec3(f, f, f); 2142: p = v * (1.0 - s); 2143: q = v * (1.0 - (s * f)); 2144: t = v * (1.0 - (s * (1.0 - f))); 2145: if (i == 0.0) 2146: rgb = vec3(v, t, p); 2147: else if (i == 1.0) 2148: rgb = vec3(q, v, p); 2149: else if (i == 2.0) 2150: rgb = vec3(p, v, t); 2151: else if (i == 3.0) 2152: rgb = vec3(p, q, v); 2153: else if (i == 4.0) 2154: rgb = vec3(t, p, v); 2155: else 2156: rgb = vec3(v, p, q); 2157: } 2158: outCol = vec4(rgb, hsv.w); 2159: } 2160: void rgbToHSV(vec4 rgb, out vec4 outCol) 2161: { 2162: float cmax, cmin, h, s, v, cdelta; 2163: vec3 c; 2164: cmax = max(rgb[0], max(rgb[1], rgb[2])); 2165: cmin = min(rgb[0], min(rgb[1], rgb[2])); 2166: cdelta = cmax - cmin; 2167: v = cmax; 2168: if (cmax != 0.0) 2169: s = cdelta / cmax; 2170: else { 2171: s = 0.0; 2172: h = 0.0; 2173: } 2174: if (s == 0.0) 2175: h = 0.0; 2176: else { 2177: c = (vec3(cmax, cmax, cmax) - rgb.xyz) / cdelta; 2178: if (rgb.x == cmax) h = c[2] - c[1]; 2179: else if (rgb.y == cmax) h = 2.0 + c[0] - c[2]; 2180: else h = 4.0 + c[1] - c[0]; 2181: h /= 6.0; 2182: if (h < 0.0) 2183: h += 1.0; 2184: } 2185: outCol = vec4(h, s, v, rgb.w); 2186: } 2187: #endif 2188: bool isPerspective(const mat4 projMatrix) 2189: { 2190: return (projMatrix[3][3] == 0.0); 2191: } 2192: #if defined(NODE_REFLECT_REFRACT_MX) || defined(NODE_BITMAP_ENV_MX) || defined(NODE_SKYDOME_LIGHT_AR) || defined(NODE_ENV_SPHERE_MY) 2193: vec4 sampleEquirectangular(sampler2D map, vec3 reflectVec, mat3 uvTransform, int encoding) 2194: { 2195: reflectVec = normalize(reflectVec); 2196: vec2 sampleUV; 2197: sampleUV.y = asin(clamp(reflectVec.y, - 1.0, 1.0)) * RECIPROCAL_PI + 0.5; 2198: sampleUV.x = atan(reflectVec.x, reflectVec.z) * RECIPROCAL_PI2 + 0.5; 2199: sampleUV.y *= -1.0; 2200: const float seamWidth = 0.15; 2201: const float seamBiasFactor = -10.0; 2202: float seam = max(0.0, 1.0 - abs (reflectVec.x) / seamWidth) * 2203: clamp (1.0 - reflectVec.z / seamWidth, 0.0, 1.0); 2204: sampleUV = (uvTransform * vec3(sampleUV, 1.0)).xy; 2205: vec4 color = texture2D(map, sampleUV, seamBiasFactor * seam); 2206: color = nodeTexelToLinear(color, encoding); 2207: return color; 2208: } 2209: #endif 2210: #if defined(NODE_FRESNEL_BL) || defined(NODE_LAYER_WEIGHT_BL) || defined(NODE_FALLOFF_MX) || defined(NODE_BSDF_GLASS_BL) || defined(NODE_BSDF_PRINCIPLED_BL) 2211: float fresnelReflection(const vec3 dir, const vec3 normal, const float ior) { 2212: float cosTheta = clamp(abs(dot(dir, normal)), -1.0, 1.0); 2213: float gSquared = pow2(ior) + pow2(cosTheta) - 1.0; 2214: if (gSquared < 0.0) return 1.0; 2215: float g = sqrt(gSquared); 2216: return 0.5 * pow2((g - cosTheta) / (g + cosTheta)) 2217: * (1.0 + pow2( 2218: ((g + cosTheta) * cosTheta - 1.0) / 2219: ((g - cosTheta) * cosTheta + 1.0) 2220: )); 2221: } 2222: #endif 2223: #if defined(NODE_BITMAP_MX) || defined(NODE_BITMAP_ENV_MX) || defined(NODE_GRADIENT_RAMP_MX) 2224: #define MAPPING_EXPLICIT_MAP_CHANNEL 1 2225: #define MAPPING_VERTEX_COLOR_CHANNEL 2 2226: #define MAPPING_PLANAR_OBJECT_XYZ 3 2227: #define MAPPING_PLANAR_WORLD_XYZ 4 2228: #define AXIS_XY 1 2229: #define AXIS_YZ 2 2230: #define AXIS_ZX 3 2231: #endif 2232: #if defined(NODE_BITMAP_MX) || defined(NODE_BITMAP_ENV_MX) || defined(NODE_BUMP_MX) || defined(NODE_GRADIENT_RAMP_MX) || defined(NODE_PLACE_2D_TEXTURE_MY) 2233: mat3 calcUvTransform(float uOffset, float vOffset, float uTiling, float vTiling, float wAngle) 2234: { 2235: if (abs(uOffset) < EPSILON && abs(vOffset) < EPSILON && 2236: (abs(uTiling - 1.0)) < EPSILON && (abs(vTiling - 1.0)) < EPSILON && 2237: abs(wAngle) < EPSILON) 2238: return mat3(1.0); 2239: float sx = uTiling; 2240: float sy = vTiling; 2241: float c = cos(-wAngle); 2242: float s = sin(-wAngle); 2243: #if defined(NODE_PLACE_2D_TEXTURE_MY) 2244: float tx = uOffset; 2245: float ty = vOffset; 2246: float cx = 0.5; 2247: float cy = 0.5; 2248: return mat3(c*sx, s*sx, 0.0, 2249: -s*sy, c*sy, 0.0, 2250: s*(ty+sy-cy)+c*(tx-cx)+cx, -c*(ty+sy-cy)+s*(tx-cx)-cy+1.0, 1.0); 2251: #else 2252: float tx = -uOffset; 2253: float ty = -vOffset; 2254: float cx = uOffset + 0.5; 2255: float cy = vOffset + 0.5; 2256: return mat3(sx * c, -sy * s, 0.0, 2257: sx * s, sy * c, 0.0, 2258: -sx * (c * cx + s * cy) + cx + tx, -sy * (- s * cx + c * cy) + cy + ty, 1.0); 2259: #endif 2260: } 2261: #endif 2262: #if defined(NODE_NOISE_MX) 2263: mat4 calcXYZTransform(vec3 offset, vec3 tiling, vec3 angle) { 2264: mat4 rot = mat4( 2265: cos(angle.y)*cos(angle.z), cos(angle.x)*sin(angle.z)+sin(angle.x)*sin(angle.y)*cos(angle.z), sin(angle.x)*sin(angle.z)-cos(angle.x)*sin(angle.y)*cos(angle.z), 0.0, 2266: -cos(angle.y)*sin(angle.z), cos(angle.x)*cos(angle.z)-sin(angle.x)*sin(angle.y)*sin(angle.z), cos(angle.x)*sin(angle.y)*sin(angle.z)+sin(angle.x)*cos(angle.z), 0.0, 2267: sin(angle.y), -sin(angle.x)*cos(angle.y), cos(angle.x)*cos(angle.y), 0.0, 2268: 0.0, 0.0, 0.0, 1.0 2269: ); 2270: mat4 til = mat4( 2271: tiling.x, 0.0, 0.0, 0.0, 2272: 0.0, tiling.y, 0.0, 0.0, 2273: 0.0, 0.0, tiling.z, 0.0, 2274: 0.0, 0.0, 0.0, 1.0 2275: ); 2276: mat4 off = mat4( 2277: 1.0, 0.0, 0.0, 0.0, 2278: 0.0, 1.0, 0.0, 0.0, 2279: 0.0, 0.0, 1.0, 0.0, 2280: offset.x, offset.y, offset.z, 1.0 2281: ); 2282: return (til * rot * off); 2283: } 2284: #endif 2285: #if defined(NODE_TEX_NOISE_BL) || defined(NODE_TEX_WAVE_BL) || defined(NODE_NOISE_MX) || defined(USE_OSL) || defined(NODE_NOISE_MY) 2286: #define NOISE_AMP_HACK 0.75 2287: #define NOISE_BLENDER_MEAN 0.78 2288: #define NOISE_SCALE_HACK 0.5 2289: #define noiseModulo(x) (x - floor(x * (1.0 / 289.0)) * 289.0) 2290: vec4 noisePermute(vec4 x) { 2291: return noiseModulo(((x * 34.0) + 1.0) * x); 2292: } 2293: vec4 taylorInvSqrt(vec4 r) { 2294: return 1.79284291400159 - 0.85373472095314 * r; 2295: } 2296: float taylorInvSqrt(float r) { 2297: return 1.79284291400159 - 0.85373472095314 * r; 2298: } 2299: float noisePerlin(vec3 v) { 2300: const vec2 C = vec2(1.0 / 6.0, 1.0 / 3.0); 2301: const vec4 D = vec4(0.0, 0.5, 1.0, 2.0); 2302: vec3 i = floor(v + dot(v, C.yyy)); 2303: vec3 x0 = v - i + dot(i, C.xxx); 2304: vec3 g = step(x0.yzx, x0.xyz); 2305: vec3 l = 1.0 - g; 2306: vec3 i1 = min(g.xyz, l.zxy); 2307: vec3 i2 = max(g.xyz, l.zxy); 2308: vec3 x1 = x0 - i1 + C.xxx; 2309: vec3 x2 = x0 - i2 + C.yyy; 2310: vec3 x3 = x0 - D.yyy; 2311: i = noiseModulo(i); 2312: vec4 p = noisePermute(noisePermute(noisePermute(i.z + vec4(0.0, i1.z, i2.z, 1.0)) 2313: + i.y + vec4(0.0, i1.y, i2.y, 1.0)) + i.x + vec4(0.0, i1.x, i2.x, 1.0)); 2314: float n_ = 0.142857142857; 2315: vec3 ns = n_ * D.wyz - D.xzx; 2316: vec4 j = p - 49.0 * floor(p * ns.z * ns.z); 2317: vec4 x_ = floor(j * ns.z); 2318: vec4 y_ = floor(j - 7.0 * x_); 2319: vec4 x = x_ * ns.x + ns.yyyy; 2320: vec4 y = y_ * ns.x + ns.yyyy; 2321: vec4 h = 1.0 - abs(x) - abs(y); 2322: vec4 b0 = vec4(x.xy, y.xy); 2323: vec4 b1 = vec4(x.zw, y.zw); 2324: vec4 s0 = floor(b0) * 2.0 + 1.0; 2325: vec4 s1 = floor(b1) * 2.0 + 1.0; 2326: vec4 sh = -step(h, vec4(0.0)); 2327: vec4 a0 = b0.xzyw + s0.xzyw * sh.xxyy; 2328: vec4 a1 = b1.xzyw + s1.xzyw * sh.zzww; 2329: vec3 p0 = vec3(a0.xy, h.x); 2330: vec3 p1 = vec3(a0.zw, h.y); 2331: vec3 p2 = vec3(a1.xy, h.z); 2332: vec3 p3 = vec3(a1.zw, h.w); 2333: vec4 norm = taylorInvSqrt(vec4(dot(p0, p0), dot(p1, p1), dot(p2, p2), dot(p3, p3))); 2334: p0 *= norm.x; 2335: p1 *= norm.y; 2336: p2 *= norm.z; 2337: p3 *= norm.w; 2338: vec4 m = max(0.6 - vec4(dot(x0, x0), dot(x1, x1), dot(x2, x2), dot(x3, x3)), 0.0); 2339: m = m * m; 2340: return 42.0 * dot(m * m, vec4(dot(p0, x0), dot(p1, x1), 2341: dot(p2, x2), dot(p3, x3))); 2342: } 2343: vec4 permute(vec4 x) { 2344: return mod(((x*34.0)+1.0)*x, 289.0); 2345: } 2346: float permute(float x) { 2347: return floor(mod(((x*34.0)+1.0)*x, 289.0)); 2348: } 2349: vec4 grad4(float j, vec4 ip) { 2350: const vec4 ones = vec4(1.0, 1.0, 1.0, -1.0); 2351: vec4 p,s; 2352: p.xyz = floor( fract (vec3(j) * ip.xyz) * 7.0) * ip.z - 1.0; 2353: p.w = 1.5 - dot(abs(p.xyz), ones.xyz); 2354: s = vec4(lessThan(p, vec4(0.0))); 2355: p.xyz = p.xyz + (s.xyz*2.0 - 1.0) * s.www; 2356: return p; 2357: } 2358: float snoise(vec4 v) { 2359: const vec2 C = vec2(0.138196601125010504, 2360: 0.309016994374947451); 2361: vec4 i = floor(v + dot(v, C.yyyy) ); 2362: vec4 x0 = v - i + dot(i, C.xxxx); 2363: vec4 i0; 2364: vec3 isX = step( x0.yzw, x0.xxx ); 2365: vec3 isYZ = step( x0.zww, x0.yyz ); 2366: i0.x = isX.x + isX.y + isX.z; 2367: i0.yzw = 1.0 - isX; 2368: i0.y += isYZ.x + isYZ.y; 2369: i0.zw += 1.0 - isYZ.xy; 2370: i0.z += isYZ.z; 2371: i0.w += 1.0 - isYZ.z; 2372: vec4 i3 = clamp( i0, 0.0, 1.0 ); 2373: vec4 i2 = clamp( i0-1.0, 0.0, 1.0 ); 2374: vec4 i1 = clamp( i0-2.0, 0.0, 1.0 ); 2375: vec4 x1 = x0 - i1 + 1.0 * C.xxxx; 2376: vec4 x2 = x0 - i2 + 2.0 * C.xxxx; 2377: vec4 x3 = x0 - i3 + 3.0 * C.xxxx; 2378: vec4 x4 = x0 - 1.0 + 4.0 * C.xxxx; 2379: i = mod(i, 289.0); 2380: float j0 = permute( permute( permute( permute(i.w) + i.z) + i.y) + i.x); 2381: vec4 j1 = permute( permute( permute( permute ( 2382: i.w + vec4(i1.w, i2.w, i3.w, 1.0 )) 2383: + i.z + vec4(i1.z, i2.z, i3.z, 1.0 )) 2384: + i.y + vec4(i1.y, i2.y, i3.y, 1.0 )) 2385: + i.x + vec4(i1.x, i2.x, i3.x, 1.0 )); 2386: vec4 ip = vec4(1.0/294.0, 1.0/49.0, 1.0/7.0, 0.0) ; 2387: vec4 p0 = grad4(j0, ip); 2388: vec4 p1 = grad4(j1.x, ip); 2389: vec4 p2 = grad4(j1.y, ip); 2390: vec4 p3 = grad4(j1.z, ip); 2391: vec4 p4 = grad4(j1.w, ip); 2392: vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3))); 2393: p0 *= norm.x; 2394: p1 *= norm.y; 2395: p2 *= norm.z; 2396: p3 *= norm.w; 2397: p4 *= taylorInvSqrt(dot(p4,p4)); 2398: vec3 m0 = max(0.6 - vec3(dot(x0,x0), dot(x1,x1), dot(x2,x2)), 0.0); 2399: vec2 m1 = max(0.6 - vec2(dot(x3,x3), dot(x4,x4) ), 0.0); 2400: m0 = m0 * m0; 2401: m1 = m1 * m1; 2402: return 49.0 * ( dot(m0*m0, vec3( dot( p0, x0 ), dot( p1, x1 ), dot( p2, x2 ))) 2403: + dot(m1*m1, vec2( dot( p3, x3 ), dot( p4, x4 ) ) ) ) ; 2404: } 2405: float noiseBlender(vec3 p) { 2406: return 0.5 * NOISE_AMP_HACK * (noisePerlin(NOISE_SCALE_HACK * vec3(p.x, p.y, p.z))) + 0.5; 2407: } 2408: float noiseSmooth(vec3 p, float octaveLenPerPixel, float falloffFactor, 2409: float dispersionFactor) { 2410: float mixFac = mix(1.0, smoothstep(0.0, 1.0, octaveLenPerPixel) * falloffFactor, 2411: dispersionFactor); 2412: return mix(noiseBlender(p), NOISE_BLENDER_MEAN, mixFac); 2413: } 2414: #define MAX_OCTAVES_NUM 16 2415: float noiseTurbulence(vec3 p, float octaves, float octaveLenPerPixel, 2416: float falloffFactor, float dispersionFactor) { 2417: float fscale = 1.0; 2418: float amp = 1.0; 2419: float sum = 0.0; 2420: octaves = clamp(octaves, 0.0, 16.0); 2421: int octavesInt = int(octaves); 2422: for (int i = 0; i <= MAX_OCTAVES_NUM; i++) { 2423: #if __VERSION__ == 300 2424: if (i <= octavesInt) { 2425: float t = noiseSmooth(fscale * p, octaveLenPerPixel, falloffFactor, 2426: dispersionFactor); 2427: sum += t * amp; 2428: amp *= 0.5; 2429: fscale *= 2.0; 2430: octaveLenPerPixel *= 2.0; 2431: } else { 2432: i = MAX_OCTAVES_NUM; 2433: } 2434: #else 2435: if (i > octavesInt) break; 2436: float t = noiseSmooth(fscale * p, octaveLenPerPixel, falloffFactor, 2437: dispersionFactor); 2438: sum += t * amp; 2439: amp *= 0.5; 2440: fscale *= 2.0; 2441: octaveLenPerPixel *= 2.0; 2442: #endif 2443: } 2444: float octavesFrac = fract(octaves); 2445: float octavesCoeff = pow(2.0, float(octavesInt)); 2446: if (octavesFrac != 0.0) { 2447: float t = noiseSmooth(fscale * p, octaveLenPerPixel, falloffFactor, 2448: dispersionFactor); 2449: float sum2 = sum + t * amp; 2450: sum *= octavesCoeff / (2.0 * octavesCoeff - 1.0); 2451: sum2 *= 2.0 * octavesCoeff / (4.0 * octavesCoeff - 1.0); 2452: return mix(sum, sum2, octavesFrac); 2453: } else { 2454: return sum * octavesCoeff / (2.0 * octavesCoeff - 1.0); 2455: } 2456: } 2457: #endif 2458: #if (defined(USE_OSL) || defined(NODE_WAVELENGTH_BL)) && __VERSION__ == 300 2459: vec3 cieColorMatch[81] = vec3[]( 2460: vec3(0.0014,0.0000,0.0065), vec3(0.0022,0.0001,0.0105), vec3(0.0042,0.0001,0.0201), 2461: vec3(0.0076,0.0002,0.0362), vec3(0.0143,0.0004,0.0679), vec3(0.0232,0.0006,0.1102), 2462: vec3(0.0435,0.0012,0.2074), vec3(0.0776,0.0022,0.3713), vec3(0.1344,0.0040,0.6456), 2463: vec3(0.2148,0.0073,1.0391), vec3(0.2839,0.0116,1.3856), vec3(0.3285,0.0168,1.6230), 2464: vec3(0.3483,0.0230,1.7471), vec3(0.3481,0.0298,1.7826), vec3(0.3362,0.0380,1.7721), 2465: vec3(0.3187,0.0480,1.7441), vec3(0.2908,0.0600,1.6692), vec3(0.2511,0.0739,1.5281), 2466: vec3(0.1954,0.0910,1.2876), vec3(0.1421,0.1126,1.0419), vec3(0.0956,0.1390,0.8130), 2467: vec3(0.0580,0.1693,0.6162), vec3(0.0320,0.2080,0.4652), vec3(0.0147,0.2586,0.3533), 2468: vec3(0.0049,0.3230,0.2720), vec3(0.0024,0.4073,0.2123), vec3(0.0093,0.5030,0.1582), 2469: vec3(0.0291,0.6082,0.1117), vec3(0.0633,0.7100,0.0782), vec3(0.1096,0.7932,0.0573), 2470: vec3(0.1655,0.8620,0.0422), vec3(0.2257,0.9149,0.0298), vec3(0.2904,0.9540,0.0203), 2471: vec3(0.3597,0.9803,0.0134), vec3(0.4334,0.9950,0.0087), vec3(0.5121,1.0000,0.0057), 2472: vec3(0.5945,0.9950,0.0039), vec3(0.6784,0.9786,0.0027), vec3(0.7621,0.9520,0.0021), 2473: vec3(0.8425,0.9154,0.0018), vec3(0.9163,0.8700,0.0017), vec3(0.9786,0.8163,0.0014), 2474: vec3(1.0263,0.7570,0.0011), vec3(1.0567,0.6949,0.0010), vec3(1.0622,0.6310,0.0008), 2475: vec3(1.0456,0.5668,0.0006), vec3(1.0026,0.5030,0.0003), vec3(0.9384,0.4412,0.0002), 2476: vec3(0.8544,0.3810,0.0002), vec3(0.7514,0.3210,0.0001), vec3(0.6424,0.2650,0.0000), 2477: vec3(0.5419,0.2170,0.0000), vec3(0.4479,0.1750,0.0000), vec3(0.3608,0.1382,0.0000), 2478: vec3(0.2835,0.1070,0.0000), vec3(0.2187,0.0816,0.0000), vec3(0.1649,0.0610,0.0000), 2479: vec3(0.1212,0.0446,0.0000), vec3(0.0874,0.0320,0.0000), vec3(0.0636,0.0232,0.0000), 2480: vec3(0.0468,0.0170,0.0000), vec3(0.0329,0.0119,0.0000), vec3(0.0227,0.0082,0.0000), 2481: vec3(0.0158,0.0057,0.0000), vec3(0.0114,0.0041,0.0000), vec3(0.0081,0.0029,0.0000), 2482: vec3(0.0058,0.0021,0.0000), vec3(0.0041,0.0015,0.0000), vec3(0.0029,0.0010,0.0000), 2483: vec3(0.0020,0.0007,0.0000), vec3(0.0014,0.0005,0.0000), vec3(0.0010,0.0004,0.0000), 2484: vec3(0.0007,0.0002,0.0000), vec3(0.0005,0.0002,0.0000), vec3(0.0003,0.0001,0.0000), 2485: vec3(0.0002,0.0001,0.0000), vec3(0.0002,0.0001,0.0000), vec3(0.0001,0.0000,0.0000), 2486: vec3(0.0001,0.0000,0.0000), vec3(0.0001,0.0000,0.0000), vec3(0.0000,0.0000,0.0000) 2487: ); 2488: #endif 2489: #if defined(USE_OSL) || defined(NODE_BLACKBODY_BL) 2490: vec3 colorTempToRGB(float temp) 2491: { 2492: vec3 retColor; 2493: temp = clamp(temp, 100.0, 40000.0) / 100.0; 2494: if (temp <= 66.0) { 2495: retColor.r = 1.0; 2496: retColor.g = saturate(0.390081578 * log(temp) - 0.631841443); 2497: } else { 2498: float t = temp - 60.0; 2499: retColor.r = saturate(1.292936186 * pow(t, -0.133204759)); 2500: retColor.g = saturate(1.129890860 * pow(t, -0.075514849)); 2501: } 2502: if (temp >= 66.0) 2503: retColor.b = 1.0; 2504: else if (temp <= 19.0) 2505: retColor.b = 0.0; 2506: else 2507: retColor.b = saturate(0.543206789 * log(temp - 10.0) - 1.196254089); 2508: return retColor; 2509: } 2510: #endif 2511: #ifdef USE_OSL 2512: #define M_PI PI 2513: #define M_PI_2 PI / 2.0 2514: #define M_PI_4 PI / 4.0 2515: #define M_2_PI 2.0 / PI 2516: #define M_2PI 2.0 * PI 2517: #define M_4PI 4.0 * PI 2518: #define M_2_SQRTPI 2.0 / sqrt(PI) 2519: #define M_E 2.718281828459 2520: #define M_LN2 0.69314718056 2521: #define M_LN10 2.30258509299 2522: #define M_LOG2E 1.4426950409 2523: #define M_LOG10E 0.43429448190 2524: #define M_SQRT2 sqrt(2.0) 2525: #define M_SQRT1_2 sqrt(0.5) 2526: #define OSL_ALPHA 33633 2527: #define OSL_ANISOTROPIC 40205 2528: #define OSL_AVERAGEALPHA 57701 2529: #define OSL_AVERAGECOLOR 46077 2530: #define OSL_BANDWIDTH 37485 2531: #define OSL_BEZIER 27645 2532: #define OSL_BLACK 62409 2533: #define OSL_BSPLINE 16959 2534: #define OSL_CAMERA 8198 2535: #define OSL_CAMERA_CLIP 34380 2536: #define OSL_CAMERA_CLIP_FAR 31933 2537: #define OSL_CAMERA_CLIP_NEAR 21485 2538: #define OSL_CAMERA_FOV 60706 2539: #define OSL_CAMERA_PIXELASPECT 4950 2540: #define OSL_CAMERA_PROJECTION 29369 2541: #define OSL_CAMERA_RESOLUTION 39679 2542: #define OSL_CAMERA_SCREEN_WINDOW 47009 2543: #define OSL_CAMERA_SHUTTER 7107 2544: #define OSL_CAMERA_SHUTTER_CLOSE 34406 2545: #define OSL_CAMERA_SHUTTER_OPEN 40085 2546: #define OSL_CATMULL_ROM 31642 2547: #define OSL_CELL 20984 2548: #define OSL_CHANNELS 8726 2549: #define OSL_CLAMP 20052 2550: #define OSL_COLOR 53753 2551: #define OSL_COMMON 49871 2552: #define OSL_CONSTANT 25144 2553: #define OSL_DATAWINDOW 54276 2554: #define OSL_DEFAULT 54870 2555: #define OSL_DIFFUSE 40389 2556: #define OSL_DIRECTION 42220 2557: #define OSL_DISPLAYWINDOW 30728 2558: #define OSL_DISTANCE 51337 2559: #define OSL_DO_FILTER 35765 2560: #define OSL_EMPTY 9314 2561: #define OSL_ERRORMESSAGE 38305 2562: #define OSL_EXISTS 41510 2563: #define OSL_FILL 39132 2564: #define OSL_FIRSTCHANNEL 48155 2565: #define OSL_GABOR 57764 2566: #define OSL_GEOM_NAME 63686 2567: #define OSL_GLOSSY 47998 2568: #define OSL_HASH 49390 2569: #define OSL_HERMITE 63643 2570: #define OSL_HIT 48491 2571: #define OSL_HITDIST 22029 2572: #define OSL_HSL 49898 2573: #define OSL_HSV 29073 2574: #define OSL_IMPULSES 56191 2575: #define OSL_INDEX 1731 2576: #define OSL_INTERP 10557 2577: #define OSL_LINEAR 2182 2578: #define OSL_MIRROR 64591 2579: #define OSL_MISSINGALPHA 39755 2580: #define OSL_MISSINGCOLOR 51667 2581: #define OSL_NDC 48899 2582: #define OSL_NORMAL 16520 2583: #define OSL_OBJECT 59084 2584: #define OSL_OSL_VERSION 47920 2585: #define OSL_PERIODIC 8749 2586: #define OSL_PERLIN 730 2587: #define OSL_POSITION 43041 2588: #define OSL_RASTER 2618 2589: #define OSL_REFLECTION 37621 2590: #define OSL_REFRACTION 37287 2591: #define OSL_RESOLUTION 48704 2592: #define OSL_RGB 26673 2593: #define OSL_RWRAP 47801 2594: #define OSL_SCREEN 55875 2595: #define OSL_SHADER 21066 2596: #define OSL_SHADER_GROUPNAME 62327 2597: #define OSL_SHADER_LAYERNAME 51796 2598: #define OSL_SHADER_SHADERNAME 65123 2599: #define OSL_SHADOW 60708 2600: #define OSL_SIMPLEX 61636 2601: #define OSL_SUBIMAGE 33526 2602: #define OSL_SUBIMAGES 2366 2603: #define OSL_SWRAP 4328 2604: #define OSL_TEXTUREFORMAT 17851 2605: #define OSL_TIME 52235 2606: #define OSL_TRACE 62908 2607: #define OSL_TWRAP 30524 2608: #define OSL_TYPE 64071 2609: #define OSL_UPERLIN 65308 2610: #define OSL_USIMPLEX 11314 2611: #define OSL_WIDTH 48751 2612: #define OSL_WORLD 9059 2613: #define OSL_WORLDTOCAMERA 32273 2614: #define OSL_WORLDTOSCREEN 33876 2615: #define OSL_WRAP 58300 2616: #define OSL_XYY 2228 2617: #define OSL_XYZ 47351 2618: #define OSL_YIQ 15839 2619: vec3 oslGetP(vec3 viewPos) { 2620: #if WORLD_NODES == 1 2621: return swizzleUpZ((invViewMatrix * vec4(-viewPos, 0.0)).xyz); 2622: #else 2623: return swizzleUpZ((invViewMatrix * vec4(-viewPos, 1.0)).xyz); 2624: #endif 2625: } 2626: vec3 oslGetI(vec3 viewPos) { 2627: #if WORLD_NODES == 1 2628: return swizzleUpZ((invViewMatrix * vec4(normalize(-viewPos), 0.0)).xyz); 2629: #else 2630: return swizzleUpZ((invViewMatrix * vec4(normalize(-viewPos), 0.0)).xyz); 2631: #endif 2632: } 2633: vec3 oslGetN(vec3 viewNorm) { 2634: return swizzleUpZ(normalize(invViewMatrix * vec4(viewNorm, 0.0)).xyz); 2635: } 2636: vec3 oslBlackbody(float temperatureK) { 2637: return sRGBToLinear(vec4(colorTempToRGB(temperatureK), 1.0)).rgb; 2638: } 2639: float oslDistance(vec3 p0, vec3 p1) { 2640: return distance(p0, p1); 2641: } 2642: float oslDistance(vec3 p0, vec3 p1, vec3 q) { 2643: vec3 d = p1 - p0; 2644: float dd = dot(d, d); 2645: if (dd == 0.0) 2646: return distance(q, p0); 2647: float t = dot(q - p0, d) / dd; 2648: return distance(q, p0 + clamp(t, 0.0, 1.0) * d); 2649: } 2650: int oslEndsWith(int name1, int name2) { 2651: return int(name1 == name2); 2652: } 2653: int oslFormat(int name1, int name2) { 2654: return name2; 2655: } 2656: int oslGetAttribute(int name, out int value) { 2657: value = 0; 2658: return 0; 2659: } 2660: int oslGetAttribute(int name, out float value) { 2661: value = 0.0; 2662: return 0; 2663: } 2664: int oslGetAttribute(int name, out vec3 vec) { 2665: vec = vec3(0.0, 0.0, 0.0); 2666: return 0; 2667: } 2668: void oslGetTextureInfo(int filename, int name, out int value) { 2669: value = 4; 2670: } 2671: void oslGetTextureInfo(int filename, int name, out int value[2]) { 2672: value[0] = 1024; 2673: value[1] = 1024; 2674: } 2675: vec3 oslHSV(float h, float s, float v) { 2676: vec4 outCol; 2677: hsvToRGB(vec4(h, s, v, 1.0), outCol); 2678: return outCol.rgb; 2679: } 2680: float oslHypot(float x, float y) { 2681: return sqrt(x*x + y*y); 2682: } 2683: float oslHypot(float x, float y, float z) { 2684: return sqrt(x*x + y*y + z*z); 2685: } 2686: float oslLog2(float x, float y) { 2687: return log(x) / log(y); 2688: } 2689: float oslLuminance(vec3 color) { 2690: return (abs(color.r) * 0.263 + abs(color.g) * 0.655 + abs(color.b) * 0.082); 2691: } 2692: float oslNoise(int type, vec3 vec, float phase) { 2693: float n = snoise(vec4(vec, phase)); 2694: if (type == OSL_UPERLIN) 2695: n = n * 0.5 + 0.5; 2696: return n; 2697: } 2698: float oslNoise(int type, float value, float phase) { 2699: return oslNoise(type, vec3(value), phase); 2700: } 2701: float oslNoise(int type, vec3 vec) { 2702: return oslNoise(type, vec, 0.0); 2703: } 2704: float oslNoise(int type, float value) { 2705: return oslNoise(type, vec3(value), 0.0); 2706: } 2707: vec3 oslNoise3D(int type, vec3 vec, float phase) { 2708: float x = snoise(vec4(vec, phase)); 2709: float y = snoise(vec4(vec.y, vec.x, vec.z, phase)); 2710: float z = snoise(vec4(vec.y, vec.z, vec.x, phase)); 2711: vec3 n = vec3(x, y, z); 2712: if (type == OSL_UPERLIN) 2713: n = n * 0.5 + 0.5; 2714: return n; 2715: } 2716: vec3 oslNoise3D(int type, float value, float phase) { 2717: return oslNoise3D(type, vec3(value), phase); 2718: } 2719: vec3 oslNoise3D(int type, vec3 vec) { 2720: return oslNoise3D(type, vec, 0.0); 2721: } 2722: vec3 oslNoise3D(int type, float value) { 2723: return oslNoise3D(type, vec3(value), 0.0); 2724: } 2725: float oslPow(float a, float b) { 2726: return pow(a, b); 2727: } 2728: vec3 oslPow(vec3 a, float b) { 2729: return pow(a, vec3(b)); 2730: } 2731: int oslRayType(int name) { 2732: if (name == OSL_CAMERA) 2733: #if LIGHT_PATH_IS_CAM_RAY 2734: return 1; 2735: #else 2736: return 0; 2737: #endif 2738: else 2739: return 0; 2740: } 2741: vec3 oslRotate(vec3 vec, float angle, vec3 p0, vec3 p1) { 2742: vec3 axis = normalize(p1 - p0); 2743: float c = cos(angle); 2744: float s = sin(angle); 2745: float x = axis[0]; 2746: float y = axis[1]; 2747: float z = axis[2]; 2748: mat4 mat = mat4( 2749: x * x + (1.0 - x * x) * c, x * y * (1.0 - c) + z * s, x * z * (1.0 - c) - y * s, 0.0, 2750: x * y * (1.0 - c) - z * s, y * y + (1.0 - y * y) * c, y * z * (1.0 - c) + x * s, 0.0, 2751: x * z * (1.0 - c) + y * s, y * z * (1.0 - c) - x * s, z * z + (1.0 - z * z) * c, 0.0, 2752: 0.0, 0.0, 0.0, 1.0 2753: ); 2754: return (mat * vec4((vec - p0), 1.0) + vec4(p0, 1.0)).xyz; 2755: } 2756: vec3 oslRotate(vec3 vec, float angle, vec3 axis) { 2757: return oslRotate(vec, angle, vec3(0.0), axis); 2758: } 2759: int oslStartsWith(int name1, int name2) { 2760: return int(name1 == name2); 2761: } 2762: int oslStrLen(int name) { 2763: if (name == OSL_EMPTY) 2764: return 0; 2765: else 2766: return 1; 2767: } 2768: int oslSubStr(int s, int start, int len) { 2769: return s; 2770: } 2771: int oslSubStr(int s, int start) { 2772: return s; 2773: } 2774: vec3 oslTexture(sampler2D image, float u, float v, int wrapModeFlag, int wrapMode, int alphaFlag, out float alpha) { 2775: if (wrapMode == OSL_DEFAULT || wrapMode == OSL_BLACK) { 2776: if (u < 0.0 || u > 1.0 || v < 0.0 || v > 1.0) 2777: return vec3(0.0); 2778: } else if (wrapMode == OSL_CLAMP) { 2779: u = clamp(u, 0.0, 1.0); 2780: v = clamp(v, 0.0, 1.0); 2781: } else if (wrapMode == OSL_PERIODIC) { 2782: u = mod(u, 1.0); 2783: v = mod(v, 1.0); 2784: } else if (wrapMode == OSL_MIRROR) { 2785: if (mod(floor(u), 2.0) == 0.0) 2786: u = u - floor(u); 2787: else 2788: u = 1.0 - (u - floor(u)); 2789: if (mod(floor(v), 2.0) == 0.0) 2790: v = v - floor(v); 2791: else 2792: v = 1.0 - (v - floor(v)); 2793: } 2794: vec4 colAlpha = texture2D(image, vec2(u, v)); 2795: alpha = colAlpha.a; 2796: return colAlpha.rgb; 2797: } 2798: vec3 oslTexture(sampler2D image, float u, float v, int alphaFlag, out float alpha, int wrapModeFlag, int wrapMode) { 2799: return oslTexture(image, u, v, wrapModeFlag, wrapMode, alphaFlag, alpha); 2800: } 2801: vec3 oslTexture(sampler2D image, float u, float v, int alphaFlag, out float alpha) { 2802: return oslTexture(image, u, v, OSL_WRAP, OSL_DEFAULT, alphaFlag, alpha); 2803: } 2804: vec3 oslTexture(sampler2D image, float u, float v, int wrapModeFlag, int wrapMode) { 2805: float alpha; 2806: return oslTexture(image, u, v, wrapModeFlag, wrapMode, OSL_ALPHA, alpha); 2807: } 2808: vec3 oslTexture(sampler2D image, float u, float v) { 2809: float alpha; 2810: return oslTexture(image, u, v, OSL_WRAP, OSL_DEFAULT, OSL_ALPHA, alpha); 2811: } 2812: vec3 oslTransform(int fromSpace, int toSpace, vec4 vec) { 2813: if (toSpace == OSL_WORLD || toSpace == OSL_SHADER || toSpace == OSL_COMMON) { 2814: return vec.xyz; 2815: } else if (toSpace == OSL_OBJECT) { 2816: vec = vec4(swizzleUpY(vec.xyz), vec.w); 2817: vec = invModelMatrix * vec; 2818: return swizzleUpZ(vec.xyz); 2819: } else if (toSpace == OSL_CAMERA) { 2820: vec = vec4(swizzleUpY(vec.xyz), vec.w); 2821: return (viewMatrix * vec).xyz; 2822: } else if (toSpace == OSL_SCREEN) { 2823: return vec.xyz; 2824: } else if (toSpace == OSL_RASTER) { 2825: return gl_FragCoord.xyz; 2826: } else if (toSpace == OSL_NDC) { 2827: return vec.xyz; 2828: } else { 2829: return vec.xyz; 2830: } 2831: } 2832: vec3 oslTransform(int fromSpace, int toSpace, vec3 vec) { 2833: return oslTransform(fromSpace, toSpace, vec4(vec, 1.0)); 2834: } 2835: vec3 oslTransform(int toSpace, vec3 vec) { 2836: return oslTransform(OSL_COMMON, toSpace, vec4(vec, 1.0)); 2837: } 2838: vec3 oslTransformDir(int fromSpace, int toSpace, vec3 vec) { 2839: return oslTransform(fromSpace, toSpace, vec4(vec, 0.0)); 2840: } 2841: vec3 oslTransformDir(int toSpace, vec3 vec) { 2842: return oslTransform(OSL_COMMON, toSpace, vec4(vec, 0.0)); 2843: } 2844: vec3 oslTransformC(int fromSpace, int toSpace, vec3 vec) { 2845: vec4 outVec = vec4(vec, 1.0); 2846: if (fromSpace == OSL_HSV && toSpace == OSL_RGB) 2847: hsvToRGB(vec4(vec, 1.0), outVec); 2848: else if (fromSpace == OSL_RGB && toSpace == OSL_HSV) 2849: rgbToHSV(vec4(vec, 1.0), outVec); 2850: return outVec.rgb; 2851: } 2852: vec3 oslTransformC(int toSpace, vec3 vec) { 2853: return oslTransformC(OSL_RGB, toSpace, vec); 2854: } 2855: void oslError() {} 2856: void oslFPrintf() {} 2857: void oslPrintf() {} 2858: void oslWarning() {} 2859: vec3 oslWaveLengthColor(float lambdaNM) { 2860: #if __VERSION__ == 300 2861: vec3 xyz = vec3(0.0); 2862: float ii = (lambdaNM - 380.0) / 5.0; 2863: int i = int(ii); 2864: if (i < 0 || i >= 80) 2865: return xyz; 2866: ii -= float(i); 2867: vec3 c1 = cieColorMatch[i]; 2868: vec3 c2 = cieColorMatch[i+1]; 2869: xyz = mix(c1, c2, ii); 2870: return xyz_to_sRGB(xyz); 2871: #else 2872: return vec3(0.0); 2873: #endif 2874: } 2875: #endif 2876: 2877: uniform vec4 nodeRGB[NODE_RGB_NUM]; 2878: void node_rgb(vec4 color, out vec4 outColor) 2879: { 2880: outColor = color; 2881: } 2882: void node_normal(vec3 norParam, out vec3 norOut) 2883: { 2884: #ifdef MT_BLENDER 2885: #if WORLD_NODES == 1 2886: vec4 viewDir = isOrtho(projectionMatrix) ? vec4(0.0, 0.0, -1.0, 0.0) : vec4(normalize(-vViewPosition), 0.0); 2887: viewDir = invViewMatrix * viewDir; 2888: norOut = -swizzleUpZ(viewDir.xyz); 2889: #else 2890: norOut = normalize(invViewMatrix * vec4(norParam, 0.0)).xyz; 2891: norOut = swizzleUpZ(norOut); 2892: #endif 2893: #else 2894: norOut = norParam; 2895: #endif 2896: } 2897: 2898: vec3 tintFromColor(vec3 color) { 2899: float lum = dot(color, vec3(0.3, 0.6, 0.1)); 2900: return lum > 0.0 ? color / lum : vec3(1.0); 2901: } 2902: vec3 fresnelBlend(float ior, float fresnel, vec3 fresnelColor) { 2903: float fresnelFac = fresnelReflection(vec3(1.0, 0.0, 0.0), vec3(1.0, 0.0, 0.0), ior); 2904: float mixFac = saturate((fresnel - fresnelFac) / max(1e-8, 1.0 - fresnelFac)); 2905: return mix(fresnelColor, vec3(1.0), mixFac); 2906: } 2907: void node_bsdf_principled( 2908: vec3 geometryNormal, 2909: vec4 baseColor, float subsurface, vec3 subsurfaceRadius, vec4 subsurfaceColor, 2910: float metallic, float specular, float specularTint, float roughness, 2911: float anisotropic, float anisotropicRotation, float sheen, float sheenTint, 2912: float clearcoat, float clearcoatRoughness, float ior, 2913: float transmission, float transmissionRoughness, vec4 emission, 2914: float emissionStrength, float alpha, vec3 normal, vec3 clearcoatNormal, 2915: vec3 tangent, 2916: out vec4 outColor) { 2917: normal = vec3(normal[0], normal[2], -normal[1]); 2918: vec3 normalWorld = normal; 2919: normal = (viewMatrix * vec4(normal.xyz, 0.0)).xyz; 2920: NodeMaterial material; 2921: metallic = clamp(metallic, 0.0, 1.0); 2922: float dielectric = 1.0 - metallic; 2923: transmission *= dielectric; 2924: material.diffuseColor = baseColor.rgb * dielectric; 2925: #ifndef CLEARCOAT 2926: float dielReflCoeff = BLENDER_SPECULAR_COEFFICIENT; 2927: #else 2928: float dielReflCoeff = MAXIMUM_SPECULAR_COEFFICIENT; 2929: material.clearcoat = saturate(clearcoat); 2930: material.clearcoatRoughness = clamp(clearcoatRoughness, 0.0, 1.0); 2931: #endif 2932: #ifdef USE_SHEEN 2933: material.sheenColor = sheen * mix(vec3(0.1), baseColor.rgb, sheenTint); 2934: material.sheenRoughness = 0.1; 2935: #endif 2936: vec3 dielRefl = dielReflCoeff * specular 2937: * mix(vec3(1.0), tintFromColor(baseColor.rgb), specularTint); 2938: material.specularColor = mix(dielRefl, baseColor.rgb, metallic); 2939: material.specularRoughness = clamp(roughness, 0.0, 1.0); 2940: vec3 viewWorld; 2941: if (isPerspective(projectionMatrix)) 2942: viewWorld = (invViewMatrix * vec4(-vViewPosition, 0.0)).xyz; 2943: else 2944: viewWorld = (invViewMatrix * vec4(0.0, 0.0, -1.0, 0.0)).xyz; 2945: viewWorld = normalize(viewWorld); 2946: float fresnel = fresnelReflection(viewWorld, normalWorld, ior); 2947: vec3 fresnelColor = mix(vec3(1.0), baseColor.rgb, specularTint); 2948: material.specularColor = mix(material.specularColor, 2949: fresnelBlend(ior, fresnel, fresnelColor) * fresnel, transmission); 2950: float isStrictDielectric = step(0.0, -length(vec4(subsurface, clearcoat, 2951: transmission, float(metallic == 1.0)))); 2952: material.fresnelRefl90 = mix(vec3(1.0), material.specularColor, 2953: (1.0 - specular) * metallic * (1.0 - isStrictDielectric)); 2954: material.refractionColor = baseColor.rgb; 2955: material.refractionIOR = ior; 2956: material.refractionRoughness = pow2(roughness); 2957: #if defined(ENVMAP_TYPE_CUBE) || defined(ENVMAP_TYPE_CUBE_UV) 2958: float geomRoughness = calcGeometryRoughness(geometryNormal); 2959: material.specularRoughness = calcCubeUVAdjustedRoughness( 2960: material.specularRoughness, geomRoughness); 2961: material.refractionRoughness = calcCubeUVAdjustedRoughness( 2962: material.refractionRoughness, geomRoughness); 2963: #ifdef CLEARCOAT 2964: material.clearcoatRoughness = calcCubeUVAdjustedRoughness( 2965: material.clearcoatRoughness, geomRoughness); 2966: clearcoatNormal = normalize(vec3(clearcoatNormal[0], clearcoatNormal[2], -clearcoatNormal[1])); 2967: clearcoatNormal = (viewMatrix * vec4(clearcoatNormal.xyz, 0.0)).xyz; 2968: #endif 2969: #endif 2970: ReflectedLight reflectedLight = ReflectedLight(vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0)); 2971: vec3 refractedLight = vec3(0.0); 2972: #define RE_DirectDiffuse RE_DirectDiffuse_Node 2973: #define RE_DirectSpecular RE_DirectSpecular_Node 2974: #define RE_IndirectDiffuse RE_IndirectDiffuse_Node 2975: #define RE_IndirectSpecular RE_IndirectSpecular_Node 2976: #define RE_Refraction RE_Refraction_Node 2977: 2978: GeometricContext geometry; 2979: geometry.position = - vViewPosition; 2980: geometry.normal = normal; 2981: geometry.viewDir = (isOrthographic) ? vec3(0, 0, 1) : normalize(vViewPosition); 2982: #ifdef CLEARCOAT 2983: geometry.clearcoatNormal = clearcoatNormal; 2984: #endif 2985: IncidentLight directLight; 2986: #if (1 > 0) && (defined(RE_Direct) || defined(RE_DirectDiffuse) || defined(RE_DirectSpecular)) 2987: PointLight pointLight; 2988: #if defined(USE_SHADOWMAP) && 1 > 0 2989: PointLightShadow pointLightShadow; 2990: #endif 2991: { 2992: pointLight = pointLights[0]; 2993: getPointDirectLightIrradiance(pointLight, geometry, directLight); 2994: #if defined(USE_SHADOWMAP) && ((0) < 1) 2995: pointLightShadow = pointLightShadows[0]; 2996: directLight.color *= all(bvec2(directLight.visible, receiveShadow)) ? 2997: getPointShadow(pointLightShadow, pointShadowMap[0], vPointShadowCoord[0]) : 1.0; 2998: #endif 2999: #if defined(RE_Direct) 3000: RE_Direct(directLight, geometry, material, reflectedLight); 3001: #else 3002: #if defined(RE_DirectDiffuse) 3003: RE_DirectDiffuse(directLight, geometry, material, reflectedLight); 3004: #endif 3005: #if defined(RE_DirectSpecular) 3006: RE_DirectSpecular(directLight, geometry, material, reflectedLight); 3007: #endif 3008: #endif 3009: } 3010: 3011: #endif 3012: #if (0 > 0) && (defined(RE_Direct) || defined(RE_DirectDiffuse) || defined(RE_DirectSpecular)) 3013: SpotLight spotLight; 3014: #if defined(USE_SHADOWMAP) && 0 > 0 3015: SpotLightShadow spotLightShadow; 3016: #endif 3017: 3018: #pragma unroll_loop_end 3019: #endif 3020: #if (0 > 0) && (defined(RE_Direct) || defined(RE_DirectDiffuse) || defined(RE_DirectSpecular)) 3021: DirectionalLight directionalLight; 3022: #if defined(USE_SHADOWMAP) && 0 > 0 3023: DirectionalLightShadow directionalLightShadow; 3024: #endif 3025: 3026: #pragma unroll_loop_end 3027: #endif 3028: #if (0 > 0) && defined(RE_Direct_RectArea) 3029: RectAreaLight rectAreaLight; 3030: #if defined(USE_SHADOWMAP) && 0 > 0 3031: RectAreaLightShadow rectAreaLightShadow; 3032: #endif 3033: 3034: #pragma unroll_loop_end 3035: #endif 3036: #if defined(RE_IndirectDiffuse) || defined(RE_IndirectSpecular) 3037: vec3 iblIrradiance = vec3(0.0); 3038: #endif 3039: #if defined(RE_IndirectDiffuse) 3040: vec3 irradiance = getAmbientLightIrradiance(ambientLightColor); 3041: irradiance += getLightProbeIrradiance(lightProbe, geometry); 3042: #if (0 > 0) 3043: 3044: #pragma unroll_loop_end 3045: #endif 3046: #endif 3047: #if defined(RE_IndirectSpecular) 3048: vec3 radiance = vec3(0.0); 3049: vec3 clearcoatRadiance = vec3(0.0); 3050: #endif 3051: #if defined(RE_Refraction) 3052: vec3 refraction = vec3(0.0); 3053: #endif 3054: #if defined(RE_IndirectDiffuse) 3055: #ifdef USE_LIGHTMAP 3056: vec4 lightMapTexel= texture2D(lightMap, vUv2); 3057: vec3 lightMapIrradiance = lightMapTexelToLinear(lightMapTexel).rgb * lightMapIntensity; 3058: #ifndef PHYSICALLY_CORRECT_LIGHTS 3059: lightMapIrradiance *= PI; 3060: #endif 3061: irradiance += lightMapIrradiance; 3062: #endif 3063: #if defined(USE_ENVMAP) && defined(STANDARD) && defined(ENVMAP_TYPE_CUBE_UV) 3064: iblIrradiance += getLightProbeIndirectIrradiance( geometry, maxMipLevel); 3065: #endif 3066: #endif 3067: #if defined(USE_ENVMAP) && (defined(STANDARD) || defined(PHYSICAL)) && defined(RE_IndirectSpecular) 3068: radiance += getLightProbeIndirectRadiance( geometry, Material_BlinnShininessExponent(material), maxMipLevel, 0); 3069: #if defined(USE_SSR) && !defined(USE_SSR_REFRACT) 3070: radiance = computeSSR(radiance, geometry.normal, 0.0); 3071: #endif 3072: #ifdef CLEARCOAT 3073: clearcoatRadiance += getLightProbeIndirectRadiance(geometry, Material_ClearCoat_BlinnShininessExponent(material), maxMipLevel, 1); 3074: #endif 3075: #endif 3076: #if defined(USE_ENVMAP) && defined(RE_Refraction) 3077: refraction += getLightProbeIndirectRefraction(geometry, 3078: Material_Refraction_BlinnShininessExponent(material), maxMipLevel, 3079: 1.0 / material.refractionIOR); 3080: #ifdef USE_SSR_REFRACT 3081: refraction = computeSSR(refraction, geometry.normal, material.refractionIOR); 3082: #endif 3083: #endif 3084: #if defined(RE_IndirectDiffuse) 3085: RE_IndirectDiffuse(irradiance, iblIrradiance, geometry, material, reflectedLight); 3086: #endif 3087: #if defined(RE_IndirectSpecular) 3088: RE_IndirectSpecular(radiance, iblIrradiance, clearcoatRadiance, geometry, material, reflectedLight); 3089: #endif 3090: #if defined(RE_Refraction) 3091: RE_Refraction(refraction, material, refractedLight); 3092: #endif 3093: #undef RE_DirectDiffuse 3094: #undef RE_DirectSpecular 3095: #undef RE_IndirectDiffuse 3096: #undef RE_IndirectSpecular 3097: #undef RE_Refraction 3098: vec3 outColor3 = (reflectedLight.directDiffuse + reflectedLight.indirectDiffuse) * (1.0 - transmission) 3099: + reflectedLight.directSpecular + reflectedLight.indirectSpecular 3100: + refractedLight * transmission * (1.0 - fresnel) 3101: + emission.rgb * emissionStrength; 3102: outColor = vec4(outColor3, alpha); 3103: } 3104: void node_bsdf_principled( 3105: vec3 geometryNormal, 3106: vec4 baseColor, float subsurface, vec3 subsurfaceRadius, vec4 subsurfaceColor, 3107: float metallic, float specular, float specularTint, float roughness, 3108: float anisotropic, float anisotropicRotation, float sheen, float sheenTint, 3109: float clearcoat, float clearcoatRoughness, float ior, 3110: float transmission, float transmissionRoughness, vec4 emission, float alpha, 3111: vec3 normal, vec3 clearcoatNormal, vec3 tangent, 3112: out vec4 outColor) { 3113: node_bsdf_principled(geometryNormal, baseColor, subsurface, subsurfaceRadius, 3114: subsurfaceColor, metallic, specular, specularTint, roughness, 3115: anisotropic, anisotropicRotation, sheen, sheenTint, clearcoat, 3116: clearcoatRoughness, ior, transmission, transmissionRoughness, 3117: emission, 1.0, alpha, normal, clearcoatNormal, tangent, outColor); 3118: } 3119: 3120: void node_output_material(vec4 surface, vec4 volume, float displacement, out vec4 outgoingLight) { 3121: outgoingLight = surface + volume; 3122: } 3123: void node_output_material(vec4 surface, vec4 volume, vec3 displacement, out vec4 outgoingLight) { 3124: outgoingLight = surface + volume; 3125: } 3126: void main() { 3127: bool _frontFacingValue = gl_FrontFacing; 3128: #define FRONT_FACING_VALUE _frontFacingValue 3129: #if 0 > 0 3130: vec4 plane; 3131: 3132: #pragma unroll_loop_end 3133: #if 0 < 0 3134: bool clipped = true; 3135: 3136: #pragma unroll_loop_end 3137: if (clipped) discard; 3138: #endif 3139: #endif 3140: #if defined(USE_LOGDEPTHBUF) && defined(USE_LOGDEPTHBUF_EXT) 3141: gl_FragDepthEXT = vIsPerspective == 0.0 ? gl_FragCoord.z : log2(vFragDepth) * logDepthBufFC * 0.5; 3142: #endif 3143: #ifdef FLAT_SHADED 3144: vec3 fdx = vec3(dFdx(vViewPosition.x), dFdx(vViewPosition.y), dFdx(vViewPosition.z)); 3145: vec3 fdy = vec3(dFdy(vViewPosition.x), dFdy(vViewPosition.y), dFdy(vViewPosition.z)); 3146: vec3 normal = normalize(cross(fdx, fdy)); 3147: #else 3148: vec3 normal = normalize(vNormal); 3149: #ifdef DOUBLE_SIDED 3150: #ifdef FRONT_FACING_VALUE 3151: bool frontFacing = FRONT_FACING_VALUE; 3152: #else 3153: bool frontFacing = gl_FrontFacing; 3154: #endif 3155: normal = normal * (float(frontFacing) * 2.0 - 1.0); 3156: #endif 3157: #ifdef USE_TANGENT 3158: vec3 tangent = normalize(vTangent); 3159: vec3 bitangent = normalize(vBitangent); 3160: #ifdef DOUBLE_SIDED 3161: tangent = tangent * (float(gl_FrontFacing) * 2.0 - 1.0); 3162: bitangent = bitangent * (float(gl_FrontFacing) * 2.0 - 1.0); 3163: #endif 3164: #if defined(TANGENTSPACE_NORMALMAP) || defined(USE_CLEARCOAT_NORMALMAP) 3165: mat3 vTBN = mat3(tangent, bitangent, normal); 3166: #endif 3167: #endif 3168: #endif 3169: vec3 geometryNormal = normal; 3170: vec4 outgoingLight = vec4(0.0); 3171: vec4 rgb_bl_out0_n3; 3172: node_rgb(nodeRGB[0],rgb_bl_out0_n3); 3173: vec3 normal_out0_n2; 3174: node_normal(normal,normal_out0_n2); 3175: vec4 bsdf_principled_bl_out0_n1; 3176: node_bsdf_principled(geometryNormal,rgb_bl_out0_n3,0.0,vec3(1.0,0.20000000298023224,0.10000000149011612),vec4(0.800000011920929,0.800000011920929,0.800000011920929,1.0),0.0,0.5,0.0,0.4000000059604645,0.0,0.0,0.0,0.5,0.0,0.029999999329447746,1.4500000476837158,0.0,0.0,vec4(0.0,0.0,0.0,1.0),1.0,1.0,normal_out0_n2,vec3(0.0,0.0,0.0),vec3(0.0,0.0,0.0),bsdf_principled_bl_out0_n1); 3177: node_output_material(bsdf_principled_bl_out0_n1,vec4(0.0,0.0,0.0,0.0),vec4(0.0,0.0,0.0,0.0),vec3(0.0,0.0,0.0),outgoingLight); 3178: #if WORLD_NODES == 1 3179: outgoingLight.a = 1.0; 3180: #endif 3181: #ifdef ALPHATEST 3182: if (outgoingLight.a < ALPHATEST) 3183: discard; 3184: else 3185: outgoingLight.a = 1.0; 3186: #endif 3187: gl_FragColor = vec4(outgoingLight); 3188: #if defined(TONE_MAPPING) 3189: gl_FragColor.rgb = toneMapping(gl_FragColor.rgb); 3190: #endif 3191: gl_FragColor = linearToOutputTexel(gl_FragColor); 3192: #ifdef USE_FOG 3193: #ifdef FOG_EXP2 3194: float fogFactor = 1.0 - exp(- fogDensity * fogDensity * fogDepth * fogDepth); 3195: #else 3196: float fogFactor = smoothstep(fogNear, fogFar, fogDepth); 3197: #endif 3198: gl_FragColor.rgb = mix(gl_FragColor.rgb, fogColor, fogFactor); 3199: #endif 3200: #ifdef PREMULTIPLIED_ALPHA 3201: gl_FragColor.rgb *= gl_FragColor.a; 3202: #endif 3203: #ifdef DITHERING 3204: gl_FragColor.rgb = dithering(gl_FragColor.rgb); 3205: #endif 3206: }" S (v3d.js:1:599058) (anonymous function) (v3d.js:1:599469) Re (v3d.js:1:698017) (anonymous function) (v3d.js:1:690619) Ee (v3d.js:1:694050) Ce (v3d.js:1:693705) (anonymous function) (v3d.js:1:705053) value (v3d.js:1:1342543) (anonymous function) (v3d.js:1:1341738) (anonymous function) (v3d.js:1:702596) i (v3d.js:1:158136) [Error] WebGL: INVALID_OPERATION: useProgram: program not valid useProgram useProgram (v3d.js:1:642188) Re (v3d.js:1:698048) (anonymous function) (v3d.js:1:690619) Ee (v3d.js:1:694050) Ce (v3d.js:1:693705) (anonymous function) (v3d.js:1:705053) value (v3d.js:1:1342543) (anonymous function) (v3d.js:1:1341738) (anonymous function) (v3d.js:1:702596) i (v3d.js:1:158136)