Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nodes: Introduce static type #29282

Merged
merged 4 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion examples/webgpu_materials.html
Original file line number Diff line number Diff line change
Expand Up @@ -398,10 +398,28 @@

}

function moduleToLib( module ) {

const lib = {};

for ( const nodeElement of Object.values( module ) ) {

if ( typeof nodeElement === 'function' && nodeElement.type !== undefined ) {

lib[ nodeElement.type ] = nodeElement;

}

}

return lib;

}

function testSerialization( mesh ) {

const json = mesh.toJSON();
const loader = new THREE.NodeObjectLoader();
const loader = new THREE.NodeObjectLoader().setNodes( moduleToLib( TSL ) );
const serializedMesh = loader.parse( json );

serializedMesh.position.x = ( objects.length % 4 ) * 200 - 400;
Expand Down
8 changes: 7 additions & 1 deletion src/loaders/MaterialLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class MaterialLoader extends Loader {

}

const material = MaterialLoader.createMaterialFromType( json.type );
const material = this.createMaterialFromType( json.type );

if ( json.uuid !== undefined ) material.uuid = json.uuid;
if ( json.name !== undefined ) material.name = json.name;
Expand Down Expand Up @@ -342,6 +342,12 @@ class MaterialLoader extends Loader {

}

createMaterialFromType( type ) {

return MaterialLoader.createMaterialFromType( type );

}

static createMaterialFromType( type ) {

const materialLib = {
Expand Down
28 changes: 24 additions & 4 deletions src/loaders/nodes/NodeLoader.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { createNodeFromType } from '../../nodes/core/Node.js';
import { nodeObject } from '../../nodes/tsl/TSLBase.js';
import { nodeObject, float } from '../../nodes/tsl/TSLBase.js';

import { Loader } from '../Loader.js';
import { FileLoader } from '../../loaders/FileLoader.js';
Expand All @@ -11,6 +10,7 @@ class NodeLoader extends Loader {
super( manager );

this.textures = {};
this.nodes = {};

}

Expand Down Expand Up @@ -56,7 +56,7 @@ class NodeLoader extends Loader {

const { uuid, type } = nodeJSON;

nodes[ uuid ] = nodeObject( createNodeFromType( type ) );
nodes[ uuid ] = this.createNodeFromType( type );
nodes[ uuid ].uuid = uuid;

}
Expand All @@ -82,7 +82,7 @@ class NodeLoader extends Loader {

parse( json ) {

const node = nodeObject( createNodeFromType( json.type ) );
const node = this.createNodeFromType( json.type );
node.uuid = json.uuid;

const nodes = this.parseNodes( json.nodes );
Expand All @@ -105,6 +105,26 @@ class NodeLoader extends Loader {

}

setNodes( value ) {

this.nodes = value;
return this;

}

createNodeFromType( type ) {

if ( this.nodes[ type ] === undefined ) {

console.error( 'THREE.NodeLoader: Node type not found:', type );
return float();

}

return nodeObject( new this.nodes[ type ]() );

}

}

export default NodeLoader;
38 changes: 21 additions & 17 deletions src/loaders/nodes/NodeMaterialLoader.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,4 @@
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 All @@ -24,6 +7,7 @@ class NodeMaterialLoader extends MaterialLoader {
super( manager );

this.nodes = {};
this.nodeMaterials = {};

}

Expand All @@ -49,11 +33,31 @@ class NodeMaterialLoader extends MaterialLoader {
setNodes( value ) {

this.nodes = value;
return this;

}

setNodeMaterials( value ) {

this.nodeMaterials = value;
return this;

}

createMaterialFromType( type ) {

const materialClass = this.nodeMaterials[ type ];

if ( materialClass !== undefined ) {

return new materialClass();

}

return super.createMaterialFromType( type );

}

}

export default NodeMaterialLoader;
19 changes: 19 additions & 0 deletions src/loaders/nodes/NodeObjectLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,27 @@ class NodeObjectLoader extends ObjectLoader {

super( manager );

this.nodes = {};
this.nodeMaterials = {};

this._nodesJSON = null;

}

setNodes( value ) {

this.nodes = value;
return this;

}

setNodeMaterials( value ) {

this.nodeMaterials = value;
return this;

}

parse( json, onLoad ) {

this._nodesJSON = json.nodes;
Expand All @@ -30,6 +47,7 @@ class NodeObjectLoader extends ObjectLoader {
if ( json !== undefined ) {

const loader = new NodeLoader();
loader.setNodes( this.nodes );
loader.setTextures( textures );

return loader.parseNodes( json );
Expand All @@ -51,6 +69,7 @@ class NodeObjectLoader extends ObjectLoader {
const loader = new NodeMaterialLoader();
loader.setTextures( textures );
loader.setNodes( nodes );
loader.setNodeMaterials( this.nodeMaterials );

for ( let i = 0, l = json.length; i < l; i ++ ) {

Expand Down
10 changes: 7 additions & 3 deletions src/materials/nodes/InstancedPointsNodeMaterial.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import NodeMaterial, { registerNodeMaterial } from './NodeMaterial.js';
import NodeMaterial 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 All @@ -16,6 +16,12 @@ const _defaultValues = /*@__PURE__*/ new PointsMaterial();

class InstancedPointsNodeMaterial extends NodeMaterial {

static get type() {

return 'InstancedPointsNodeMaterial';

}

constructor( params = {} ) {

super();
Expand Down Expand Up @@ -167,5 +173,3 @@ class InstancedPointsNodeMaterial extends NodeMaterial {
}

export default InstancedPointsNodeMaterial;

InstancedPointsNodeMaterial.type = /*@__PURE__*/ registerNodeMaterial( 'InstancedPoints', InstancedPointsNodeMaterial );
10 changes: 7 additions & 3 deletions src/materials/nodes/Line2NodeMaterial.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import NodeMaterial, { registerNodeMaterial } from './NodeMaterial.js';
import NodeMaterial 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 All @@ -17,6 +17,12 @@ const _defaultValues = /*@__PURE__*/ new LineDashedMaterial();

class Line2NodeMaterial extends NodeMaterial {

static get type() {

return 'Line2NodeMaterial';

}

constructor( params = {} ) {

super();
Expand Down Expand Up @@ -433,5 +439,3 @@ class Line2NodeMaterial extends NodeMaterial {
}

export default Line2NodeMaterial;

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

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

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

class LineBasicNodeMaterial extends NodeMaterial {

static get type() {

return 'LineBasicNodeMaterial';

}

constructor( parameters ) {

super();
Expand All @@ -23,5 +29,3 @@ class LineBasicNodeMaterial extends NodeMaterial {
}

export default LineBasicNodeMaterial;

LineBasicNodeMaterial.type = /*@__PURE__*/ registerNodeMaterial( 'LineBasic', LineBasicNodeMaterial );
10 changes: 7 additions & 3 deletions src/materials/nodes/LineDashedNodeMaterial.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import NodeMaterial, { registerNodeMaterial } from './NodeMaterial.js';
import NodeMaterial 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 All @@ -10,6 +10,12 @@ const _defaultValues = /*@__PURE__*/ new LineDashedMaterial();

class LineDashedNodeMaterial extends NodeMaterial {

static get type() {

return 'LineDashedNodeMaterial';

}

constructor( parameters ) {

super();
Expand Down Expand Up @@ -49,5 +55,3 @@ class LineDashedNodeMaterial extends NodeMaterial {
}

export default LineDashedNodeMaterial;

LineDashedNodeMaterial.type = /*@__PURE__*/ registerNodeMaterial( 'LineDashed', LineDashedNodeMaterial );
10 changes: 7 additions & 3 deletions src/materials/nodes/MeshBasicNodeMaterial.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import NodeMaterial, { registerNodeMaterial } from './NodeMaterial.js';
import NodeMaterial 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 All @@ -12,6 +12,12 @@ const _defaultValues = /*@__PURE__*/ new MeshBasicMaterial();

class MeshBasicNodeMaterial extends NodeMaterial {

static get type() {

return 'MeshBasicNodeMaterial';

}

constructor( parameters ) {

super();
Expand Down Expand Up @@ -69,5 +75,3 @@ class MeshBasicNodeMaterial extends NodeMaterial {
}

export default MeshBasicNodeMaterial;

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

Expand All @@ -8,6 +8,12 @@ const _defaultValues = /*@__PURE__*/ new MeshLambertMaterial();

class MeshLambertNodeMaterial extends NodeMaterial {

static get type() {

return 'MeshLambertNodeMaterial';

}

constructor( parameters ) {

super();
Expand Down Expand Up @@ -39,5 +45,3 @@ class MeshLambertNodeMaterial extends NodeMaterial {
}

export default MeshLambertNodeMaterial;

MeshLambertNodeMaterial.type = /*@__PURE__*/ registerNodeMaterial( 'MeshLambert', MeshLambertNodeMaterial );
10 changes: 7 additions & 3 deletions src/materials/nodes/MeshMatcapNodeMaterial.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import NodeMaterial, { registerNodeMaterial } from './NodeMaterial.js';
import NodeMaterial 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 All @@ -11,6 +11,12 @@ const _defaultValues = /*@__PURE__*/ new MeshMatcapMaterial();

class MeshMatcapNodeMaterial extends NodeMaterial {

static get type() {

return 'MeshMatcapNodeMaterial';

}

constructor( parameters ) {

super();
Expand Down Expand Up @@ -49,5 +55,3 @@ class MeshMatcapNodeMaterial extends NodeMaterial {


export default MeshMatcapNodeMaterial;

MeshMatcapNodeMaterial.type = /*@__PURE__*/ registerNodeMaterial( 'MeshMatcap', MeshMatcapNodeMaterial );
Loading