Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

three.js clipping example has shader with too many uniforms #20928

Closed
jdm opened this issue Jun 5, 2018 · 4 comments
Closed

three.js clipping example has shader with too many uniforms #20928

jdm opened this issue Jun 5, 2018 · 4 comments
Labels
A-content/webgl 3d canvas API

Comments

@jdm
Copy link
Member

jdm commented Jun 5, 2018

https://threejs.org/examples/webgl_clipping.html
https://threejs.org/examples/webgl_effects_parallaxbarrier.html
https://threejs.org/examples/webgl_lights_pointlights2.html
https://threejs.org/examples/webgl_lights_spotlights.html
https://threejs.org/examples/webgl_loader_ctm_materials.html
https://threejs.org/examples/webgl_loader_gltf_extensions.html
https://threejs.org/examples/webgl_loader_md2.html
https://threejs.org/examples/webgl_loader_msgpack.html
https://threejs.org/examples/webgl_loader_ply.html
https://threejs.org/examples/webgl_loader_sea3d.html
https://threejs.org/examples/webgl_loader_stl.html
https://threejs.org/examples/webgl_loader_ttf.html
https://threejs.org/examples/webgl_marchingcubes.html
https://threejs.org/examples/webgl_materials_cars.html
https://threejs.org/examples/webgl_materials_cubemap_dynamic.html
https://threejs.org/examples/webgl_materials_displacementmap.html
https://threejs.org/examples/webgl_materials_bumpmap.html
https://threejs.org/examples/webgl_materials_normalmap.html
https://threejs.org/examples/webgl_materials_reflectivity.html
https://threejs.org/examples/webgl_materials_skin.html
https://threejs.org/examples/webgl_materials_translucency.html
https://threejs.org/examples/webgl_materials_transparency.html
https://threejs.org/examples/webgl_materials_variations_phong.html
https://threejs.org/examples/webgl_materials_variations_standard.html
https://threejs.org/examples/webgl_materials_variations_physical.html
https://threejs.org/examples/webgl_materials_variations_toon.html
https://threejs.org/examples/webgl_materials_video.html
https://threejs.org/examples/webgl_mirror.html
https://threejs.org/examples/webgl_mirror_nodes.html
https://threejs.org/examples/webgl_morphtargets_sphere.html
https://threejs.org/examples/webgl_performance_doublesided.html
https://threejs.org/examples/webgl_postprocessing_advanced.html
https://threejs.org/examples/webgl_postprocessing_backgrounds.html
https://threejs.org/examples/webgl_postprocessing_ssaa_unbiased.html
https://threejs.org/examples/webgl_postprocessing_pixel.html
https://threejs.org/examples/webgl_postprocessing_sao.html
https://threejs.org/examples/webgl_shader_lava.html
https://threejs.org/examples/webgl_shaders_tonemapping.html
https://threejs.org/examples/webgl_shading_physical.html
https://threejs.org/examples/webgl_shadowmap_pointlight.html
https://threejs.org/examples/webgl_terrain_dynamic.html

THREE.WebGLProgram: shader error:
0
gl.VALIDATE_STATUS
false
gl.getProgramInfoLog
One or more shaders failed to compile

ERROR: too many uniforms
THREE.WebGLShader: Shader couldn't compile.
THREE.WebGLShader: gl.getShaderInfoLog()
fragment
ERROR: too many uniforms

