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

Node: Document more modules. #30142

Merged
merged 2 commits into from
Dec 17, 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
3 changes: 1 addition & 2 deletions examples/webgpu_materials.html
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@

// Scriptable

ScriptableNodeResources.set( 'THREE', THREE );
sunag marked this conversation as resolved.
Show resolved Hide resolved
ScriptableNodeResources.set( 'TSL', TSL );

const asyncNode = scriptable( js( `
Expand Down Expand Up @@ -420,7 +419,7 @@
function testSerialization( mesh ) {

const json = mesh.toJSON();
const loader = new THREE.NodeObjectLoader().setNodes( moduleToLib( TSL ) ).setNodeMaterials( moduleToLib( THREE ) );
const loader = new THREE.NodeObjectLoader().setNodes( moduleToLib( THREE ) ).setNodeMaterials( moduleToLib( THREE ) );
sunag marked this conversation as resolved.
Show resolved Hide resolved
const serializedMesh = loader.parse( json );

serializedMesh.position.x = ( objects.length % 4 ) * 200 - 400;
Expand Down
59 changes: 59 additions & 0 deletions src/loaders/nodes/NodeLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,46 @@ import { nodeObject, float } from '../../nodes/tsl/TSLBase.js';
import { Loader } from '../Loader.js';
import { FileLoader } from '../../loaders/FileLoader.js';

/**
* A loader for loading node objects in the three.js JSON Object/Scene format.
*
* @augments Loader
*/
class NodeLoader extends Loader {

/**
* Constructs a new node loader.
*
* @param {LoadingManager?} manager - A reference to a loading manager.
*/
constructor( manager ) {

super( manager );

/**
* Represents a dictionary of textures.
*
* @type {Object<String,Texture>}
*/
this.textures = {};

/**
* Represents a dictionary of node types.
*
* @type {Object<String,Node.constructor>}
*/
this.nodes = {};

}

/**
* Loads the node definitions from the given URL.
*
* @param {String} url - The path/URL of the file to be loaded.
* @param {Function} onLoad - Will be called when load completes.
* @param {Function} onProgress - Will be called while load progresses.
* @param {Function} onError - Will be called when errors are thrown during the loading process.
*/
load( url, onLoad, onProgress, onError ) {

const loader = new FileLoader( this.manager );
Expand Down Expand Up @@ -46,6 +75,12 @@ class NodeLoader extends Loader {

}

/**
* Parse the node dependencies for the loaded node.
*
* @param {Object} json - The JSON definition
* @return {Object<String,Node>} A dictionary with node dependencies.
*/
parseNodes( json ) {

const nodes = {};
Expand Down Expand Up @@ -80,6 +115,12 @@ class NodeLoader extends Loader {

}

/**
* Parses the node from the given JSON.
*
* @param {Object} json - The JSON definition
* @return {Node} The parsed node.
*/
parse( json ) {

const node = this.createNodeFromType( json.type );
Expand All @@ -98,20 +139,38 @@ class NodeLoader extends Loader {

}

/**
* Defines the dictionary of textures.
*
* @param {Object<String,Texture>} value - The texture library defines as `<uuid,texture>`.
* @return {NodeLoader} A reference to this loader.
*/
setTextures( value ) {

this.textures = value;
return this;

}

/**
* Defines the dictionary of node types.
*
* @param {Object<String,Node.constructor>} value - The node library defined as `<classname,class>`.
* @return {NodeLoader} A reference to this loader.
*/
setNodes( value ) {

this.nodes = value;
return this;

}

/**
* Creates a node object from the given type.
*
* @param {String} type - The node type.
* @return {Node} The created node instance.
*/
createNodeFromType( type ) {

if ( this.nodes[ type ] === undefined ) {
Expand Down
45 changes: 45 additions & 0 deletions src/loaders/nodes/NodeMaterialLoader.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,43 @@
import { MaterialLoader } from '../../loaders/MaterialLoader.js';

/**
* A special type of material loader for loading node materials.
*
* @augments MaterialLoader
*/
class NodeMaterialLoader extends MaterialLoader {

/**
* Constructs a new node material loader.
*
* @param {LoadingManager?} manager - A reference to a loading manager.
*/
constructor( manager ) {

super( manager );

/**
* Represents a dictionary of node types.
*
* @type {Object<String,Node.constructor>}
*/
this.nodes = {};

/**
* Represents a dictionary of node material types.
*
* @type {Object<String,NodeMaterial.constructor>}
*/
this.nodeMaterials = {};

}

/**
* Parses the node material from the given JSON.
*
* @param {Object} json - The JSON definition
* @return {NodeMaterial}. The parsed material.
*/
parse( json ) {

const material = super.parse( json );
Expand All @@ -30,20 +57,38 @@ class NodeMaterialLoader extends MaterialLoader {

}

/**
* Defines the dictionary of node types.
*
* @param {Object<String,Node.constructor>} value - The node library defined as `<classname,class>`.
* @return {NodeLoader} A reference to this loader.
*/
setNodes( value ) {

this.nodes = value;
return this;

}

/**
* Defines the dictionary of node material types.
*
* @param {Object<String,NodeMaterial.constructor>} value - The node material library defined as `<classname,class>`.
* @return {NodeLoader} A reference to this loader.
*/
setNodeMaterials( value ) {

this.nodeMaterials = value;
return this;

}

/**
* Creates a node material from the given type.
*
* @param {String} type - The node material type.
* @return {Node} The created node material instance.
*/
createMaterialFromType( type ) {

const materialClass = this.nodeMaterials[ type ];
Expand Down
61 changes: 61 additions & 0 deletions src/loaders/nodes/NodeObjectLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,80 @@ import NodeMaterialLoader from './NodeMaterialLoader.js';

import { ObjectLoader } from '../../loaders/ObjectLoader.js';

/**
* A special type of object loader for loading 3D objects using
* node materials.
*
* @augments ObjectLoader
*/
class NodeObjectLoader extends ObjectLoader {

/**
* Constructs a new node object loader.
*
* @param {LoadingManager?} manager - A reference to a loading manager.
*/
constructor( manager ) {

super( manager );

/**
* Represents a dictionary of node types.
*
* @type {Object<String,Node.constructor>}
*/
this.nodes = {};

/**
* Represents a dictionary of node material types.
*
* @type {Object<String,NodeMaterial.constructor>}
*/
this.nodeMaterials = {};

/**
* A reference for holdng the `nodes` JSON property.
*
* @private
* @type {Object?}
*/
this._nodesJSON = null;

}

/**
* Defines the dictionary of node types.
*
* @param {Object<String,Node.constructor>} value - The node library defined as `<classname,class>`.
* @return {NodeLoader} A reference to this loader.
*/
setNodes( value ) {

this.nodes = value;
return this;

}

/**
* Defines the dictionary of node material types.
*
* @param {Object<String,NodeMaterial.constructor>} value - The node material library defined as `<classname,class>`.
* @return {NodeLoader} A reference to this loader.
*/
setNodeMaterials( value ) {

this.nodeMaterials = value;
return this;

}

/**
* Parses the node objects from the given JSON.
*
* @param {Object} json - The JSON definition
* @param {Function} onLoad - The onLoad callback function.
* @return {Object3D}. The parsed 3D object.
*/
parse( json, onLoad ) {

this._nodesJSON = json.nodes;
Expand All @@ -42,6 +89,13 @@ class NodeObjectLoader extends ObjectLoader {

}

/**
* Parses the node objects from the given JSON and textures.
*
* @param {Object} json - The JSON definition
* @param {Object<String,Texture>} textures - The texture library.
* @return {Object<String,Node>}. The parsed nodes.
*/
parseNodes( json, textures ) {

if ( json !== undefined ) {
Expand All @@ -58,6 +112,13 @@ class NodeObjectLoader extends ObjectLoader {

}

/**
* Parses the node objects from the given JSON and textures.
*
* @param {Object} json - The JSON definition
* @param {Object<String,Texture>} textures - The texture library.
* @return {Object<String,NodeMaterial>}. The parsed materials.
*/
parseMaterials( json, textures ) {

const materials = {};
Expand Down
Loading
Loading