Skip to content

Commit

Permalink
WebGPURenderer: Tree-shaking 1/2 - revision (mrdoob#29250)
Browse files Browse the repository at this point in the history
* revision of `registerNode()`

* added `registerNodeMaterial`

* revision

* revision

* revision

* adding and standardizing PURE

* added PURE and rev
  • Loading branch information
sunag committed Aug 29, 2024
1 parent 7506a35 commit 881073e
Show file tree
Hide file tree
Showing 193 changed files with 835 additions and 733 deletions.
17 changes: 17 additions & 0 deletions src/loaders/nodes/NodeMaterialLoader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
import { MaterialLoader } from '../../loaders/MaterialLoader.js';
import { createNodeMaterialFromType } from '../../materials/nodes/NodeMaterial.js';

const superFromTypeFunction = MaterialLoader.createMaterialFromType;

MaterialLoader.createMaterialFromType = function ( type ) {

const material = createNodeMaterialFromType( type );

if ( material !== undefined ) {

return material;

}

return superFromTypeFunction.call( this, type );

};

class NodeMaterialLoader extends MaterialLoader {

Expand Down
4 changes: 3 additions & 1 deletion src/materials/nodes/InstancedPointsNodeMaterial.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import NodeMaterial from './NodeMaterial.js';
import NodeMaterial, { registerNodeMaterial } from './NodeMaterial.js';
import { property } from '../../nodes/core/PropertyNode.js';
import { attribute } from '../../nodes/core/AttributeNode.js';
import { cameraProjectionMatrix } from '../../nodes/accessors/Camera.js';
Expand Down Expand Up @@ -165,3 +165,5 @@ class InstancedPointsNodeMaterial extends NodeMaterial {
}

export default InstancedPointsNodeMaterial;

InstancedPointsNodeMaterial.type = /*@__PURE__*/ registerNodeMaterial( 'InstancedPoints', InstancedPointsNodeMaterial );
4 changes: 3 additions & 1 deletion src/materials/nodes/Line2NodeMaterial.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import NodeMaterial from './NodeMaterial.js';
import NodeMaterial, { registerNodeMaterial } from './NodeMaterial.js';
import { property, varyingProperty } from '../../nodes/core/PropertyNode.js';
import { attribute } from '../../nodes/core/AttributeNode.js';
import { cameraProjectionMatrix } from '../../nodes/accessors/Camera.js';
Expand Down Expand Up @@ -433,3 +433,5 @@ class Line2NodeMaterial extends NodeMaterial {
}

export default Line2NodeMaterial;

Line2NodeMaterial.type = /*@__PURE__*/ registerNodeMaterial( 'Line2', Line2NodeMaterial );
4 changes: 3 additions & 1 deletion src/materials/nodes/LineBasicNodeMaterial.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import NodeMaterial from './NodeMaterial.js';
import NodeMaterial, { registerNodeMaterial } from './NodeMaterial.js';

import { LineBasicMaterial } from '../LineBasicMaterial.js';

Expand All @@ -23,3 +23,5 @@ class LineBasicNodeMaterial extends NodeMaterial {
}

export default LineBasicNodeMaterial;

LineBasicNodeMaterial.type = /*@__PURE__*/ registerNodeMaterial( 'LineBasic', LineBasicNodeMaterial );
4 changes: 3 additions & 1 deletion src/materials/nodes/LineDashedNodeMaterial.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import NodeMaterial from './NodeMaterial.js';
import NodeMaterial, { registerNodeMaterial } from './NodeMaterial.js';
import { attribute } from '../../nodes/core/AttributeNode.js';
import { materialLineDashSize, materialLineGapSize, materialLineScale } from '../../nodes/accessors/MaterialNode.js';
import { dashSize, gapSize } from '../../nodes/core/PropertyNode.js';
Expand Down Expand Up @@ -49,3 +49,5 @@ class LineDashedNodeMaterial extends NodeMaterial {
}

export default LineDashedNodeMaterial;

LineDashedNodeMaterial.type = /*@__PURE__*/ registerNodeMaterial( 'LineDashed', LineDashedNodeMaterial );
4 changes: 3 additions & 1 deletion src/materials/nodes/MeshBasicNodeMaterial.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import NodeMaterial from './NodeMaterial.js';
import NodeMaterial, { registerNodeMaterial } from './NodeMaterial.js';
import { materialLightMap } from '../../nodes/accessors/MaterialNode.js';
import BasicEnvironmentNode from '../../nodes/lighting/BasicEnvironmentNode.js';
import BasicLightMapNode from '../../nodes/lighting/BasicLightMapNode.js';
Expand Down Expand Up @@ -69,3 +69,5 @@ class MeshBasicNodeMaterial extends NodeMaterial {
}

export default MeshBasicNodeMaterial;

MeshBasicNodeMaterial.type = /*@__PURE__*/ registerNodeMaterial( 'MeshBasic', MeshBasicNodeMaterial );
4 changes: 3 additions & 1 deletion src/materials/nodes/MeshLambertNodeMaterial.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import NodeMaterial from './NodeMaterial.js';
import NodeMaterial, { registerNodeMaterial } from './NodeMaterial.js';
import BasicEnvironmentNode from '../../nodes/lighting/BasicEnvironmentNode.js';
import PhongLightingModel from '../../nodes/functions/PhongLightingModel.js';

Expand Down Expand Up @@ -39,3 +39,5 @@ class MeshLambertNodeMaterial extends NodeMaterial {
}

export default MeshLambertNodeMaterial;

MeshLambertNodeMaterial.type = /*@__PURE__*/ registerNodeMaterial( 'MeshLambert', MeshLambertNodeMaterial );
4 changes: 3 additions & 1 deletion src/materials/nodes/MeshMatcapNodeMaterial.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import NodeMaterial from './NodeMaterial.js';
import NodeMaterial, { registerNodeMaterial } from './NodeMaterial.js';
import { materialReference } from '../../nodes/accessors/MaterialReferenceNode.js';
import { diffuseColor } from '../../nodes/core/PropertyNode.js';
import { vec3 } from '../../nodes/tsl/TSLBase.js';
Expand Down Expand Up @@ -49,3 +49,5 @@ class MeshMatcapNodeMaterial extends NodeMaterial {


export default MeshMatcapNodeMaterial;

MeshMatcapNodeMaterial.type = /*@__PURE__*/ registerNodeMaterial( 'MeshMatcap', MeshMatcapNodeMaterial );
4 changes: 3 additions & 1 deletion src/materials/nodes/MeshNormalNodeMaterial.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import NodeMaterial from './NodeMaterial.js';
import NodeMaterial, { registerNodeMaterial } from './NodeMaterial.js';
import { diffuseColor } from '../../nodes/core/PropertyNode.js';
import { directionToColor } from '../../nodes/utils/Packing.js';
import { materialOpacity } from '../../nodes/accessors/MaterialNode.js';
Expand Down Expand Up @@ -36,3 +36,5 @@ class MeshNormalNodeMaterial extends NodeMaterial {
}

export default MeshNormalNodeMaterial;

MeshNormalNodeMaterial.type = /*@__PURE__*/ registerNodeMaterial( 'MeshNormal', MeshNormalNodeMaterial );
4 changes: 3 additions & 1 deletion src/materials/nodes/MeshPhongNodeMaterial.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import NodeMaterial from './NodeMaterial.js';
import NodeMaterial, { registerNodeMaterial } from './NodeMaterial.js';
import { shininess, specularColor } from '../../nodes/core/PropertyNode.js';
import { materialShininess, materialSpecular } from '../../nodes/accessors/MaterialNode.js';
import { float } from '../../nodes/tsl/TSLBase.js';
Expand Down Expand Up @@ -70,3 +70,5 @@ class MeshPhongNodeMaterial extends NodeMaterial {
}

export default MeshPhongNodeMaterial;

MeshPhongNodeMaterial.type = /*@__PURE__*/ registerNodeMaterial( 'MeshPhong', MeshPhongNodeMaterial );
3 changes: 3 additions & 0 deletions src/materials/nodes/MeshPhysicalNodeMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import MeshStandardNodeMaterial from './MeshStandardNodeMaterial.js';
import { mix, pow2, min } from '../../nodes/math/MathNode.js';

import { MeshPhysicalMaterial } from '../MeshPhysicalMaterial.js';
import { registerNodeMaterial } from './NodeMaterial.js';

const _defaultValues = /*@__PURE__*/ new MeshPhysicalMaterial();

Expand Down Expand Up @@ -240,3 +241,5 @@ class MeshPhysicalNodeMaterial extends MeshStandardNodeMaterial {
}

export default MeshPhysicalNodeMaterial;

MeshPhysicalNodeMaterial.type = /*@__PURE__*/ registerNodeMaterial( 'MeshPhysical', MeshPhysicalNodeMaterial );
3 changes: 3 additions & 0 deletions src/materials/nodes/MeshSSSNodeMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import PhysicalLightingModel from '../../nodes/functions/PhysicalLightingModel.j
import { transformedNormalView } from '../../nodes/accessors/Normal.js';
import { positionViewDirection } from '../../nodes/accessors/Position.js';
import { float, vec3 } from '../../nodes/tsl/TSLBase.js';
import { registerNodeMaterial } from './NodeMaterial.js';

class SSSLightingModel extends PhysicalLightingModel {

Expand Down Expand Up @@ -79,3 +80,5 @@ class MeshSSSNodeMaterial extends MeshPhysicalNodeMaterial {
}

export default MeshSSSNodeMaterial;

MeshSSSNodeMaterial.type = /*@__PURE__*/ registerNodeMaterial( 'MeshSSS', MeshSSSNodeMaterial );
4 changes: 3 additions & 1 deletion src/materials/nodes/MeshStandardNodeMaterial.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import NodeMaterial from './NodeMaterial.js';
import NodeMaterial, { registerNodeMaterial } from './NodeMaterial.js';
import { diffuseColor, metalness, roughness, specularColor, specularF90 } from '../../nodes/core/PropertyNode.js';
import { mix } from '../../nodes/math/MathNode.js';
import { materialRoughness, materialMetalness } from '../../nodes/accessors/MaterialNode.js';
Expand Down Expand Up @@ -100,3 +100,5 @@ class MeshStandardNodeMaterial extends NodeMaterial {
}

export default MeshStandardNodeMaterial;

MeshStandardNodeMaterial.type = /*@__PURE__*/ registerNodeMaterial( 'MeshStandard', MeshStandardNodeMaterial );
4 changes: 3 additions & 1 deletion src/materials/nodes/MeshToonNodeMaterial.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import NodeMaterial from './NodeMaterial.js';
import NodeMaterial, { registerNodeMaterial } from './NodeMaterial.js';
import ToonLightingModel from '../../nodes/functions/ToonLightingModel.js';

import { MeshToonMaterial } from '../MeshToonMaterial.js';
Expand Down Expand Up @@ -30,3 +30,5 @@ class MeshToonNodeMaterial extends NodeMaterial {
}

export default MeshToonNodeMaterial;

MeshToonNodeMaterial.type = /*@__PURE__*/ registerNodeMaterial( 'MeshToon', MeshToonNodeMaterial );
44 changes: 44 additions & 0 deletions src/materials/nodes/NodeMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import { depth } from '../../nodes/display/ViewportDepthNode.js';
import { cameraLogDepth } from '../../nodes/accessors/Camera.js';
import { clipping, clippingAlpha } from '../../nodes/accessors/ClippingNode.js';

const NodeMaterials = new Map();

class NodeMaterial extends Material {

constructor() {
Expand Down Expand Up @@ -621,3 +623,45 @@ class NodeMaterial extends Material {
}

export default NodeMaterial;

NodeMaterial.type = /*@__PURE__*/ registerNodeMaterial( '', NodeMaterial );

export function registerNodeMaterial( type, nodeMaterialClass ) {

const suffix = 'NodeMaterial';
const nodeMaterialType = type + suffix;

if ( typeof nodeMaterialClass !== 'function' ) throw new Error( `THREE.Node: NodeMaterial class "${ type }" is not a class.` );

if ( NodeMaterials.has( nodeMaterialType ) ) {

console.warn( `THREE.Node: Redefinition of NodeMaterial class "${ nodeMaterialType }".` );
return;

}

if ( type.slice( - suffix.length ) === suffix ) {

console.warn( `THREE.NodeMaterial: NodeMaterial class ${ nodeMaterialType } should not have '${ suffix }' suffix.` );
return;

}

NodeMaterials.set( nodeMaterialType, nodeMaterialClass );
nodeMaterialClass.type = nodeMaterialType;

return nodeMaterialType;

}

export function createNodeMaterialFromType( type ) {

const Material = NodeMaterials.get( type );

if ( Material !== undefined ) {

return new Material();

}

}
4 changes: 3 additions & 1 deletion src/materials/nodes/PointsNodeMaterial.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import NodeMaterial from './NodeMaterial.js';
import NodeMaterial, { registerNodeMaterial } from './NodeMaterial.js';

import { PointsMaterial } from '../PointsMaterial.js';

Expand Down Expand Up @@ -34,3 +34,5 @@ class PointsNodeMaterial extends NodeMaterial {
}

export default PointsNodeMaterial;

PointsNodeMaterial.type = /*@__PURE__*/ registerNodeMaterial( 'Points', PointsNodeMaterial );
4 changes: 3 additions & 1 deletion src/materials/nodes/ShadowNodeMaterial.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import NodeMaterial from './NodeMaterial.js';
import NodeMaterial, { registerNodeMaterial } from './NodeMaterial.js';
import ShadowMaskModel from '../../nodes/functions/ShadowMaskModel.js';

import { ShadowMaterial } from '../ShadowMaterial.js';
Expand Down Expand Up @@ -30,3 +30,5 @@ class ShadowNodeMaterial extends NodeMaterial {
}

export default ShadowNodeMaterial;

ShadowNodeMaterial.type = /*@__PURE__*/ registerNodeMaterial( 'Shadow', ShadowNodeMaterial );
4 changes: 3 additions & 1 deletion src/materials/nodes/SpriteNodeMaterial.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import NodeMaterial from './NodeMaterial.js';
import NodeMaterial, { registerNodeMaterial } from './NodeMaterial.js';
import { uniform } from '../../nodes/core/UniformNode.js';
import { cameraProjectionMatrix } from '../../nodes/accessors/Camera.js';
import { materialRotation } from '../../nodes/accessors/MaterialNode.js';
Expand Down Expand Up @@ -86,3 +86,5 @@ class SpriteNodeMaterial extends NodeMaterial {
}

export default SpriteNodeMaterial;

SpriteNodeMaterial.type = /*@__PURE__*/ registerNodeMaterial( 'Sprite', SpriteNodeMaterial );
4 changes: 3 additions & 1 deletion src/materials/nodes/VolumeNodeMaterial.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import NodeMaterial from './NodeMaterial.js';
import NodeMaterial, { registerNodeMaterial } from './NodeMaterial.js';
import { property } from '../../nodes/core/PropertyNode.js';
import { materialReference } from '../../nodes/accessors/MaterialReferenceNode.js';
import { modelWorldMatrixInverse } from '../../nodes/accessors/ModelNode.js';
Expand Down Expand Up @@ -100,3 +100,5 @@ class VolumeNodeMaterial extends NodeMaterial {
}

export default VolumeNodeMaterial;

VolumeNodeMaterial.type = /*@__PURE__*/ registerNodeMaterial( 'Volume', VolumeNodeMaterial );
2 changes: 1 addition & 1 deletion src/nodes/Nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export { default as ConstNode } from './core/ConstNode.js';
export { default as ContextNode } from './core/ContextNode.js';
export { default as IndexNode } from './core/IndexNode.js';
export { default as LightingModel } from './core/LightingModel.js';
export { default as Node } from './core/Node.js';
export { default as Node, registerNode } from './core/Node.js';
export { default as VarNode } from './core/VarNode.js';
export { default as NodeAttribute } from './core/NodeAttribute.js';
export { default as NodeBuilder } from './core/NodeBuilder.js';
Expand Down
6 changes: 3 additions & 3 deletions src/nodes/accessors/AccessorsUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import { mix } from '../math/MathNode.js';
import { anisotropy, anisotropyB, roughness } from '../core/PropertyNode.js';
import { positionViewDirection } from './Position.js';

export const TBNViewMatrix = mat3( tangentView, bitangentView, normalView );
export const TBNViewMatrix = /*@__PURE__*/ mat3( tangentView, bitangentView, normalView );

export const parallaxDirection = positionViewDirection.mul( TBNViewMatrix )/*.normalize()*/;
export const parallaxDirection = /*@__PURE__*/ positionViewDirection.mul( TBNViewMatrix )/*.normalize()*/;
export const parallaxUV = ( uv, scale ) => uv.sub( parallaxDirection.mul( scale ) );

export const transformedBentNormalView = ( () => {
export const transformedBentNormalView = /*@__PURE__*/ ( () => {

// https://google.github.io/filament/Filament.md.html#lighting/imagebasedlights/anisotropy

Expand Down
6 changes: 3 additions & 3 deletions src/nodes/accessors/BatchNode.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Node, { registerNodeClass } from '../core/Node.js';
import Node, { registerNode } from '../core/Node.js';
import { normalLocal } from './Normal.js';
import { positionLocal } from './Position.js';
import { nodeProxy, vec3, mat3, mat4, int, ivec2, float, Fn } from '../tsl/TSLBase.js';
Expand Down Expand Up @@ -119,6 +119,6 @@ class BatchNode extends Node {

export default BatchNode;

registerNodeClass( 'Batch', BatchNode );
BatchNode.type = /*@__PURE__*/ registerNode( 'Batch', BatchNode );

export const batch = nodeProxy( BatchNode );
export const batch = /*@__PURE__*/ nodeProxy( BatchNode );
12 changes: 6 additions & 6 deletions src/nodes/accessors/Bitangent.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { tangentGeometry, tangentLocal, tangentView, tangentWorld, transformedTa

const getBitangent = ( crossNormalTangent ) => crossNormalTangent.mul( tangentGeometry.w ).xyz;

export const bitangentGeometry = /*#__PURE__*/ varying( getBitangent( normalGeometry.cross( tangentGeometry ) ), 'v_bitangentGeometry' ).normalize().toVar( 'bitangentGeometry' );
export const bitangentLocal = /*#__PURE__*/ varying( getBitangent( normalLocal.cross( tangentLocal ) ), 'v_bitangentLocal' ).normalize().toVar( 'bitangentLocal' );
export const bitangentView = /*#__PURE__*/ varying( getBitangent( normalView.cross( tangentView ) ), 'v_bitangentView' ).normalize().toVar( 'bitangentView' );
export const bitangentWorld = /*#__PURE__*/ varying( getBitangent( normalWorld.cross( tangentWorld ) ), 'v_bitangentWorld' ).normalize().toVar( 'bitangentWorld' );
export const transformedBitangentView = /*#__PURE__*/ getBitangent( transformedNormalView.cross( transformedTangentView ) ).normalize().toVar( 'transformedBitangentView' );
export const transformedBitangentWorld = /*#__PURE__*/ transformedBitangentView.transformDirection( cameraViewMatrix ).normalize().toVar( 'transformedBitangentWorld' );
export const bitangentGeometry = /*@__PURE__*/ varying( getBitangent( normalGeometry.cross( tangentGeometry ) ), 'v_bitangentGeometry' ).normalize().toVar( 'bitangentGeometry' );
export const bitangentLocal = /*@__PURE__*/ varying( getBitangent( normalLocal.cross( tangentLocal ) ), 'v_bitangentLocal' ).normalize().toVar( 'bitangentLocal' );
export const bitangentView = /*@__PURE__*/ varying( getBitangent( normalView.cross( tangentView ) ), 'v_bitangentView' ).normalize().toVar( 'bitangentView' );
export const bitangentWorld = /*@__PURE__*/ varying( getBitangent( normalWorld.cross( tangentWorld ) ), 'v_bitangentWorld' ).normalize().toVar( 'bitangentWorld' );
export const transformedBitangentView = /*@__PURE__*/ getBitangent( transformedNormalView.cross( transformedTangentView ) ).normalize().toVar( 'transformedBitangentView' );
export const transformedBitangentWorld = /*@__PURE__*/ transformedBitangentView.transformDirection( cameraViewMatrix ).normalize().toVar( 'transformedBitangentWorld' );
4 changes: 2 additions & 2 deletions src/nodes/accessors/BufferAttributeNode.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { registerNodeClass } from '../core/Node.js';
import { registerNode } from '../core/Node.js';
import InputNode from '../core/InputNode.js';
import { nodeObject, addMethodChaining } from '../tsl/TSLCore.js';
import { varying } from '../core/VaryingNode.js';
Expand Down Expand Up @@ -151,7 +151,7 @@ class BufferAttributeNode extends InputNode {

export default BufferAttributeNode;

registerNodeClass( 'BufferAttribute', BufferAttributeNode );
BufferAttributeNode.type = /*@__PURE__*/ registerNode( 'BufferAttribute', BufferAttributeNode );

export const bufferAttribute = ( array, type, stride, offset ) => nodeObject( new BufferAttributeNode( array, type, stride, offset ) );
export const dynamicBufferAttribute = ( array, type, stride, offset ) => bufferAttribute( array, type, stride, offset ).setUsage( DynamicDrawUsage );
Expand Down
4 changes: 2 additions & 2 deletions src/nodes/accessors/BufferNode.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { registerNodeClass } from '../core/Node.js';
import { registerNode } from '../core/Node.js';
import UniformNode from '../core/UniformNode.js';
import { nodeObject } from '../tsl/TSLBase.js';

Expand Down Expand Up @@ -31,6 +31,6 @@ class BufferNode extends UniformNode {

export default BufferNode;

registerNodeClass( 'Buffer', BufferNode );
BufferNode.type = /*@__PURE__*/ registerNode( 'Buffer', BufferNode );

export const buffer = ( value, type, count ) => nodeObject( new BufferNode( value, type, count ) );
Loading

0 comments on commit 881073e

Please sign in to comment.