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

NodeMaterial: serialize/deserialize and Material.fromType() #23314

Merged
merged 12 commits into from
Feb 6, 2022

Conversation

sunag
Copy link
Collaborator

@sunag sunag commented Jan 24, 2022

Related issue: #22552

Introduction

This PR add support to serialize NodeMaterial in the Scene Graph of Three.js.

Create NodeMaterial base class extended of ShaderNode.

Node.serialize() / Node.deserialize()

This can be used to serialize / deserialize specific Node fields, these methods can be extended.

Node.fromType()

It can be used to create extended Nodes and share the same NodeMaterialLoader.

Loaders

NodeLoader - only Nodes
NodeMaterialLoader - Nodes in Materials
NodeObjectLoader - Nodes in Scene Graph

Serialization test

  • webgl_materials_standard_nodes - serialize objects after load to test

Core

Object3D

  • src/core/Object3D.js - add nodes to toJSON()

Material.fromType()

  • src/loaders/MaterialLoader.js
  • src/materials/Material.js
  • src/materials/Materials.js
  • Commit - 4cdbfac

This contribution is funded by Google via Igalia.

@sunag sunag marked this pull request as draft January 24, 2022 09:13
@sunag sunag marked this pull request as ready for review January 24, 2022 09:32
@mrdoob
Copy link
Owner

mrdoob commented Jan 25, 2022

Hmm, one problem with this approach is that it makes NodeMaterial non-treeshakeable 😕

I was hoping that NodeMaterial was going to bring us closer to smaller builds, but I suspect this would actually produce bigger builds?

@mrdoob
Copy link
Owner

mrdoob commented Jan 25, 2022

One of the key things about three.js is the fact that it's ~150kb gzipped.

Could you do some tests to see how the new NodeMaterial system impacts on this? For example, what would be the bundle size of a simple example that displays a red cube with the WebGPURenderer+NodeMaterial?

@sunag
Copy link
Collaborator Author

sunag commented Jan 25, 2022

Hmm, one problem with this approach is that it makes NodeMaterial non-treeshakeable 😕

I think you are referring to Node.fromType()?
In this case, we can just move NodeLib of Loader to a variable, the same that already happened with the textures in MaterialLoader.setTextures().

Because some others approaches like Node.serialize() I was doing it tree shakeable.

@mrdoob
Copy link
Owner

mrdoob commented Jan 25, 2022

I think you are referring to Node.fromType()?

Yes 👍

@sunag
Copy link
Collaborator Author

sunag commented Jan 25, 2022

I build here a .min files to reference in gzipped:

145 KB - Three.js

import * as THREE from './three.min.js';

152 KB - Three.js + WebGLNodeBuilder + ColorNode

import * as THREE from './three.min.js';
import ColorNode from './jsm/renderers/nodes/inputs/ColorNode.js';
import { nodeFrame } from './jsm/renderers/webgl/nodes/WebGLNodes.js';

155 KB - Three.js + WebGLNodeBuilder + All Nodes

import * as THREE from './three.min.js';
import * as Nodes from './jsm/renderers/nodes/Nodes.js';
import { nodeFrame } from './jsm/renderers/webgl/nodes/WebGLNodes.js';

--
All Nodes + WebGLNodeBuilder gzipped was a filesize of 10 KB. If used only ColorNode this goes down to 7 KB.

@mrdoob
Copy link
Owner

mrdoob commented Jan 25, 2022

Nice!

Just remembered that we have a script that helps visualizing tree-shaking: #21657

@mrdoob mrdoob added this to the r138 milestone Feb 6, 2022
@mrdoob mrdoob merged commit 9898592 into mrdoob:dev Feb 6, 2022
@mrdoob
Copy link
Owner

mrdoob commented Feb 6, 2022

Thanks!

@sunag
Copy link
Collaborator Author

sunag commented Feb 6, 2022

I was thinking of an approach to register classes imported. Like:

class NoiseNode extends Node {
...
}

Node.library.add( NoiseNode );

So we can register only the imported classes and preserve this design using .fromType().

@0b5vr 0b5vr mentioned this pull request Mar 1, 2022
16 tasks
donmccurdy pushed a commit to donmccurdy/three.js that referenced this pull request Mar 10, 2022
…3314)

* Node serializer (draft)

* add Material.fromType()

* NodeObjectLoader: serialize/deserialize

* update default values

* fix analytic light

* force refresh material uniforms using .uniformsNeedUpdate

* Revert "force refresh material uniforms using .uniformsNeedUpdate"

This reverts commit b083fb4.

* mrdoob code style

* cleanup

* cleanup (2)

* Update NodeMaterial.js

Co-authored-by: mrdoob <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants