Skip to content
cjcliffe edited this page Oct 24, 2011 · 1 revision

CubicVR.get function / functionality

CubicVR.get() is primarly used in a transparent fashion to provide managed loading of JSON and XML objects, recursive loading of constructor parameters, JSON data referenced via script tags and more.

Many (and soon all) of the CubicVR objects support using CubicVR.get() directly via their constructor.

For example instead of passing an object constructor to Mesh you can call one of:

  • new CubicVR.Mesh("myMesh.xml")
  • new CubicVR.Mesh("#myMeshId")
  • new CubicVR.Mesh("myMesh.json")
  • new CubicVR.Mesh("http://www.someurl.com/myMeshService?file=1234&ext=json");

URL extension over-ride is also supported via ?_ext= or ?ext= suffix.

CubicVR.get( subject, classtype )

Typically you won't use it directly, but it's good to know what it's doing under the hood. Note that repeat calls to URLs are cached and their resulting object will be referenced instead of duplicating on load. To clear the loading cache you can call CubicVR.clearCache();

Parameters:

  • subject : An absolute or relative URL to a JSON or XML file, a script element or an ID for a script element format: "#myId".
  • classtype (optional) : If provided CubicVR.get will attempt to construct an object using the given class. i.e. CubicVR.Mesh (no quotes).

Typical usage:

// assumes myMesh.xml is in XML constructor format.
var myMesh = CubicVR.get("myMesh.xml",CubicVR.Mesh);

// assumes remote service provides JSON constructor format.
var myMesh = CubicVR.get("http://www.someurl.com/myMeshService?file=1234&ext=json",CubicVR.Mesh);

// assumes script tag with id 'myId' with a src parameter pointing to the js file.
var myMesh = CubicVR.get("#myId",CubicVR.Mesh);

// objLight will be constructed directly from passed constructor.
var objLight = {type:"directional",direction:[-0.5,-0.5,-0.5]};
var myLight = CubicVR.get(objLight,CubicVR.Light);

// otherLight will pass-through by reference since it is of required type.
var otherLight = new CubicVR.Light({type:"directional",direction:[-0.5,-0.5,-0.5]});
var myLight = CubicVR.get(otherLight,CubicVR.Light); // referenced

// coming soon.. support for direct JSON and XML strings..

Supported Classes:

Growing list of supported classes, may not be up to date as more are added each day.

What does this mean? It means any of these classes can use the same parameters as the subject in CubicVR.get() and any parameters that require one of these classes will accept a subject as well. They can also be used in a recursive fashion such as an xml file referencing another xml file in itself, or even a json referencing an xml which references a json file, ... -- they will all be loaded, resolved and cached when possible.

The most up-to-date examples can be found within samples/definitions/ within the repository.

Returns:

The constructed object if classtype provided, otherwise JSON object representing the loaded data.

XML/JSON format information

The JSON format is identical to the original object that would have been passed to the constructor of a class with quotes added for identifiers. XML files follow the exact same form with a few exceptions and differences in the way Arrays are defined.

scene.json: Example of a Scene in CubicVR.get() JSON format

{
    "camera": { 
        "position": [0, 1, 1],
        "target": [0, 0, 0],
        "fov": 60
    },
    "sceneObjects": [{
        "mesh": {
            "primitive": "plane1.json",
            "compile": true
        },
        "position": [-1,0,0]
    },
    {   
        "mesh": { 
            "primitive": "plane1.json",
            "compile": true
        },
        "position": [1,0,0]
    }],
    "light": "light1.json"
}

scene.xml: Example of that same scene in XML format

<scene>
    <camera>
        <position>0, 1, 1</position>
        <target>0, 0, 0</target>
        <fov>60</fov>
    </camera>
    <sceneObject>
        <mesh>
            <primitive>plane1.xml</primitive>
            <compile>true</compile>
        </mesh>
        <position>-1,0,0</position>
    </sceneObject>
    <sceneObject>
        <mesh> 
            <primitive>plane1.xml</primitive>
            <compile>true</compile>
        </mesh>
        <position>1,0,0</position>
    </sceneObject>
    <light>light1.xml</light>
</scene>

Note the repeating sceneObject tags to represent an array where necessary. Most classes will accomodate this functionality by providing both the plural and non-plural version of each. Any of the plural or non parameters will accept both a single or array of objects since they are the same operation.

For more information please refer to the examples in samples/definitions/ in the repository as they will provide additional files for research and review.

Clone this wiki locally