123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631 |
- import { Material, NormalBlending } from 'three';
- import { getNodeChildren, getCacheKey } from '../core/NodeUtils.js';
- import { attribute } from '../core/AttributeNode.js';
- import { output, diffuseColor, varyingProperty } from '../core/PropertyNode.js';
- import { materialAlphaTest, materialColor, materialOpacity, materialEmissive, materialNormal } from '../accessors/MaterialNode.js';
- import { modelViewProjection } from '../accessors/ModelViewProjectionNode.js';
- import { transformedNormalView, normalLocal } from '../accessors/NormalNode.js';
- import { instance } from '../accessors/InstanceNode.js';
- import { batch } from '../accessors/BatchNode.js';
- import { materialReference } from '../accessors/MaterialReferenceNode.js';
- import { positionLocal, positionView } from '../accessors/PositionNode.js';
- import { skinningReference } from '../accessors/SkinningNode.js';
- import { morphReference } from '../accessors/MorphNode.js';
- import { texture } from '../accessors/TextureNode.js';
- import { cubeTexture } from '../accessors/CubeTextureNode.js';
- import { lightsNode } from '../lighting/LightsNode.js';
- import { mix } from '../math/MathNode.js';
- import { float, vec3, vec4 } from '../shadernode/ShaderNode.js';
- import AONode from '../lighting/AONode.js';
- import { lightingContext } from '../lighting/LightingContextNode.js';
- import EnvironmentNode from '../lighting/EnvironmentNode.js';
- import IrradianceNode from '../lighting/IrradianceNode.js';
- import { depth } from '../display/ViewportDepthNode.js';
- import { cameraLogDepth } from '../accessors/CameraNode.js';
- import { clipping, clippingAlpha } from '../accessors/ClippingNode.js';
- import { faceDirection } from '../display/FrontFacingNode.js';
- const NodeMaterials = new Map();
- class NodeMaterial extends Material {
- constructor() {
- super();
- this.isNodeMaterial = true;
- this.type = this.constructor.type;
- this.forceSinglePass = false;
- this.fog = true;
- this.lights = true;
- this.normals = true;
- this.lightsNode = null;
- this.envNode = null;
- this.aoNode = null;
- this.colorNode = null;
- this.normalNode = null;
- this.opacityNode = null;
- this.backdropNode = null;
- this.backdropAlphaNode = null;
- this.alphaTestNode = null;
- this.positionNode = null;
- this.depthNode = null;
- this.shadowNode = null;
- this.shadowPositionNode = null;
- this.outputNode = null;
- this.fragmentNode = null;
- this.vertexNode = null;
- }
- customProgramCacheKey() {
- return this.type + getCacheKey( this );
- }
- build( builder ) {
- this.setup( builder );
- }
- setup( builder ) {
- // < VERTEX STAGE >
- builder.addStack();
- builder.stack.outputNode = this.vertexNode || this.setupPosition( builder );
- builder.addFlow( 'vertex', builder.removeStack() );
- // < FRAGMENT STAGE >
- builder.addStack();
- let resultNode;
- const clippingNode = this.setupClipping( builder );
- if ( this.depthWrite === true ) this.setupDepth( builder );
- if ( this.fragmentNode === null ) {
- if ( this.normals === true ) this.setupNormal( builder );
- this.setupDiffuseColor( builder );
- this.setupVariants( builder );
- const outgoingLightNode = this.setupLighting( builder );
- if ( clippingNode !== null ) builder.stack.add( clippingNode );
- // force unsigned floats - useful for RenderTargets
- const basicOutput = vec4( outgoingLightNode, diffuseColor.a ).max( 0 );
- resultNode = this.setupOutput( builder, basicOutput );
- // OUTPUT NODE
- output.assign( resultNode );
- //
- if ( this.outputNode !== null ) resultNode = this.outputNode;
- } else {
- let fragmentNode = this.fragmentNode;
- if ( fragmentNode.isOutputStructNode !== true ) {
- fragmentNode = vec4( fragmentNode );
- }
- resultNode = this.setupOutput( builder, fragmentNode );
- }
- builder.stack.outputNode = resultNode;
- builder.addFlow( 'fragment', builder.removeStack() );
- }
- setupClipping( builder ) {
- if ( builder.clippingContext === null ) return null;
- const { globalClippingCount, localClippingCount } = builder.clippingContext;
- let result = null;
- if ( globalClippingCount || localClippingCount ) {
- if ( this.alphaToCoverage ) {
- // to be added to flow when the color/alpha value has been determined
- result = clippingAlpha();
- } else {
- builder.stack.add( clipping() );
- }
- }
- return result;
- }
- setupDepth( builder ) {
- const { renderer } = builder;
- // Depth
- let depthNode = this.depthNode;
- if ( depthNode === null && renderer.logarithmicDepthBuffer === true ) {
- const fragDepth = modelViewProjection().w.add( 1 );
- depthNode = fragDepth.log2().mul( cameraLogDepth ).mul( 0.5 );
- }
- if ( depthNode !== null ) {
- depth.assign( depthNode ).append();
- }
- }
- setupPosition( builder ) {
- const { object } = builder;
- const geometry = object.geometry;
- builder.addStack();
- // Vertex
- if ( geometry.morphAttributes.position || geometry.morphAttributes.normal || geometry.morphAttributes.color ) {
- morphReference( object ).append();
- }
- if ( object.isSkinnedMesh === true ) {
- skinningReference( object ).append();
- }
- if ( this.displacementMap ) {
- const displacementMap = materialReference( 'displacementMap', 'texture' );
- const displacementScale = materialReference( 'displacementScale', 'float' );
- const displacementBias = materialReference( 'displacementBias', 'float' );
- positionLocal.addAssign( normalLocal.normalize().mul( ( displacementMap.x.mul( displacementScale ).add( displacementBias ) ) ) );
- }
- if ( object.isBatchedMesh ) {
- batch( object ).append();
- }
- if ( ( object.instanceMatrix && object.instanceMatrix.isInstancedBufferAttribute === true ) ) {
- instance( object ).append();
- }
- if ( this.positionNode !== null ) {
- positionLocal.assign( this.positionNode );
- }
- const mvp = modelViewProjection();
- builder.context.vertex = builder.removeStack();
- builder.context.mvp = mvp;
- return mvp;
- }
- setupDiffuseColor( { object, geometry } ) {
- let colorNode = this.colorNode ? vec4( this.colorNode ) : materialColor;
- // VERTEX COLORS
- if ( this.vertexColors === true && geometry.hasAttribute( 'color' ) ) {
- colorNode = vec4( colorNode.xyz.mul( attribute( 'color', 'vec3' ) ), colorNode.a );
- }
- // Instanced colors
- if ( object.instanceColor ) {
- const instanceColor = varyingProperty( 'vec3', 'vInstanceColor' );
- colorNode = instanceColor.mul( colorNode );
- }
- // COLOR
- diffuseColor.assign( colorNode );
- // OPACITY
- const opacityNode = this.opacityNode ? float( this.opacityNode ) : materialOpacity;
- diffuseColor.a.assign( diffuseColor.a.mul( opacityNode ) );
- // ALPHA TEST
- if ( this.alphaTestNode !== null || this.alphaTest > 0 ) {
- const alphaTestNode = this.alphaTestNode !== null ? float( this.alphaTestNode ) : materialAlphaTest;
- diffuseColor.a.lessThanEqual( alphaTestNode ).discard();
- }
- if ( this.transparent === false && this.blending === NormalBlending && this.alphaToCoverage === false ) {
- diffuseColor.a.assign( 1.0 );
- }
- }
- setupVariants( /*builder*/ ) {
- // Interface function.
- }
- setupNormal() {
- // NORMAL VIEW
- if ( this.flatShading === true ) {
- const normalNode = positionView.dFdx().cross( positionView.dFdy() ).normalize();
- transformedNormalView.assign( normalNode.mul( faceDirection ) );
- } else {
- const normalNode = this.normalNode ? vec3( this.normalNode ) : materialNormal;
- transformedNormalView.assign( normalNode.mul( faceDirection ) );
- }
- }
- getEnvNode( builder ) {
- let node = null;
- if ( this.envNode ) {
- node = this.envNode;
- } else if ( this.envMap ) {
- node = this.envMap.isCubeTexture ? cubeTexture( this.envMap ) : texture( this.envMap );
- } else if ( builder.environmentNode ) {
- node = builder.environmentNode;
- }
- return node;
- }
- setupLights( builder ) {
- const envNode = this.getEnvNode( builder );
- //
- const materialLightsNode = [];
- if ( envNode ) {
- materialLightsNode.push( new EnvironmentNode( envNode ) );
- }
- if ( builder.material.lightMap ) {
- materialLightsNode.push( new IrradianceNode( materialReference( 'lightMap', 'texture' ) ) );
- }
- if ( this.aoNode !== null || builder.material.aoMap ) {
- const aoNode = this.aoNode !== null ? this.aoNode : texture( builder.material.aoMap );
- materialLightsNode.push( new AONode( aoNode ) );
- }
- let lightsN = this.lightsNode || builder.lightsNode;
- if ( materialLightsNode.length > 0 ) {
- lightsN = lightsNode( [ ...lightsN.lightNodes, ...materialLightsNode ] );
- }
- return lightsN;
- }
- setupLightingModel( /*builder*/ ) {
- // Interface function.
- }
- setupLighting( builder ) {
- const { material } = builder;
- const { backdropNode, backdropAlphaNode, emissiveNode } = this;
- // OUTGOING LIGHT
- const lights = this.lights === true || this.lightsNode !== null;
- const lightsNode = lights ? this.setupLights( builder ) : null;
- let outgoingLightNode = diffuseColor.rgb;
- if ( lightsNode && lightsNode.hasLight !== false ) {
- const lightingModel = this.setupLightingModel( builder );
- outgoingLightNode = lightingContext( lightsNode, lightingModel, backdropNode, backdropAlphaNode );
- } else if ( backdropNode !== null ) {
- outgoingLightNode = vec3( backdropAlphaNode !== null ? mix( outgoingLightNode, backdropNode, backdropAlphaNode ) : backdropNode );
- }
- // EMISSIVE
- if ( ( emissiveNode && emissiveNode.isNode === true ) || ( material.emissive && material.emissive.isColor === true ) ) {
- outgoingLightNode = outgoingLightNode.add( vec3( emissiveNode ? emissiveNode : materialEmissive ) );
- }
- return outgoingLightNode;
- }
- setupOutput( builder, outputNode ) {
- // FOG
- if ( this.fog === true ) {
- const fogNode = builder.fogNode;
- if ( fogNode ) outputNode = vec4( fogNode.mix( outputNode.rgb, fogNode.colorNode ), outputNode.a );
- }
- return outputNode;
- }
- setDefaultValues( material ) {
- // This approach is to reuse the native refreshUniforms*
- // and turn available the use of features like transmission and environment in core
- for ( const property in material ) {
- const value = material[ property ];
- if ( this[ property ] === undefined ) {
- this[ property ] = value;
- if ( value && value.clone ) this[ property ] = value.clone();
- }
- }
- const descriptors = Object.getOwnPropertyDescriptors( material.constructor.prototype );
- for ( const key in descriptors ) {
- if ( Object.getOwnPropertyDescriptor( this.constructor.prototype, key ) === undefined &&
- descriptors[ key ].get !== undefined ) {
- Object.defineProperty( this.constructor.prototype, key, descriptors[ key ] );
- }
- }
- }
- toJSON( meta ) {
- const isRoot = ( meta === undefined || typeof meta === 'string' );
- if ( isRoot ) {
- meta = {
- textures: {},
- images: {},
- nodes: {}
- };
- }
- const data = Material.prototype.toJSON.call( this, meta );
- const nodeChildren = getNodeChildren( this );
- data.inputNodes = {};
- for ( const { property, childNode } of nodeChildren ) {
- data.inputNodes[ property ] = childNode.toJSON( meta ).uuid;
- }
- // TODO: Copied from Object3D.toJSON
- function extractFromCache( cache ) {
- const values = [];
- for ( const key in cache ) {
- const data = cache[ key ];
- delete data.metadata;
- values.push( data );
- }
- return values;
- }
- if ( isRoot ) {
- const textures = extractFromCache( meta.textures );
- const images = extractFromCache( meta.images );
- const nodes = extractFromCache( meta.nodes );
- if ( textures.length > 0 ) data.textures = textures;
- if ( images.length > 0 ) data.images = images;
- if ( nodes.length > 0 ) data.nodes = nodes;
- }
- return data;
- }
- copy( source ) {
- this.lightsNode = source.lightsNode;
- this.envNode = source.envNode;
- this.colorNode = source.colorNode;
- this.normalNode = source.normalNode;
- this.opacityNode = source.opacityNode;
- this.backdropNode = source.backdropNode;
- this.backdropAlphaNode = source.backdropAlphaNode;
- this.alphaTestNode = source.alphaTestNode;
- this.positionNode = source.positionNode;
- this.depthNode = source.depthNode;
- this.shadowNode = source.shadowNode;
- this.shadowPositionNode = source.shadowPositionNode;
- this.outputNode = source.outputNode;
- this.fragmentNode = source.fragmentNode;
- this.vertexNode = source.vertexNode;
- return super.copy( source );
- }
- static fromMaterial( material ) {
- if ( material.isNodeMaterial === true ) { // is already a node material
- return material;
- }
- const type = material.type.replace( 'Material', 'NodeMaterial' );
- const nodeMaterial = createNodeMaterialFromType( type );
- if ( nodeMaterial === undefined ) {
- throw new Error( `NodeMaterial: Material "${ material.type }" is not compatible.` );
- }
- for ( const key in material ) {
- nodeMaterial[ key ] = material[ key ];
- }
- return nodeMaterial;
- }
- }
- export default NodeMaterial;
- export function addNodeMaterial( type, nodeMaterial ) {
- if ( typeof nodeMaterial !== 'function' || ! type ) throw new Error( `Node material ${ type } is not a class` );
- if ( NodeMaterials.has( type ) ) {
- console.warn( `Redefinition of node material ${ type }` );
- return;
- }
- NodeMaterials.set( type, nodeMaterial );
- nodeMaterial.type = type;
- }
- export function createNodeMaterialFromType( type ) {
- const Material = NodeMaterials.get( type );
- if ( Material !== undefined ) {
- return new Material();
- }
- }
- addNodeMaterial( 'NodeMaterial', NodeMaterial );
|