123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- import { Material, ShaderMaterial } from 'three';
- import { getNodesKeys, getCacheKey } from '../core/NodeUtils.js';
- import ExpressionNode from '../core/ExpressionNode.js';
- import {
- float, vec3, vec4,
- assign, label, mul, bypass,
- positionLocal, skinning, instance, modelViewProjection, lightingContext, colorSpace,
- materialAlphaTest, materialColor, materialOpacity
- } from '../shadernode/ShaderNodeElements.js';
- class NodeMaterial extends ShaderMaterial {
- constructor() {
- super();
- this.isNodeMaterial = true;
- this.type = this.constructor.name;
- this.lights = true;
- }
- build( builder ) {
- this.generatePosition( builder );
- const { lightsNode } = this;
- const { diffuseColorNode } = this.generateDiffuseColor( builder );
- const outgoingLightNode = this.generateLight( builder, { diffuseColorNode, lightsNode } );
- this.generateOutput( builder, { diffuseColorNode, outgoingLightNode } );
- }
- customProgramCacheKey() {
- return getCacheKey( this );
- }
- generatePosition( builder ) {
- const object = builder.object;
- // < VERTEX STAGE >
- let vertex = positionLocal;
- if ( this.positionNode !== null ) {
- vertex = bypass( vertex, assign( positionLocal, this.positionNode ) );
- }
- if ( object.instanceMatrix?.isInstancedBufferAttribute === true && builder.isAvailable( 'instance' ) === true ) {
- vertex = bypass( vertex, instance( object ) );
- }
- if ( object.isSkinnedMesh === true ) {
- vertex = bypass( vertex, skinning( object ) );
- }
- builder.context.vertex = vertex;
- builder.addFlow( 'vertex', modelViewProjection() );
- }
- generateDiffuseColor( builder ) {
- // < FRAGMENT STAGE >
- let colorNode = vec4( this.colorNode || materialColor );
- let opacityNode = this.opacityNode ? float( this.opacityNode ) : materialOpacity;
- // COLOR
- colorNode = builder.addFlow( 'fragment', label( colorNode, 'Color' ) );
- const diffuseColorNode = builder.addFlow( 'fragment', label( colorNode, 'DiffuseColor' ) );
- // OPACITY
- opacityNode = builder.addFlow( 'fragment', label( opacityNode, 'OPACITY' ) );
- builder.addFlow( 'fragment', assign( diffuseColorNode.a, mul( diffuseColorNode.a, opacityNode ) ) );
- // ALPHA TEST
- if ( this.alphaTestNode || this.alphaTest > 0 ) {
- const alphaTestNode = this.alphaTestNode ? float( this.alphaTestNode ) : materialAlphaTest;
- builder.addFlow( 'fragment', label( alphaTestNode, 'AlphaTest' ) );
- // @TODO: remove ExpressionNode here and then possibly remove it completely
- builder.addFlow( 'fragment', new ExpressionNode( 'if ( DiffuseColor.a <= AlphaTest ) { discard; }' ) );
- }
- return { colorNode, diffuseColorNode };
- }
- generateLight( builder, { diffuseColorNode, lightingModelNode, lightsNode = builder.lightsNode } ) {
- // < ANALYTIC LIGHTS >
- // OUTGOING LIGHT
- let outgoingLightNode = diffuseColorNode.xyz;
- if ( lightsNode && lightsNode.hasLight !== false ) outgoingLightNode = builder.addFlow( 'fragment', label( lightingContext( lightsNode, lightingModelNode ), 'Light' ) );
- return outgoingLightNode;
- }
- generateOutput( builder, { diffuseColorNode, outgoingLightNode } ) {
- // OUTPUT
- let outputNode = vec4( outgoingLightNode, diffuseColorNode.a );
- // ENCODING
- outputNode = colorSpace( outputNode, builder.renderer.outputEncoding );
- // FOG
- if ( builder.fogNode ) outputNode = vec4( vec3( builder.fogNode.mix( outputNode ) ), outputNode.w );
- // RESULT
- builder.addFlow( 'fragment', label( outputNode, 'Output' ) );
- return outputNode;
- }
- setDefaultValues( values ) {
- // 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 values ) {
- const value = values[ property ];
- if ( this[ property ] === undefined ) {
- this[ property ] = value?.clone?.() || value;
- }
- }
- Object.assign( this.defines, values.defines );
- }
- toJSON( meta ) {
- const isRoot = ( meta === undefined || typeof meta === 'string' );
- if ( isRoot ) {
- meta = {
- textures: {},
- images: {},
- nodes: {}
- };
- }
- const data = Material.prototype.toJSON.call( this, meta );
- const nodeKeys = getNodesKeys( this );
- data.inputNodes = {};
- for ( const name of nodeKeys ) {
- data.inputNodes[ name ] = this[ name ].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;
- }
- static fromMaterial( /*material*/ ) { }
- }
- export default NodeMaterial;
|