ソースを参照

Merge pull request #18439 from sunag/dev-nodemtlfix

NodeMaterial: fixes
Mr.doob 5 年 前
コミット
d511f60831

+ 14 - 0
examples/jsm/nodes/accessors/NormalNode.js

@@ -29,6 +29,20 @@ NormalNode.prototype.getShared = function () {
 
 
 };
 };
 
 
+NormalNode.prototype.build = function ( builder, output, uuid, ns ) {
+
+	var contextNormal = builder.context[ this.scope + 'Normal' ];
+
+	if ( contextNormal ) {
+
+		return contextNormal.build( builder, output, uuid, ns );
+
+	}
+
+	return TempNode.prototype.build.call( this, builder, output, uuid );
+
+}
+
 NormalNode.prototype.generate = function ( builder, output ) {
 NormalNode.prototype.generate = function ( builder, output ) {
 
 
 	var result;
 	var result;

+ 1 - 1
examples/jsm/nodes/accessors/ReflectNode.js

@@ -54,7 +54,7 @@ ReflectNode.prototype.generate = function ( builder, output ) {
 
 
 			case ReflectNode.VECTOR:
 			case ReflectNode.VECTOR:
 
 
-				var viewNormalNode = builder.context.viewNormal || new NormalNode( NormalNode.VIEW );
+				var viewNormalNode = new NormalNode( NormalNode.VIEW );
 				var roughnessNode = builder.context.roughness;
 				var roughnessNode = builder.context.roughness;
 
 
 				var viewNormal = viewNormalNode.build( builder, 'v3' );
 				var viewNormal = viewNormalNode.build( builder, 'v3' );

+ 23 - 8
examples/jsm/nodes/materials/nodes/StandardNode.js

@@ -17,11 +17,9 @@ function StandardNode() {
 
 
 	Node.call( this );
 	Node.call( this );
 
 
-	this.color = new ColorNode( 0xEEEEEE );
-	this.roughness = new FloatNode( 0.5 );
-	this.metalness = new FloatNode( 0.5 );
-
-	this.energyPreservation = true;
+	this.color = new ColorNode( 0xFFFFFF );
+	this.roughness = new FloatNode( 1 );
+	this.metalness = new FloatNode( 0 );
 
 
 }
 }
 
 
@@ -45,6 +43,7 @@ StandardNode.prototype.build = function ( builder ) {
 
 
 	builder.requires.lights = true;
 	builder.requires.lights = true;
 
 
+	builder.extensions.derivatives = true;
 	builder.extensions.shaderTextureLOD = true;
 	builder.extensions.shaderTextureLOD = true;
 
 
 	if ( builder.isShader( 'vertex' ) ) {
 	if ( builder.isShader( 'vertex' ) ) {
@@ -136,6 +135,7 @@ StandardNode.prototype.build = function ( builder ) {
 			roughness: specularRoughness,
 			roughness: specularRoughness,
 			bias: new SpecularMIPLevelNode( specularRoughness ),
 			bias: new SpecularMIPLevelNode( specularRoughness ),
 			viewNormal: new ExpressionNode( 'normal', 'v3' ),
 			viewNormal: new ExpressionNode( 'normal', 'v3' ),
+			worldNormal: new ExpressionNode( 'inverseTransformDirection( geometry.normal, viewMatrix )', 'v3' ),
 			gamma: true
 			gamma: true
 		};
 		};
 
 
@@ -147,6 +147,7 @@ StandardNode.prototype.build = function ( builder ) {
 			roughness: clearcoatRoughness,
 			roughness: clearcoatRoughness,
 			bias: new SpecularMIPLevelNode( clearcoatRoughness ),
 			bias: new SpecularMIPLevelNode( clearcoatRoughness ),
 			viewNormal: new ExpressionNode( 'clearcoatNormal', 'v3' ),
 			viewNormal: new ExpressionNode( 'clearcoatNormal', 'v3' ),
+			worldNormal: new ExpressionNode( 'inverseTransformDirection( geometry.clearcoatNormal, viewMatrix )', 'v3' ),
 			gamma: true
 			gamma: true
 		};
 		};
 
 
@@ -323,10 +324,22 @@ StandardNode.prototype.build = function ( builder ) {
 
 
 		}
 		}
 
 
+		// anti-aliasing code by @elalish
+
+		output.push(
+			'vec3 dxy = max( abs( dFdx( geometryNormal ) ), abs( dFdy( geometryNormal ) ) );',
+			'float geometryRoughness = max( max( dxy.x, dxy.y ), dxy.z );',
+		);
+
 		// optimization for now
 		// optimization for now
 
 
 		output.push(
 		output.push(
-			'material.diffuseColor = ' + ( light ? 'vec3( 1.0 )' : 'diffuseColor * (1.0 - metalnessFactor)' ) + ';',
+			'material.diffuseColor = ' + ( light ? 'vec3( 1.0 )' : 'diffuseColor * ( 1.0 - metalnessFactor )' ) + ';',
+
+			'material.specularRoughness = max( roughnessFactor, 0.0525 );',
+			'material.specularRoughness += geometryRoughness;',
+			'material.specularRoughness = min( material.specularRoughness, 1.0 );',
+			
 			'material.specularRoughness = clamp( roughnessFactor, 0.04, 1.0 );'
 			'material.specularRoughness = clamp( roughnessFactor, 0.04, 1.0 );'
 		);
 		);
 
 
@@ -334,7 +347,7 @@ StandardNode.prototype.build = function ( builder ) {
 
 
 			output.push(
 			output.push(
 				clearcoat.code,
 				clearcoat.code,
-				'material.clearcoat = saturate( ' + clearcoat.result + ' );'
+				'material.clearcoat = saturate( ' + clearcoat.result + ' );' // Burley clearcoat model
 			);
 			);
 
 
 		} else if ( useClearcoat ) {
 		} else if ( useClearcoat ) {
@@ -347,7 +360,9 @@ StandardNode.prototype.build = function ( builder ) {
 
 
 			output.push(
 			output.push(
 				clearcoatRoughness.code,
 				clearcoatRoughness.code,
-				'material.clearcoatRoughness = clamp( ' + clearcoatRoughness.result + ', 0.04, 1.0 );'
+				'material.clearcoatRoughness = max( ' + clearcoatRoughness.result + ', 0.0525 );',
+				'material.clearcoatRoughness += geometryRoughness;',
+				'material.clearcoatRoughness = min( material.clearcoatRoughness, 1.0 );'
 			);
 			);
 
 
 		} else if ( useClearcoat ) {
 		} else if ( useClearcoat ) {

+ 4 - 0
examples/jsm/nodes/misc/TextureCubeUVNode.js

@@ -84,11 +84,14 @@ TextureCubeUVNode.Nodes = ( function () {
 
 
 	var bilinearCubeUV = new FunctionNode(
 	var bilinearCubeUV = new FunctionNode(
 		`TextureCubeUVData bilinearCubeUV(sampler2D envMap, vec3 direction, float mipInt) {
 		`TextureCubeUVData bilinearCubeUV(sampler2D envMap, vec3 direction, float mipInt) {
+
 			float face = getFace(direction);
 			float face = getFace(direction);
 			float filterInt = max(cubeUV_minMipLevel - mipInt, 0.0);
 			float filterInt = max(cubeUV_minMipLevel - mipInt, 0.0);
 			mipInt = max(mipInt, cubeUV_minMipLevel);
 			mipInt = max(mipInt, cubeUV_minMipLevel);
 			float faceSize = exp2(mipInt);
 			float faceSize = exp2(mipInt);
+
 			float texelSize = 1.0 / (3.0 * cubeUV_maxTileSize);
 			float texelSize = 1.0 / (3.0 * cubeUV_maxTileSize);
+
 			vec2 uv = getUV(direction, face) * (faceSize - 1.0);
 			vec2 uv = getUV(direction, face) * (faceSize - 1.0);
 			vec2 f = fract(uv);
 			vec2 f = fract(uv);
 			uv += 0.5 - f;
 			uv += 0.5 - f;
@@ -103,6 +106,7 @@ TextureCubeUVNode.Nodes = ( function () {
 			uv.y += filterInt * 2.0 * cubeUV_minTileSize;
 			uv.y += filterInt * 2.0 * cubeUV_minTileSize;
 			uv.x += 3.0 * max(0.0, cubeUV_maxTileSize - 2.0 * faceSize);
 			uv.x += 3.0 * max(0.0, cubeUV_maxTileSize - 2.0 * faceSize);
 			uv *= texelSize;
 			uv *= texelSize;
+ 
 			vec4 tl = texture2D(envMap, uv);
 			vec4 tl = texture2D(envMap, uv);
 			uv.x += texelSize;
 			uv.x += texelSize;
 			vec4 tr = texture2D(envMap, uv);
 			vec4 tr = texture2D(envMap, uv);