From 41d738ad9677a09f615a919c12495c52802ea030 Mon Sep 17 00:00:00 2001 From: sunag Date: Tue, 20 Aug 2019 17:21:08 -0300 Subject: [PATCH] fix ibl normals in reflectnode using StandardNode --- examples/jsm/nodes/accessors/NormalNode.js | 2 +- examples/jsm/nodes/accessors/ReflectNode.js | 56 ++++++++++++++++--- .../jsm/nodes/materials/nodes/StandardNode.js | 12 +++- 3 files changed, 60 insertions(+), 10 deletions(-) diff --git a/examples/jsm/nodes/accessors/NormalNode.js b/examples/jsm/nodes/accessors/NormalNode.js index e5990f9424..e6fff40270 100644 --- a/examples/jsm/nodes/accessors/NormalNode.js +++ b/examples/jsm/nodes/accessors/NormalNode.js @@ -87,7 +87,7 @@ NormalNode.prototype.toJSON = function ( meta ) { }; -NodeLib.addKeyword( 'normal', function () { +NodeLib.addKeyword( 'viewNormal', function () { return new NormalNode(); diff --git a/examples/jsm/nodes/accessors/ReflectNode.js b/examples/jsm/nodes/accessors/ReflectNode.js index 9020048751..73502a8dc2 100644 --- a/examples/jsm/nodes/accessors/ReflectNode.js +++ b/examples/jsm/nodes/accessors/ReflectNode.js @@ -8,7 +8,7 @@ import { NormalNode } from './NormalNode.js'; function ReflectNode( scope ) { - TempNode.call( this, 'v3', { unique: true } ); + TempNode.call( this, 'v3' ); this.scope = scope || ReflectNode.CUBE; @@ -22,6 +22,12 @@ ReflectNode.prototype = Object.create( TempNode.prototype ); ReflectNode.prototype.constructor = ReflectNode; ReflectNode.prototype.nodeType = "Reflect"; +ReflectNode.prototype.getUnique = function ( builder ) { + + return !builder.context.viewNormal; + +}; + ReflectNode.prototype.getType = function ( /* builder */ ) { switch ( this.scope ) { @@ -38,6 +44,8 @@ ReflectNode.prototype.getType = function ( /* builder */ ) { ReflectNode.prototype.generate = function ( builder, output ) { + var isUnique = this.getUnique( builder ); + if ( builder.isShader( 'fragment' ) ) { var result; @@ -46,12 +54,24 @@ ReflectNode.prototype.generate = function ( builder, output ) { case ReflectNode.VECTOR: - var viewNormal = new NormalNode().build( builder, 'v3' ); + var viewNormalNode = builder.context.viewNormal || new NormalNode(); + + var viewNormal = viewNormalNode.build( builder, 'v3' ); var viewPosition = new PositionNode( PositionNode.VIEW ).build( builder, 'v3' ); - builder.addNodeCode( 'vec3 reflectVec = inverseTransformDirection( reflect( -normalize( ' + viewPosition + ' ), ' + viewNormal + ' ), viewMatrix );' ); + var code = `inverseTransformDirection( reflect( -normalize( ${viewPosition} ), ${viewNormal} ), viewMatrix )`; + + if ( isUnique ) { + + builder.addNodeCode( `vec3 reflectVec = ${result};` ); + + result = 'reflectVec'; + + } else { + + result = code; - result = 'reflectVec'; + } break; @@ -59,9 +79,19 @@ ReflectNode.prototype.generate = function ( builder, output ) { var reflectVec = new ReflectNode( ReflectNode.VECTOR ).build( builder, 'v3' ); - builder.addNodeCode( 'vec3 reflectCubeVec = vec3( -1.0 * ' + reflectVec + '.x, ' + reflectVec + '.yz );' ); + var code = 'vec3( -' + reflectVec + '.x, ' + reflectVec + '.yz )'; - result = 'reflectCubeVec'; + if ( isUnique ) { + + builder.addNodeCode( `vec3 reflectCubeVec = ${result};` ); + + result = 'reflectCubeVec'; + + } else { + + result = code; + + } break; @@ -69,9 +99,19 @@ ReflectNode.prototype.generate = function ( builder, output ) { var reflectVec = new ReflectNode( ReflectNode.VECTOR ).build( builder, 'v3' ); - builder.addNodeCode( 'vec2 reflectSphereVec = normalize( ( viewMatrix * vec4( ' + reflectVec + ', 0.0 ) ).xyz + vec3( 0.0, 0.0, 1.0 ) ).xy * 0.5 + 0.5;' ); + var code = 'normalize( ( viewMatrix * vec4( ' + reflectVec + ', 0.0 ) ).xyz + vec3( 0.0, 0.0, 1.0 ) ).xy * 0.5 + 0.5'; + + if ( isUnique ) { + + builder.addNodeCode( `vec2 reflectSphereVec = ${result};` ); + + result = 'reflectSphereVec'; + + } else { + + result = code; - result = 'reflectSphereVec'; + } break; diff --git a/examples/jsm/nodes/materials/nodes/StandardNode.js b/examples/jsm/nodes/materials/nodes/StandardNode.js index df43751331..23993b6579 100644 --- a/examples/jsm/nodes/materials/nodes/StandardNode.js +++ b/examples/jsm/nodes/materials/nodes/StandardNode.js @@ -8,6 +8,7 @@ import { } from '../../../../../build/three.module.js'; import { Node } from '../../core/Node.js'; +import { ExpressionNode } from '../../core/ExpressionNode.js'; import { ColorNode } from '../../inputs/ColorNode.js'; import { FloatNode } from '../../inputs/FloatNode.js'; import { RoughnessToBlinnExponentNode } from '../../bsdfs/RoughnessToBlinnExponentNode.js'; @@ -122,6 +123,7 @@ StandardNode.prototype.build = function ( builder ) { var contextEnvironment = { bias: RoughnessToBlinnExponentNode, + viewNormal: new ExpressionNode('normal', 'v3'), gamma: true }; @@ -129,6 +131,12 @@ StandardNode.prototype.build = function ( builder ) { gamma: true }; + var contextClearCoatEnvironment = { + bias: RoughnessToBlinnExponentNode, + viewNormal: new ExpressionNode('clearCoatNormal', 'v3'), + gamma: true + }; + var useClearCoat = ! builder.isDefined( 'STANDARD' ); // analyze all nodes to reuse generate codes @@ -212,7 +220,7 @@ StandardNode.prototype.build = function ( builder ) { } - var clearCoatEnv = useClearCoat && environment ? this.environment.flow( builder, 'c', { cache: 'clearCoat', context: contextEnvironment, slot: 'environment' } ) : undefined; + var clearCoatEnv = useClearCoat && environment ? this.environment.flow( builder, 'c', { cache: 'clearCoat', context: contextClearCoatEnvironment, slot: 'environment' } ) : undefined; builder.requires.transparent = alpha !== undefined; @@ -493,6 +501,7 @@ StandardNode.prototype.copy = function ( source ) { if ( source.clearCoat ) this.clearCoat = source.clearCoat; if ( source.clearCoatRoughness ) this.clearCoatRoughness = source.clearCoatRoughness; + if ( source.clearCoatNormal ) this.clearCoatNormal = source.clearCoatNormal; if ( source.reflectivity ) this.reflectivity = source.reflectivity; @@ -536,6 +545,7 @@ StandardNode.prototype.toJSON = function ( meta ) { if ( this.clearCoat ) data.clearCoat = this.clearCoat.toJSON( meta ).uuid; if ( this.clearCoatRoughness ) data.clearCoatRoughness = this.clearCoatRoughness.toJSON( meta ).uuid; + if ( this.clearCoatNormal ) data.clearCoatNormal = this.clearCoatNormal.toJSON( meta ).uuid; if ( this.reflectivity ) data.reflectivity = this.reflectivity.toJSON( meta ).uuid; -- GitLab