1: precision highp float;
2: precision highp int;
3: #define SHADER_NAME MeshPhongMaterial
4: #define GAMMA_FACTOR 2
5: #define USE_SHADOWMAP
6: #define SHADOWMAP_TYPE_PCF
7: uniform mat4 viewMatrix;
8: uniform vec3 cameraPosition;
9: #define TONE_MAPPING
10: #ifndef saturate
11: 	#define saturate(a) clamp( a, 0.0, 1.0 )
12: #endif
13: uniform float toneMappingExposure;
14: uniform float toneMappingWhitePoint;
15: vec3 LinearToneMapping( vec3 color ) {
16: 	return toneMappingExposure * color;
17: }
18: vec3 ReinhardToneMapping( vec3 color ) {
19: 	color *= toneMappingExposure;
20: 	return saturate( color / ( vec3( 1.0 ) + color ) );
21: }
22: #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 ) )
23: vec3 Uncharted2ToneMapping( vec3 color ) {
24: 	color *= toneMappingExposure;
25: 	return saturate( Uncharted2Helper( color ) / Uncharted2Helper( vec3( toneMappingWhitePoint ) ) );
26: }
27: vec3 OptimizedCineonToneMapping( vec3 color ) {
28: 	color *= toneMappingExposure;
29: 	color = max( vec3( 0.0 ), color - 0.004 );
30: 	return pow( ( color * ( 6.2 * color + 0.5 ) ) / ( color * ( 6.2 * color + 1.7 ) + 0.06 ), vec3( 2.2 ) );
31: }
32:
33: vec3 toneMapping( vec3 color ) { return LinearToneMapping( color ); }
34:
35: vec4 LinearToLinear( in vec4 value ) {
36: 	return value;
37: }
38: vec4 GammaToLinear( in vec4 value, in float gammaFactor ) {
39: 	return vec4( pow( value.xyz, vec3( gammaFactor ) ), value.w );
40: }
41: vec4 LinearToGamma( in vec4 value, in float gammaFactor ) {
42: 	return vec4( pow( value.xyz, vec3( 1.0 / gammaFactor ) ), value.w );
43: }
44: vec4 sRGBToLinear( in vec4 value ) {
45: 	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 );
46: }
47: vec4 LinearTosRGB( in vec4 value ) {
48: 	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 );
49: }
50: vec4 RGBEToLinear( in vec4 value ) {
51: 	return vec4( value.rgb * exp2( value.a * 255.0 - 128.0 ), 1.0 );
52: }
53: vec4 LinearToRGBE( in vec4 value ) {
54: 	float maxComponent = max( max( value.r, value.g ), value.b );
55: 	float fExp = clamp( ceil( log2( maxComponent ) ), -128.0, 127.0 );
56: 	return vec4( value.rgb / exp2( fExp ), ( fExp + 128.0 ) / 255.0 );
57: }
58: vec4 RGBMToLinear( in vec4 value, in float maxRange ) {
59: 	return vec4( value.xyz * value.w * maxRange, 1.0 );
60: }
61: vec4 LinearToRGBM( in vec4 value, in float maxRange ) {
62: 	float maxRGB = max( value.x, max( value.g, value.b ) );
63: 	float M      = clamp( maxRGB / maxRange, 0.0, 1.0 );
64: 	M            = ceil( M * 255.0 ) / 255.0;
65: 	return vec4( value.rgb / ( M * maxRange ), M );
66: }
67: vec4 RGBDToLinear( in vec4 value, in float maxRange ) {
68: 	return vec4( value.rgb * ( ( maxRange / 255.0 ) / value.a ), 1.0 );
69: }
70: vec4 LinearToRGBD( in vec4 value, in float maxRange ) {
71: 	float maxRGB = max( value.x, max( value.g, value.b ) );
72: 	float D      = max( maxRange / maxRGB, 1.0 );
73: 	D            = min( floor( D ) / 255.0, 1.0 );
74: 	return vec4( value.rgb * ( D * ( 255.0 / maxRange ) ), D );
75: }
76: const mat3 cLogLuvM = mat3( 0.2209, 0.3390, 0.4184, 0.1138, 0.6780, 0.7319, 0.0102, 0.1130, 0.2969 );
77: vec4 LinearToLogLuv( in vec4 value )  {
78: 	vec3 Xp_Y_XYZp = value.rgb * cLogLuvM;
79: 	Xp_Y_XYZp = max(Xp_Y_XYZp, vec3(1e-6, 1e-6, 1e-6));
80: 	vec4 vResult;
81: 	vResult.xy = Xp_Y_XYZp.xy / Xp_Y_XYZp.z;
82: 	float Le = 2.0 * log2(Xp_Y_XYZp.y) + 127.0;
83: 	vResult.w = fract(Le);
84: 	vResult.z = (Le - (floor(vResult.w*255.0))/255.0)/255.0;
85: 	return vResult;
86: }
87: const mat3 cLogLuvInverseM = mat3( 6.0014, -2.7008, -1.7996, -1.3320, 3.1029, -5.7721, 0.3008, -1.0882, 5.6268 );
88: vec4 LogLuvToLinear( in vec4 value ) {
89: 	float Le = value.z * 255.0 + value.w;
90: 	vec3 Xp_Y_XYZp;
91: 	Xp_Y_XYZp.y = exp2((Le - 127.0) / 2.0);
92: 	Xp_Y_XYZp.z = Xp_Y_XYZp.y / value.y;
93: 	Xp_Y_XYZp.x = value.x * Xp_Y_XYZp.z;
94: 	vec3 vRGB = Xp_Y_XYZp.rgb * cLogLuvInverseM;
95: 	return vec4( max(vRGB, 0.0), 1.0 );
96: }
97:
98: vec4 mapTexelToLinear( vec4 value ) { return LinearToLinear( value ); }
99: vec4 envMapTexelToLinear( vec4 value ) { return LinearToLinear( value ); }
100: vec4 emissiveMapTexelToLinear( vec4 value ) { return LinearToLinear( value ); }
101: vec4 linearToOutputTexel( vec4 value ) { return LinearToLinear( value ); }
102:
103: #define PHONG
104: uniform vec3 diffuse;
105: uniform vec3 emissive;
106: uniform vec3 specular;
107: uniform float shininess;
108: uniform float opacity;
109: #define PI 3.14159265359
110: #define PI2 6.28318530718
111: #define PI_HALF 1.5707963267949
112: #define RECIPROCAL_PI 0.31830988618
113: #define RECIPROCAL_PI2 0.15915494
114: #define LOG2 1.442695
115: #define EPSILON 1e-6
116: #define saturate(a) clamp( a, 0.0, 1.0 )
117: #define whiteCompliment(a) ( 1.0 - saturate( a ) )
118: float pow2( const in float x ) { return x*x; }
119: float pow3( const in float x ) { return x*x*x; }
120: float pow4( const in float x ) { float x2 = x*x; return x2*x2; }
121: float average( const in vec3 color ) { return dot( color, vec3( 0.3333 ) ); }
122: highp float rand( const in vec2 uv ) {
123: 	const highp float a = 12.9898, b = 78.233, c = 43758.5453;
124: 	highp float dt = dot( uv.xy, vec2( a,b ) ), sn = mod( dt, PI );
125: 	return fract(sin(sn) * c);
126: }
127: struct IncidentLight {
128: 	vec3 color;
129: 	vec3 direction;
130: 	bool visible;
131: };
132: struct ReflectedLight {
133: 	vec3 directDiffuse;
134: 	vec3 directSpecular;
135: 	vec3 indirectDiffuse;
136: 	vec3 indirectSpecular;
137: };
138: struct GeometricContext {
139: 	vec3 position;
140: 	vec3 normal;
141: 	vec3 viewDir;
142: };
143: vec3 transformDirection( in vec3 dir, in mat4 matrix ) {
144: 	return normalize( ( matrix * vec4( dir, 0.0 ) ).xyz );
145: }
146: vec3 inverseTransformDirection( in vec3 dir, in mat4 matrix ) {
147: 	return normalize( ( vec4( dir, 0.0 ) * matrix ).xyz );
148: }
149: vec3 projectOnPlane(in vec3 point, in vec3 pointOnPlane, in vec3 planeNormal ) {
150: 	float distance = dot( planeNormal, point - pointOnPlane );
151: 	return - distance * planeNormal + point;
152: }
153: float sideOfPlane( in vec3 point, in vec3 pointOnPlane, in vec3 planeNormal ) {
154: 	return sign( dot( point - pointOnPlane, planeNormal ) );
155: }
156: vec3 linePlaneIntersect( in vec3 pointOnLine, in vec3 lineDirection, in vec3 pointOnPlane, in vec3 planeNormal ) {
157: 	return lineDirection * ( dot( planeNormal, pointOnPlane - pointOnLine ) / dot( planeNormal, lineDirection ) ) + pointOnLine;
158: }
159: mat3 transposeMat3( const in mat3 m ) {
160: 	mat3 tmp;
161: 	tmp[ 0 ] = vec3( m[ 0 ].x, m[ 1 ].x, m[ 2 ].x );
162: 	tmp[ 1 ] = vec3( m[ 0 ].y, m[ 1 ].y, m[ 2 ].y );
163: 	tmp[ 2 ] = vec3( m[ 0 ].z, m[ 1 ].z, m[ 2 ].z );
164: 	return tmp;
165: }
166: float linearToRelativeLuminance( const in vec3 color ) {
167: 	vec3 weights = vec3( 0.2126, 0.7152, 0.0722 );
168: 	return dot( weights, color.rgb );
169: }
170:
171: vec3 packNormalToRGB( const in vec3 normal ) {
172: 	return normalize( normal ) * 0.5 + 0.5;
173: }
174: vec3 unpackRGBToNormal( const in vec3 rgb ) {
175: 	return 2.0 * rgb.xyz - 1.0;
176: }
177: const float PackUpscale = 256. / 255.;const float UnpackDownscale = 255. / 256.;
178: const vec3 PackFactors = vec3( 256. * 256. * 256., 256. * 256.,  256. );
179: const vec4 UnpackFactors = UnpackDownscale / vec4( PackFactors, 1. );
180: const float ShiftRight8 = 1. / 256.;
181: vec4 packDepthToRGBA( const in float v ) {
182: 	vec4 r = vec4( fract( v * PackFactors ), v );
183: 	r.yzw -= r.xyz * ShiftRight8;	return r * PackUpscale;
184: }
185: float unpackRGBAToDepth( const in vec4 v ) {
186: 	return dot( v, UnpackFactors );
187: }
188: float viewZToOrthographicDepth( const in float viewZ, const in float near, const in float far ) {
189: 	return ( viewZ + near ) / ( near - far );
190: }
191: float orthographicDepthToViewZ( const in float linearClipZ, const in float near, const in float far ) {
192: 	return linearClipZ * ( near - far ) - near;
193: }
194: float viewZToPerspectiveDepth( const in float viewZ, const in float near, const in float far ) {
195: 	return (( near + viewZ ) * far ) / (( far - near ) * viewZ );
196: }
197: float perspectiveDepthToViewZ( const in float invClipZ, const in float near, const in float far ) {
198: 	return ( near * far ) / ( ( far - near ) * invClipZ - far );
199: }
200:
201: #if defined( DITHERING )
202: 	vec3 dithering( vec3 color ) {
203: 		float grid_position = rand( gl_FragCoord.xy );
204: 		vec3 dither_shift_RGB = vec3( 0.25 / 255.0, -0.25 / 255.0, 0.25 / 255.0 );
205: 		dither_shift_RGB = mix( 2.0 * dither_shift_RGB, -2.0 * dither_shift_RGB, grid_position );
206: 		return color + dither_shift_RGB;
207: 	}
208: #endif
209:
210: #ifdef USE_COLOR
211: 	varying vec3 vColor;
212: #endif
213:
214: #if defined( USE_MAP ) || defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( USE_SPECULARMAP ) || defined( USE_ALPHAMAP ) || defined( USE_EMISSIVEMAP ) || defined( USE_ROUGHNESSMAP ) || defined( USE_METALNESSMAP )
215: 	varying vec2 vUv;
216: #endif
217: #if defined( USE_LIGHTMAP ) || defined( USE_AOMAP )
218: 	varying vec2 vUv2;
219: #endif
220: #ifdef USE_MAP
221: 	uniform sampler2D map;
222: #endif
223:
224: #ifdef USE_ALPHAMAP
225: 	uniform sampler2D alphaMap;
226: #endif
227:
228: #ifdef USE_AOMAP
229: 	uniform sampler2D aoMap;
230: 	uniform float aoMapIntensity;
231: #endif
232: #ifdef USE_LIGHTMAP
233: 	uniform sampler2D lightMap;
234: 	uniform float lightMapIntensity;
235: #endif
236: #ifdef USE_EMISSIVEMAP
237: 	uniform sampler2D emissiveMap;
238: #endif
239:
240: #if defined( USE_ENVMAP ) || defined( PHYSICAL )
241: 	uniform float reflectivity;
242: 	uniform float envMapIntensity;
243: #endif
244: #ifdef USE_ENVMAP
245: 	#if ! defined( PHYSICAL ) && ( defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( PHONG ) )
246: 		varying vec3 vWorldPosition;
247: 	#endif
248: 	#ifdef ENVMAP_TYPE_CUBE
249: 		uniform samplerCube envMap;
250: 	#else
251: 		uniform sampler2D envMap;
252: 	#endif
253: 	uniform float flipEnvMap;
254: 	uniform int maxMipLevel;
255: 	#if defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( PHONG ) || defined( PHYSICAL )
256: 		uniform float refractionRatio;
257: 	#else
258: 		varying vec3 vReflect;
259: 	#endif
260: #endif
261:
262: #ifdef TOON
263: 	uniform sampler2D gradientMap;
264: 	vec3 getGradientIrradiance( vec3 normal, vec3 lightDirection ) {
265: 		float dotNL = dot( normal, lightDirection );
266: 		vec2 coord = vec2( dotNL * 0.5 + 0.5, 0.0 );
267: 		#ifdef USE_GRADIENTMAP
268: 			return texture2D( gradientMap, coord ).rgb;
269: 		#else
270: 			return ( coord.x < 0.7 ) ? vec3( 0.7 ) : vec3( 1.0 );
271: 		#endif
272: 	}
273: #endif
274:
275: #ifdef USE_FOG
276: 	uniform vec3 fogColor;
277: 	varying float fogDepth;
278: 	#ifdef FOG_EXP2
279: 		uniform float fogDensity;
280: 	#else
281: 		uniform float fogNear;
282: 		uniform float fogFar;
283: 	#endif
284: #endif
285:
286: float punctualLightIntensityToIrradianceFactor( const in float lightDistance, const in float cutoffDistance, const in float decayExponent ) {
287: 	if( decayExponent > 0.0 ) {
288: #if defined ( PHYSICALLY_CORRECT_LIGHTS )
289: 		float distanceFalloff = 1.0 / max( pow( lightDistance, decayExponent ), 0.01 );
290: 		float maxDistanceCutoffFactor = pow2( saturate( 1.0 - pow4( lightDistance / cutoffDistance ) ) );
291: 		return distanceFalloff * maxDistanceCutoffFactor;
292: #else
293: 		return pow( saturate( -lightDistance / cutoffDistance + 1.0 ), decayExponent );
294: #endif
295: 	}
296: 	return 1.0;
297: }
298: vec3 BRDF_Diffuse_Lambert( const in vec3 diffuseColor ) {
299: 	return RECIPROCAL_PI * diffuseColor;
300: }
301: vec3 F_Schlick( const in vec3 specularColor, const in float dotLH ) {
302: 	float fresnel = exp2( ( -5.55473 * dotLH - 6.98316 ) * dotLH );
303: 	return ( 1.0 - specularColor ) * fresnel + specularColor;
304: }
305: float G_GGX_Smith( const in float alpha, const in float dotNL, const in float dotNV ) {
306: 	float a2 = pow2( alpha );
307: 	float gl = dotNL + sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNL ) );
308: 	float gv = dotNV + sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNV ) );
309: 	return 1.0 / ( gl * gv );
310: }
311: float G_GGX_SmithCorrelated( const in float alpha, const in float dotNL, const in float dotNV ) {
312: 	float a2 = pow2( alpha );
313: 	float gv = dotNL * sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNV ) );
314: 	float gl = dotNV * sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNL ) );
315: 	return 0.5 / max( gv + gl, EPSILON );
316: }
317: float D_GGX( const in float alpha, const in float dotNH ) {
318: 	float a2 = pow2( alpha );
319: 	float denom = pow2( dotNH ) * ( a2 - 1.0 ) + 1.0;
320: 	return RECIPROCAL_PI * a2 / pow2( denom );
321: }
322: vec3 BRDF_Specular_GGX( const in IncidentLight incidentLight, const in GeometricContext geometry, const in vec3 specularColor, const in float roughness ) {
323: 	float alpha = pow2( roughness );
324: 	vec3 halfDir = normalize( incidentLight.direction + geometry.viewDir );
325: 	float dotNL = saturate( dot( geometry.normal, incidentLight.direction ) );
326: 	float dotNV = saturate( dot( geometry.normal, geometry.viewDir ) );
327: 	float dotNH = saturate( dot( geometry.normal, halfDir ) );
328: 	float dotLH = saturate( dot( incidentLight.direction, halfDir ) );
329: 	vec3 F = F_Schlick( specularColor, dotLH );
330: 	float G = G_GGX_SmithCorrelated( alpha, dotNL, dotNV );
331: 	float D = D_GGX( alpha, dotNH );
332: 	return F * ( G * D );
333: }
334: vec2 LTC_Uv( const in vec3 N, const in vec3 V, const in float roughness ) {
335: 	const float LUT_SIZE  = 64.0;
336: 	const float LUT_SCALE = ( LUT_SIZE - 1.0 ) / LUT_SIZE;
337: 	const float LUT_BIAS  = 0.5 / LUT_SIZE;
338: 	float dotNV = saturate( dot( N, V ) );
339: 	vec2 uv = vec2( roughness, sqrt( 1.0 - dotNV ) );
340: 	uv = uv * LUT_SCALE + LUT_BIAS;
341: 	return uv;
342: }
343: float LTC_ClippedSphereFormFactor( const in vec3 f ) {
344: 	float l = length( f );
345: 	return max( ( l * l + f.z ) / ( l + 1.0 ), 0.0 );
346: }
347: vec3 LTC_EdgeVectorFormFactor( const in vec3 v1, const in vec3 v2 ) {
348: 	float x = dot( v1, v2 );
349: 	float y = abs( x );
350: 	float a = 0.8543985 + ( 0.4965155 + 0.0145206 * y ) * y;
351: 	float b = 3.4175940 + ( 4.1616724 + y ) * y;
352: 	float v = a / b;
353: 	float theta_sintheta = ( x > 0.0 ) ? v : 0.5 * inversesqrt( max( 1.0 - x * x, 1e-7 ) ) - v;
354: 	return cross( v1, v2 ) * theta_sintheta;
355: }
356: vec3 LTC_Evaluate( const in vec3 N, const in vec3 V, const in vec3 P, const in mat3 mInv, const in vec3 rectCoords[ 4 ] ) {
357: 	vec3 v1 = rectCoords[ 1 ] - rectCoords[ 0 ];
358: 	vec3 v2 = rectCoords[ 3 ] - rectCoords[ 0 ];
359: 	vec3 lightNormal = cross( v1, v2 );
360: 	if( dot( lightNormal, P - rectCoords[ 0 ] ) < 0.0 ) return vec3( 0.0 );
361: 	vec3 T1, T2;
362: 	T1 = normalize( V - N * dot( V, N ) );
363: 	T2 = - cross( N, T1 );
364: 	mat3 mat = mInv * transposeMat3( mat3( T1, T2, N ) );
365: 	vec3 coords[ 4 ];
366: 	coords[ 0 ] = mat * ( rectCoords[ 0 ] - P );
367: 	coords[ 1 ] = mat * ( rectCoords[ 1 ] - P );
368: 	coords[ 2 ] = mat * ( rectCoords[ 2 ] - P );
369: 	coords[ 3 ] = mat * ( rectCoords[ 3 ] - P );
370: 	coords[ 0 ] = normalize( coords[ 0 ] );
371: 	coords[ 1 ] = normalize( coords[ 1 ] );
372: 	coords[ 2 ] = normalize( coords[ 2 ] );
373: 	coords[ 3 ] = normalize( coords[ 3 ] );
374: 	vec3 vectorFormFactor = vec3( 0.0 );
375: 	vectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 0 ], coords[ 1 ] );
376: 	vectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 1 ], coords[ 2 ] );
377: 	vectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 2 ], coords[ 3 ] );
378: 	vectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 3 ], coords[ 0 ] );
379: 	float result = LTC_ClippedSphereFormFactor( vectorFormFactor );
380: 	return vec3( result );
381: }
382: vec3 BRDF_Specular_GGX_Environment( const in GeometricContext geometry, const in vec3 specularColor, const in float roughness ) {
383: 	float dotNV = saturate( dot( geometry.normal, geometry.viewDir ) );
384: 	const vec4 c0 = vec4( - 1, - 0.0275, - 0.572, 0.022 );
385: 	const vec4 c1 = vec4( 1, 0.0425, 1.04, - 0.04 );
386: 	vec4 r = roughness * c0 + c1;
387: 	float a004 = min( r.x * r.x, exp2( - 9.28 * dotNV ) ) * r.x + r.y;
388: 	vec2 AB = vec2( -1.04, 1.04 ) * a004 + r.zw;
389: 	return specularColor * AB.x + AB.y;
390: }
391: float G_BlinnPhong_Implicit( ) {
392: 	return 0.25;
393: }
394: float D_BlinnPhong( const in float shininess, const in float dotNH ) {
395: 	return RECIPROCAL_PI * ( shininess * 0.5 + 1.0 ) * pow( dotNH, shininess );
396: }
397: vec3 BRDF_Specular_BlinnPhong( const in IncidentLight incidentLight, const in GeometricContext geometry, const in vec3 specularColor, const in float shininess ) {
398: 	vec3 halfDir = normalize( incidentLight.direction + geometry.viewDir );
399: 	float dotNH = saturate( dot( geometry.normal, halfDir ) );
400: 	float dotLH = saturate( dot( incidentLight.direction, halfDir ) );
401: 	vec3 F = F_Schlick( specularColor, dotLH );
402: 	float G = G_BlinnPhong_Implicit( );
403: 	float D = D_BlinnPhong( shininess, dotNH );
404: 	return F * ( G * D );
405: }
406: float GGXRoughnessToBlinnExponent( const in float ggxRoughness ) {
407: 	return ( 2.0 / pow2( ggxRoughness + 0.0001 ) - 2.0 );
408: }
409: float BlinnExponentToGGXRoughness( const in float blinnExponent ) {
410: 	return sqrt( 2.0 / ( blinnExponent + 2.0 ) );
411: }
412:
413: uniform vec3 ambientLightColor;
414: vec3 getAmbientLightIrradiance( const in vec3 ambientLightColor ) {
415: 	vec3 irradiance = ambientLightColor;
416: 	#ifndef PHYSICALLY_CORRECT_LIGHTS
417: 		irradiance *= PI;
418: 	#endif
419: 	return irradiance;
420: }
421: #if 1 > 0
422: 	struct DirectionalLight {
423: 		vec3 direction;
424: 		vec3 color;
425: 		int shadow;
426: 		float shadowBias;
427: 		float shadowRadius;
428: 		vec2 shadowMapSize;
429: 	};
430: 	uniform DirectionalLight directionalLights[ 1 ];
431: 	void getDirectionalDirectLightIrradiance( const in DirectionalLight directionalLight, const in GeometricContext geometry, out IncidentLight directLight ) {
432: 		directLight.color = directionalLight.color;
433: 		directLight.direction = directionalLight.direction;
434: 		directLight.visible = true;
435: 	}
436: #endif
437: #if 0 > 0
438: 	struct PointLight {
439: 		vec3 position;
440: 		vec3 color;
441: 		float distance;
442: 		float decay;
443: 		int shadow;
444: 		float shadowBias;
445: 		float shadowRadius;
446: 		vec2 shadowMapSize;
447: 		float shadowCameraNear;
448: 		float shadowCameraFar;
449: 	};
450: 	uniform PointLight pointLights[ 0 ];
451: 	void getPointDirectLightIrradiance( const in PointLight pointLight, const in GeometricContext geometry, out IncidentLight directLight ) {
452: 		vec3 lVector = pointLight.position - geometry.position;
453: 		directLight.direction = normalize( lVector );
454: 		float lightDistance = length( lVector );
455: 		directLight.color = pointLight.color;
456: 		directLight.color *= punctualLightIntensityToIrradianceFactor( lightDistance, pointLight.distance, pointLight.decay );
457: 		directLight.visible = ( directLight.color != vec3( 0.0 ) );
458: 	}
459: #endif
460: #if 1 > 0
461: 	struct SpotLight {
462: 		vec3 position;
463: 		vec3 direction;
464: 		vec3 color;
465: 		float distance;
466: 		float decay;
467: 		float coneCos;
468: 		float penumbraCos;
469: 		int shadow;
470: 		float shadowBias;
471: 		float shadowRadius;
472: 		vec2 shadowMapSize;
473: 	};
474: 	uniform SpotLight spotLights[ 1 ];
475: 	void getSpotDirectLightIrradiance( const in SpotLight spotLight, const in GeometricContext geometry, out IncidentLight directLight  ) {
476: 		vec3 lVector = spotLight.position - geometry.position;
477: 		directLight.direction = normalize( lVector );
478: 		float lightDistance = length( lVector );
479: 		float angleCos = dot( directLight.direction, spotLight.direction );
480: 		if ( angleCos > spotLight.coneCos ) {
481: 			float spotEffect = smoothstep( spotLight.coneCos, spotLight.penumbraCos, angleCos );
482: 			directLight.color = spotLight.color;
483: 			directLight.color *= spotEffect * punctualLightIntensityToIrradianceFactor( lightDistance, spotLight.distance, spotLight.decay );
484: 			directLight.visible = true;
485: 		} else {
486: 			directLight.color = vec3( 0.0 );
487: 			directLight.visible = false;
488: 		}
489: 	}
490: #endif
491: #if 0 > 0
492: 	struct RectAreaLight {
493: 		vec3 color;
494: 		vec3 position;
495: 		vec3 halfWidth;
496: 		vec3 halfHeight;
497: 	};
498: 	uniform sampler2D ltc_1;	uniform sampler2D ltc_2;
499: 	uniform RectAreaLight rectAreaLights[ 0 ];
500: #endif
501: #if 0 > 0
502: 	struct HemisphereLight {
503: 		vec3 direction;
504: 		vec3 skyColor;
505: 		vec3 groundColor;
506: 	};
507: 	uniform HemisphereLight hemisphereLights[ 0 ];
508: 	vec3 getHemisphereLightIrradiance( const in HemisphereLight hemiLight, const in GeometricContext geometry ) {
509: 		float dotNL = dot( geometry.normal, hemiLight.direction );
510: 		float hemiDiffuseWeight = 0.5 * dotNL + 0.5;
511: 		vec3 irradiance = mix( hemiLight.groundColor, hemiLight.skyColor, hemiDiffuseWeight );
512: 		#ifndef PHYSICALLY_CORRECT_LIGHTS
513: 			irradiance *= PI;
514: 		#endif
515: 		return irradiance;
516: 	}
517: #endif
518:
519: #if defined( USE_ENVMAP ) && defined( PHYSICAL )
520: 	vec3 getLightProbeIndirectIrradiance( const in GeometricContext geometry, const in int maxMIPLevel ) {
521: 		vec3 worldNormal = inverseTransformDirection( geometry.normal, viewMatrix );
522: 		#ifdef ENVMAP_TYPE_CUBE
523: 			vec3 queryVec = vec3( flipEnvMap * worldNormal.x, worldNormal.yz );
524: 			#ifdef TEXTURE_LOD_EXT
525: 				vec4 envMapColor = textureCubeLodEXT( envMap, queryVec, float( maxMIPLevel ) );
526: 			#else
527: 				vec4 envMapColor = textureCube( envMap, queryVec, float( maxMIPLevel ) );
528: 			#endif
529: 			envMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;
530: 		#elif defined( ENVMAP_TYPE_CUBE_UV )
531: 			vec3 queryVec = vec3( flipEnvMap * worldNormal.x, worldNormal.yz );
532: 			vec4 envMapColor = textureCubeUV( queryVec, 1.0 );
533: 		#else
534: 			vec4 envMapColor = vec4( 0.0 );
535: 		#endif
536: 		return PI * envMapColor.rgb * envMapIntensity;
537: 	}
538: 	float getSpecularMIPLevel( const in float blinnShininessExponent, const in int maxMIPLevel ) {
539: 		float maxMIPLevelScalar = float( maxMIPLevel );
540: 		float desiredMIPLevel = maxMIPLevelScalar + 0.79248 - 0.5 * log2( pow2( blinnShininessExponent ) + 1.0 );
541: 		return clamp( desiredMIPLevel, 0.0, maxMIPLevelScalar );
542: 	}
543: 	vec3 getLightProbeIndirectRadiance( const in GeometricContext geometry, const in float blinnShininessExponent, const in int maxMIPLevel ) {
544: 		#ifdef ENVMAP_MODE_REFLECTION
545: 			vec3 reflectVec = reflect( -geometry.viewDir, geometry.normal );
546: 		#else
547: 			vec3 reflectVec = refract( -geometry.viewDir, geometry.normal, refractionRatio );
548: 		#endif
549: 		reflectVec = inverseTransformDirection( reflectVec, viewMatrix );
550: 		float specularMIPLevel = getSpecularMIPLevel( blinnShininessExponent, maxMIPLevel );
551: 		#ifdef ENVMAP_TYPE_CUBE
552: 			vec3 queryReflectVec = vec3( flipEnvMap * reflectVec.x, reflectVec.yz );
553: 			#ifdef TEXTURE_LOD_EXT
554: 				vec4 envMapColor = textureCubeLodEXT( envMap, queryReflectVec, specularMIPLevel );
555: 			#else
556: 				vec4 envMapColor = textureCube( envMap, queryReflectVec, specularMIPLevel );
557: 			#endif
558: 			envMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;
559: 		#elif defined( ENVMAP_TYPE_CUBE_UV )
560: 			vec3 queryReflectVec = vec3( flipEnvMap * reflectVec.x, reflectVec.yz );
561: 			vec4 envMapColor = textureCubeUV(queryReflectVec, BlinnExponentToGGXRoughness(blinnShininessExponent));
562: 		#elif defined( ENVMAP_TYPE_EQUIREC )
563: 			vec2 sampleUV;
564: 			sampleUV.y = asin( clamp( reflectVec.y, - 1.0, 1.0 ) ) * RECIPROCAL_PI + 0.5;
565: 			sampleUV.x = atan( reflectVec.z, reflectVec.x ) * RECIPROCAL_PI2 + 0.5;
566: 			#ifdef TEXTURE_LOD_EXT
567: 				vec4 envMapColor = texture2DLodEXT( envMap, sampleUV, specularMIPLevel );
568: 			#else
569: 				vec4 envMapColor = texture2D( envMap, sampleUV, specularMIPLevel );
570: 			#endif
571: 			envMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;
572: 		#elif defined( ENVMAP_TYPE_SPHERE )
573: 			vec3 reflectView = normalize( ( viewMatrix * vec4( reflectVec, 0.0 ) ).xyz + vec3( 0.0,0.0,1.0 ) );
574: 			#ifdef TEXTURE_LOD_EXT
575: 				vec4 envMapColor = texture2DLodEXT( envMap, reflectView.xy * 0.5 + 0.5, specularMIPLevel );
576: 			#else
577: 				vec4 envMapColor = texture2D( envMap, reflectView.xy * 0.5 + 0.5, specularMIPLevel );
578: 			#endif
579: 			envMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;
580: 		#endif
581: 		return envMapColor.rgb * envMapIntensity;
582: 	}
583: #endif
584:
585: varying vec3 vViewPosition;
586: #ifndef FLAT_SHADED
587: 	varying vec3 vNormal;
588: #endif
589: struct BlinnPhongMaterial {
590: 	vec3	diffuseColor;
591: 	vec3	specularColor;
592: 	float	specularShininess;
593: 	float	specularStrength;
594: };
595: void RE_Direct_BlinnPhong( const in IncidentLight directLight, const in GeometricContext geometry, const in BlinnPhongMaterial material, inout ReflectedLight reflectedLight ) {
596: 	#ifdef TOON
597: 		vec3 irradiance = getGradientIrradiance( geometry.normal, directLight.direction ) * directLight.color;
598: 	#else
599: 		float dotNL = saturate( dot( geometry.normal, directLight.direction ) );
600: 		vec3 irradiance = dotNL * directLight.color;
601: 	#endif
602: 	#ifndef PHYSICALLY_CORRECT_LIGHTS
603: 		irradiance *= PI;
604: 	#endif
605: 	reflectedLight.directDiffuse += irradiance * BRDF_Diffuse_Lambert( material.diffuseColor );
606: 	reflectedLight.directSpecular += irradiance * BRDF_Specular_BlinnPhong( directLight, geometry, material.specularColor, material.specularShininess ) * material.specularStrength;
607: }
608: void RE_IndirectDiffuse_BlinnPhong( const in vec3 irradiance, const in GeometricContext geometry, const in BlinnPhongMaterial material, inout ReflectedLight reflectedLight ) {
609: 	reflectedLight.indirectDiffuse += irradiance * BRDF_Diffuse_Lambert( material.diffuseColor );
610: }
611: #define RE_Direct				RE_Direct_BlinnPhong
612: #define RE_IndirectDiffuse		RE_IndirectDiffuse_BlinnPhong
613: #define Material_LightProbeLOD( material )	(0)
614:
615: #ifdef USE_SHADOWMAP
616: 	#if 1 > 0
617: 		uniform sampler2D directionalShadowMap[ 1 ];
618: 		varying vec4 vDirectionalShadowCoord[ 1 ];
619: 	#endif
620: 	#if 1 > 0
621: 		uniform sampler2D spotShadowMap[ 1 ];
622: 		varying vec4 vSpotShadowCoord[ 1 ];
623: 	#endif
624: 	#if 0 > 0
625: 		uniform sampler2D pointShadowMap[ 0 ];
626: 		varying vec4 vPointShadowCoord[ 0 ];
627: 	#endif
628: 	float texture2DCompare( sampler2D depths, vec2 uv, float compare ) {
629: 		return step( compare, unpackRGBAToDepth( texture2D( depths, uv ) ) );
630: 	}
631: 	float texture2DShadowLerp( sampler2D depths, vec2 size, vec2 uv, float compare ) {
632: 		const vec2 offset = vec2( 0.0, 1.0 );
633: 		vec2 texelSize = vec2( 1.0 ) / size;
634: 		vec2 centroidUV = floor( uv * size + 0.5 ) / size;
635: 		float lb = texture2DCompare( depths, centroidUV + texelSize * offset.xx, compare );
636: 		float lt = texture2DCompare( depths, centroidUV + texelSize * offset.xy, compare );
637: 		float rb = texture2DCompare( depths, centroidUV + texelSize * offset.yx, compare );
638: 		float rt = texture2DCompare( depths, centroidUV + texelSize * offset.yy, compare );
639: 		vec2 f = fract( uv * size + 0.5 );
640: 		float a = mix( lb, lt, f.y );
641: 		float b = mix( rb, rt, f.y );
642: 		float c = mix( a, b, f.x );
643: 		return c;
644: 	}
645: 	float getShadow( sampler2D shadowMap, vec2 shadowMapSize, float shadowBias, float shadowRadius, vec4 shadowCoord ) {
646: 		float shadow = 1.0;
647: 		shadowCoord.xyz /= shadowCoord.w;
648: 		shadowCoord.z += shadowBias;
649: 		bvec4 inFrustumVec = bvec4 ( shadowCoord.x >= 0.0, shadowCoord.x <= 1.0, shadowCoord.y >= 0.0, shadowCoord.y <= 1.0 );
650: 		bool inFrustum = all( inFrustumVec );
651: 		bvec2 frustumTestVec = bvec2( inFrustum, shadowCoord.z <= 1.0 );
652: 		bool frustumTest = all( frustumTestVec );
653: 		if ( frustumTest ) {
654: 		#if defined( SHADOWMAP_TYPE_PCF )
655: 			vec2 texelSize = vec2( 1.0 ) / shadowMapSize;
656: 			float dx0 = - texelSize.x * shadowRadius;
657: 			float dy0 = - texelSize.y * shadowRadius;
658: 			float dx1 = + texelSize.x * shadowRadius;
659: 			float dy1 = + texelSize.y * shadowRadius;
660: 			shadow = (
661: 				texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx0, dy0 ), shadowCoord.z ) +
662: 				texture2DCompare( shadowMap, shadowCoord.xy + vec2( 0.0, dy0 ), shadowCoord.z ) +
663: 				texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx1, dy0 ), shadowCoord.z ) +
664: 				texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx0, 0.0 ), shadowCoord.z ) +
665: 				texture2DCompare( shadowMap, shadowCoord.xy, shadowCoord.z ) +
666: 				texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx1, 0.0 ), shadowCoord.z ) +
667: 				texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx0, dy1 ), shadowCoord.z ) +
668: 				texture2DCompare( shadowMap, shadowCoord.xy + vec2( 0.0, dy1 ), shadowCoord.z ) +
669: 				texture2DCompare( shadowMap, shadowCoord.xy + vec2( dx1, dy1 ), shadowCoord.z )
670: 			) * ( 1.0 / 9.0 );
671: 		#elif defined( SHADOWMAP_TYPE_PCF_SOFT )
672: 			vec2 texelSize = vec2( 1.0 ) / shadowMapSize;
673: 			float dx0 = - texelSize.x * shadowRadius;
674: 			float dy0 = - texelSize.y * shadowRadius;
675: 			float dx1 = + texelSize.x * shadowRadius;
676: 			float dy1 = + texelSize.y * shadowRadius;
677: 			shadow = (
678: 				texture2DShadowLerp( shadowMap, shadowMapSize, shadowCoord.xy + vec2( dx0, dy0 ), shadowCoord.z ) +
679: 				texture2DShadowLerp( shadowMap, shadowMapSize, shadowCoord.xy + vec2( 0.0, dy0 ), shadowCoord.z ) +
680: 				texture2DShadowLerp( shadowMap, shadowMapSize, shadowCoord.xy + vec2( dx1, dy0 ), shadowCoord.z ) +
681: 				texture2DShadowLerp( shadowMap, shadowMapSize, shadowCoord.xy + vec2( dx0, 0.0 ), shadowCoord.z ) +
682: 				texture2DShadowLerp( shadowMap, shadowMapSize, shadowCoord.xy, shadowCoord.z ) +
683: 				texture2DShadowLerp( shadowMap, shadowMapSize, shadowCoord.xy + vec2( dx1, 0.0 ), shadowCoord.z ) +
684: 				texture2DShadowLerp( shadowMap, shadowMapSize, shadowCoord.xy + vec2( dx0, dy1 ), shadowCoord.z ) +
685: 				texture2DShadowLerp( shadowMap, shadowMapSize, shadowCoord.xy + vec2( 0.0, dy1 ), shadowCoord.z ) +
686: 				texture2DShadowLerp( shadowMap, shadowMapSize, shadowCoord.xy + vec2( dx1, dy1 ), shadowCoord.z )
687: 			) * ( 1.0 / 9.0 );
688: 		#else
689: 			shadow = texture2DCompare( shadowMap, shadowCoord.xy, shadowCoord.z );
690: 		#endif
691: 		}
692: 		return shadow;
693: 	}
694: 	vec2 cubeToUV( vec3 v, float texelSizeY ) {
695: 		vec3 absV = abs( v );
696: 		float scaleToCube = 1.0 / max( absV.x, max( absV.y, absV.z ) );
697: 		absV *= scaleToCube;
698: 		v *= scaleToCube * ( 1.0 - 2.0 * texelSizeY );
699: 		vec2 planar = v.xy;
700: 		float almostATexel = 1.5 * texelSizeY;
701: 		float almostOne = 1.0 - almostATexel;
702: 		if ( absV.z >= almostOne ) {
703: 			if ( v.z > 0.0 )
704: 				planar.x = 4.0 - v.x;
705: 		} else if ( absV.x >= almostOne ) {
706: 			float signX = sign( v.x );
707: 			planar.x = v.z * signX + 2.0 * signX;
708: 		} else if ( absV.y >= almostOne ) {
709: 			float signY = sign( v.y );
710: 			planar.x = v.x + 2.0 * signY + 2.0;
711: 			planar.y = v.z * signY - 2.0;
712: 		}
713: 		return vec2( 0.125, 0.25 ) * planar + vec2( 0.375, 0.75 );
714: 	}
715: 	float getPointShadow( sampler2D shadowMap, vec2 shadowMapSize, float shadowBias, float shadowRadius, vec4 shadowCoord, float shadowCameraNear, float shadowCameraFar ) {
716: 		vec2 texelSize = vec2( 1.0 ) / ( shadowMapSize * vec2( 4.0, 2.0 ) );
717: 		vec3 lightToPosition = shadowCoord.xyz;
718: 		float dp = ( length( lightToPosition ) - shadowCameraNear ) / ( shadowCameraFar - shadowCameraNear );		dp += shadowBias;
719: 		vec3 bd3D = normalize( lightToPosition );
720: 		#if defined( SHADOWMAP_TYPE_PCF ) || defined( SHADOWMAP_TYPE_PCF_SOFT )
721: 			vec2 offset = vec2( - 1, 1 ) * shadowRadius * texelSize.y;
722: 			return (
723: 				texture2DCompare( shadowMap, cubeToUV( bd3D + offset.xyy, texelSize.y ), dp ) +
724: 				texture2DCompare( shadowMap, cubeToUV( bd3D + offset.yyy, texelSize.y ), dp ) +
725: 				texture2DCompare( shadowMap, cubeToUV( bd3D + offset.xyx, texelSize.y ), dp ) +
726: 				texture2DCompare( shadowMap, cubeToUV( bd3D + offset.yyx, texelSize.y ), dp ) +
727: 				texture2DCompare( shadowMap, cubeToUV( bd3D, texelSize.y ), dp ) +
728: 				texture2DCompare( shadowMap, cubeToUV( bd3D + offset.xxy, texelSize.y ), dp ) +
729: 				texture2DCompare( shadowMap, cubeToUV( bd3D + offset.yxy, texelSize.y ), dp ) +
730: 				texture2DCompare( shadowMap, cubeToUV( bd3D + offset.xxx, texelSize.y ), dp ) +
731: 				texture2DCompare( shadowMap, cubeToUV( bd3D + offset.yxx, texelSize.y ), dp )
732: 			) * ( 1.0 / 9.0 );
733: 		#else
734: 			return texture2DCompare( shadowMap, cubeToUV( bd3D, texelSize.y ), dp );
735: 		#endif
736: 	}
737: #endif
738:
739: #ifdef USE_BUMPMAP
740: 	uniform sampler2D bumpMap;
741: 	uniform float bumpScale;
742: 	vec2 dHdxy_fwd() {
743: 		vec2 dSTdx = dFdx( vUv );
744: 		vec2 dSTdy = dFdy( vUv );
745: 		float Hll = bumpScale * texture2D( bumpMap, vUv ).x;
746: 		float dBx = bumpScale * texture2D( bumpMap, vUv + dSTdx ).x - Hll;
747: 		float dBy = bumpScale * texture2D( bumpMap, vUv + dSTdy ).x - Hll;
748: 		return vec2( dBx, dBy );
749: 	}
750: 	vec3 perturbNormalArb( vec3 surf_pos, vec3 surf_norm, vec2 dHdxy ) {
751: 		vec3 vSigmaX = vec3( dFdx( surf_pos.x ), dFdx( surf_pos.y ), dFdx( surf_pos.z ) );
752: 		vec3 vSigmaY = vec3( dFdy( surf_pos.x ), dFdy( surf_pos.y ), dFdy( surf_pos.z ) );
753: 		vec3 vN = surf_norm;
754: 		vec3 R1 = cross( vSigmaY, vN );
755: 		vec3 R2 = cross( vN, vSigmaX );
756: 		float fDet = dot( vSigmaX, R1 );
757: 		fDet *= ( float( gl_FrontFacing ) * 2.0 - 1.0 );
758: 		vec3 vGrad = sign( fDet ) * ( dHdxy.x * R1 + dHdxy.y * R2 );
759: 		return normalize( abs( fDet ) * surf_norm - vGrad );
760: 	}
761: #endif
762:
763: #ifdef USE_NORMALMAP
764: 	uniform sampler2D normalMap;
765: 	uniform vec2 normalScale;
766: 	vec3 perturbNormal2Arb( vec3 eye_pos, vec3 surf_norm ) {
767: 		vec3 q0 = vec3( dFdx( eye_pos.x ), dFdx( eye_pos.y ), dFdx( eye_pos.z ) );
768: 		vec3 q1 = vec3( dFdy( eye_pos.x ), dFdy( eye_pos.y ), dFdy( eye_pos.z ) );
769: 		vec2 st0 = dFdx( vUv.st );
770: 		vec2 st1 = dFdy( vUv.st );
771: 		float scale = sign( st1.t * st0.s - st0.t * st1.s );
772: 		vec3 S = normalize( ( q0 * st1.t - q1 * st0.t ) * scale );
773: 		vec3 T = normalize( ( - q0 * st1.s + q1 * st0.s ) * scale );
774: 		vec3 N = normalize( surf_norm );
775: 		mat3 tsn = mat3( S, T, N );
776: 		vec3 mapN = texture2D( normalMap, vUv ).xyz * 2.0 - 1.0;
777: 		mapN.xy *= normalScale;
778: 		mapN.xy *= ( float( gl_FrontFacing ) * 2.0 - 1.0 );
779: 		return normalize( tsn * mapN );
780: 	}
781: #endif
782:
783: #ifdef USE_SPECULARMAP
784: 	uniform sampler2D specularMap;
785: #endif
786: #ifdef USE_LOGDEPTHBUF
787: 	uniform float logDepthBufFC;
788: 	#ifdef USE_LOGDEPTHBUF_EXT
789: 		varying float vFragDepth;
790: 	#endif
791: #endif
792:
793: #if 0 > 0
794: 	#if ! defined( PHYSICAL ) && ! defined( PHONG )
795: 		varying vec3 vViewPosition;
796: 	#endif
797: 	uniform vec4 clippingPlanes[ 0 ];
798: #endif
799:
800: void main() {
801: #if 0 > 0
802: 	vec4 plane;
803:
804: 	#if 0 < 0
805: 		bool clipped = true;
806:
807: 		if ( clipped ) discard;
808: 	#endif
809: #endif
810:
811: 	vec4 diffuseColor = vec4( diffuse, opacity );
812: 	ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );
813: 	vec3 totalEmissiveRadiance = emissive;
814: #if defined( USE_LOGDEPTHBUF ) && defined( USE_LOGDEPTHBUF_EXT )
815: 	gl_FragDepthEXT = log2( vFragDepth ) * logDepthBufFC * 0.5;
816: #endif
817: #ifdef USE_MAP
818: 	vec4 texelColor = texture2D( map, vUv );
819: 	texelColor = mapTexelToLinear( texelColor );
820: 	diffuseColor *= texelColor;
821: #endif
822:
823: #ifdef USE_COLOR
824: 	diffuseColor.rgb *= vColor;
825: #endif
826: #ifdef USE_ALPHAMAP
827: 	diffuseColor.a *= texture2D( alphaMap, vUv ).g;
828: #endif
829:
830: #ifdef ALPHATEST
831: 	if ( diffuseColor.a < ALPHATEST ) discard;
832: #endif
833:
834: float specularStrength;
835: #ifdef USE_SPECULARMAP
836: 	vec4 texelSpecular = texture2D( specularMap, vUv );
837: 	specularStrength = texelSpecular.r;
838: #else
839: 	specularStrength = 1.0;
840: #endif
841: #ifdef FLAT_SHADED
842: 	vec3 fdx = vec3( dFdx( vViewPosition.x ), dFdx( vViewPosition.y ), dFdx( vViewPosition.z ) );
843: 	vec3 fdy = vec3( dFdy( vViewPosition.x ), dFdy( vViewPosition.y ), dFdy( vViewPosition.z ) );
844: 	vec3 normal = normalize( cross( fdx, fdy ) );
845: #else
846: 	vec3 normal = normalize( vNormal );
847: 	#ifdef DOUBLE_SIDED
848: 		normal = normal * ( float( gl_FrontFacing ) * 2.0 - 1.0 );
849: 	#endif
850: #endif
851:
852: #ifdef USE_NORMALMAP
853: 	normal = perturbNormal2Arb( -vViewPosition, normal );
854: #elif defined( USE_BUMPMAP )
855: 	normal = perturbNormalArb( -vViewPosition, normal, dHdxy_fwd() );
856: #endif
857:
858: #ifdef USE_EMISSIVEMAP
859: 	vec4 emissiveColor = texture2D( emissiveMap, vUv );
860: 	emissiveColor.rgb = emissiveMapTexelToLinear( emissiveColor ).rgb;
861: 	totalEmissiveRadiance *= emissiveColor.rgb;
862: #endif
863:
864: BlinnPhongMaterial material;
865: material.diffuseColor = diffuseColor.rgb;
866: material.specularColor = specular;
867: material.specularShininess = shininess;
868: material.specularStrength = specularStrength;
869:
870:
871: GeometricContext geometry;
872: geometry.position = - vViewPosition;
873: geometry.normal = normal;
874: geometry.viewDir = normalize( vViewPosition );
875: IncidentLight directLight;
876: #if ( 0 > 0 ) && defined( RE_Direct )
877: 	PointLight pointLight;
878:
879: #endif
880: #if ( 1 > 0 ) && defined( RE_Direct )
881: 	SpotLight spotLight;
882:
883: 		spotLight = spotLights[ 0 ];
884: 		getSpotDirectLightIrradiance( spotLight, geometry, directLight );
885: 		#ifdef USE_SHADOWMAP
886: 		directLight.color *= all( bvec2( spotLight.shadow, directLight.visible ) ) ? getShadow( spotShadowMap[ 0 ], spotLight.shadowMapSize, spotLight.shadowBias, spotLight.shadowRadius, vSpotShadowCoord[ 0 ] ) : 1.0;
887: 		#endif
888: 		RE_Direct( directLight, geometry, material, reflectedLight );
889:
890: #endif
891: #if ( 1 > 0 ) && defined( RE_Direct )
892: 	DirectionalLight directionalLight;
893:
894: 		directionalLight = directionalLights[ 0 ];
895: 		getDirectionalDirectLightIrradiance( directionalLight, geometry, directLight );
896: 		#ifdef USE_SHADOWMAP
897: 		directLight.color *= all( bvec2( directionalLight.shadow, directLight.visible ) ) ? getShadow( directionalShadowMap[ 0 ], directionalLight.shadowMapSize, directionalLight.shadowBias, directionalLight.shadowRadius, vDirectionalShadowCoord[ 0 ] ) : 1.0;
898: 		#endif
899: 		RE_Direct( directLight, geometry, material, reflectedLight );
900:
901: #endif
902: #if ( 0 > 0 ) && defined( RE_Direct_RectArea )
903: 	RectAreaLight rectAreaLight;
904:
905: #endif
906: #if defined( RE_IndirectDiffuse )
907: 	vec3 irradiance = getAmbientLightIrradiance( ambientLightColor );
908: 	#if ( 0 > 0 )
909:
910: 	#endif
911: #endif
912: #if defined( RE_IndirectSpecular )
913: 	vec3 radiance = vec3( 0.0 );
914: 	vec3 clearCoatRadiance = vec3( 0.0 );
915: #endif
916:
917: #if defined( RE_IndirectDiffuse )
918: 	#ifdef USE_LIGHTMAP
919: 		vec3 lightMapIrradiance = texture2D( lightMap, vUv2 ).xyz * lightMapIntensity;
920: 		#ifndef PHYSICALLY_CORRECT_LIGHTS
921: 			lightMapIrradiance *= PI;
922: 		#endif
923: 		irradiance += lightMapIrradiance;
924: 	#endif
925: 	#if defined( USE_ENVMAP ) && defined( PHYSICAL ) && defined( ENVMAP_TYPE_CUBE_UV )
926: 		irradiance += getLightProbeIndirectIrradiance( geometry, maxMipLevel );
927: 	#endif
928: #endif
929: #if defined( USE_ENVMAP ) && defined( RE_IndirectSpecular )
930: 	radiance += getLightProbeIndirectRadiance( geometry, Material_BlinnShininessExponent( material ), maxMipLevel );
931: 	#ifndef STANDARD
932: 		clearCoatRadiance += getLightProbeIndirectRadiance( geometry, Material_ClearCoat_BlinnShininessExponent( material ), maxMipLevel );
933: 	#endif
934: #endif
935:
936: #if defined( RE_IndirectDiffuse )
937: 	RE_IndirectDiffuse( irradiance, geometry, material, reflectedLight );
938: #endif
939: #if defined( RE_IndirectSpecular )
940: 	RE_IndirectSpecular( radiance, clearCoatRadiance, geometry, material, reflectedLight );
941: #endif
942:
943: #ifdef USE_AOMAP
944: 	float ambientOcclusion = ( texture2D( aoMap, vUv2 ).r - 1.0 ) * aoMapIntensity + 1.0;
945: 	reflectedLight.indirectDiffuse *= ambientOcclusion;
946: 	#if defined( USE_ENVMAP ) && defined( PHYSICAL )
947: 		float dotNV = saturate( dot( geometry.normal, geometry.viewDir ) );
948: 		reflectedLight.indirectSpecular *= computeSpecularOcclusion( dotNV, ambientOcclusion, material.specularRoughness );
949: 	#endif
950: #endif
951:
952: 	vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveRadiance;
953: #ifdef USE_ENVMAP
954: 	#if defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( PHONG )
955: 		vec3 cameraToVertex = normalize( vWorldPosition - cameraPosition );
956: 		vec3 worldNormal = inverseTransformDirection( normal, viewMatrix );
957: 		#ifdef ENVMAP_MODE_REFLECTION
958: 			vec3 reflectVec = reflect( cameraToVertex, worldNormal );
959: 		#else
960: 			vec3 reflectVec = refract( cameraToVertex, worldNormal, refractionRatio );
961: 		#endif
962: 	#else
963: 		vec3 reflectVec = vReflect;
964: 	#endif
965: 	#ifdef ENVMAP_TYPE_CUBE
966: 		vec4 envColor = textureCube( envMap, vec3( flipEnvMap * reflectVec.x, reflectVec.yz ) );
967: 	#elif defined( ENVMAP_TYPE_EQUIREC )
968: 		vec2 sampleUV;
969: 		reflectVec = normalize( reflectVec );
970: 		sampleUV.y = asin( clamp( reflectVec.y, - 1.0, 1.0 ) ) * RECIPROCAL_PI + 0.5;
971: 		sampleUV.x = atan( reflectVec.z, reflectVec.x ) * RECIPROCAL_PI2 + 0.5;
972: 		vec4 envColor = texture2D( envMap, sampleUV );
973: 	#elif defined( ENVMAP_TYPE_SPHERE )
974: 		reflectVec = normalize( reflectVec );
975: 		vec3 reflectView = normalize( ( viewMatrix * vec4( reflectVec, 0.0 ) ).xyz + vec3( 0.0, 0.0, 1.0 ) );
976: 		vec4 envColor = texture2D( envMap, reflectView.xy * 0.5 + 0.5 );
977: 	#else
978: 		vec4 envColor = vec4( 0.0 );
979: 	#endif
980: 	envColor = envMapTexelToLinear( envColor );
981: 	#ifdef ENVMAP_BLENDING_MULTIPLY
982: 		outgoingLight = mix( outgoingLight, outgoingLight * envColor.xyz, specularStrength * reflectivity );
983: 	#elif defined( ENVMAP_BLENDING_MIX )
984: 		outgoingLight = mix( outgoingLight, envColor.xyz, specularStrength * reflectivity );
985: 	#elif defined( ENVMAP_BLENDING_ADD )
986: 		outgoingLight += envColor.xyz * specularStrength * reflectivity;
987: 	#endif
988: #endif
989:
990: 	gl_FragColor = vec4( outgoingLight, diffuseColor.a );
991: #if defined( TONE_MAPPING )
992:   gl_FragColor.rgb = toneMapping( gl_FragColor.rgb );
993: #endif
994:
995:   gl_FragColor = linearToOutputTexel( gl_FragColor );
996:
997: #ifdef USE_FOG
998: 	#ifdef FOG_EXP2
999: 		float fogFactor = whiteCompliment( exp2( - fogDensity * fogDensity * fogDepth * fogDepth * LOG2 ) );
1000: 	#else
1001: 		float fogFactor = smoothstep( fogNear, fogFar, fogDepth );
1002: 	#endif
1003: 	gl_FragColor.rgb = mix( gl_FragColor.rgb, fogColor, fogFactor );
1004: #endif
1005:
1006: #ifdef PREMULTIPLIED_ALPHA
1007: 	gl_FragColor.rgb *= gl_FragColor.a;
1008: #endif
1009:
1010: #if defined( DITHERING )
1011:   gl_FragColor.rgb = dithering( gl_FragColor.rgb );
1012: #endif
1013:
1014: }
1015:
THREE.WebGLProgram: shader error:
1282
gl.VALIDATE_STATUS
false
gl.getProgramInfoLog
One or more shaders failed to compile

