zadvorsky / three.bas

THREE.JS Buffer Animation System

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

toJSON() not working with AnimationMaterials

martenzander opened this issue · comments

As far as I can tell using builtin toJSON() is not working.

I have a mesh using a BAS.StandardAnimationMaterial() and get the following error if I try to mesh.toJSON():

Uncaught TypeError: Cannot read property 'isTexture' of null

Hello.

I just pushed a fix for the actual error, but I'm not sure if you'll be able to re-create your object from JSON. StandardAnimationMaterial (and the other animation materials) all have some extended logic which is not stored in the JSON.

Also, JSON loader has been deprecated. How are you loading / parsing your JSON files?

actually I just want to parse the toJSON() Object as a string to compare two instances and debug another error I am currently dealing with. Maybe the error might be related to this.

I am trying to clone the mesh to place it multiple times in my scene at different positions. But if I clone the mesh using the StandardAnimationMaterial(), somehow the shadows break. I also tried to add a new customDepthMaterial to the cloned Mesh, but without success.
Further debugging lead me to the point where I wanted to clone only a BasicAnimationMaterial and here it says:

Uncaught TypeError: Cannot read property 'uniformValues' of undefined

I'm not used to the cloning workflow, so I've never tested it, and I'm not sure what kind of things might break because I don't implement the clone/copy methods on any of the BAS geometries and materials (they just use the method from the THREE.js class they're extending). I can look into adding clone/copy methods, but that may take some time.

If you want to create several meshes, you might be better off either creating a mesh subclass with the geometry and material (like Animation here, and creating instances of that, or working with a factory function.

I can look into adding clone/copy methods, but that may take some time.

Don't worry, thanks for all your work on this anyway, very helpful to inject built-in THREE Shaders :).

If you want to create several meshes, you might be better off either creating a mesh subclass with the geometry and material (like Animation here, and creating instances of that, or working with a factory function.

The Subclass thing might be an option and actually is kind of close to the way I work around the problem at the moment. I simply create a new THREE.Mesh() Instance each time I used to clone instead. What do you mean by factory function?

A factory function is just a (pure) function that takes arguments, and returns an object / class instance. Depending on your use case, it can be cleaner than extending Mesh.

Something like:

function createMesh (options) {
 const material = new StandardAnimationMaterial({
  // defaults + material specific options
 }

 const geometry = new PrefabBufferGeometry(
  // prefab, prefab count
 )
 
 const mesh = new Mesh(geometry, material)
 
 return mesh
}

https://en.wikipedia.org/wiki/Factory_method_pattern

This may be what you are already doing though :).