[X3D-Public] [x3dom-developers] initial X3D JSON conversion support using X3dToJson.xslt

Cecile Muller newsletter at wildpeaks.fr
Fri Oct 10 10:53:41 PDT 2014


Hi everyone,


> JSON is a built-in JavaScript object. This works out of the box: the
parse method simply creates a valid JS object from a JSON string.
> The resulting object tree however still has to be traversed to attach
meaning and behavior.

Exactly: the way the engine chooses to render the scenegraph is up to it,
the JSON data is only meant to initialize the scenegraph in the 3D engine
(and optionally export its state at a given moment if needed).


Which is actually why it would feel redundant to have prototypes inside
JSON if you use it in for Javascript because in real life I'd use real JS
modules if I need a custom object type with properties and methods
(CommonJS, AMD, ES6 modules, typescript modules, factories or whatever the
author prefers) because:
 - it's the same scripting language as the rest of the application
 - it can be optimized with the rest of the application
 - it can be tested with the rest of the application
 - it can refer to and interact with non-3D modules (e.g. a thirdparty
library for formatting Date timestamps)
 - no eval()


My goals for a scenegraph format are:
 - lightweight to parse
 - meaningful and low complexity because it's simpler to think with, easier
to read, easier to diff between two states, and easier to sync between
devices
 - easy to require(), to use the same package management as the rest of the
application code and assets

However I'm fine not having feature parity with XML if it keeps it
lightweight and straightforward, so this is more a VRML/X3D-inspired
format, not "real X3D".


Example (the "_type" is omitted in some nodes because I also have a concept
of "Default type" for some fields, e.g. how most of the time the value in
"Shape.appearance.material" is a Material node):

[
{
"_type": "Shape",
"appearance": {
"material": {
"transparency": 0.5,
"diffuseColor": [0.8, 1, 0.8]
}
},
"geometry": {
"_type": 'Box',
"size": [2, 1, 2]
}
},
{
"_type": "MyCustomObjectType",
"translation": [0, 0, 1],
"scale": [2, 2, 2]
}
]

And an example (without messaging or engine-specific code) of one of many
ways to create something similar to Protos:

var MyCustomObjectType = function(data){
'use strict';
var _data = data || {};
var _json = {
_type: 'Transform',
translation: _data.translation || [0, 0, 0],
scale: _data.scale || [1, 1, 1],
children: [
{
_type: 'Shape',
appearance: {
material: {
transparency: 0.5,
diffuseColor: [0.8, 1, 0.8]
}
},
geometry: {
_type: 'Sphere',
radius: 5
}
}
]
};
Object.defineProperty(
this,
'json',
{
configurable: false,
enumerable: true,
writable: false,
value: function(){
return _json;
}
}
);
};

MyCustomObjectType.prototype.modifySomething = function(){
'use strict';
//
//
};

module.exports = MyCustomObjectType;


See you,
Cecile
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://web3d.org/pipermail/x3d-public_web3d.org/attachments/20141010/ca3902d1/attachment-0001.html>


More information about the X3D-Public mailing list