Closed semleti closed 6 years ago
THREE.WebGLShader: gl.getShaderInfoLog() fragment ERROR: 0:493: 'DirectionalLight' : undeclared identifier ERROR: 0:493: 'directionalLight' : syntax error
https://htmlpreview.github.io/?https://github.com/semleti/three.ts/blob/master/examples/webgl_shaders_ocean.html
same for tonemapping: https://htmlpreview.github.io/?https://github.com/semleti/three.ts/blob/master/examples/webgl_shaders_tonemapping.html
Solved. Don't know why :/
THREE.WebGLShader: gl.getShaderInfoLog() fragment ERROR: 0:493: 'DirectionalLight' : undeclared identifier ERROR: 0:493: 'directionalLight' : syntax error
shader code:
```glsl 1: precision highp float; 2: precision highp int; 3: #define SHADER_NAME ShaderMaterial 4: #define GAMMA_FACTOR 2 5: #define USE_FOG 6: #define FOG_EXP2 7: #define USE_SHADOWMAP 8: #define SHADOWMAP_TYPE_PCF 9: uniform mat4 viewMatrix; 10: uniform vec3 cameraPosition; 11: #define TONE_MAPPING 12: #ifndef saturate 13: #define saturate(a) clamp( a, 0.0, 1.0 ) 14: #endif 15: uniform float toneMappingExposure; 16: uniform float toneMappingWhitePoint; 17: vec3 LinearToneMapping( vec3 color ) { 18: return toneMappingExposure * color; 19: } 20: vec3 ReinhardToneMapping( vec3 color ) { 21: color *= toneMappingExposure; 22: return saturate( color / ( vec3( 1.0 ) + color ) ); 23: } 24: #define Uncharted2Helper( x ) max( ( ( x * ( 0.15 * x + 0.10 * 0.50 ) + 0.20 * 0.02 ) / ( x * ( 0.15 * x + 0.50 ) + 0.20 * 0.30 ) ) - 0.02 / 0.30, vec3( 0.0 ) ) 25: vec3 Uncharted2ToneMapping( vec3 color ) { 26: color *= toneMappingExposure; 27: return saturate( Uncharted2Helper( color ) / Uncharted2Helper( vec3( toneMappingWhitePoint ) ) ); 28: } 29: vec3 OptimizedCineonToneMapping( vec3 color ) { 30: color *= toneMappingExposure; 31: color = max( vec3( 0.0 ), color - 0.004 ); 32: return pow( ( color * ( 6.2 * color + 0.5 ) ) / ( color * ( 6.2 * color + 1.7 ) + 0.06 ), vec3( 2.2 ) ); 33: } 34: 35: vec3 toneMapping( vec3 color ) { return LinearToneMapping( color ); } 36: 37: vec4 LinearToLinear( in vec4 value ) { 38: return value; 39: } 40: vec4 GammaToLinear( in vec4 value, in float gammaFactor ) { 41: return vec4( pow( value.xyz, vec3( gammaFactor ) ), value.w ); 42: } 43: vec4 LinearToGamma( in vec4 value, in float gammaFactor ) { 44: return vec4( pow( value.xyz, vec3( 1.0 / gammaFactor ) ), value.w ); 45: } 46: vec4 sRGBToLinear( in vec4 value ) { 47: 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.w ); 48: } 49: vec4 LinearTosRGB( in vec4 value ) { 50: 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.w ); 51: } 52: vec4 RGBEToLinear( in vec4 value ) { 53: return vec4( value.rgb * exp2( value.a * 255.0 - 128.0 ), 1.0 ); 54: } 55: vec4 LinearToRGBE( in vec4 value ) { 56: float maxComponent = max( max( value.r, value.g ), value.b ); 57: float fExp = clamp( ceil( log2( maxComponent ) ), -128.0, 127.0 ); 58: return vec4( value.rgb / exp2( fExp ), ( fExp + 128.0 ) / 255.0 ); 59: } 60: vec4 RGBMToLinear( in vec4 value, in float maxRange ) { 61: return vec4( value.xyz * value.w * maxRange, 1.0 ); 62: } 63: vec4 LinearToRGBM( in vec4 value, in float maxRange ) { 64: float maxRGB = max( value.x, max( value.g, value.b ) ); 65: float M = clamp( maxRGB / maxRange, 0.0, 1.0 ); 66: M = ceil( M * 255.0 ) / 255.0; 67: return vec4( value.rgb / ( M * maxRange ), M ); 68: } 69: vec4 RGBDToLinear( in vec4 value, in float maxRange ) { 70: return vec4( value.rgb * ( ( maxRange / 255.0 ) / value.a ), 1.0 ); 71: } 72: vec4 LinearToRGBD( in vec4 value, in float maxRange ) { 73: float maxRGB = max( value.x, max( value.g, value.b ) ); 74: float D = max( maxRange / maxRGB, 1.0 ); 75: D = min( floor( D ) / 255.0, 1.0 ); 76: return vec4( value.rgb * ( D * ( 255.0 / maxRange ) ), D ); 77: } 78: const mat3 cLogLuvM = mat3( 0.2209, 0.3390, 0.4184, 0.1138, 0.6780, 0.7319, 0.0102, 0.1130, 0.2969 ); 79: vec4 LinearToLogLuv( in vec4 value ) { 80: vec3 Xp_Y_XYZp = value.rgb * cLogLuvM; 81: Xp_Y_XYZp = max(Xp_Y_XYZp, vec3(1e-6, 1e-6, 1e-6)); 82: vec4 vResult; 83: vResult.xy = Xp_Y_XYZp.xy / Xp_Y_XYZp.z; 84: float Le = 2.0 * log2(Xp_Y_XYZp.y) + 127.0; 85: vResult.w = fract(Le); 86: vResult.z = (Le - (floor(vResult.w*255.0))/255.0)/255.0; 87: return vResult; 88: } 89: const mat3 cLogLuvInverseM = mat3( 6.0014, -2.7008, -1.7996, -1.3320, 3.1029, -5.7721, 0.3008, -1.0882, 5.6268 ); 90: vec4 LogLuvToLinear( in vec4 value ) { 91: float Le = value.z * 255.0 + value.w; 92: vec3 Xp_Y_XYZp; 93: Xp_Y_XYZp.y = exp2((Le - 127.0) / 2.0); 94: Xp_Y_XYZp.z = Xp_Y_XYZp.y / value.y; 95: Xp_Y_XYZp.x = value.x * Xp_Y_XYZp.z; 96: vec3 vRGB = Xp_Y_XYZp.rgb * cLogLuvInverseM; 97: return vec4( max(vRGB, 0.0), 1.0 ); 98: } 99: 100: vec4 mapTexelToLinear( vec4 value ) { return LinearToLinear( value ); } 101: vec4 envMapTexelToLinear( vec4 value ) { return LinearToLinear( value ); } 102: vec4 emissiveMapTexelToLinear( vec4 value ) { return LinearToLinear( value ); } 103: vec4 linearToOutputTexel( vec4 value ) { return LinearToLinear( value ); } 104: 105: uniform sampler2D mirrorSampler; 106: uniform float alpha; 107: uniform float time; 108: uniform float size; 109: uniform float distortionScale; 110: uniform sampler2D normalSampler; 111: uniform vec3 sunColor; 112: uniform vec3 sunDirection; 113: uniform vec3 eye; 114: uniform vec3 waterColor; 115: varying vec4 mirrorCoord; 116: varying vec4 worldPosition; 117: vec4 getNoise( vec2 uv ) { 118: vec2 uv0 = ( uv / 103.0 ) + vec2(time / 17.0, time / 29.0); 119: vec2 uv1 = uv / 107.0-vec2( time / -19.0, time / 31.0 ); 120: vec2 uv2 = uv / vec2( 8907.0, 9803.0 ) + vec2( time / 101.0, time / 97.0 ); 121: vec2 uv3 = uv / vec2( 1091.0, 1027.0 ) - vec2( time / 109.0, time / -113.0 ); 122: vec4 noise = texture2D( normalSampler, uv0 ) + 123: texture2D( normalSampler, uv1 ) + 124: texture2D( normalSampler, uv2 ) + 125: texture2D( normalSampler, uv3 ); 126: return noise * 0.5 - 1.0; 127: } 128: void sunLight( const vec3 surfaceNormal, const vec3 eyeDirection, float shiny, float spec, float diffuse, inout vec3 diffuseColor, inout vec3 specularColor ) { 129: vec3 reflection = normalize( reflect( -sunDirection, surfaceNormal ) ); 130: float direction = max( 0.0, dot( eyeDirection, reflection ) ); 131: specularColor += pow( direction, shiny ) * sunColor * spec; 132: diffuseColor += max( dot( sunDirection, surfaceNormal ), 0.0 ) * sunColor * diffuse; 133: } 134: #define PI 3.14159265359 135: #define PI2 6.28318530718 136: #define PI_HALF 1.5707963267949 137: #define RECIPROCAL_PI 0.31830988618 138: #define RECIPROCAL_PI2 0.15915494 139: #define LOG2 1.442695 140: #define EPSILON 1e-6 141: #define saturate(a) clamp( a, 0.0, 1.0 ) 142: #define whiteCompliment(a) ( 1.0 - saturate( a ) ) 143: float pow2( const in float x ) { return x*x; } 144: float pow3( const in float x ) { return x*x*x; } 145: float pow4( const in float x ) { float x2 = x*x; return x2*x2; } 146: float average( const in vec3 color ) { return dot( color, vec3( 0.3333 ) ); } 147: highp float rand( const in vec2 uv ) { 148: const highp float a = 12.9898, b = 78.233, c = 43758.5453; 149: highp float dt = dot( uv.xy, vec2( a,b ) ), sn = mod( dt, PI ); 150: return fract(sin(sn) * c); 151: } 152: struct IncidentLight { 153: vec3 color; 154: vec3 direction; 155: bool visible; 156: }; 157: struct ReflectedLight { 158: vec3 directDiffuse; 159: vec3 directSpecular; 160: vec3 indirectDiffuse; 161: vec3 indirectSpecular; 162: }; 163: struct GeometricContext { 164: vec3 position; 165: vec3 normal; 166: vec3 viewDir; 167: }; 168: vec3 transformDirection( in vec3 dir, in mat4 matrix ) { 169: return normalize( ( matrix * vec4( dir, 0.0 ) ).xyz ); 170: } 171: vec3 inverseTransformDirection( in vec3 dir, in mat4 matrix ) { 172: return normalize( ( vec4( dir, 0.0 ) * matrix ).xyz ); 173: } 174: vec3 projectOnPlane(in vec3 point, in vec3 pointOnPlane, in vec3 planeNormal ) { 175: float distance = dot( planeNormal, point - pointOnPlane ); 176: return - distance * planeNormal + point; 177: } 178: float sideOfPlane( in vec3 point, in vec3 pointOnPlane, in vec3 planeNormal ) { 179: return sign( dot( point - pointOnPlane, planeNormal ) ); 180: } 181: vec3 linePlaneIntersect( in vec3 pointOnLine, in vec3 lineDirection, in vec3 pointOnPlane, in vec3 planeNormal ) { 182: return lineDirection * ( dot( planeNormal, pointOnPlane - pointOnLine ) / dot( planeNormal, lineDirection ) ) + pointOnLine; 183: } 184: mat3 transposeMat3( const in mat3 m ) { 185: mat3 tmp; 186: tmp[ 0 ] = vec3( m[ 0 ].x, m[ 1 ].x, m[ 2 ].x ); 187: tmp[ 1 ] = vec3( m[ 0 ].y, m[ 1 ].y, m[ 2 ].y ); 188: tmp[ 2 ] = vec3( m[ 0 ].z, m[ 1 ].z, m[ 2 ].z ); 189: return tmp; 190: } 191: float linearToRelativeLuminance( const in vec3 color ) { 192: vec3 weights = vec3( 0.2126, 0.7152, 0.0722 ); 193: return dot( weights, color.rgb ); 194: } 195: 196: vec3 packNormalToRGB( const in vec3 normal ) { 197: return normalize( normal ) * 0.5 + 0.5; 198: } 199: vec3 unpackRGBToNormal( const in vec3 rgb ) { 200: return 2.0 * rgb.xyz - 1.0; 201: } 202: const float PackUpscale = 256. / 255.;const float UnpackDownscale = 255. / 256.; 203: const vec3 PackFactors = vec3( 256. * 256. * 256., 256. * 256., 256. ); 204: const vec4 UnpackFactors = UnpackDownscale / vec4( PackFactors, 1. ); 205: const float ShiftRight8 = 1. / 256.; 206: vec4 packDepthToRGBA( const in float v ) { 207: vec4 r = vec4( fract( v * PackFactors ), v ); 208: r.yzw -= r.xyz * ShiftRight8; return r * PackUpscale; 209: } 210: float unpackRGBAToDepth( const in vec4 v ) { 211: return dot( v, UnpackFactors ); 212: } 213: float viewZToOrthographicDepth( const in float viewZ, const in float near, const in float far ) { 214: return ( viewZ + near ) / ( near - far ); 215: } 216: float orthographicDepthToViewZ( const in float linearClipZ, const in float near, const in float far ) { 217: return linearClipZ * ( near - far ) - near; 218: } 219: float viewZToPerspectiveDepth( const in float viewZ, const in float near, const in float far ) { 220: return (( near + viewZ ) * far ) / (( far - near ) * viewZ ); 221: } 222: float perspectiveDepthToViewZ( const in float invClipZ, const in float near, const in float far ) { 223: return ( near * far ) / ( ( far - near ) * invClipZ - far ); 224: } 225: 226: float punctualLightIntensityToIrradianceFactor( const in float lightDistance, const in float cutoffDistance, const in float decayExponent ) { 227: if( decayExponent > 0.0 ) { 228: #if defined ( PHYSICALLY_CORRECT_LIGHTS ) 229: float distanceFalloff = 1.0 / max( pow( lightDistance, decayExponent ), 0.01 ); 230: float maxDistanceCutoffFactor = pow2( saturate( 1.0 - pow4( lightDistance / cutoffDistance ) ) ); 231: return distanceFalloff * maxDistanceCutoffFactor; 232: #else 233: return pow( saturate( -lightDistance / cutoffDistance + 1.0 ), decayExponent ); 234: #endif 235: } 236: return 1.0; 237: } 238: vec3 BRDF_Diffuse_Lambert( const in vec3 diffuseColor ) { 239: return RECIPROCAL_PI * diffuseColor; 240: } 241: vec3 F_Schlick( const in vec3 specularColor, const in float dotLH ) { 242: float fresnel = exp2( ( -5.55473 * dotLH - 6.98316 ) * dotLH ); 243: return ( 1.0 - specularColor ) * fresnel + specularColor; 244: } 245: float G_GGX_Smith( const in float alpha, const in float dotNL, const in float dotNV ) { 246: float a2 = pow2( alpha ); 247: float gl = dotNL + sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNL ) ); 248: float gv = dotNV + sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNV ) ); 249: return 1.0 / ( gl * gv ); 250: } 251: float G_GGX_SmithCorrelated( const in float alpha, const in float dotNL, const in float dotNV ) { 252: float a2 = pow2( alpha ); 253: float gv = dotNL * sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNV ) ); 254: float gl = dotNV * sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNL ) ); 255: return 0.5 / max( gv + gl, EPSILON ); 256: } 257: float D_GGX( const in float alpha, const in float dotNH ) { 258: float a2 = pow2( alpha ); 259: float denom = pow2( dotNH ) * ( a2 - 1.0 ) + 1.0; 260: return RECIPROCAL_PI * a2 / pow2( denom ); 261: } 262: vec3 BRDF_Specular_GGX( const in IncidentLight incidentLight, const in GeometricContext geometry, const in vec3 specularColor, const in float roughness ) { 263: float alpha = pow2( roughness ); 264: vec3 halfDir = normalize( incidentLight.direction + geometry.viewDir ); 265: float dotNL = saturate( dot( geometry.normal, incidentLight.direction ) ); 266: float dotNV = saturate( dot( geometry.normal, geometry.viewDir ) ); 267: float dotNH = saturate( dot( geometry.normal, halfDir ) ); 268: float dotLH = saturate( dot( incidentLight.direction, halfDir ) ); 269: vec3 F = F_Schlick( specularColor, dotLH ); 270: float G = G_GGX_SmithCorrelated( alpha, dotNL, dotNV ); 271: float D = D_GGX( alpha, dotNH ); 272: return F * ( G * D ); 273: } 274: vec2 LTC_Uv( const in vec3 N, const in vec3 V, const in float roughness ) { 275: const float LUT_SIZE = 64.0; 276: const float LUT_SCALE = ( LUT_SIZE - 1.0 ) / LUT_SIZE; 277: const float LUT_BIAS = 0.5 / LUT_SIZE; 278: float dotNV = saturate( dot( N, V ) ); 279: vec2 uv = vec2( roughness, sqrt( 1.0 - dotNV ) ); 280: uv = uv * LUT_SCALE + LUT_BIAS; 281: return uv; 282: } 283: float LTC_ClippedSphereFormFactor( const in vec3 f ) { 284: float l = length( f ); 285: return max( ( l * l + f.z ) / ( l + 1.0 ), 0.0 ); 286: } 287: vec3 LTC_EdgeVectorFormFactor( const in vec3 v1, const in vec3 v2 ) { 288: float x = dot( v1, v2 ); 289: float y = abs( x ); 290: float a = 0.8543985 + ( 0.4965155 + 0.0145206 * y ) * y; 291: float b = 3.4175940 + ( 4.1616724 + y ) * y; 292: float v = a / b; 293: float theta_sintheta = ( x > 0.0 ) ? v : 0.5 * inversesqrt( max( 1.0 - x * x, 1e-7 ) ) - v; 294: return cross( v1, v2 ) * theta_sintheta; 295: } 296: vec3 LTC_Evaluate( const in vec3 N, const in vec3 V, const in vec3 P, const in mat3 mInv, const in vec3 rectCoords[ 4 ] ) { 297: vec3 v1 = rectCoords[ 1 ] - rectCoords[ 0 ]; 298: vec3 v2 = rectCoords[ 3 ] - rectCoords[ 0 ]; 299: vec3 lightNormal = cross( v1, v2 ); 300: if( dot( lightNormal, P - rectCoords[ 0 ] ) < 0.0 ) return vec3( 0.0 ); 301: vec3 T1, T2; 302: T1 = normalize( V - N * dot( V, N ) ); 303: T2 = - cross( N, T1 ); 304: mat3 mat = mInv * transposeMat3( mat3( T1, T2, N ) ); 305: vec3 coords[ 4 ]; 306: coords[ 0 ] = mat * ( rectCoords[ 0 ] - P ); 307: coords[ 1 ] = mat * ( rectCoords[ 1 ] - P ); 308: coords[ 2 ] = mat * ( rectCoords[ 2 ] - P ); 309: coords[ 3 ] = mat * ( rectCoords[ 3 ] - P ); 310: coords[ 0 ] = normalize( coords[ 0 ] ); 311: coords[ 1 ] = normalize( coords[ 1 ] ); 312: coords[ 2 ] = normalize( coords[ 2 ] ); 313: coords[ 3 ] = normalize( coords[ 3 ] ); 314: vec3 vectorFormFactor = vec3( 0.0 ); 315: vectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 0 ], coords[ 1 ] ); 316: vectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 1 ], coords[ 2 ] ); 317: vectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 2 ], coords[ 3 ] ); 318: vectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 3 ], coords[ 0 ] ); 319: float result = LTC_ClippedSphereFormFactor( vectorFormFactor ); 320: return vec3( result ); 321: } 322: vec3 BRDF_Specular_GGX_Environment( const in GeometricContext geometry, const in vec3 specularColor, const in float roughness ) { 323: float dotNV = saturate( dot( geometry.normal, geometry.viewDir ) ); 324: const vec4 c0 = vec4( - 1, - 0.0275, - 0.572, 0.022 ); 325: const vec4 c1 = vec4( 1, 0.0425, 1.04, - 0.04 ); 326: vec4 r = roughness * c0 + c1; 327: float a004 = min( r.x * r.x, exp2( - 9.28 * dotNV ) ) * r.x + r.y; 328: vec2 AB = vec2( -1.04, 1.04 ) * a004 + r.zw; 329: return specularColor * AB.x + AB.y; 330: } 331: float G_BlinnPhong_Implicit( ) { 332: return 0.25; 333: } 334: float D_BlinnPhong( const in float shininess, const in float dotNH ) { 335: return RECIPROCAL_PI * ( shininess * 0.5 + 1.0 ) * pow( dotNH, shininess ); 336: } 337: vec3 BRDF_Specular_BlinnPhong( const in IncidentLight incidentLight, const in GeometricContext geometry, const in vec3 specularColor, const in float shininess ) { 338: vec3 halfDir = normalize( incidentLight.direction + geometry.viewDir ); 339: float dotNH = saturate( dot( geometry.normal, halfDir ) ); 340: float dotLH = saturate( dot( incidentLight.direction, halfDir ) ); 341: vec3 F = F_Schlick( specularColor, dotLH ); 342: float G = G_BlinnPhong_Implicit( ); 343: float D = D_BlinnPhong( shininess, dotNH ); 344: return F * ( G * D ); 345: } 346: float GGXRoughnessToBlinnExponent( const in float ggxRoughness ) { 347: return ( 2.0 / pow2( ggxRoughness + 0.0001 ) - 2.0 ); 348: } 349: float BlinnExponentToGGXRoughness( const in float blinnExponent ) { 350: return sqrt( 2.0 / ( blinnExponent + 2.0 ) ); 351: } 352: 353: #ifdef USE_FOG 354: uniform vec3 fogColor; 355: varying float fogDepth; 356: #ifdef FOG_EXP2 357: uniform float fogDensity; 358: #else 359: uniform float fogNear; 360: uniform float fogFar; 361: #endif 362: #endif 363: 364: 365: #ifdef USE_SHADOWMAP 366: #if 1 > 0 367: uniform sampler2D directionalShadowMap[ 1 ]; 368: varying vec4 vDirectionalShadowCoord[ 1 ]; 369: #endif 370: #if 0 > 0 371: uniform sampler2D spotShadowMap[ 0 ]; 372: varying vec4 vSpotShadowCoord[ 0 ]; 373: #endif 374: #if 0 > 0 375: uniform sampler2D pointShadowMap[ 0 ]; 376: varying vec4 vPointShadowCoord[ 0 ]; 377: #endif 378: float texture2DCompare( sampler2D depths, vec2 uv, float compare ) { 379: return step( compare, unpackRGBAToDepth( texture2D( depths, uv ) ) ); 380: } 381: float texture2DShadowLerp( sampler2D depths, vec2 size, vec2 uv, float compare ) { 382: const vec2 offset = vec2( 0.0, 1.0 ); 383: vec2 texelSize = vec2( 1.0 ) / size; 384: vec2 centroidUV = floor( uv * size + 0.5 ) / size; 385: float lb = texture2DCompare( depths, centroidUV + texelSize * offset.xx, compare ); 386: float lt = texture2DCompare( depths, centroidUV + texelSize * offset.xy, compare ); 387: float rb = texture2DCompare( depths, centroidUV + texelSize * offset.yx, compare ); 388: float rt = texture2DCompare( depths, centroidUV + texelSize * offset.yy, compare ); 389: vec2 f = fract( uv * size + 0.5 ); 390: float a = mix( lb, lt, f.y ); 391: float b = mix( rb, rt, f.y ); 392: float c = mix( a, b, f.x ); 393: return c; 394: } 395: float getShadow( sampler2D shadowMap, vec2 shadowMapSize, float shadowBias, float shadowRadius, vec4 shadowCoord ) { 396: float shadow = 1.0; 397: shadowCoord.xyz /= shadowCoord.w; 398: shadowCoord.z += shadowBias; 399: bvec4 inFrustumVec = bvec4 ( shadowCoord.x >= 0.0, shadowCoord.x <= 1.0, shadowCoord.y >= 0.0, shadowCoord.y <= 1.0 ); 400: bool inFrustum = all( inFrustumVec ); 401: bvec2 frustumTestVec = bvec2( inFrustum, shadowCoord.z <= 1.0 ); 402: bool frustumTest = all( frustumTestVec ); 403: if ( frustumTest ) { 404: #if defined( SHADOWMAP_TYPE_PCF ) 405: vec2 texelSize = vec2( 1.0 ) / shadowMapSize; 406: float dx0 = - texelSize.x * shadowRadius; 407: float dy0 = - texelSize.y * shadowRadius; 408: float dx1 = + texelSize.x * shadowRadius; 409: float dy1 = + texelSize.y * shadowRadius; 410: shadow = ( 411: texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx0, dy0 ), shadowCoord.z ) + 412: texture2DCompare( shadowMap, shadowCoord.xy + vec2( 0.0, dy0 ), shadowCoord.z ) + 413: texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx1, dy0 ), shadowCoord.z ) + 414: texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx0, 0.0 ), shadowCoord.z ) + 415: texture2DCompare( shadowMap, shadowCoord.xy, shadowCoord.z ) + 416: texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx1, 0.0 ), shadowCoord.z ) + 417: texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx0, dy1 ), shadowCoord.z ) + 418: texture2DCompare( shadowMap, shadowCoord.xy + vec2( 0.0, dy1 ), shadowCoord.z ) + 419: texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx1, dy1 ), shadowCoord.z ) 420: ) * ( 1.0 / 9.0 ); 421: #elif defined( SHADOWMAP_TYPE_PCF_SOFT ) 422: vec2 texelSize = vec2( 1.0 ) / shadowMapSize; 423: float dx0 = - texelSize.x * shadowRadius; 424: float dy0 = - texelSize.y * shadowRadius; 425: float dx1 = + texelSize.x * shadowRadius; 426: float dy1 = + texelSize.y * shadowRadius; 427: shadow = ( 428: texture2DShadowLerp( shadowMap, shadowMapSize, shadowCoord.xy + vec2( dx0, dy0 ), shadowCoord.z ) + 429: texture2DShadowLerp( shadowMap, shadowMapSize, shadowCoord.xy + vec2( 0.0, dy0 ), shadowCoord.z ) + 430: texture2DShadowLerp( shadowMap, shadowMapSize, shadowCoord.xy + vec2( dx1, dy0 ), shadowCoord.z ) + 431: texture2DShadowLerp( shadowMap, shadowMapSize, shadowCoord.xy + vec2( dx0, 0.0 ), shadowCoord.z ) + 432: texture2DShadowLerp( shadowMap, shadowMapSize, shadowCoord.xy, shadowCoord.z ) + 433: texture2DShadowLerp( shadowMap, shadowMapSize, shadowCoord.xy + vec2( dx1, 0.0 ), shadowCoord.z ) + 434: texture2DShadowLerp( shadowMap, shadowMapSize, shadowCoord.xy + vec2( dx0, dy1 ), shadowCoord.z ) + 435: texture2DShadowLerp( shadowMap, shadowMapSize, shadowCoord.xy + vec2( 0.0, dy1 ), shadowCoord.z ) + 436: texture2DShadowLerp( shadowMap, shadowMapSize, shadowCoord.xy + vec2( dx1, dy1 ), shadowCoord.z ) 437: ) * ( 1.0 / 9.0 ); 438: #else 439: shadow = texture2DCompare( shadowMap, shadowCoord.xy, shadowCoord.z ); 440: #endif 441: } 442: return shadow; 443: } 444: vec2 cubeToUV( vec3 v, float texelSizeY ) { 445: vec3 absV = abs( v ); 446: float scaleToCube = 1.0 / max( absV.x, max( absV.y, absV.z ) ); 447: absV *= scaleToCube; 448: v *= scaleToCube * ( 1.0 - 2.0 * texelSizeY ); 449: vec2 planar = v.xy; 450: float almostATexel = 1.5 * texelSizeY; 451: float almostOne = 1.0 - almostATexel; 452: if ( absV.z >= almostOne ) { 453: if ( v.z > 0.0 ) 454: planar.x = 4.0 - v.x; 455: } else if ( absV.x >= almostOne ) { 456: float signX = sign( v.x ); 457: planar.x = v.z * signX + 2.0 * signX; 458: } else if ( absV.y >= almostOne ) { 459: float signY = sign( v.y ); 460: planar.x = v.x + 2.0 * signY + 2.0; 461: planar.y = v.z * signY - 2.0; 462: } 463: return vec2( 0.125, 0.25 ) * planar + vec2( 0.375, 0.75 ); 464: } 465: float getPointShadow( sampler2D shadowMap, vec2 shadowMapSize, float shadowBias, float shadowRadius, vec4 shadowCoord, float shadowCameraNear, float shadowCameraFar ) { 466: vec2 texelSize = vec2( 1.0 ) / ( shadowMapSize * vec2( 4.0, 2.0 ) ); 467: vec3 lightToPosition = shadowCoord.xyz; 468: float dp = ( length( lightToPosition ) - shadowCameraNear ) / ( shadowCameraFar - shadowCameraNear ); dp += shadowBias; 469: vec3 bd3D = normalize( lightToPosition ); 470: #if defined( SHADOWMAP_TYPE_PCF ) || defined( SHADOWMAP_TYPE_PCF_SOFT ) 471: vec2 offset = vec2( - 1, 1 ) * shadowRadius * texelSize.y; 472: return ( 473: texture2DCompare( shadowMap, cubeToUV( bd3D + offset.xyy, texelSize.y ), dp ) + 474: texture2DCompare( shadowMap, cubeToUV( bd3D + offset.yyy, texelSize.y ), dp ) + 475: texture2DCompare( shadowMap, cubeToUV( bd3D + offset.xyx, texelSize.y ), dp ) + 476: texture2DCompare( shadowMap, cubeToUV( bd3D + offset.yyx, texelSize.y ), dp ) + 477: texture2DCompare( shadowMap, cubeToUV( bd3D, texelSize.y ), dp ) + 478: texture2DCompare( shadowMap, cubeToUV( bd3D + offset.xxy, texelSize.y ), dp ) + 479: texture2DCompare( shadowMap, cubeToUV( bd3D + offset.yxy, texelSize.y ), dp ) + 480: texture2DCompare( shadowMap, cubeToUV( bd3D + offset.xxx, texelSize.y ), dp ) + 481: texture2DCompare( shadowMap, cubeToUV( bd3D + offset.yxx, texelSize.y ), dp ) 482: ) * ( 1.0 / 9.0 ); 483: #else 484: return texture2DCompare( shadowMap, cubeToUV( bd3D, texelSize.y ), dp ); 485: #endif 486: } 487: #endif 488: 489: float getShadowMask() { 490: float shadow = 1.0; 491: #ifdef USE_SHADOWMAP 492: #if 1 > 0 493: DirectionalLight directionalLight; 494: 495: directionalLight = directionalLights[ 0 ]; 496: shadow *= bool( directionalLight.shadow ) ? getShadow( directionalShadowMap[ 0 ], directionalLight.shadowMapSize, directionalLight.shadowBias, directionalLight.shadowRadius, vDirectionalShadowCoord[ 0 ] ) : 1.0; 497: 498: #endif 499: #if 0 > 0 500: SpotLight spotLight; 501: 502: #endif 503: #if 0 > 0 504: PointLight pointLight; 505: 506: #endif 507: #endif 508: return shadow; 509: } 510: 511: void main() { 512: vec4 noise = getNoise( worldPosition.xz * size ); 513: vec3 surfaceNormal = normalize( noise.xzy * vec3( 1.5, 1.0, 1.5 ) ); 514: vec3 diffuseLight = vec3(0.0); 515: vec3 specularLight = vec3(0.0); 516: vec3 worldToEye = eye-worldPosition.xyz; 517: vec3 eyeDirection = normalize( worldToEye ); 518: sunLight( surfaceNormal, eyeDirection, 100.0, 2.0, 0.5, diffuseLight, specularLight ); 519: float distance = length(worldToEye); 520: vec2 distortion = surfaceNormal.xz * ( 0.001 + 1.0 / distance ) * distortionScale; 521: vec3 reflectionSample = vec3( texture2D( mirrorSampler, mirrorCoord.xy / mirrorCoord.z + distortion ) ); 522: float theta = max( dot( eyeDirection, surfaceNormal ), 0.0 ); 523: float rf0 = 0.3; 524: float reflectance = rf0 + ( 1.0 - rf0 ) * pow( ( 1.0 - theta ), 5.0 ); 525: vec3 scatter = max( 0.0, dot( surfaceNormal, eyeDirection ) ) * waterColor; 526: vec3 albedo = mix( ( sunColor * diffuseLight * 0.3 + scatter ) * getShadowMask(), ( vec3( 0.1 ) + reflectionSample * 0.9 + reflectionSample * specularLight ), reflectance); 527: vec3 outgoingLight = albedo; 528: gl_FragColor = vec4( outgoingLight, alpha ); 529: #if defined( TONE_MAPPING ) 530: gl_FragColor.rgb = toneMapping( gl_FragColor.rgb ); 531: #endif 532: 533: #ifdef USE_FOG 534: #ifdef FOG_EXP2 535: float fogFactor = whiteCompliment( exp2( - fogDensity * fogDensity * fogDepth * fogDepth * LOG2 ) ); 536: #else 537: float fogFactor = smoothstep( fogNear, fogFar, fogDepth ); 538: #endif 539: gl_FragColor.rgb = mix( gl_FragColor.rgb, fogColor, fogFactor ); 540: #endif 541: 542: } ```https://htmlpreview.github.io/?https://github.com/semleti/three.ts/blob/master/examples/webgl_shaders_ocean.html