最新代码
This commit is contained in:
108
public/sdk/three/jsm/nodes/loaders/NodeLoader.js
Normal file
108
public/sdk/three/jsm/nodes/loaders/NodeLoader.js
Normal file
@ -0,0 +1,108 @@
|
||||
import { createNodeFromType } from '../core/Node.js';
|
||||
import { nodeObject } from '../shadernode/ShaderNode.js';
|
||||
import { FileLoader, Loader } from 'three';
|
||||
|
||||
class NodeLoader extends Loader {
|
||||
|
||||
constructor( manager ) {
|
||||
|
||||
super( manager );
|
||||
|
||||
this.textures = {};
|
||||
|
||||
}
|
||||
|
||||
load( url, onLoad, onProgress, onError ) {
|
||||
|
||||
const loader = new FileLoader( this.manager );
|
||||
loader.setPath( this.path );
|
||||
loader.setRequestHeader( this.requestHeader );
|
||||
loader.setWithCredentials( this.withCredentials );
|
||||
loader.load( url, ( text ) => {
|
||||
|
||||
try {
|
||||
|
||||
onLoad( this.parse( JSON.parse( text ) ) );
|
||||
|
||||
} catch ( e ) {
|
||||
|
||||
if ( onError ) {
|
||||
|
||||
onError( e );
|
||||
|
||||
} else {
|
||||
|
||||
console.error( e );
|
||||
|
||||
}
|
||||
|
||||
this.manager.itemError( url );
|
||||
|
||||
}
|
||||
|
||||
}, onProgress, onError );
|
||||
|
||||
}
|
||||
|
||||
parseNodes( json ) {
|
||||
|
||||
const nodes = {};
|
||||
|
||||
if ( json !== undefined ) {
|
||||
|
||||
for ( const nodeJSON of json ) {
|
||||
|
||||
const { uuid, type } = nodeJSON;
|
||||
|
||||
nodes[ uuid ] = nodeObject( createNodeFromType( type ) ); // @TODO: Maybe nodeObjectify the node in createNodeFromType?
|
||||
nodes[ uuid ].uuid = uuid;
|
||||
|
||||
}
|
||||
|
||||
const meta = { nodes, textures: this.textures };
|
||||
|
||||
for ( const nodeJSON of json ) {
|
||||
|
||||
nodeJSON.meta = meta;
|
||||
|
||||
const node = nodes[ nodeJSON.uuid ];
|
||||
node.deserialize( nodeJSON );
|
||||
|
||||
delete nodeJSON.meta;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return nodes;
|
||||
|
||||
}
|
||||
|
||||
parse( json ) {
|
||||
|
||||
const node = nodeObject( createNodeFromType( json.type ) );
|
||||
node.uuid = json.uuid;
|
||||
|
||||
const nodes = this.parseNodes( json.nodes );
|
||||
const meta = { nodes, textures: this.textures };
|
||||
|
||||
json.meta = meta;
|
||||
|
||||
node.deserialize( json );
|
||||
|
||||
delete json.meta;
|
||||
|
||||
return node;
|
||||
|
||||
}
|
||||
|
||||
setTextures( value ) {
|
||||
|
||||
this.textures = value;
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default NodeLoader;
|
59
public/sdk/three/jsm/nodes/loaders/NodeMaterialLoader.js
Normal file
59
public/sdk/three/jsm/nodes/loaders/NodeMaterialLoader.js
Normal file
@ -0,0 +1,59 @@
|
||||
import { MaterialLoader } from 'three';
|
||||
import { createNodeMaterialFromType } from '../materials/Materials.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 {
|
||||
|
||||
constructor( manager ) {
|
||||
|
||||
super( manager );
|
||||
|
||||
this.nodes = {};
|
||||
|
||||
}
|
||||
|
||||
parse( json ) {
|
||||
|
||||
const material = super.parse( json );
|
||||
|
||||
const nodes = this.nodes;
|
||||
const inputNodes = json.inputNodes;
|
||||
|
||||
for ( const property in inputNodes ) {
|
||||
|
||||
const uuid = inputNodes[ property ];
|
||||
|
||||
material[ property ] = nodes[ uuid ];
|
||||
|
||||
}
|
||||
|
||||
return material;
|
||||
|
||||
}
|
||||
|
||||
setNodes( value ) {
|
||||
|
||||
this.nodes = value;
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default NodeMaterialLoader;
|
70
public/sdk/three/jsm/nodes/loaders/NodeObjectLoader.js
Normal file
70
public/sdk/three/jsm/nodes/loaders/NodeObjectLoader.js
Normal file
@ -0,0 +1,70 @@
|
||||
import NodeLoader from './NodeLoader.js';
|
||||
import NodeMaterialLoader from './NodeMaterialLoader.js';
|
||||
import { ObjectLoader } from 'three';
|
||||
|
||||
class NodeObjectLoader extends ObjectLoader {
|
||||
|
||||
constructor( manager ) {
|
||||
|
||||
super( manager );
|
||||
|
||||
this._nodesJSON = null;
|
||||
|
||||
}
|
||||
|
||||
parse( json, onLoad ) {
|
||||
|
||||
this._nodesJSON = json.nodes;
|
||||
|
||||
const data = super.parse( json, onLoad );
|
||||
|
||||
this._nodesJSON = null; // dispose
|
||||
|
||||
return data;
|
||||
|
||||
}
|
||||
|
||||
parseNodes( json, textures ) {
|
||||
|
||||
if ( json !== undefined ) {
|
||||
|
||||
const loader = new NodeLoader();
|
||||
loader.setTextures( textures );
|
||||
|
||||
return loader.parseNodes( json );
|
||||
|
||||
}
|
||||
|
||||
return {};
|
||||
|
||||
}
|
||||
|
||||
parseMaterials( json, textures ) {
|
||||
|
||||
const materials = {};
|
||||
|
||||
if ( json !== undefined ) {
|
||||
|
||||
const nodes = this.parseNodes( this._nodesJSON, textures );
|
||||
|
||||
const loader = new NodeMaterialLoader();
|
||||
loader.setTextures( textures );
|
||||
loader.setNodes( nodes );
|
||||
|
||||
for ( let i = 0, l = json.length; i < l; i ++ ) {
|
||||
|
||||
const data = json[ i ];
|
||||
|
||||
materials[ data.uuid ] = loader.parse( data );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return materials;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default NodeObjectLoader;
|
Reference in New Issue
Block a user