浏览代码

linted glsls using mrdood guidelines

arobertson0 6 年之前
父节点
当前提交
e620cf3db4

+ 169 - 125
examples/webgl_materials_clearcoat_normalmap.html

@@ -37,7 +37,7 @@
 		import { PMREMGenerator } from './jsm/pmrem/PMREMGenerator.js';
 		import { PMREMCubeUVPacker } from './jsm/pmrem/PMREMCubeUVPacker.js';
 
-		var container, stats, loader;
+		var container, stats;
 
 		var camera, scene, renderer;
 
@@ -66,31 +66,36 @@
 		var mats = [];
 		var normalMaps = {};
 
-		var windowHalfX = window.innerWidth / 2;
-		var windowHalfY = window.innerHeight / 2;
-
 		const sphereSize = 80;
 		const sphereSpacing = 1.25 * sphereSize * 2;
 
 		const variationsX = [
-			(mat, scale, map) => { mat.clearCoatNormalScale.copy(mat.normalScale); },
-			(mat, scale, map) => {
-				mat.clearCoatNormalScale = new THREE.Vector2(scale, scale);
-				if (mat.clearCoatNormalMap !== map) {
+			( mat, scale, map ) => { },
+			( mat, scale, map ) => {
+
+				mat.clearCoatNormalScale = new THREE.Vector2( scale, scale );
+				if ( mat.clearCoatNormalMap !== map ) {
+
 					mat.clearCoatNormalMap = map;
 					mat.needsUpdate = true;
+
 				}
+
 			},
 		];
 
 		const variationsY = [
-			(mat, scale, map) => { },
-			(mat, scale, map) => {
-				mat.normalScale = new THREE.Vector2(scale, scale);
-				if (mat.normalMap !== map) {
+			( mat, scale, map ) => { },
+			( mat, scale, map ) => {
+
+				mat.normalScale = new THREE.Vector2( scale, scale );
+				if ( mat.normalMap !== map ) {
+
 					mat.normalMap = map;
 					mat.needsUpdate = true;
+
 				}
+
 			},
 		];
 
@@ -98,21 +103,25 @@
 		const maxJ = variationsY.length;
 
 		new THREE.FontLoader()
-			.load('fonts/gentilis_regular.typeface.json', function (font) {
-				init(font);
-				animate();
-			});
+			.load( 'fonts/gentilis_regular.typeface.json',
+				function ( font ) {
+
+					init( font );
+					animate();
+
+				}
+			);
 
 
-		function init(font) {
+		function init( font ) {
 
-			container = document.createElement('div');
-			document.body.appendChild(container);
+			container = document.createElement( 'div' );
+			document.body.appendChild( container );
 
-			camera = new THREE.PerspectiveCamera(27, window.innerWidth / window.innerHeight, 1, 10000);
+			camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 1, 10000 );
 			camera.position.z = 1500;
 
-			var genCubeUrls = function (prefix, postfix) {
+			var genCubeUrls = function ( prefix, postfix ) {
 
 				return [
 					prefix + 'px' + postfix, prefix + 'nx' + postfix,
@@ -124,119 +133,138 @@
 
 			scene = new THREE.Scene();
 
-			var hdrUrls = genCubeUrls('./textures/cube/pisaHDR/', '.hdr');
+			var hdrUrls = genCubeUrls( './textures/cube/pisaHDR/', '.hdr' );
 
 			new HDRCubeTextureLoader()
-				.setType(THREE.UnsignedByteType)
-				.load(hdrUrls, function (hdrCubeMap) {
+				.setType( THREE.UnsignedByteType )
+				.load( hdrUrls,
+					function ( hdrCubeMap ) {
 
-					var pmremGenerator = new PMREMGenerator(hdrCubeMap);
-					pmremGenerator.update(renderer);
+						var pmremGenerator = new PMREMGenerator( hdrCubeMap );
+						pmremGenerator.update( renderer );
 
-					var pmremCubeUVPacker = new PMREMCubeUVPacker(pmremGenerator.cubeLods);
-					pmremCubeUVPacker.update(renderer);
+						var pmremCubeUVPacker = new PMREMCubeUVPacker( pmremGenerator.cubeLods );
+						pmremCubeUVPacker.update( renderer );
 
-					var hdrCubeRenderTarget = pmremCubeUVPacker.CubeUVRenderTarget;
+						var hdrCubeRenderTarget = pmremCubeUVPacker.CubeUVRenderTarget;
 
-					var geometry = new THREE.SphereBufferGeometry(80, 64, 32);
+						var geometry = new THREE.SphereBufferGeometry( 80, 64, 32 );
 
-					var textureLoader = new THREE.TextureLoader();
+						var textureLoader = new THREE.TextureLoader();
 
-					normalMaps.tentacle = textureLoader
-						.load("textures/nvidia_tentacle/tentacle_tangent_space.png");
+						normalMaps.tentacle = textureLoader
+							.load( "textures/nvidia_tentacle/tentacle_tangent_space.png" );
 
-					normalMaps.face = textureLoader
-						.load("models/gltf/LeePerrySmith/Infinite-Level_02_Tangent_SmoothUV.jpg");
+						normalMaps.face = textureLoader
+							.load( "models/gltf/LeePerrySmith/Infinite-Level_02_Tangent_SmoothUV.jpg" );
 
-					// objectNormalMap = textureLoader
-					// 	.load("textures/nvidia_tentacle/tentacle_object_space.png");
 
-					var matParams = {
-						envMap: hdrCubeRenderTarget.texture,
-					};
+						var matParams = {
+							envMap: hdrCubeRenderTarget.texture,
+						};
 
 
-					for (var ii = 0; ii < maxI; ii++) {
-						if (!mats[ii]) {
-							mats[ii] = [];
-						}
-						for (var jj = 0; jj < maxJ; jj++) {
+						for ( var ii = 0; ii < maxI; ii ++ ) {
+
+							if ( ! mats[ ii ] ) {
+
+								mats[ ii ] = [];
 
-							var mat = mats[ii][jj] = new THREE.MeshPhysicalMaterial(matParams);
+							}
+
+							for ( var jj = 0; jj < maxJ; jj ++ ) {
+
+								var mat = mats[ ii ][ jj ] = new THREE.MeshPhysicalMaterial( matParams );
+
+								variationsX[ ii ]( mat, 1, normalMaps.tentacle );
+								variationsY[ jj ]( mat, 1, normalMaps.face );
+								var mesh = new THREE.Mesh( geometry, mat );
+								meshes.push( mesh );
+								mesh.position.x = ( ii - ( maxI - 1 ) / 2 ) * sphereSpacing;
+								mesh.position.y = ( jj - ( maxJ - 1 ) / 2 ) * sphereSpacing;
+								scene.add( mesh );
+
+							}
 
-							variationsX[ii](mat, 1, normalMaps.tentacle);
-							variationsY[jj](mat, 1, normalMaps.face);
-							var mesh = new THREE.Mesh(geometry, mat);
-							meshes.push(mesh);
-							mesh.position.x = (ii - (maxI - 1) / 2) * sphereSpacing;
-							mesh.position.y = (jj - (maxJ - 1) / 2) * sphereSpacing;
-							scene.add(mesh);
 						}
+
+						hdrCubeMap.magFilter = THREE.LinearFilter;
+						hdrCubeMap.needsUpdate = true;
+						scene.background = hdrCubeMap;
+
+						pmremGenerator.dispose();
+						pmremCubeUVPacker.dispose();
+
 					}
 
-					hdrCubeMap.magFilter = THREE.LinearFilter;
-					hdrCubeMap.needsUpdate = true;
-					scene.background = hdrCubeMap;
+				);
 
-					pmremGenerator.dispose();
-					pmremCubeUVPacker.dispose();
-				});
+			function addLabel( name, location, fontSize ) {
 
-			function addLabel(name, location, fontSize) {
 				fontSize = fontSize | 20;
-				var textGeo = new THREE.TextBufferGeometry(name, {
+				var textGeo = new THREE.TextBufferGeometry( name,
+					{
 
-					font: font,
-					size: fontSize,
-					height: 1,
-					curveSegments: 1
+						font: font,
+						size: fontSize,
+						height: 1,
+						curveSegments: 1
 
-				});
+					}
+				);
 
-				var textMaterial = new THREE.MeshBasicMaterial({ color: 0xffffff });
-				var textMesh = new THREE.Mesh(textGeo, textMaterial);
+				var textMaterial = new THREE.MeshBasicMaterial( { color: 0xffffff } );
+				var textMesh = new THREE.Mesh( textGeo, textMaterial );
 
+				// Center the bounding box on the specified position w/ translation of -half diagonal
 				textGeo.computeBoundingBox();
 				var bb = textGeo.boundingBox.clone();
-				var displace = bb.max.sub(bb.min).divide(new THREE.Vector3(2, 2, 2));
+				var displace = bb.max.sub( bb.min ).divide( new THREE.Vector3( 2, 2, 2 ) );
 
-				textMesh.position.copy(location.sub(displace));
-				scene.add(textMesh);
+				textMesh.position.copy( location.sub( displace ) );
+				scene.add( textMesh );
 
 			}
 
 
-			addLabel("Normal Map", new THREE.Vector3(-350, 0, 0), 30);
-			addLabel("None", new THREE.Vector3(-250, -100, 0));
-			addLabel("Map", new THREE.Vector3(-300, 100, 0));
+			addLabel( "Normal Map", new THREE.Vector3( -350, 0, 0 ), 30 );
+			addLabel( "None", new THREE.Vector3( -250, -100, 0 ) );
+			addLabel( "Map", new THREE.Vector3( -300, 100, 0 ) );
 
-			addLabel("Clearcoat Normal Map", new THREE.Vector3(0, 260, 0), 30);
-			addLabel("None", new THREE.Vector3(-100, 200, 0));
-			addLabel("Map", new THREE.Vector3(100, 200, 0));
+			addLabel( "Clearcoat Normal Map", new THREE.Vector3( 0, 260, 0 ), 30 );
+			addLabel( "None", new THREE.Vector3( -100, 200, 0 ) );
+			addLabel( "Map", new THREE.Vector3( 100, 200, 0 ) );
 
 			// LIGHTS
 
-			particleLight = new THREE.Mesh(new THREE.SphereBufferGeometry(4, 8, 8), new THREE.MeshBasicMaterial({ color: 0xffffff }));
-			scene.add(particleLight);
+			particleLight = new THREE.Mesh( new THREE.SphereBufferGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
+			scene.add( particleLight );
 
 			{
-				var ambientLight = new THREE.AmbientLight(0x444444);
-				scene.add(ambientLight);
+
+				var ambientLight = new THREE.AmbientLight( 0x444444 );
+				scene.add( ambientLight );
+
 			}
+
 			{
-				var pointLight = new THREE.PointLight(0xffffff, 1.25, 1000);
-				particleLight.add(pointLight);
+
+				var pointLight = new THREE.PointLight( 0xffffff, 1.25, 1000 );
+				particleLight.add( pointLight );
+
 			}
+
 			{
-				var directionalLight = new THREE.DirectionalLight(0xffffff);
-				directionalLight.position.set(1, - 0.5, - 1);
-				scene.add(directionalLight);
-			}
 
+				var directionalLight = new THREE.DirectionalLight( 0xffffff );
+				directionalLight.position.set( 1, - 0.5, - 1 );
+				scene.add( directionalLight );
+
+			}
 
 			renderer = new THREE.WebGLRenderer();
-			renderer.setSize(window.innerWidth, window.innerHeight);
-			container.appendChild(renderer.domElement);
+			renderer.setSize( window.innerWidth, window.innerHeight );
+			container.appendChild( renderer.domElement );
 
 			//
 
@@ -246,62 +274,69 @@
 			//
 
 			stats = new Stats();
-			container.appendChild(stats.dom);
+			container.appendChild( stats.dom );
 
 			// EVENTS
 
-			var controls = new OrbitControls(camera, renderer.domElement);
+			var controls = new OrbitControls( camera, renderer.domElement );
 
-			window.addEventListener('resize', onWindowResize, false);
+			window.addEventListener( 'resize', onWindowResize, false );
 
 			var gui = new GUI();
 
 			{
+
 				var materialGui = gui.addFolder( 'Physical Material' );
-				materialGui.add(guiParams, 'metalness', 0, 1, 0.01);
-				materialGui.add(guiParams, 'roughness', 0, 1, 0.01);
-				materialGui.add(guiParams, 'reflectivity', 0, 1, 0.01);
+				materialGui.add( guiParams, 'metalness', 0, 1, 0.01 );
+				materialGui.add( guiParams, 'roughness', 0, 1, 0.01 );
+				materialGui.add( guiParams, 'reflectivity', 0, 1, 0.01 );
 				materialGui.open();
+
 			}
+
 			{
+
 				var clearCoatGui = gui.addFolder( 'ClearCoat' );
-				clearCoatGui.add(guiParams, 'clearCoat', 0, 1, 0.01);
-				clearCoatGui.add(guiParams, 'clearCoatRoughness', 0, 1, 0.01);
+				clearCoatGui.add( guiParams, 'clearCoat', 0, 1, 0.01 );
+				clearCoatGui.add( guiParams, 'clearCoatRoughness', 0, 1, 0.01 );
 				clearCoatGui.open();
+
 			}
+
 			{
+
 				var normalGui = gui.addFolder( 'Normal Maps' );
-				normalGui.add(guiMatParams, 'normalMap', guiNormalMapNames);
-				normalGui.add(guiMatParams, 'normalScale', 0, 1, 0.01);
-				normalGui.add(guiMatParams, 'clearCoatNormalMap', guiNormalMapNames);
-				normalGui.add(guiMatParams, 'clearCoatNormalScale', 0, 1, 0.01);
+				normalGui.add( guiMatParams, 'normalMap', guiNormalMapNames );
+				normalGui.add( guiMatParams, 'normalScale', 0, 1, 0.01 );
+				normalGui.add( guiMatParams, 'clearCoatNormalMap', guiNormalMapNames );
+				normalGui.add( guiMatParams, 'clearCoatNormalScale', 0, 1, 0.01 );
 				normalGui.open();
-			}
-
-
-
 
+			}
 
 			gui.open();
+
 		}
 
 		//
 
 		function onWindowResize() {
+
 			var width = window.innerWidth;
 			var height = window.innerHeight;
 
 			camera.aspect = width / height;
 			camera.updateProjectionMatrix();
 
-			renderer.setSize(width, height);
+			renderer.setSize( width, height );
+
 		}
 
 		//
 
 		function animate() {
 
-			requestAnimationFrame(animate);
+			requestAnimationFrame( animate );
 
 			render();
 
@@ -311,43 +346,52 @@
 
 		function render() {
 
-			var matColor = new THREE.Color().setHSL(0.0, 0.5, 0.25);
+			var matColor = new THREE.Color().setHSL( 0.0, 0.5, 0.25 );
+
+			for ( var ii = 0; ii < maxI; ii ++ ) {
+
+				if ( mats[ ii ] ) { // avoid exceptions on first render before materials are set. TODO: lauch animation only after setup
 
-			for (var ii = 0; ii < maxI; ii++) {
-				if (mats[ii]) {
-					for (var jj = 0; jj < maxJ; jj++) {
-						var mat = mats[ii][jj];
+					for ( var jj = 0; jj < maxJ; jj ++ ) {
+
+						var mat = mats[ ii ][ jj ];
 						mat.color = matColor;
-						for (var matParam in guiParams) {
-							mat[matParam] = guiParams[matParam];
+						for ( var matParam in guiParams ) {
+
+							mat[ matParam ] = guiParams[ matParam ];
+
 						}
 
-						var normalMap = normalMaps[guiMatParams.normalMap];
-						var clearCoatNormalMap = normalMaps[guiMatParams.clearCoatNormalMap];
+						var normalMap = normalMaps[ guiMatParams.normalMap ];
+						var clearCoatNormalMap = normalMaps[ guiMatParams.clearCoatNormalMap ];
+
+						variationsX[ ii ]( mat, guiMatParams.clearCoatNormalScale, clearCoatNormalMap );
+						variationsY[ jj ]( mat, guiMatParams.normalScale, normalMap );
 
-						variationsX[ii](mat, guiMatParams.clearCoatNormalScale, clearCoatNormalMap);
-						variationsY[jj](mat, guiMatParams.normalScale, normalMap);
 					}
+
 				}
+
 			}
 
 			var timer = Date.now() * 0.00025;
-			particleLight.position.x = Math.sin(timer * 7) * 300;
-			particleLight.position.y = Math.cos(timer * 5) * 400;
-			particleLight.position.z = Math.cos(timer * 3) * 300;
+			particleLight.position.x = Math.sin( timer * 7 ) * 300;
+			particleLight.position.y = Math.cos( timer * 5 ) * 400;
+			particleLight.position.z = Math.cos( timer * 3 ) * 300;
 
 
-			for (var mesh of meshes) {
+			for ( var mesh of meshes ) {
+
 				mesh.rotation.y += 0.01;
+
 			}
 
 
-			renderer.render(scene, camera);
+			renderer.render( scene, camera );
 
 		}
 
-	</script>
 
+	</script>
 </body>
-
 </html>

+ 2 - 0
src/materials/MeshPhysicalMaterial.d.ts

@@ -7,12 +7,14 @@ import {
 
 export interface MeshPhysicalMaterialParameters
 	extends MeshStandardMaterialParameters {
+
 	reflectivity?: number;
 	clearCoat?: number;
 	clearCoatRoughness?: number;
 
 	clearCoatNormalScale?: Vector2;
 	clearCoatNormalMap?: Texture;
+
 }
 
 export class MeshPhysicalMaterial extends MeshStandardMaterial {

+ 4 - 4
src/renderers/shaders/ShaderChunk/clearcoat_normal_fragment_begin.glsl.js

@@ -1,7 +1,7 @@
 export default /* glsl */`
-#ifndef STANDARD
-  #ifdef USE_CLEARCOAT_NORMALMAP
-    vec3 clearCoatNormal = geometryNormal;
-  #endif
+#ifdef USE_CLEARCOAT_NORMALMAP
+
+  vec3 clearCoatNormal = geometryNormal;
+
 #endif
 `;

+ 9 - 10
src/renderers/shaders/ShaderChunk/clearcoat_normal_fragment_maps.glsl.js

@@ -1,19 +1,18 @@
 export default /* glsl */`
-#ifndef STANDARD
-	#ifdef USE_CLEARCOAT_NORMALMAP
+#ifdef USE_CLEARCOAT_NORMALMAP
 
-		#ifdef USE_TANGENT
+	#ifdef USE_TANGENT
 
-			mat3 vTBN = mat3( tangent, bitangent, clearCoatNormal );
-			vec3 mapN = texture2D( normalMap, vUv ).xyz * 2.0 - 1.0;
-			mapN.xy = clearCoatNormalScale * mapN.xy;
-			clearCoatNormal = normalize( vTBN * mapN );
+		mat3 vTBN = mat3( tangent, bitangent, clearCoatNormal );
+		vec3 mapN = texture2D( normalMap, vUv ).xyz * 2.0 - 1.0;
+		mapN.xy = clearCoatNormalScale * mapN.xy;
+		clearCoatNormal = normalize( vTBN * mapN );
 
-		#else
+	#else
 
-			clearCoatNormal = perturbNormal2Arb( -vViewPosition, clearCoatNormal, clearCoatNormalScale, clearCoatNormalMap );
+		clearCoatNormal = perturbNormal2Arb( - vViewPosition, clearCoatNormal, clearCoatNormalScale, clearCoatNormalMap );
 
-		#endif
 	#endif
+
 #endif
 `;

+ 3 - 5
src/renderers/shaders/ShaderChunk/clearcoat_normalmap_pars_fragment.glsl.js

@@ -1,10 +1,8 @@
 export default /* glsl */`
-#ifndef STANDARD
-	#ifdef USE_CLEARCOAT_NORMALMAP
+#ifdef USE_CLEARCOAT_NORMALMAP
 
-	  uniform sampler2D clearCoatNormalMap;
-		uniform vec2 clearCoatNormalScale;
+	uniform sampler2D clearCoatNormalMap;
+	uniform vec2 clearCoatNormalScale;
 
-	#endif
 #endif
 `;

+ 2 - 4
src/renderers/shaders/ShaderChunk/common.glsl.js

@@ -40,12 +40,10 @@ struct GeometricContext {
 	vec3 normal;
 	vec3 viewDir;
 
-	#ifndef STANDARD
-		#ifdef USE_CLEARCOAT_NORMALMAP
+	#ifdef USE_CLEARCOAT_NORMALMAP
 
-			vec3 clearCoatNormal;
+		vec3 clearCoatNormal;
 
-		#endif
   #endif
 };
 

+ 3 - 6
src/renderers/shaders/ShaderChunk/lights_fragment_begin.glsl.js

@@ -20,15 +20,12 @@ geometry.position = - vViewPosition;
 geometry.normal = normal;
 geometry.viewDir = normalize( vViewPosition );
 
-#ifdef PHYSICAL
-	#ifndef STANDARD
-		#ifdef USE_CLEARCOAT_NORMALMAP
+#ifdef USE_CLEARCOAT_NORMALMAP
 
-			geometry.clearCoatNormal = clearCoatNormal;
+	geometry.clearCoatNormal = clearCoatNormal;
 
-		#endif
-	#endif
 #endif
+
 IncidentLight directLight;
 
 #if ( NUM_POINT_LIGHTS > 0 ) && defined( RE_Direct )

+ 7 - 0
src/renderers/shaders/ShaderChunk/lights_fragment_maps.glsl.js

@@ -28,11 +28,18 @@ export default /* glsl */`
   radiance += getLightProbeIndirectRadiance( /*specularLightProbe,*/ geometry.viewDir, geometry.normal, Material_BlinnShininessExponent( material ), maxMipLevel );
 
 	#ifndef STANDARD
+
 		#ifdef USE_CLEARCOAT_NORMALMAP
+
 			clearCoatRadiance += getLightProbeIndirectRadiance( /*specularLightProbe,*/ geometry.viewDir, geometry.clearCoatNormal, Material_ClearCoat_BlinnShininessExponent( material ), maxMipLevel );
+
 		#else
+
 			clearCoatRadiance += getLightProbeIndirectRadiance( /*specularLightProbe,*/ geometry.viewDir, geometry.normal, Material_ClearCoat_BlinnShininessExponent( material ), maxMipLevel );
+
 		#endif
+
 	#endif
+
 #endif
 `;

+ 29 - 21
src/renderers/shaders/ShaderChunk/lights_physical_pars_fragment.glsl.js

@@ -1,4 +1,4 @@
-export default /* glsl */`
+export default /* glsl */ `
 struct PhysicalMaterial {
 
 	vec3	diffuseColor;
@@ -65,8 +65,9 @@ float clearCoatDHRApprox( const in float roughness, const in float dotNL ) {
 #endif
 
 void RE_Direct_Physical( const in IncidentLight directLight, const in GeometricContext geometry, const in PhysicalMaterial material, inout ReflectedLight reflectedLight ) {
- 
+
 	float dotNL = saturate( dot( geometry.normal, directLight.direction ) );
+
 	vec3 irradiance = dotNL * directLight.color;
 
 	#ifndef PHYSICALLY_CORRECT_LIGHTS
@@ -76,8 +77,11 @@ void RE_Direct_Physical( const in IncidentLight directLight, const in GeometricC
 	#endif
 
 	#ifndef STANDARD
+
 		#ifdef USE_CLEARCOAT_NORMALMAP
+
 			float ccDotNL = saturate( dot( geometry.clearCoatNormal, directLight.direction ) );
+
 			vec3 ccIrradiance = ccDotNL * directLight.color;
 
 			#ifndef PHYSICALLY_CORRECT_LIGHTS
@@ -85,31 +89,29 @@ void RE_Direct_Physical( const in IncidentLight directLight, const in GeometricC
 				ccIrradiance *= PI; // punctual light
 
 			#endif
-		#endif
-	#endif
 
-	#ifndef STANDARD
-		#ifdef USE_CLEARCOAT_NORMALMAP
 			float clearCoatDHR = material.clearCoat * clearCoatDHRApprox( material.clearCoatRoughness, ccDotNL );
+
+			reflectedLight.directSpecular += ccIrradiance * material.clearCoat * BRDF_Specular_GGX( directLight, geometry.viewDir, geometry.clearCoatNormal, vec3( DEFAULT_SPECULAR_COEFFICIENT ), material.clearCoatRoughness );
+
 		#else
+
 			float clearCoatDHR = material.clearCoat * clearCoatDHRApprox( material.clearCoatRoughness, dotNL );
+
+			reflectedLight.directSpecular += irradiance * material.clearCoat * BRDF_Specular_GGX( directLight, geometry.viewDir, geometry.normal, vec3( DEFAULT_SPECULAR_COEFFICIENT ), material.clearCoatRoughness );
+
 		#endif
+
 	#else
+
 		float clearCoatDHR = 0.0;
+
 	#endif
 
 	reflectedLight.directSpecular += ( 1.0 - clearCoatDHR ) * irradiance * BRDF_Specular_GGX( directLight, geometry.viewDir, geometry.normal, material.specularColor, material.specularRoughness );
 
 	reflectedLight.directDiffuse += ( 1.0 - clearCoatDHR ) * irradiance * BRDF_Diffuse_Lambert( material.diffuseColor );
 
-	#ifndef STANDARD
-		#ifdef USE_CLEARCOAT_NORMALMAP
-			reflectedLight.directSpecular += ccIrradiance * material.clearCoat * BRDF_Specular_GGX( directLight, geometry.viewDir, geometry.clearCoatNormal, vec3( DEFAULT_SPECULAR_COEFFICIENT ), material.clearCoatRoughness );
-		#else
-			reflectedLight.directSpecular += irradiance * material.clearCoat * BRDF_Specular_GGX( directLight, geometry.viewDir, geometry.normal, vec3( DEFAULT_SPECULAR_COEFFICIENT ), material.clearCoatRoughness );
-		#endif
-	#endif
-
 }
 
 void RE_IndirectDiffuse_Physical( const in vec3 irradiance, const in GeometricContext geometry, const in PhysicalMaterial material, inout ReflectedLight reflectedLight ) {
@@ -127,15 +129,28 @@ void RE_IndirectDiffuse_Physical( const in vec3 irradiance, const in GeometricCo
 void RE_IndirectSpecular_Physical( const in vec3 radiance, const in vec3 irradiance, const in vec3 clearCoatRadiance, const in GeometricContext geometry, const in PhysicalMaterial material, inout ReflectedLight reflectedLight) {
 
 	#ifndef STANDARD
+
 		#ifdef USE_CLEARCOAT_NORMALMAP
+
 			float ccDotNV = saturate( dot( geometry.clearCoatNormal, geometry.viewDir ) );
+
+			reflectedLight.indirectSpecular += clearCoatRadiance * material.clearCoat * BRDF_Specular_GGX_Environment( geometry.viewDir, geometry.clearCoatNormal, vec3( DEFAULT_SPECULAR_COEFFICIENT ), material.clearCoatRoughness );
+
 		#else
+
 			float ccDotNV = saturate( dot( geometry.normal, geometry.viewDir ) );
+
+			reflectedLight.indirectSpecular += clearCoatRadiance * material.clearCoat * BRDF_Specular_GGX_Environment( geometry.viewDir, geometry.normal, vec3( DEFAULT_SPECULAR_COEFFICIENT ), material.clearCoatRoughness );
+
 		#endif
+
 		float ccDotNL = ccDotNV;
 		float clearCoatDHR = material.clearCoat * clearCoatDHRApprox( material.clearCoatRoughness, ccDotNL );
+
 	#else
+
 		float clearCoatDHR = 0.0;
+
 	#endif
 
 	float clearCoatInv = 1.0 - clearCoatDHR;
@@ -162,13 +177,6 @@ void RE_IndirectSpecular_Physical( const in vec3 radiance, const in vec3 irradia
 
 	#endif
 
-	#ifndef STANDARD
-		#ifdef USE_CLEARCOAT_NORMALMAP
-			reflectedLight.indirectSpecular += clearCoatRadiance * material.clearCoat * BRDF_Specular_GGX_Environment( geometry.viewDir, geometry.clearCoatNormal, vec3( DEFAULT_SPECULAR_COEFFICIENT ), material.clearCoatRoughness );
-		#else
-			reflectedLight.indirectSpecular += clearCoatRadiance * material.clearCoat * BRDF_Specular_GGX_Environment( geometry.viewDir, geometry.normal, vec3( DEFAULT_SPECULAR_COEFFICIENT ), material.clearCoatRoughness );
-		#endif
-	#endif
 }
 
 #define RE_Direct				RE_Direct_Physical

+ 4 - 2
src/renderers/shaders/ShaderChunk/normal_fragment_begin.glsl.js

@@ -1,4 +1,4 @@
-export default /* glsl */`
+export default /* glsl */ `
 #ifdef FLAT_SHADED
 
 	// Workaround for Adreno/Nexus5 not able able to do dFdx( vViewPosition ) ...
@@ -32,7 +32,9 @@ export default /* glsl */`
 	#endif
 
 #endif
-#if defined( PHYSICAL ) && !defined( STANDARD )
+#ifdef USE_CLEARCOAT_NORMALMAP
+
 	vec3 geometryNormal = normal;
+
 #endif
 `;

+ 19 - 18
src/renderers/shaders/ShaderChunk/normalmap_pars_fragment.glsl.js

@@ -13,32 +13,33 @@ export default /* glsl */`
 
 #if ( defined ( USE_NORMALMAP ) && !defined ( OBJECTSPACE_NORMALMAP )) || defined ( USE_CLEARCOAT_NORMALMAP )
 
-		// Per-Pixel Tangent Space Normal Mapping
-		// http://hacksoflife.blogspot.ch/2009/11/per-pixel-tangent-space-normal-mapping.html
+	// Per-Pixel Tangent Space Normal Mapping
+	// http://hacksoflife.blogspot.ch/2009/11/per-pixel-tangent-space-normal-mapping.html
 
-		vec3 perturbNormal2Arb( vec3 eye_pos, vec3 surf_norm, vec2 normalScale, in sampler2D normalMap ) {
+	vec3 perturbNormal2Arb( vec3 eye_pos, vec3 surf_norm, vec2 normalScale, in sampler2D normalMap ) {
 
-			// Workaround for Adreno 3XX dFd*( vec3 ) bug. See #9988
+		// Workaround for Adreno 3XX dFd*( vec3 ) bug. See #9988
 
-			vec3 q0 = vec3( dFdx( eye_pos.x ), dFdx( eye_pos.y ), dFdx( eye_pos.z ) );
-			vec3 q1 = vec3( dFdy( eye_pos.x ), dFdy( eye_pos.y ), dFdy( eye_pos.z ) );
-			vec2 st0 = dFdx( vUv.st );
-			vec2 st1 = dFdy( vUv.st );
+		vec3 q0 = vec3( dFdx( eye_pos.x ), dFdx( eye_pos.y ), dFdx( eye_pos.z ) );
+		vec3 q1 = vec3( dFdy( eye_pos.x ), dFdy( eye_pos.y ), dFdy( eye_pos.z ) );
+		vec2 st0 = dFdx( vUv.st );
+		vec2 st1 = dFdy( vUv.st );
 
-			float scale = sign( st1.t * st0.s - st0.t * st1.s ); // we do not care about the magnitude
+		float scale = sign( st1.t * st0.s - st0.t * st1.s ); // we do not care about the magnitude
 
-			vec3 S = normalize( ( q0 * st1.t - q1 * st0.t ) * scale );
-			vec3 T = normalize( ( - q0 * st1.s + q1 * st0.s ) * scale );
-			vec3 N = normalize( surf_norm );
-			mat3 tsn = mat3( S, T, N );
+		vec3 S = normalize( ( q0 * st1.t - q1 * st0.t ) * scale );
+		vec3 T = normalize( ( - q0 * st1.s + q1 * st0.s ) * scale );
+		vec3 N = normalize( surf_norm );
+		mat3 tsn = mat3( S, T, N );
 
-			vec3 mapN = texture2D( normalMap, vUv ).xyz * 2.0 - 1.0;
+		vec3 mapN = texture2D( normalMap, vUv ).xyz * 2.0 - 1.0;
 
-			mapN.xy *= normalScale;
-			mapN.xy *= ( float( gl_FrontFacing ) * 2.0 - 1.0 );
+		mapN.xy *= normalScale;
+		mapN.xy *= ( float( gl_FrontFacing ) * 2.0 - 1.0 );
 
-			return normalize( tsn * mapN );
+		return normalize( tsn * mapN );
+
+	}
 
-		}
 #endif
 `;