|
@@ -1,12 +1,15 @@
|
|
|
import NodeMaterial from './NodeMaterial.js';
|
|
|
import {
|
|
|
- float, vec3, vec4,
|
|
|
- context, assign, label, mul, invert, mix,
|
|
|
- normalView,
|
|
|
- materialRoughness, materialMetalness
|
|
|
+ float, vec3, vec4, normalView, add, context,
|
|
|
+ assign, label, mul, invert, mix, texture, uniform,
|
|
|
+ materialRoughness, materialMetalness, materialEmissive
|
|
|
} from '../shadernode/ShaderNodeElements.js';
|
|
|
+import LightsNode from '../lighting/LightsNode.js';
|
|
|
+import EnvironmentLightNode from '../lighting/EnvironmentLightNode.js';
|
|
|
+import AONode from '../lighting/AONode.js';
|
|
|
import getRoughness from '../functions/material/getRoughness.js';
|
|
|
import PhysicalLightingModel from '../functions/PhysicalLightingModel.js';
|
|
|
+import NormalMapNode from '../display/NormalMapNode.js';
|
|
|
|
|
|
import { MeshStandardMaterial } from 'three';
|
|
|
|
|
@@ -35,7 +38,7 @@ export default class MeshStandardNodeMaterial extends NodeMaterial {
|
|
|
|
|
|
this.envNode = null;
|
|
|
|
|
|
- this.lightNode = null;
|
|
|
+ this.lightsNode = null;
|
|
|
|
|
|
this.positionNode = null;
|
|
|
|
|
@@ -47,34 +50,43 @@ export default class MeshStandardNodeMaterial extends NodeMaterial {
|
|
|
|
|
|
build( builder ) {
|
|
|
|
|
|
- const lightNode = this.lightNode || builder.lightNode; // use scene lights
|
|
|
-
|
|
|
let { colorNode, diffuseColorNode } = this.generateMain( builder );
|
|
|
+ const envNode = this.envNode || builder.scene.environmentNode;
|
|
|
|
|
|
diffuseColorNode = this.generateStandardMaterial( builder, { colorNode, diffuseColorNode } );
|
|
|
|
|
|
- const outgoingLightNode = this.generateLight( builder, { diffuseColorNode, lightNode } );
|
|
|
+ if ( this.lightsNode ) builder.lightsNode = this.lightsNode;
|
|
|
|
|
|
- this.generateOutput( builder, { diffuseColorNode, outgoingLightNode } );
|
|
|
+ let materialLightsNode = [];
|
|
|
|
|
|
- }
|
|
|
+ if ( envNode ) {
|
|
|
|
|
|
- generateLight( builder, { diffuseColorNode, lightNode } ) {
|
|
|
+ materialLightsNode.push( new EnvironmentLightNode( envNode ) );
|
|
|
|
|
|
- let outgoingLightNode = super.generateLight( builder, { diffuseColorNode, lightNode, lightingModelNode: PhysicalLightingModel } );
|
|
|
+ }
|
|
|
|
|
|
- // TONE MAPPING
|
|
|
+ if ( builder.material.aoMap ) {
|
|
|
|
|
|
- const renderer = builder.renderer;
|
|
|
+ materialLightsNode.push( new AONode( texture( builder.material.aoMap ) ) );
|
|
|
|
|
|
- if ( renderer.toneMappingNode ) outgoingLightNode = context( renderer.toneMappingNode, { color: outgoingLightNode } );
|
|
|
+ }
|
|
|
|
|
|
- return outgoingLightNode;
|
|
|
+ if ( materialLightsNode.length > 0 ) {
|
|
|
+
|
|
|
+ builder.lightsNode = new LightsNode( [ ...builder.lightsNode.lightNodes, ...materialLightsNode ] );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ const outgoingLightNode = this.generateLight( builder, { diffuseColorNode, lightingModelNode: PhysicalLightingModel } );
|
|
|
+
|
|
|
+ this.generateOutput( builder, { diffuseColorNode, outgoingLightNode } );
|
|
|
|
|
|
}
|
|
|
|
|
|
generateStandardMaterial( builder, { colorNode, diffuseColorNode } ) {
|
|
|
|
|
|
+ const { material } = builder;
|
|
|
+
|
|
|
// METALNESS
|
|
|
|
|
|
let metalnessNode = this.metalnessNode ? float( this.metalnessNode ) : materialMetalness;
|
|
@@ -97,7 +109,7 @@ export default class MeshStandardNodeMaterial extends NodeMaterial {
|
|
|
|
|
|
// NORMAL VIEW
|
|
|
|
|
|
- const normalNode = this.normalNode ? vec3( this.normalNode ) : normalView;
|
|
|
+ const normalNode = this.normalNode ? vec3( this.normalNode ) : ( material.normalMap ? new NormalMapNode( texture( material.normalMap ), uniform( material.normalScale ) ) : normalView );
|
|
|
|
|
|
builder.addFlow( 'fragment', label( normalNode, 'TransformedNormalView' ) );
|
|
|
|
|
@@ -105,6 +117,26 @@ export default class MeshStandardNodeMaterial extends NodeMaterial {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ generateLight( builder, { diffuseColorNode, lightingModelNode, lightsNode = builder.lightsNode } ) {
|
|
|
+
|
|
|
+ const renderer = builder.renderer;
|
|
|
+
|
|
|
+ // OUTGOING LIGHT
|
|
|
+
|
|
|
+ let outgoingLightNode = super.generateLight( builder, { diffuseColorNode, lightingModelNode, lightsNode } );
|
|
|
+
|
|
|
+ // EMISSIVE
|
|
|
+
|
|
|
+ outgoingLightNode = add( vec3( this.emissiveNode || materialEmissive ), outgoingLightNode );
|
|
|
+
|
|
|
+ // TONE MAPPING
|
|
|
+
|
|
|
+ if ( renderer.toneMappingNode ) outgoingLightNode = context( renderer.toneMappingNode, { color: outgoingLightNode } );
|
|
|
+
|
|
|
+ return outgoingLightNode;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
copy( source ) {
|
|
|
|
|
|
this.colorNode = source.colorNode;
|
|
@@ -124,7 +156,7 @@ export default class MeshStandardNodeMaterial extends NodeMaterial {
|
|
|
|
|
|
this.envNode = source.envNode;
|
|
|
|
|
|
- this.lightNode = source.lightNode;
|
|
|
+ this.lightsNode = source.lightsNode;
|
|
|
|
|
|
this.positionNode = source.positionNode;
|
|
|
|