ERROR: too many uniforms
@jdm jdm added the A-content/webgl 3d canvas API label Jun 5, 2018
@jdm
Copy link
Member Author

jdm commented Jun 8, 2018

I suspect this may be related to the fact that these fields are initialized to (presumably minimum) values by InitBuiltInResources. We should try overriding them with the known limits here.

@jdm
Copy link
Member Author

jdm commented Jun 8, 2018

There is prior art in Firefox.

bors-servo pushed a commit that referenced this issue Jun 8, 2018
Use real values for GL maximums when compiling shaders.

This matches [the equivalent Gecko code](https://searchfox.org/mozilla-central/rev/c621276fbdd9591f52009042d959b9e19b66d49f/dom/canvas/WebGLShaderValidator.cpp#150-156) and makes a lot of three.js examples render as expected.

---
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes fix #20928.
- [ ] There are tests for these changes OR
- [ ] These changes do not require tests because _____
@jdm jdm added this to To do in WebGL content Jun 19, 2018
@jdm jdm moved this from To do to In progress in WebGL content Jun 19, 2018
@atouchet
Copy link
Contributor

atouchet commented Jul 9, 2018

@nox did #21129 fix this?

@jdm
Copy link
Member Author

jdm commented Jul 20, 2018

Yes.

@jdm jdm closed this as completed Jul 20, 2018
@jdm jdm moved this from In progress to Done in WebGL content Jul 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-content/webgl 3d canvas API
Projects
Status: Done
WebGL content
  
Done
Development

Successfully merging a pull request may close this issue.

2 participants