last day (14 days later) » 

12:13 AM
0
Q: Three.js BufferGeometry.toJSON - Why are the Attribute data not exported?

steveOwI am trying to export a buffer geometry to a JSON file. Here is the code I am using: var output = object.geometry.toJSON(); try { output = JSON.stringify( output, null, '\t' ); output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' ); } catch ( e ) { output = JSON.stringify( ...

 
BufferGeometry is not tracked by javascript. For true export of geometry you need to convert bufferGeometry to geometry. new THREE.Geometry().fromBufferGeometry(yourBufferGeometry)....toJSON()
 
@Radio. Thanks. I see that might be a possible route to follow. (But if you are right then it is rather strange that the THREE.js code in my question describes specifically a conversion from BufferGeometry to JSON.)
 
Not really. The function toJSON can be used for other things. For example I use it for state of an application for saving a scene. The uuids map to OBJ files sitting on a cloud server. The function toJSON is specifically a literal object state stringification, not a 3d file format conversion. For examples of export of 3d from three.js see github.com/mrdoob/three.js/tree/dev/examples/js/exporters
 
@Radio. Maybe you are referring to a toJSON function in javascript? Whereas I am referring to the BufferGeometry.toJSON function in THREE.js (whose opening code is listed in my question).
 
Nope. I am not sure where you got the idea that toJSON has any expectation of containing geometry. It converts the literal JavaScript object to convenient JSON that won't have application level junk in it like drawRange and listeners, which you'd get if you used JSON.stringify directly. The JavaScript object type you're using, bufferGeometry, does not hold geometry, and so you don't get any.
 
12:13 AM
@Radio Please note that I am using THREE.BufferGeometry.toJSON() not the javascript object/function you keep referring to.
 
1:01 AM
@Radio. I have tried your suggestion of converting to standard THREE.Geometry and using geometry.toJSON and that seems to work OK as a work-around. I am still ignorant as to why THREE.BufferGeometry.toJSON() doesn't work.
 
 
12 hours later…
12:42 PM
See update U3 at the end of the Question. As a workaround I can get BufferGeometry.toJSON() to work by deleting the geometry.parameters property. But I dont know if this will cause any consequential problems further down the line in the Sculpter program.
 
 
3 hours later…
4:04 PM
Oh, I am referring to this function in this particular conversation: github.com/mrdoob/three.js/blob/dev/src/core/… You can see there is no provision for exporting geometry in it. It is not a bug. BufferGeometry does not retain geometry information and so it's toJSON method at line 880 in the source linked, does not provide any. A similar function is also available for other three.js classes like lights, materials, cameras, etc.
"particular code" not "particular conversation" sorry. :)
 
4:28 PM
I guess I am looking at the same code which I was seeing copied in the THREE.js build. But BufferGeometry DOES retain geometry information, it is in 1-D arrays (vertice coordinates, normals, UVs, colors) under the ".attributes" property. So it is strange to me that (a) BufferGeometry.toJSON will NOT write geometry data to the output text object when the BufferGeometry.parameters object is PRESENT ... yet (b) it WILL write that geometry data when .parameters is ABSENT.
I guess I am looking at the same code which I was seeing copied in the THREE.js build. But BufferGeometry DOES retain geometry information, it is in 1-D arrays (vertice coordinates, normals, UVs, colors) under the ".attributes" property. So it is strange to me that BufferGeometry.toJSON (a) will NOT write geometry data to the output text object when the geometry.parameters object is PRESENT ... yet (b) it WILL write that same data when .parameters is ABSENT.
 
You could.... rewrite the function to perform as you need it to, and submit it as a pull request, which will trigger a conversation on the function's purpose and nature. I did this with the obj exporter/importer inconsistencies a bit ago, and the community around the github source were very receptive to making changes.
 
4:42 PM
Hmm. I would not trust me to amend THREE.js code - my javascript is poor. Maybe I will just raise it as a possible bug or illogical feature.
 
Oh! that's why you submit it as a pull. It doesn't mean it becomes part of the core until it's accepted by the project maintainers. The pull simply initiates a review. Sometimes its rejected outright, sometimes it triggers a discussion, and in rare cases accepted right away. The project maintainers are generally a group of people authorized by the original code author to maintain the source. remember that three.js is still in alpha and community participation progresses it closer to version one.
 
4:58 PM
Yeah but I don't know enough about the wider use context of BufferGeometry to attempt a sensible alternative coding.
 
5:15 PM
You can also write your own toJSON function that loads after the library is loaded, so you're not messing with the core. I have a three.utils class I wrote that i use in my projects that has methods that accept geometry and buffergeometry as a parameter.
 
 
1 hour later…
6:33 PM
Yes, writing my own utility is the ultimate backstop. For now I have raised an issue on github.
 

last day (14 days later) »