Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

merged with mod-conversion

  • Loading branch information...
commit 7b0d44f2822e616dc98696972672b903830d5660 2 parents 34d9213 + 6b13dde
@cjcliffe authored
Showing with 13,178 additions and 12,978 deletions.
  1. +114 −12,967 CubicVR.js
  2. +10 −0 HEADER
  3. +50 −9 Makefile
  4. +12 −1 README.markdown
  5. +1 −1  samples/basic/basic_primitives.html
  6. +71 −0 samples/collada/wall_smash_cam_attach.html
  7. +1,662 −0 source/CubicVR.COLLADA.js
  8. +567 −0 source/CubicVR.CVRXML.js
  9. +374 −0 source/CubicVR.Camera.js
  10. +377 −0 source/CubicVR.GML.js
  11. +211 −0 source/CubicVR.Landscape.js
  12. +226 −0 source/CubicVR.Layout.js
  13. +441 −0 source/CubicVR.Light.js
  14. +504 −0 source/CubicVR.MainLoop.js
  15. +422 −0 source/CubicVR.Material.js
  16. +798 −0 source/CubicVR.Math.js
  17. +899 −0 source/CubicVR.Mesh.js
  18. +586 −0 source/CubicVR.Motion.js
  19. +952 −0 source/CubicVR.Octree.js
  20. +311 −0 source/CubicVR.Particles.js
  21. +598 −0 source/CubicVR.PostProcess.js
  22. +398 −0 source/CubicVR.Primitives.js
  23. +257 −0 source/CubicVR.Renderer.js
  24. +1,044 −0 source/CubicVR.Scene.js
  25. +375 −0 source/CubicVR.Shader.js
  26. +517 −0 source/CubicVR.Texture.js
  27. +447 −0 source/CubicVR.UVMapper.js
  28. +226 −0 source/CubicVR.Utility.js
  29. +670 −0 source/CubicVR.Worker.js
  30. 0  { → source}/collada.js
  31. +48 −0 tests/workers/resource-pool.html
  32. +10 −0 tools/cutter.py
View
13,081 CubicVR.js
114 additions, 12,967 deletions not shown
View
10 HEADER
@@ -0,0 +1,10 @@
+/*
+ Javascript port of CubicVR 3D engine for WebGL
+ by Charles J. Cliffe
+ http://www.cubicvr.org/
+
+ May be used under the terms of the MIT license.
+ http://www.opensource.org/licenses/mit-license.php
+*/
+
+/*globals alert: false */
View
59 Makefile
@@ -2,15 +2,50 @@
CUBICVR := CubicVR
SRC_DIR := .
DIST_DIR := $(SRC_DIR)/dist
-CUBICVR_SRC := $(SRC_DIR)/$(CUBICVR).js
CUBICVR_DIST := $(DIST_DIR)/$(CUBICVR).js
CUBICVR_MIN := $(DIST_DIR)/$(CUBICVR).min.js
+CUBICVR_CORE := $(SRC_DIR)/CubicVR.js
CUBICVR_VS := $(SRC_DIR)/$(CUBICVR)_Core.vs
CUBICVR_FS := $(SRC_DIR)/$(CUBICVR)_Core.fs
TOOLS_DIR := $(SRC_DIR)/tools
TESTS_DIR := $(DIST_DIR)/tests
+SOURCE_DIR := $(SRC_DIR)/source
-compile = java -jar $(TOOLS_DIR)/closure/compiler.jar --js $(CUBICVR_DIST) \
+JS_SRCS := \
+ $(SOURCE_DIR)/CubicVR.COLLADA.js \
+ $(SOURCE_DIR)/CubicVR.CVRXML.js \
+ $(SOURCE_DIR)/CubicVR.Camera.js \
+ $(SOURCE_DIR)/CubicVR.GML.js \
+ $(SOURCE_DIR)/CubicVR.Landscape.js \
+ $(SOURCE_DIR)/CubicVR.Layout.js \
+ $(SOURCE_DIR)/CubicVR.Light.js \
+ $(SOURCE_DIR)/CubicVR.MainLoop.js \
+ $(SOURCE_DIR)/CubicVR.Material.js \
+ $(SOURCE_DIR)/CubicVR.Math.js \
+ $(SOURCE_DIR)/CubicVR.Mesh.js \
+ $(SOURCE_DIR)/CubicVR.Motion.js \
+ $(SOURCE_DIR)/CubicVR.Octree.js \
+ $(SOURCE_DIR)/CubicVR.Particles.js \
+ $(SOURCE_DIR)/CubicVR.PostProcess.js \
+ $(SOURCE_DIR)/CubicVR.Primitives.js \
+ $(SOURCE_DIR)/CubicVR.Renderer.js \
+ $(SOURCE_DIR)/CubicVR.Scene.js \
+ $(SOURCE_DIR)/CubicVR.Shader.js \
+ $(SOURCE_DIR)/CubicVR.Texture.js \
+ $(SOURCE_DIR)/CubicVR.UVMapper.js \
+ $(SOURCE_DIR)/CubicVR.Utility.js \
+ $(SOURCE_DIR)/CubicVR.Worker.js
+
+cutcore = ( python $(TOOLS_DIR)/cutter.py $(1) )
+
+addheader = @@cat $(SRC_DIR)/HEADER > $(DIST_DIR)/header.tmp && \
+ cat $(1) >> $(DIST_DIR)/header.tmp && \
+ rm -f $(1) && \
+ mv $(DIST_DIR)/header.tmp $(1)
+
+compile = java -jar $(TOOLS_DIR)/closure/compiler.jar \
+ --js $(CUBICVR_CORE) \
+ $(shell for js in $(JS_SRCS) ; do echo --js $$js ; done) \
--compilation_level SIMPLE_OPTIMIZATIONS \
--js_output_file $(1)
@@ -22,19 +57,25 @@ stringify = ( echo '/* Auto Embed $(2) */' ; \
all: $(DIST_DIR) $(CUBICVR_DIST) $(CUBICVR_MIN)
@@echo "Finished, see $(DIST_DIR)"
-$(DIST_DIR):
- @@echo "Creating $(DIST_DIR)"
- @@mkdir $(DIST_DIR)
-
-$(CUBICVR_DIST): $(DIST_DIR) $(CUBICVR_SRC)
+$(CUBICVR_DIST): $(DIST_DIR) $(JS_SRCS)
@@echo "Building $(CUBICVR_DIST)"
- @@cp $(CUBICVR_SRC) $(CUBICVR_DIST)
+ @@$(call cutcore, $(CUBICVR_CORE)) > $(CUBICVR_DIST)
+ @@cat $(JS_SRCS) >> $(CUBICVR_DIST)
+ @@$(call cutfile,$(CUBICVR_DIST))
@@$(call stringify,CubicVRCoreVS,$(CUBICVR_VS)) >> $(CUBICVR_DIST)
@@$(call stringify,CubicVRCoreFS,$(CUBICVR_FS)) >> $(CUBICVR_DIST)
+ @@$(call addheader,$(CUBICVR_DIST))
+
+$(DIST_DIR):
+ @@echo "Creating $(DIST_DIR)"
+ @@mkdir $(DIST_DIR)
-$(CUBICVR_MIN): $(DIST_DIR) $(CUBICVR_SRC) $(CUBICVR_DIST)
+$(CUBICVR_MIN): $(DIST_DIR) $(CUBICVR_DIST)
@@echo "Building $(CUBICVR_MIN)"
@@$(call compile,$(CUBICVR_MIN))
+ @@$(call addheader,$(CUBICVR_MIN))
+ @@$(call stringify,CubicVRCoreVS,$(CUBICVR_VS)) >> $(CUBICVR_MIN)
+ @@$(call stringify,CubicVRCoreFS,$(CUBICVR_FS)) >> $(CUBICVR_MIN)
tests: $(DIST_DIR) $(CUBICVR_MIN)
@@echo "Creating tests in $(TESTS_DIR)"
View
13 README.markdown
@@ -20,7 +20,9 @@ Javascript Port of the [CubicVR 3D Engine](http://www.cubicvr.org) by Charles J.
### Folders ###
- / -> core library (to be included as submodule)
+ / -> core library and shaders
+ /source -> source modules
+ /editor -> web-based 3D editor (work in progress)
/samples -> sample projects, useful as a starting point
/tests -> tests and prototypes for various core features
/lib -> external dependencies for any tests/examples
@@ -31,3 +33,12 @@ Javascript Port of the [CubicVR 3D Engine](http://www.cubicvr.org) by Charles J.
### Branches ###
master -> main branch
+
+### Building / Minification ###
+
+ To build the consolidated and minified versions of CubicVR.js simply run "make" in the repository root.
+
+ The resulting CubicVR.js and CubicVR.min.js builds with self-contained core shaders will be placed in dist/
+
+ Minification requires Python and Java to be installed, please review installation instructions for your platform.
+
View
2  samples/basic/basic_primitives.html
@@ -130,4 +130,4 @@
<body onLoad="webGLStart();"></body>
-</html>
+</html>
View
71 samples/collada/wall_smash_cam_attach.html
@@ -0,0 +1,71 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
+<html>
+
+ <head>
+ <title>
+ CubicVR.js: Using a COLLADA scene from a .dae file
+ </title>
+ <script src="../../CubicVR.js" type="text/javascript">
+ </script>
+ <script type='text/javascript'>
+ function webGLStart() {
+ // by default generate a full screen canvas with automatic resize
+ var gl = CubicVR.init();
+ var canvas = CubicVR.getCanvas();
+
+ if (!gl) {
+ alert("Sorry, no WebGL support.");
+ return;
+ };
+
+ // load the collada file, specify path for images
+ var scene = CubicVR.loadCollada("WallSmash/wall_smash.dae","WallSmash/");
+ scene.resize(canvas.width,canvas.height);
+// scene.camera.position = [20,20,-20]; // set our camera position
+// scene.camera.target = [0,5,-5]; // set our camera position
+ scene.camera.position = [0,1,3.5];
+ scene.camera.rotation = [0,0,0];
+ scene.camera.targeted = false;
+ scene.camera.setParent(scene.getSceneObject("Sphere"));
+ scene.camera.motion = null; // remove any camera motion
+ scene.lights = []; // clear the loaded lights
+
+ // Add a simple shadowed directional light
+ scene.bindLight(new CubicVR.Light({
+ type:CubicVR.enums.light.type.AREA,
+ distance:90,
+ areaCeiling:90,
+ specular:[1,1,1],
+ areaAxis:[-25,-10]
+ }));
+
+ CubicVR.setSoftShadows(true);
+
+ // Add our scene to the window resize list
+ CubicVR.addResizeable(scene);
+
+ // Use a basic envelope to loop/stretch the time
+ var looper = new CubicVR.Envelope({
+ in_behavior:CubicVR.enums.envelope.behavior.REPEAT,
+ out_behavior:CubicVR.enums.envelope.behavior.REPEAT
+ });
+ looper.addKey(0,0);
+ looper.addKey(10,10);
+ looper.addKey(15,0);
+
+ // Start our main drawing loop, it provides a timer and the gl context as parameters
+ CubicVR.MainLoop(function(timer, gl) {
+ scene.evaluate(looper.evaluate(timer.getSeconds()));
+ scene.updateShadows();
+ scene.render();
+ });
+
+ // initialize a mouse view controller
+ mvc = new CubicVR.MouseViewController(canvas, scene.camera);
+ }
+ </script>
+ </head>
+
+ <body onLoad="webGLStart();"></body>
+
+</html>
View
1,662 source/CubicVR.COLLADA.js
@@ -0,0 +1,1662 @@
+
+CubicVR.RegisterModule("COLLADA",function(base) {
+
+ var undef = base.undef;
+ var nop = function(){ };
+ var enums = CubicVR.enums;
+ var GLCore = base.GLCore;
+
+ var collada_tools = {
+ fixuaxis: function (up_axis, v) {
+ if (up_axis === 0) { // untested
+ return [v[1], v[0], v[2]];
+ } else if (up_axis === 1) {
+ return v;
+ } else if (up_axis === 2) {
+ return [v[0], v[2], -v[1]];
+ }
+ },
+ fixscaleaxis: function (up_axis, v) {
+ if (up_axis === 0) { // untested
+ return [v[1], v[0], v[2]];
+ } else if (up_axis === 1) {
+ return v;
+ } else if (up_axis === 2) {
+ return [v[0], v[2], v[1]];
+ }
+ },
+ fixukaxis: function (up_axis, mot, chan, val) {
+ // if (mot === enums.motion.POS && chan === enums.motion.Y && up_axis === enums.motion.Z) return -val;
+ if (mot === enums.motion.POS && chan === enums.motion.Z && up_axis === enums.motion.Z) {
+ return -val;
+ }
+ return val;
+ },
+ getAllOf: function (root_node, leaf_name) {
+ var nStack = [root_node];
+ var results = [];
+
+ while (nStack.length) {
+ var n = nStack.pop();
+
+ for (var i in n) {
+ if (!n.hasOwnProperty(i)) continue;
+
+ if (i === leaf_name) {
+ if (n[i].length) {
+ for (var p = 0, pMax = n[i].length; p < pMax; p++) {
+ results.push(n[i][p]);
+ }
+ } else {
+ results.push(n[i]);
+ }
+ }
+ if (typeof(n[i]) == 'object') {
+ if (n[i].length) {
+ for (var p = 0, pMax = n[i].length; p < pMax; p++) {
+ nStack.push(n[i][p]);
+ }
+ } else {
+ nStack.push(n[i]);
+ }
+ }
+ }
+ }
+
+ return results;
+ },
+ quaternionFilterZYYZ: function (rot, ofs) {
+ var vec3 = CubicVR.vec3;
+ var r = rot;
+ var temp_q = new CubicVR.Quaternion();
+
+ if (ofs !== undef) {
+ r = vec3.add(rot, ofs);
+ }
+
+ temp_q.fromEuler(r[0], r[2], -r[1]);
+
+ return temp_q.toEuler();
+ },
+ cl_getInitalTransform: function (up_axis, scene_node) {
+ var util = CubicVR.util;
+ var retObj = {
+ position: [0, 0, 0],
+ rotation: [0, 0, 0],
+ scale: [1, 1, 1]
+ };
+
+ var translate = scene_node.translate;
+ var rotate = scene_node.rotate;
+ var scale = scene_node.scale;
+
+ if (translate) {
+ retObj.position = collada_tools.fixuaxis(up_axis, util.floatDelimArray(translate.$, " "));
+ }
+
+
+ if (rotate) {
+ for (var r = 0, rMax = rotate.length; r < rMax; r++) {
+ var cl_rot = rotate[r];
+
+ var rType = cl_rot["@sid"];
+
+ var rVal = util.floatDelimArray(cl_rot.$, " ");
+
+ if (rType == "rotateX" || rType == "rotationX") {
+ retObj.rotation[0] = rVal[3];
+ } else if (rType == "rotateY" || rType == "rotationY") {
+ retObj.rotation[1] = rVal[3];
+ } else if (rType == "rotateZ" || rType == "rotationZ") {
+ retObj.rotation[2] = rVal[3];
+ } //if
+ } //for
+ } //if
+ if (scale) {
+ retObj.scale = collada_tools.fixscaleaxis(up_axis, util.floatDelimArray(scale.$, " "));
+ }
+
+ // var cl_matrix = scene_node.getElementsByTagName("matrix");
+ //
+ // if (cl_matrix.length)
+ // {
+ // console.log(util.collectTextNode(cl_matrix[0]));
+ // }
+ return retObj;
+ }
+ };
+
+
+ function cubicvr_parseCollada(meshUrl, prefix, deferred_bin) {
+ // if (MeshPool[meshUrl] !== undef) return MeshPool[meshUrl];
+ var util = CubicVR.util;
+ var obj = new CubicVR.Mesh();
+ var scene = new CubicVR.Scene();
+ var cl = util.getXML(meshUrl);
+ var tech;
+ var sourceId;
+ var materialRef, nameRef, nFace, meshName;
+
+ var norm, vert, uv, mapLen, computedLen;
+
+ var i, iCount, iMax, iMod, mCount, mMax, k, kMax, cCount, cMax, sCount, sMax, pCount, pMax, j, jMax;
+
+ var cl_source = CubicVR.util.xml2badgerfish(cl);
+
+ cl = null;
+
+ if (!cl_source.COLLADA) {
+ throw new Error(meshUrl + " does not appear to be a valid COLLADA file.");
+ }
+
+ cl_source = cl_source.COLLADA;
+
+ var clib = {
+ up_axis: 1,
+ images: [],
+ effects: [],
+ materials: [],
+ meshes: [],
+ scenes: [],
+ lights: [],
+ cameras: [],
+ animations: []
+ };
+
+
+ // var up_axis = 1; // Y
+ if (cl_source.asset) {
+ var sAxis = cl_source.asset.up_axis.$;
+ if (sAxis === "X_UP") {
+ clib.up_axis = 0;
+ } else if (sAxis === "Y_UP") {
+ clib.up_axis = 1;
+ } else if (sAxis === "Z_UP") {
+ clib.up_axis = 2;
+ }
+ }
+
+ var up_axis = clib.up_axis;
+
+
+ if (cl_source.library_images) {
+ if (cl_source.library_images.image && !cl_source.library_images.image.length) cl_source.library_images.image = [cl_source.library_images.image];
+ if (cl_source.library_images.image.length) {
+ var cl_images = cl_source.library_images.image;
+ for (var imgCount = 0, imgCountMax = cl_images.length; imgCount < imgCountMax; imgCount++) {
+ var cl_img = cl_images[imgCount];
+ var imageId = cl_img["@id"];
+ var imageName = cl_img["@name"];
+ var cl_imgsrc = cl_img.init_from;
+
+ if (cl_imgsrc.$) {
+ var imageSource = cl_imgsrc.$;
+
+ if (prefix !== undef && (imageSource.lastIndexOf("/") !== -1)) {
+ imageSource = imageSource.substr(imageSource.lastIndexOf("/") + 1);
+ }
+ if (prefix !== undef && (imageSource.lastIndexOf("\\") !== -1)) {
+ imageSource = imageSource.substr(imageSource.lastIndexOf("\\") + 1);
+ }
+ // console.log("Image reference: "+imageSource+" @"+imageId+":"+imageName);
+ clib.images[imageId] = {
+ source: imageSource,
+ id: imageId,
+ name: imageName
+ };
+ }
+ }
+ }
+ }
+
+ // Effects
+ var effectId;
+ var effectCount, effectMax;
+ var tCount, tMax, inpCount, inpMax;
+ var cl_params, cl_inputs, cl_input, cl_inputmap, cl_samplers, cl_camera, cl_cameras, cl_scene;
+ var ofs;
+
+
+ if (cl_source.library_effects) {
+ var cl_effects = cl_source.library_effects.effect;
+
+ if (cl_effects && !cl_effects.length) cl_effects = [cl_effects];
+
+ for (effectCount = 0, effectMax = cl_effects.length; effectCount < effectMax; effectCount++) {
+ var cl_effect = cl_effects[effectCount];
+
+ effectId = cl_effect["@id"];
+
+ var effect = {};
+
+ effect.id = effectId;
+
+ effect.surfaces = [];
+ effect.samplers = [];
+
+ cl_params = cl_effect.profile_COMMON.newparam;
+
+ if (cl_params && !cl_params.length) {
+ cl_params = [cl_params];
+ };
+
+ var params = [];
+
+ var cl_init;
+
+ if (cl_params) {
+ for (pCount = 0, pMax = cl_params.length; pCount < pMax; pCount++) {
+ var cl_param = cl_params[pCount];
+
+ var paramId = cl_param["@sid"];
+
+ if (cl_param.surface) {
+ effect.surfaces[paramId] = {};
+
+ var initFrom = cl_param.surface.init_from.$;
+
+ if (typeof(clib.images[initFrom]) === 'object') {
+
+ var img_path = prefix + "/" + clib.images[initFrom].source;
+ effect.surfaces[paramId].source = img_path;
+ // console.log(prefix+"/"+clib.images[initFrom].source);
+ }
+ } else if (cl_param.sampler2D) {
+ effect.samplers[paramId] = {};
+
+ effect.samplers[paramId].source = cl_param.sampler2D.source.$;
+
+ if (cl_param.sampler2D.minfilter) {
+ effect.samplers[paramId].minfilter = cl_param.sampler2D.minfilter.$;
+ }
+
+ if (cl_param.sampler2D.magfilter) {
+ effect.samplers[paramId].magfiter = cl_param.sampler2D.magfilter.$;
+ }
+ }
+
+ }
+ }
+
+ var cl_technique = cl_effect.profile_COMMON.technique;
+
+ if (cl_technique && !cl_technique.length) cl_technique = [cl_technique];
+
+ var getColorNode = (function () {
+ return function (n) {
+ var el = n.color;
+ if (!el) {
+ return false;
+ }
+
+ var cn = n.color;
+ var ar = cn ? util.floatDelimArray(cn.$, " ") : false;
+
+ return ar;
+ };
+ }());
+
+ var getFloatNode = (function () {
+ return function (n) {
+ var el = n['float'];
+ if (!el) {
+ return false;
+ }
+
+ var cn = n['float'];
+ cn = cn ? parseFloat(cn.$) : 0;
+
+ return cn;
+ };
+ }());
+
+ var getTextureNode = (function () {
+ return function (n) {
+ var el = n.texture;
+ if (!el) {
+ return false;
+ }
+
+ var cn = n.texture["@texture"];
+
+ return cn;
+ };
+ }());
+
+ // effect.material = new Material(effectId);
+ effect.material = {
+ textures_ref: []
+ }
+
+ for (tCount = 0, tMax = cl_technique.length; tCount < tMax; tCount++) {
+ // if (cl_technique[tCount].getAttribute("sid") === 'common') {
+ tech = cl_technique[tCount].blinn;
+
+ if (!tech) {
+ tech = cl_technique[tCount].phong;
+ }
+ if (!tech) {
+ tech = cl_technique[tCount].lambert;
+ }
+
+ if (tech) {
+ // for (var eCount = 0, eMax = tech[0].childNodes.length; eCount < eMax; eCount++) {
+ // var node = tech[0].childNodes[eCount];
+ for (var tagName in tech) {
+ var node = tech[tagName];
+
+ var c = getColorNode(node);
+ var f = getFloatNode(node);
+ var t = getTextureNode(node);
+
+ if (c !== false) {
+ if (c.length > 3) {
+ c.pop();
+ }
+ }
+
+ if (tagName == "emission") {
+ if (c !== false) {
+ effect.material.ambient = c;
+ }
+ } else if (tagName == "ambient") {} else if (tagName == "diffuse") {
+ if (c !== false) {
+ effect.material.color = c;
+ }
+ } else if (tagName == "specular") {
+ if (c !== false) {
+ effect.material.specular = c;
+ }
+ } else if (tagName == "shininess") {
+ if (f !== false) {
+ effect.material.shininess = f;
+ }
+ } else if (tagName == "reflective") {
+ nop();
+ } else if (tagName == "reflectivity") {
+ nop();
+ } else if (tagName == "transparent") {
+ nop();
+ } else if (tagName == "index_of_refraction") {
+ nop();
+ }
+
+ // case "transparency": if (f!==false) effect.material.opacity = 1.0-f; break;
+ if (t !== false) {
+ var srcTex = effect.surfaces[effect.samplers[t].source].source;
+ if (tagName == "emission") {
+ effect.material.textures_ref.push({
+ image: srcTex,
+ type: enums.texture.map.AMBIENT
+ });
+ } else if (tagName == "ambient") {
+ effect.material.textures_ref.push({
+ image: srcTex,
+ type: enums.texture.map.AMBIENT
+ });
+ } else if (tagName == "diffuse") {
+ effect.material.textures_ref.push({
+ image: srcTex,
+ type: enums.texture.map.COLOR
+ });
+ } else if (tagName == "specular") {
+ effect.material.textures_ref.push({
+ image: srcTex,
+ type: enums.texture.map.SPECULAR
+ });
+ } else if (tagName == "shininess") {} else if (tagName == "reflective") {
+ effect.material.textures_ref.push({
+ image: srcTex,
+ type: enums.texture.map.REFLECT
+ });
+ } else if (tagName == "reflectivity") {} else if (tagName == "transparent") {
+ effect.material.textures_ref.push({
+ image: srcTex,
+ type: enums.texture.map.ALPHA
+ });
+ } else if (tagName == "transparency") {
+ nop();
+ } else if (tagName == "index_of_refraction") {
+ nop();
+ }
+ }
+ }
+ }
+
+ clib.effects[effectId] = effect;
+ }
+ }
+ }
+
+ // End Effects
+
+ var cl_lib_mat_inst = collada_tools.getAllOf(cl_source.library_visual_scenes, "instance_material");
+ var materialMap = [];
+
+ if (cl_lib_mat_inst.length) {
+ for (i = 0, iMax = cl_lib_mat_inst.length; i < iMax; i++) {
+ var cl_mat_inst = cl_lib_mat_inst[i];
+
+ var symbolId = cl_mat_inst["@symbol"];
+ var targetId = cl_mat_inst["@target"].substr(1);
+
+ materialMap[symbolId] = targetId;
+ }
+ }
+
+
+ var cl_lib_materials = cl_source.library_materials;
+
+ if (cl_lib_materials.material) {
+ var cl_materials = cl_lib_materials.material;
+ if (cl_materials && !cl_materials.length) cl_materials = [cl_materials];
+
+ for (mCount = 0, mMax = cl_materials.length; mCount < mMax; mCount++) {
+ var cl_material = cl_materials[mCount];
+
+ var materialId = cl_material["@id"];
+ var materialName = cl_material["@name"];
+
+ var cl_einst = cl_material.instance_effect;
+
+ if (cl_einst) {
+ effectId = cl_einst["@url"].substr(1);
+ clib.materials.push({
+ id: materialId,
+ name: materialName,
+ mat: clib.effects[effectId].material
+ });
+ }
+ }
+ }
+
+ var cl_lib_geo = cl_source.library_geometries;
+
+ if (cl_lib_geo) {
+ var cl_geo_node = cl_lib_geo.geometry;
+
+ if (cl_geo_node && !cl_geo_node.length) cl_geo_node = [cl_geo_node];
+
+ if (cl_geo_node.length) {
+ for (var meshCount = 0, meshMax = cl_geo_node.length; meshCount < meshMax; meshCount++) {
+ var meshData = {
+ id: undef,
+ points: [],
+ parts: []
+ };
+
+ var currentMaterial;
+
+ var cl_geomesh = cl_geo_node[meshCount].mesh;
+
+ // console.log("found "+meshUrl+"@"+meshName);
+ if (cl_geomesh) {
+ var meshId = cl_geo_node[meshCount]["@id"];
+ meshName = cl_geo_node[meshCount]["@name"];
+
+ // MeshPool[meshUrl + "@" + meshName] = newObj;
+ var cl_geosources = cl_geomesh.source;
+ if (cl_geosources && !cl_geosources.length) cl_geosources = [cl_geosources];
+
+ var geoSources = [];
+
+ for (var sourceCount = 0, sourceMax = cl_geosources.length; sourceCount < sourceMax; sourceCount++) {
+ var cl_geosource = cl_geosources[sourceCount];
+
+ sourceId = cl_geosource["@id"];
+ var sourceName = cl_geosource["@name"];
+ var cl_floatarray = cl_geosource.float_array;
+
+
+ if (cl_floatarray) {
+ geoSources[sourceId] = {
+ id: sourceId,
+ name: sourceName,
+ data: util.floatDelimArray(cl_floatarray.$?cl_floatarray.$:"", " ")
+ };
+ }
+
+ var cl_accessor = cl_geosource.technique_common.accessor;
+
+ if (cl_accessor) {
+ geoSources[sourceId].count = parseInt(cl_accessor["@count"]);
+ geoSources[sourceId].stride = parseInt(cl_accessor["@stride"]);
+ if (geoSources[sourceId].count) {
+ geoSources[sourceId].data = util.repackArray(geoSources[sourceId].data, geoSources[sourceId].stride, geoSources[sourceId].count);
+ }
+ }
+ }
+
+ var geoVerticies = [];
+
+ var cl_vertices = cl_geomesh.vertices;
+
+ var pointRef = null;
+ var pointRefId = null;
+ var triangleRef = null;
+ var normalRef = null;
+ var colorRef = null;
+ var uvRef = null;
+
+
+ if (cl_vertices) {
+ pointRefId = cl_vertices["@id"];
+ cl_inputs = cl_vertices.input;
+
+ if (cl_inputs && !cl_inputs.length) cl_inputs = [cl_inputs];
+
+ if (cl_inputs) {
+ for (inpCount = 0, inpMax = cl_inputs.length; inpCount < inpMax; inpCount++) {
+ cl_input = cl_inputs[inpCount];
+
+ if (cl_input["@semantic"] === "POSITION") {
+ pointRef = cl_input["@source"].substr(1);
+ }
+ }
+ }
+ }
+
+ var CL_VERTEX = 0,
+ CL_NORMAL = 1,
+ CL_TEXCOORD = 2,
+ CL_COLOR = 3,
+ CL_OTHER = 4;
+
+
+ var cl_triangles = cl_geomesh.triangles;
+ if (cl_triangles && !cl_triangles.length) cl_triangles = [cl_triangles];
+
+ if (cl_triangles) {
+ for (tCount = 0, tMax = cl_triangles.length; tCount < tMax; tCount++) {
+ var meshPart = {
+ material: 0,
+ faces: [],
+ normals: [],
+ texcoords: [],
+ colors: []
+ }
+
+ var cl_trianglesCount = parseInt(cl_triangles[tCount]["@count"], 10);
+ cl_inputs = cl_triangles[tCount].input;
+ if (cl_inputs && !cl_inputs.length) cl_inputs = [cl_inputs];
+
+ cl_inputmap = [];
+
+ if (cl_inputs.length) {
+ for (inpCount = 0, inpMax = cl_inputs.length; inpCount < inpMax; inpCount++) {
+ cl_input = cl_inputs[inpCount];
+
+ ofs = parseInt(cl_input["@offset"], 10);
+ nameRef = cl_input["@source"].substr(1);
+
+ if (cl_input["@semantic"] === "VERTEX") {
+ if (nameRef === pointRefId) {
+ nameRef = triangleRef = pointRef;
+ } else {
+ triangleRef = nameRef;
+ }
+ cl_inputmap[ofs] = CL_VERTEX;
+ } else if (cl_input["@semantic"] === "NORMAL") {
+ normalRef = nameRef;
+ if (geoSources[normalRef].count) {
+ cl_inputmap[ofs] = CL_NORMAL;
+ }
+ } else if (cl_input["@semantic"] === "TEXCOORD") {
+ uvRef = nameRef;
+ if (geoSources[uvRef].count) {
+ cl_inputmap[ofs] = CL_TEXCOORD;
+ }
+ } else if (cl_input["@semantic"] === "COLOR") {
+ colorRef = nameRef;
+ if (geoSources[colorRef].count) {
+ cl_inputmap[ofs] = CL_COLOR;
+ }
+ } else {
+ cl_inputmap[ofs] = CL_OTHER;
+ }
+ }
+ }
+ mapLen = cl_inputmap.length;
+
+ materialRef = cl_triangles[tCount]["@material"];
+
+ if (materialRef === null) {
+ meshPart.material = 0;
+ } else {
+ if (materialMap[materialRef] === undef) {
+ log("missing material [" + materialRef + "]@" + meshName + "?");
+ meshPart.material = 0;
+ } else {
+ meshPart.material = materialMap[materialRef];
+ }
+ }
+
+
+ var cl_triangle_source = cl_triangles[tCount].p;
+
+ var triangleData = [];
+
+ if (cl_triangle_source) {
+ triangleData = util.intDelimArray(cl_triangle_source.$, " ");
+ }
+
+ if (triangleData.length) {
+ computedLen = ((triangleData.length) / cl_inputmap.length) / 3;
+
+ if (computedLen !== cl_trianglesCount) {
+ // console.log("triangle data doesn't add up, skipping object load: "+computedLen+" !== "+cl_trianglesCount);
+ } else {
+ if (meshData.points.length === 0) {
+ meshData.points = geoSources[pointRef].data;
+ }
+
+ ofs = 0;
+
+ for (i = 0, iMax = triangleData.length, iMod = cl_inputmap.length; i < iMax; i += iMod * 3) {
+ norm = [];
+ vert = [];
+ uv = [];
+ color = [];
+
+ for (j = 0; j < iMod * 3; j++) {
+ var jMod = j % iMod;
+
+ if (cl_inputmap[jMod] === CL_VERTEX) {
+ vert.push(triangleData[i + j]);
+ } else if (cl_inputmap[jMod] === CL_NORMAL) {
+ norm.push(triangleData[i + j]);
+ } else if (cl_inputmap[jMod] === CL_TEXCOORD) {
+ uv.push(triangleData[i + j]);
+ } else if (cl_inputmap[jMod] === CL_COLOR) {
+ color.push(triangleData[i + j]);
+ }
+ }
+
+ if (vert.length) {
+ meshPart.faces.push(vert);
+
+ if (norm.length === 3) {
+ meshPart.normals.push([collada_tools.fixuaxis(clib.up_axis, geoSources[normalRef].data[norm[0]]), collada_tools.fixuaxis(clib.up_axis, geoSources[normalRef].data[norm[1]]), collada_tools.fixuaxis(clib.up_axis, geoSources[normalRef].data[norm[2]])]);
+ }
+
+
+ if (uv.length === 3) {
+ meshPart.texcoords.push([geoSources[uvRef].data[uv[0]], geoSources[uvRef].data[uv[1]], geoSources[uvRef].data[uv[2]]]);
+ }
+
+ if (color.length === 3) {
+ meshPart.colors.push([ geoSources[colorRef].data[color[0]],
+ geoSources[colorRef].data[color[1]],
+ geoSources[colorRef].data[color[2]] ]);
+ }
+ }
+ }
+ }
+ }
+
+ meshData.parts.push(meshPart);
+ }
+ }
+
+
+ var cl_polylist = cl_geomesh.polylist;
+ if (!cl_polylist) {
+ cl_polylist = cl_geomesh.polygons; // try polygons
+ }
+
+ if (cl_polylist && !cl_polylist.length) cl_polylist = [cl_polylist];
+
+ if (cl_polylist) {
+ for (tCount = 0, tMax = cl_polylist.length; tCount < tMax; tCount++) {
+ var meshPart = {
+ material: 0,
+ faces: [],
+ normals: [],
+ texcoords: [],
+ colors: []
+ }
+
+ var cl_polylistCount = parseInt(cl_polylist[tCount]["@count"], 10);
+
+ cl_inputs = cl_polylist[tCount].input;
+
+ if (cl_inputs && !cl_inputs.length) cl_inputs = [cl_inputs];
+
+ cl_inputmap = [];
+
+ if (cl_inputs.length) {
+ for (inpCount = 0, inpMax = cl_inputs.length; inpCount < inpMax; inpCount++) {
+ cl_input = cl_inputs[inpCount];
+
+ var cl_ofs = cl_input["@offset"];
+
+ if (cl_ofs === null) {
+ cl_ofs = cl_input["@idx"];
+ }
+
+ ofs = parseInt(cl_ofs, 10);
+ nameRef = cl_input["@source"].substr(1);
+
+ if (cl_input["@semantic"] === "VERTEX") {
+ if (nameRef === pointRefId) {
+ nameRef = triangleRef = pointRef;
+
+ } else {
+ triangleRef = nameRef;
+ }
+ cl_inputmap[ofs] = CL_VERTEX;
+ } else if (cl_input["@semantic"] === "NORMAL") {
+ normalRef = nameRef;
+ cl_inputmap[ofs] = CL_NORMAL;
+ } else if (cl_input["@semantic"] === "TEXCOORD") {
+ uvRef = nameRef;
+ cl_inputmap[ofs] = CL_TEXCOORD;
+ } else if (cl_input["@semantic"] === "COLOR") {
+ colorRef = nameRef;
+ cl_inputmap[ofs] = CL_COLOR;
+ } else {
+ cl_inputmap[ofs] = CL_OTHER;
+ }
+ }
+ }
+
+
+ var cl_vcount = cl_polylist[tCount].vcount;
+ var vcount = [];
+
+ if (cl_vcount) {
+ vcount = util.intDelimArray(cl_vcount.$, " ");
+ }
+
+ materialRef = cl_polylist[tCount]["@material"];
+
+ if (materialRef === undef) {
+ meshPart.material = 0;
+ } else {
+ meshPart.material = materialMap[materialRef];
+ }
+
+ var cl_poly_source = cl_polylist[tCount].p;
+
+ mapLen = cl_inputmap.length;
+
+ var polyData = [];
+
+ if ((cl_poly_source.length > 1) && !vcount.length) // blender 2.49 style
+ {
+ var pText = "";
+ for (pCount = 0, pMax = cl_poly_source.length; pCount < pMax; pCount++) {
+ var tmp = util.intDelimArray(cl_poly_source[pCount].$, " ");
+
+ vcount[pCount] = parseInt(tmp.length / mapLen, 10);
+
+ polyData.splice(polyData.length, 0, tmp);
+ }
+ } else {
+ if (cl_poly_source) {
+ polyData = util.intDelimArray(cl_poly_source.$, " ");
+ }
+ }
+
+ if (polyData.length) {
+ computedLen = vcount.length;
+
+ if (computedLen !== cl_polylistCount) {
+ log("poly vcount data doesn't add up, skipping object load: " + computedLen + " !== " + cl_polylistCount);
+ } else {
+ if (meshData.points.length === 0) {
+ meshData.points = geoSources[pointRef].data;
+ }
+
+ ofs = 0;
+
+ for (i = 0, iMax = vcount.length; i < iMax; i++) {
+ norm = [];
+ vert = [];
+ uv = [];
+ color = [];
+
+ for (j = 0, jMax = vcount[i] * mapLen; j < jMax; j++) {
+ if (cl_inputmap[j % mapLen] === CL_VERTEX) {
+ vert.push(polyData[ofs]);
+ ofs++;
+ } else if (cl_inputmap[j % mapLen] === CL_NORMAL) {
+ norm.push(polyData[ofs]);
+ ofs++;
+ } else if (cl_inputmap[j % mapLen] === CL_TEXCOORD) {
+ uv.push(polyData[ofs]);
+ ofs++;
+ } else if (cl_inputmap[j % mapLen] === CL_COLOR) {
+ color.push(polyData[ofs]);
+ ofs++;
+ } else {
+ ofs++;
+ }
+ }
+
+
+ if (vert.length) {
+ // if (up_axis !== 1)
+ // {
+ // vert.reverse();
+ // }
+ // nFace = newObj.addFace(vert);
+ meshPart.faces.push(vert);
+
+ if (norm.length) {
+ var nlist = [];
+ for (k = 0, kMax = norm.length; k < kMax; k++) {
+ // newObj.faces[nFace].point_normals[k] = fixuaxis(geoSources[normalRef].data[norm[k]]);
+ nlist.push(collada_tools.fixuaxis(clib.up_axis, geoSources[normalRef].data[norm[k]]));
+ }
+ meshPart.normals.push(nlist);
+ }
+
+ if (uv.length) {
+ var tlist = [];
+ for (k = 0, kMax = uv.length; k < kMax; k++) {
+ // newObj.faces[nFace].uvs[k] = geoSources[uvRef].data[uv[k]];
+ tlist.push(geoSources[uvRef].data[uv[k]]);
+ }
+ meshPart.texcoords.push(tlist);
+ }
+ if (color.length) {
+ var tlist = [];
+ for (k = 0, kMax = color.length; k < kMax; k++) {
+ // newObj.faces[nFace].uvs[k] = geoSources[uvRef].data[uv[k]];
+ tlist.push(geoSources[colorRef].data[color[k]]);
+ }
+ meshPart.colors.push(tlist);
+ }
+ }
+ }
+ }
+ }
+
+ meshData.parts.push(meshPart);
+ }
+ }
+
+ if (up_axis !== 1) {
+ for (i = 0, iMax = meshData.points.length; i < iMax; i++) {
+ meshData.points[i] = collada_tools.fixuaxis(clib.up_axis, meshData.points[i]);
+ }
+ }
+
+
+
+ meshData.id = meshId;
+ clib.meshes.push(meshData);
+
+ }
+ }
+ }
+ }
+
+
+
+
+
+ var cl_lib_cameras = cl_source.library_cameras;
+ var camerasBoundRef = [];
+
+ if (cl_lib_cameras) {
+ cl_cameras = cl_lib_cameras.camera;
+ if (cl_cameras && !cl_cameras.length) cl_cameras = [cl_cameras];
+
+ for (cCount = 0, cMax = cl_cameras.length; cCount < cMax; cCount++) {
+ cl_camera = cl_cameras[cCount];
+
+ var cameraId = cl_camera["@id"];
+ var cameraName = cl_camera["@name"];
+
+ // var cl_perspective = cl_camera.getElementsByTagName("perspective");
+ // if (cl_perspective.length) {
+ // var perspective = cl_perspective[0];
+ var cl_yfov = 0;
+ var cl_znear = 0;
+ var cl_zfar = 0;
+
+ if (cl_camera.optics) if (cl_camera.optics.technique_common) if (cl_camera.optics.technique_common.perspective) {
+ cl_yfov = cl_camera.optics.technique_common.perspective.yfov;
+ cl_znear = cl_camera.optics.technique_common.perspective.znear;
+ cl_zfar = cl_camera.optics.technique_common.perspective.zfar;
+ }
+
+
+ var yfov;
+ var znear;
+ var zfar;
+
+ if (!cl_yfov && !cl_znear && !cl_zfar) {
+ cl_params = cl_camera.param;
+ if (cl_params && !cl_params.length) cl_params = [cl_params];
+
+ for (i = 0, iMax = cl_params.length; i < iMax; i++) {
+ var txt = cl_params[i].$;
+ var pName = cl_params[i]["@name"];
+ if (pName == "YFOV") {
+ yfov = parseFloat(txt);
+ } else if (pName == "ZNEAR") {
+ znear = parseFloat(txt);
+ } else if (pName == "ZFAR") {
+ zfar = parseFloat(txt);
+ }
+ }
+ } else {
+ yfov = cl_yfov ? parseFloat(cl_yfov.$) : 60;
+ znear = cl_znear ? parseFloat(cl_znear.$) : 0.1;
+ zfar = cl_zfar ? parseFloat(cl_zfar.$) : 1000.0;
+ }
+
+ clib.cameras.push({
+ id: cameraId,
+ targeted: false,
+ fov: parseFloat(yfov),
+ nearclip: parseFloat(znear),
+ farclip: parseFloat(zfar)
+ });
+ }
+ }
+
+
+ var cl_lib_lights = cl_source.library_lights;
+
+ if (cl_lib_lights) {
+ var cl_lights = cl_lib_lights.light;
+ if (cl_lights && !cl_lights.length) cl_lights = [cl_lights];
+
+ if (cl_lights) for (var lightCount = 0, lightMax = cl_lights.length; lightCount < lightMax; lightCount++) {
+
+ var cl_light = cl_lights[lightCount];
+
+ var cl_point = cl_light.technique_common.point;
+ var cl_pointLight = cl_point ? cl_point : null;
+
+ var lightId = cl_light["@id"];
+ var lightName = cl_light["@name"];
+
+ if (cl_pointLight !== null) {
+
+ var cl_intensity = cl_pointLight.intensity;
+ var intensity = cl_intensity ? parseFloat(cl_intensity.$) : 1.0;
+ var cl_distance = cl_pointLight.distance;
+ var distance = cl_distance ? parseFloat(cl_distance.$) : 10.0;
+
+ var cl_color = cl_pointLight.color;
+ var color = [1, 1, 1];
+
+ if (cl_color) {
+ color = util.floatDelimArray(cl_color.$, " ");
+ }
+
+ clib.lights.push({
+ id: lightId,
+ name: lightId,
+ type: enums.light.type.POINT,
+ method: enums.light.method.STATIC,
+ diffuse: color,
+ specular: [0, 0, 0],
+ distance: distance,
+ intensity: intensity
+ });
+ }
+ }
+ }
+
+ var cl_lib_scenes = cl_source.library_visual_scenes;
+
+ if (cl_lib_scenes) {
+ var cl_scenes = null;
+
+ cl_scenes = cl_lib_scenes.visual_scene;
+ if (cl_scenes && !cl_scenes.length) cl_scenes = [cl_scenes];
+
+ for (var sceneCount = 0, sceneMax = cl_scenes.length; sceneCount < sceneMax; sceneCount++) {
+
+ cl_scene = cl_scenes[sceneCount];
+
+ var sceneId = cl_scene["@id"];
+ var sceneName = cl_scene["@name"];
+
+ var sceneData = {
+ id: sceneId,
+ sceneObjects: [],
+ cameras: [],
+ lights: [],
+ parentMap: []
+ };
+
+ var nodeMap = {};
+
+ var cl_nodes = [];
+ var cl_stack = [cl_scene];
+
+ while (cl_stack.length) {
+ var ntemp = cl_stack.pop();
+ if (ntemp.node) {
+ var nlist = ntemp.node;
+ if (nlist && !nlist.length) nlist = [nlist];
+
+ if (nlist) {
+ for (var i = 0, iMax = nlist.length; i < iMax; i++) {
+ nlist[i].parentNode = ntemp;
+ cl_nodes.push(nlist[i]);
+ cl_stack.push(nlist[i]);
+ }
+ }
+ }
+ }
+
+ if (cl_nodes.length) {
+ for (var nodeCount = 0, nodeMax = cl_nodes.length; nodeCount < nodeMax; nodeCount++) {
+ var cl_node = cl_nodes[nodeCount];
+
+ var cl_geom = cl_node.instance_geometry;
+ var cl_light = cl_nodes[nodeCount].instance_light;
+ cl_camera = cl_nodes[nodeCount].instance_camera;
+
+ var nodeId = cl_node["@id"];
+ var nodeName = cl_node["@name"];
+
+ var it = collada_tools.cl_getInitalTransform(clib.up_axis, cl_node);
+
+ if (up_axis === 2) {
+ it.rotation = collada_tools.quaternionFilterZYYZ(it.rotation, (cl_camera) ? [-90, 0, 0] : undef);
+ }
+
+ var sceneObject = {};
+
+ if (cl_geom) {
+ meshName = cl_geom["@url"].substr(1);
+
+ sceneObject.name = sceneObject.id = (nodeName) ? nodeName : nodeId;
+
+ sceneObject.position = it.position;
+ sceneObject.rotation = it.rotation;
+ sceneObject.scale = it.scale;
+ sceneObject.meshId = meshId;
+ sceneObject.meshName = meshName;
+
+ sceneData.sceneObjects.push(sceneObject);
+
+ nodeMap[sceneObject.id] = true;;
+
+ if (cl_node.parentNode) {
+ var parentNodeId = cl_node.parentNode["@id"];
+ var parentNodeName = cl_node.parentNode["@name"];
+ if (parentNodeId) {
+
+ if (nodeMap[parentNodeId]) {
+ sceneData.parentMap.push({
+ parent: parentNodeId,
+ child: sceneObject.id
+ });
+ }
+ }
+ }
+ } else if (cl_camera) {
+ var cam_instance = cl_camera;
+
+ var camRefId = cam_instance["@url"].substr(1);
+
+ sceneData.cameras.push({
+ name: (nodeName) ? nodeName : nodeId,
+ id: (nodeName) ? nodeName : nodeId,
+ source: camRefId,
+ position: it.position,
+ rotation: it.rotation
+ });
+
+
+ } else if (cl_light) {
+
+ var lightRefId = cl_light["@url"].substr(1);
+
+ sceneData.lights.push({
+ name: (nodeName) ? nodeName : nodeId,
+ id: (nodeName) ? nodeName : nodeId,
+ source: lightRefId,
+ position: it.position
+ });
+
+ } else {
+ sceneData.sceneObjects.push({
+ id: (nodeName !== null) ? nodeName : nodeId,
+ name: (nodeName !== null) ? nodeName : nodeId,
+ position: it.position,
+ rotation: it.rotation,
+ scale: it.scale
+ });
+
+ }
+
+ }
+ }
+
+ clib.scenes.push(sceneData);
+ }
+ }
+
+
+ var cl_lib_anim = cl_source.library_animations;
+
+ var animId;
+ if (cl_lib_anim) {
+ var cl_anim_sources = cl_lib_anim.animation;
+ if (cl_anim_sources && !cl_anim_sources.length) cl_anim_sources = [cl_anim_sources];
+
+ if (cl_anim_sources) {
+ for (var aCount = 0, aMax = cl_anim_sources.length; aCount < aMax; aCount++) {
+ var cl_anim = cl_anim_sources[aCount];
+
+ animId = cl_anim["@id"];
+ var animName = cl_anim["@name"];
+
+ clib.animations[animId] = {};
+ clib.animations[animId].sources = [];
+
+ var cl_sources = cl_anim.source;
+ if (cl_sources && !cl_sources.length) cl_sources = [cl_sources];
+
+ if (cl_sources.length) {
+ for (sCount = 0, sMax = cl_sources.length; sCount < sMax; sCount++) {
+ var cl_csource = cl_sources[sCount];
+
+ sourceId = cl_csource["@id"];
+
+
+ var tech_common = cl_csource.technique_common;
+
+ var name_array = null;
+ var float_array = null;
+ var data = null;
+
+ if (cl_csource.name_array) {
+ name_array = util.textDelimArray(cl_csource.name_array.$, " ");
+ } else if (cl_csource.Name_array) {
+ name_array = util.textDelimArray(cl_csource.Name_array.$, " ");
+ } else if (cl_csource.float_array) {
+ float_array = util.floatDelimArray(cl_csource.float_array.$, " ");
+ }
+
+ var acCount = 0;
+ var acSource = "";
+ var acStride = 1;
+
+ if (tech_common) {
+ tech = tech_common;
+ var acc = tech.accessor;
+
+ acCount = parseInt(acc["@count"], 10);
+ acSource = acc["@source"].substr(1);
+ var aStride = acc["@stride"];
+
+ if (aStride) {
+ acStride = parseInt(aStride, 10);
+ }
+ }
+
+ clib.animations[animId].sources[sourceId] = {
+ data: name_array ? name_array : float_array,
+ count: acCount,
+ source: acSource,
+ stride: acStride
+ };
+
+ if (acStride !== 1) {
+ clib.animations[animId].sources[sourceId].data = util.repackArray(clib.animations[animId].sources[sourceId].data, acStride, acCount);
+ }
+ }
+ }
+
+ cl_samplers = cl_anim.sampler;
+ if (cl_samplers && !cl_samplers.length) cl_samplers = [cl_samplers];
+
+ if (cl_samplers) {
+ clib.animations[animId].samplers = [];
+
+ for (sCount = 0, sMax = cl_samplers.length; sCount < sMax; sCount++) {
+ var cl_sampler = cl_samplers[sCount];
+
+ var samplerId = cl_sampler["@id"];
+
+ cl_inputs = cl_sampler.input;
+
+ if (cl_inputs && !cl_inputs.length) cl_inputs = [cl_inputs];
+
+ if (cl_inputs) {
+ var inputs = [];
+
+ for (iCount = 0, iMax = cl_inputs.length; iCount < iMax; iCount++) {
+ cl_input = cl_inputs[iCount];
+
+ var semanticName = cl_input["@semantic"];
+
+ inputs[semanticName] = cl_input["@source"].substr(1);
+ }
+
+ clib.animations[animId].samplers[samplerId] = inputs;
+ }
+ }
+ }
+
+ var cl_channels = cl_anim.channel;
+ if (cl_channels && !cl_channels.length) cl_channels = [cl_channels];
+
+
+ if (cl_channels) {
+ clib.animations[animId].channels = [];
+
+ for (cCount = 0, cMax = cl_channels.length; cCount < cMax; cCount++) {
+ var channel = cl_channels[cCount];
+
+ var channelSource = channel["@source"].substr(1);
+ var channelTarget = channel["@target"];
+
+ var channelSplitA = channelTarget.split("/");
+ var channelTargetName = channelSplitA[0];
+ var channelSplitB = channelSplitA[1].split(".");
+ var channelParam = channelSplitB[0];
+ var channelType = channelSplitB[1];
+
+ clib.animations[animId].channels.push({
+ source: channelSource,
+ target: channelTarget,
+ targetName: channelTargetName,
+ paramName: channelParam,
+ typeName: channelType
+ });
+ }
+ }
+ }
+ }
+ }
+
+ var cl_lib_scene = cl_source.scene;
+
+ if (cl_lib_scene) {
+ cl_scene = cl_lib_scene.instance_visual_scene;
+
+ if (cl_scene) {
+ var sceneUrl = cl_scene["@url"].substr(1);
+ clib.scene = sceneUrl;
+ }
+ }
+
+
+ return clib;
+ }
+
+
+ function cubicvr_loadCollada(meshUrl, prefix, deferred_bin) {
+
+ var clib = cubicvr_parseCollada(meshUrl, prefix, deferred_bin);
+
+ var up_axis = clib.up_axis;
+
+ var materialRef = [];
+
+ for (var m = 0, mMax = clib.materials.length; m < mMax; m++) {
+
+ var material = clib.materials[m];
+ var newMaterial = new CubicVR.Material(material.mat);
+
+ for (var t = 0, tMax = material.mat.textures_ref.length; t < tMax; t++) {
+ var tex = material.mat.textures_ref[t];
+
+ var texObj = null;
+
+ if (base.Textures_ref[tex.image] === undefined) {
+ texObj = new CubicVR.Texture(tex.image, GLCore.default_filter, deferred_bin, meshUrl);
+ } else {
+ texObj = base.Textures_obj[base.Textures_ref[tex.image]];
+ }
+
+ newMaterial.setTexture(texObj, tex.type);
+ }
+
+ materialRef[material.id] = newMaterial;
+ }
+
+
+ var meshRef = [];
+
+ for (var m = 0, mMax = clib.meshes.length; m < mMax; m++) {
+
+ var meshData = clib.meshes[m];
+
+ var newObj = new CubicVR.Mesh(meshData.id);
+
+ newObj.points = meshData.points;
+
+ for (var mp = 0, mpMax = meshData.parts.length; mp < mpMax; mp++) {
+ var part = meshData.parts[mp];
+
+ if (part.material !== 0) {
+ newObj.setFaceMaterial(materialRef[part.material]);
+ }
+
+ var bNorm = part.normals.length ? true : false;
+ var bTex = part.texcoords.length ? true : false;
+ var bColor = part.colors.length ? true : false;
+ if (bColor) materialRef[part.material].color_map = true;
+
+ for (var p = 0, pMax = part.faces.length; p < pMax; p++) {
+ var faceNum = newObj.addFace(part.faces[p]);
+ if (bNorm) newObj.faces[faceNum].point_normals = part.normals[p];
+ if (bTex) newObj.faces[faceNum].uvs = part.texcoords[p];
+ if (bColor) newObj.faces[faceNum].point_colors = part.colors[p];
+ }
+ }
+
+ // newObj.calcNormals();
+ if (!deferred_bin) {
+ newObj.triangulateQuads();
+ newObj.compile();
+ } else {
+ deferred_bin.addMesh(meshUrl, meshUrl + ":" + meshId, newObj);
+ }
+
+ meshRef[meshData.id] = newObj;
+ }
+
+
+ var camerasRef = [];
+
+ for (var c = 0, cMax = clib.cameras.length; c < cMax; c++) {
+ camerasRef[clib.cameras[c].id] = clib.cameras[c];
+ }
+
+
+ var lightsRef = [];
+
+ for (var l = 0, lMax = clib.lights.length; l < lMax; l++) {
+ lightsRef[clib.lights[l].id] = clib.lights[l];
+ }
+
+
+
+ var sceneObjectMap = {};
+ var sceneLightMap = {};
+ var sceneCameraMap = {};
+
+ var scenesRef = {};
+
+ for (var s = 0, sMax = clib.scenes.length; s < sMax; s++) {
+ var scn = clib.scenes[s];
+
+ var newScene = new CubicVR.Scene();
+
+ for (var so = 0, soMax = scn.sceneObjects.length; so < soMax; so++) {
+ var sceneObj = scn.sceneObjects[so];
+ var newSceneObject = new CubicVR.SceneObject(sceneObj);
+ var srcMesh = (meshRef[sceneObj.meshName]?meshRef[sceneObj.meshName]:meshRef[sceneObj.meshId]) || null;
+ newSceneObject.obj = srcMesh;
+
+ sceneObjectMap[sceneObj.id] = newSceneObject;
+ newScene.bindSceneObject(newSceneObject);
+ }
+
+ for (var l = 0, lMax = scn.lights.length; l < lMax; l++) {
+ var lt = scn.lights[l];
+
+ var newLight = new CubicVR.Light(lightsRef[lt.source]);
+ newLight.position = lt.position;
+
+ sceneLightMap[lt.id] = newLight;
+ newScene.bindLight(newLight);
+ }
+
+ if (scn.cameras.length) { // single camera for the moment until we support it
+ var cam = scn.cameras[0];
+ var newCam = new CubicVR.Camera(camerasRef[cam.source]);
+ newCam.position = cam.position;
+ newCam.rotation = cam.rotation;
+
+ sceneCameraMap[cam.id] = newCam;
+ newScene.camera = newCam;
+ }
+ for (var p = 0, pMax = scn.parentMap.length; p < pMax; p++) {
+ var pmap = scn.parentMap[p];
+ sceneObjectMap[pmap.parent].bindChild(sceneObjectMap[pmap.child]);
+ }
+
+ scenesRef[scn.id] = newScene;
+ }
+
+
+
+ for (animId in clib.animations) {
+ if (clib.animations.hasOwnProperty(animId)) {
+ var anim = clib.animations[animId];
+
+ if (anim.channels.length) {
+ for (cCount = 0, cMax = anim.channels.length; cCount < cMax; cCount++) {
+ var chan = anim.channels[cCount];
+ var sampler = anim.samplers[chan.source];
+ var samplerInput = anim.sources[sampler["INPUT"]];
+ var samplerOutput = anim.sources[sampler["OUTPUT"]];
+ var samplerInterp = anim.sources[sampler["INTERPOLATION"]];
+ var samplerInTangent = anim.sources[sampler["IN_TANGENT"]];
+ var samplerOutTangent = anim.sources[sampler["OUT_TANGENT"]];
+ var hasInTangent = (sampler["IN_TANGENT"] !== undef);
+ var hasOutTangent = (sampler["OUT_TANGENT"] !== undef);
+ var mtn = null;
+
+ var targetSceneObject = sceneObjectMap[chan.targetName];
+ var targetCamera = sceneCameraMap[chan.targetName];
+ var targetLight = sceneLightMap[chan.targetName];
+
+ if (targetSceneObject) {
+ if (targetSceneObject.motion === null) {
+ targetSceneObject.motion = new CubicVR.Motion();
+ }
+ mtn = targetSceneObject.motion;
+ } else if (targetCamera) {
+ if (targetCamera.motion === null) {
+ targetCamera.motion = new CubicVR.Motion();
+ }
+
+ mtn = targetCamera.motion;
+ } else if (targetLight) {
+ if (targetLight.motion === null) {
+ targetLight.motion = new CubicVR.Motion();
+ }
+
+ mtn = targetLight.motion;
+ }
+ // else
+ // {
+ // console.log("missing",chan.targetName);
+ // console.log("missing",chan.paramName);
+ // }
+ if (mtn === null) {
+ continue;
+ }
+
+ var controlTarget = enums.motion.POS;
+ var motionTarget = enums.motion.X;
+
+ if (up_axis === 2) {
+ mtn.yzflip = true;
+ }
+
+ var pName = chan.paramName;
+
+ if (pName === "rotateX" || pName === "rotationX") {
+ controlTarget = enums.motion.ROT;
+ motionTarget = enums.motion.X;
+ } else if (pName === "rotateY" || pName === "rotationY") {
+ controlTarget = enums.motion.ROT;
+ motionTarget = enums.motion.Y;
+ } else if (pName === "rotateZ" || pName === "rotationZ") {
+ controlTarget = enums.motion.ROT;
+ motionTarget = enums.motion.Z;
+ } else if (pName === "location") {
+ controlTarget = enums.motion.POS;
+ if (chan.typeName === "X") {
+ motionTarget = enums.motion.X;
+ }
+ if (chan.typeName === "Y") {
+ motionTarget = enums.motion.Y;
+ }
+ if (chan.typeName === "Z") {
+ motionTarget = enums.motion.Z;
+ }
+ } else if (pName === "translate") {
+ controlTarget = enums.motion.POS;
+ if (chan.typeName === "X") {
+ motionTarget = enums.motion.X;
+ }
+ if (chan.typeName === "Y") {
+ motionTarget = enums.motion.Y;
+ }
+ if (chan.typeName === "Z") {
+ motionTarget = enums.motion.Z;
+ }
+ } else if (pName === "LENS") {
+ // controlTarget = enums.motion.LENS;
+ // motionTarget = 4;
+ controlTarget = 10;
+ motionTarget = 10;
+ continue; // disabled, only here for temporary collada files
+ } else if (pName === "FOV") {
+ controlTarget = enums.motion.FOV;
+ motionTarget = 3; // ensure no axis fixes are applied
+ } else if (pName === "ZNEAR") {
+ controlTarget = enums.motion.NEARCLIP;
+ motionTarget = 3; // ensure no axis fixes are applied
+ } else if (pName === "ZFAR") {
+ controlTarget = enums.motion.FARCLIP;
+ motionTarget = 3; // ensure no axis fixes are applied
+ } else if (pName === "intensity") {
+ controlTarget = enums.motion.INTENSITY;
+ motionTarget = 3; // ensure no axis fixes are applied
+ }
+
+ if (targetLight && controlTarget < 3) targetLight.method = enums.light.method.DYNAMIC;
+
+ // if (up_axis === 2 && motionTarget === enums.motion.Z) motionTarget = enums.motion.Y;
+ // else if (up_axis === 2 && motionTarget === enums.motion.Y) motionTarget = enums.motion.Z;
+ //
+ var ival;
+ for (mCount = 0, mMax = samplerInput.data.length; mCount < mMax; mCount++) { // in the process of being deprecated
+ k = null;
+
+ if (typeof(samplerOutput.data[mCount]) === 'object') {
+ for (i = 0, iMax = samplerOutput.data[mCount].length; i < iMax; i++) {
+ ival = i;
+
+ if (up_axis === 2 && i === 2) {
+ ival = 1;
+ } else if (up_axis === 2 && i === 1) {
+ ival = 2;
+ }
+
+ k = mtn.setKey(controlTarget, ival, samplerInput.data[mCount], collada_tools.fixukaxis(clib.up_axis, controlTarget, ival, samplerOutput.data[mCount][i]));
+
+ if (samplerInterp) {
+ var pInterp = samplerInterp.data[mCount][i];
+ if (pInterp === "LINEAR") {
+ k.shape = enums.envelope.shape.LINE;
+ } else if (pInterp === "BEZIER") {
+ if (!(hasInTangent || hasOutTangent)) {
+ k.shape = enums.envelope.shape.LINEAR;
+ } else {
+ k.shape = enums.envelope.shape.BEZI;
+ }
+ }
+ }
+ }
+ } else {
+ ival = motionTarget;
+ ofs = 0;
+
+ if (targetCamera) {
+ if (controlTarget === enums.motion.ROT) {
+ if (up_axis === 2 && ival === 0) {
+ ofs = -90;
+ }
+ }
+ }
+
+ if (controlTarget === enums.motion.ROT) {
+ k = mtn.setKey(controlTarget, ival, samplerInput.data[mCount], samplerOutput.data[mCount] + ofs);
+ } else {
+ if (up_axis === 2 && motionTarget === 2) {
+ ival = 1;
+ } else if (up_axis === 2 && motionTarget === 1) {
+ ival = 2;
+ }
+
+ k = mtn.setKey(controlTarget, ival, samplerInput.data[mCount], collada_tools.fixukaxis(clib.up_axis, controlTarget, ival, samplerOutput.data[mCount]));
+ }
+
+ if (samplerInterp) {
+ var pInterp = samplerInterp.data[mCount];
+ if (pInterp === "LINEAR") {
+ k.shape = enums.envelope.shape.LINE;
+ } else if (pInterp === "BEZIER") {
+ if (!(hasInTangent || hasOutTangent)) {
+ k.shape = enums.envelope.shape.LINEAR;
+ k.continutity = 1.0;
+ } else {
+ k.shape = enums.envelope.shape.BEZ2;
+
+ var itx = samplerInTangent.data[mCount][0],
+ ity;
+ var otx = samplerOutTangent.data[mCount][0],
+ oty;
+
+ if (controlTarget === enums.motion.ROT) {
+ ity = samplerInTangent.data[mCount][1];
+ oty = samplerOutTangent.data[mCount][1];
+
+ // k.value = k.value/10;
+ // mtn.rscale = 10;
+ k.param[0] = itx - k.time;
+ k.param[1] = ity - k.value + ofs;
+ k.param[2] = otx - k.time;
+ k.param[3] = oty - k.value + ofs;
+ } else {
+ ity = collada_tools.fixukaxis(clib.up_axis, controlTarget, ival, samplerInTangent.data[mCount][1]);
+ oty = collada_tools.fixukaxis(clib.up_axis, controlTarget, ival, samplerOutTangent.data[mCount][1]);
+
+ k.param[0] = itx - k.time;
+ k.param[1] = ity - k.value;
+ k.param[2] = otx - k.time;
+ k.param[3] = oty - k.value;
+ }
+
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+
+
+ var sceneRef = null;
+
+ if (clib.scene) {
+ sceneRef = scenesRef[clib.scene];
+ } else {
+ sceneRef = scenesRef.pop();
+ }
+
+
+ return sceneRef;
+ }
+
+ var exports = {
+ loadCollada: cubicvr_loadCollada,
+ parseCollada: cubicvr_parseCollada
+ };
+
+ return exports;
+});
View
567 source/CubicVR.CVRXML.js
@@ -0,0 +1,567 @@
+
+CubicVR.RegisterModule("CVRXML",function(base) {
+
+ var undef = base.undef;
+ var nop = function(){ };
+ var enums = CubicVR.enums;
+ var GLCore = base.GLCore;
+
+ var MeshPool = [];
+
+
+ function cubicvr_isMotion(node) {
+ if (node === null) {
+ return false;
+ }
+
+ return (node.getElementsByTagName("x").length || node.getElementsByTagName("y").length || node.getElementsByTagName("z").length || node.getElementsByTagName("fov").length);
+ }
+
+
+ function cubicvr_nodeToMotion(node, controllerId, motion) {
+ var util = CubicVR.util;
+ var c = [];
+ c[0] = node.getElementsByTagName("x");
+ c[1] = node.getElementsByTagName("y");
+ c[2] = node.getElementsByTagName("z");
+ c[3] = node.getElementsByTagName("fov");
+
+ var etime, evalue, ein, eout, etcb;
+
+ for (var k in c) {
+ if (c.hasOwnProperty(k)) {
+ if (c[k] !== undef) {
+ if (c[k].length) {
+ etime = c[k][0].getElementsByTagName("time");
+ evalue = c[k][0].getElementsByTagName("value");
+ ein = c[k][0].getElementsByTagName("in");
+ eout = c[k][0].getElementsByTagName("out");
+ etcb = c[k][0].getElementsByTagName("tcb");
+
+ var time = null,
+ value = null,
+ tcb = null;
+
+ var intype = null,
+ outtype = null;
+
+ if (ein.length) {
+ intype = util.collectTextNode(ein[0]);
+ }
+
+ if (eout.length) {
+ outtype = util.collectTextNode(eout[0]);
+ }
+
+ if (etime.length) {
+ time = util.floatDelimArray(util.collectTextNode(etime[0]), " ");
+ }
+
+ if (evalue.length) {
+ value = util.floatDelimArray(util.collectTextNode(evalue[0]), " ");
+ }
+
+ if (etcb.length) {
+ tcb = util.floatDelimArray(util.collectTextNode(etcb[0]), " ");
+ }
+
+
+ if (time !== null && value !== null) {
+ for (var i = 0, iMax = time.length; i < iMax; i++) {
+ var mkey = motion.setKey(controllerId, k, time[i], value[i]);
+
+ if (tcb) {
+ mkey.tension = tcb[i * 3];
+ mkey.continuity = tcb[i * 3 + 1];
+ mkey.bias = tcb[i * 3 + 2];
+ }
+ }
+ }
+
+ var in_beh = enums.envelope.behavior.CONSTANT;
+ var out_beh = enums.envelope.behavior.CONSTANT;
+
+ if (intype) {
+ switch (intype) {
+ case "reset":
+ in_beh = enums.envelope.behavior.RESET;
+ break;
+ case "constant":
+ in_beh = enums.envelope.behavior.CONSTANT;
+ break;
+ case "repeat":
+ in_beh = enums.envelope.behavior.REPEAT;
+ break;
+ case "oscillate":
+ in_beh = enums.envelope.behavior.OSCILLATE;
+ break;
+ case "offset":
+ in_beh = enums.envelope.behavior.OFFSET;
+ break;
+ case "linear":
+ in_beh = enums.envelope.behavior.LINEAR;
+ break;
+ }
+ }
+
+ if (outtype) {
+ switch (outtype) {
+ case "reset":
+ out_beh = enums.envelope.behavior.RESET;
+ break;
+ case "constant":
+ out_beh = enums.envelope.behavior.CONSTANT;
+ break;
+ case "repeat":
+ out_beh = enums.envelope.behavior.REPEAT;
+ break;
+ case "oscillate":
+ out_beh = enums.envelope.behavior.OSCILLATE;
+ break;
+ case "offset":
+ out_beh = enums.envelope.behavior.OFFSET;
+ break;
+ case "linear":
+ out_beh = enums.envelope.behavior.LINEAR;
+ break;
+ }
+ }
+
+ motion.setBehavior(controllerId, k, in_beh, out_beh);
+ }
+ }
+ }
+ }
+ }
+
+ function cubicvr_loadMesh(meshUrl, prefix) {
+ if (MeshPool[meshUrl] !== undef) {
+ return MeshPool[meshUrl];
+ }
+
+ var util = CubicVR.util;
+
+ var i, j, p, iMax, jMax, pMax;
+
+ var obj = new CubicVR.Mesh();
+ var mesh = util.getXML(meshUrl);
+ var pts_elem = mesh.getElementsByTagName("points");
+
+ var pts_str = util.collectTextNode(pts_elem[0]);
+ var pts = pts_str.split(" ");
+
+ var texName, tex;
+
+ for (i = 0, iMax = pts.length; i < iMax; i++) {
+ pts[i] = pts[i].split(",");
+ for (j = 0, jMax = pts[i].length; j < jMax; j++) {
+ pts[i][j] = parseFloat(pts[i][j]);
+ }
+ }
+
+ obj.addPoint(pts);
+
+ var material_elem = mesh.getElementsByTagName("material");
+ var mappers = [];
+
+
+ for (i = 0, iMax = material_elem.length; i < iMax; i++) {
+ var melem = material_elem[i];
+
+ var matName = (melem.getElementsByTagName("name").length) ? (melem.getElementsByTagName("name")[0].firstChild.nodeValue) : null;
+ var mat = new CubicVR.Material(matName);
+
+ if (melem.getElementsByTagName("alpha").length) {
+ mat.opacity = parseFloat(melem.getElementsByTagName("alpha")[0].firstChild.nodeValue);
+ }
+ if (melem.getElementsByTagName("shininess").length) {
+ mat.shininess = (parseFloat(melem.getElementsByTagName("shininess")[0].firstChild.nodeValue) / 100.0);
+ }
+ if (melem.getElementsByTagName("max_smooth").length) {
+ mat.max_smooth = parseFloat(melem.getElementsByTagName("max_smooth")[0].firstChild.nodeValue);
+ }
+
+ if (melem.getElementsByTagName("color").length) {
+ mat.color = util.floatDelimArray(melem.getElementsByTagName("color")[0].firstChild.nodeValue);
+ }
+ if (melem.getElementsByTagName("ambient").length) {
+ mat.ambient = util.floatDelimArray(melem.getElementsByTagName("ambient")[0].firstChild.nodeValue);
+ }
+ if (melem.getElementsByTagName("diffuse").length) {
+ mat.diffuse = util.floatDelimArray(melem.getElementsByTagName("diffuse")[0].firstChild.nodeValue);
+ }
+ if (melem.getElementsByTagName("specular").length) {
+ mat.specular = util.floatDelimArray(melem.getElementsByTagName("specular")[0].firstChild.nodeValue);
+ }
+ if (melem.getElementsByTagName("texture").length) {
+ texName = (prefix ? prefix : "") + melem.getElementsByTagName("texture")[0].firstChild.nodeValue;
+ tex = (base.Textures_ref[texName] !== undef) ? base.Textures_obj[base.Textures_ref[texName]] : (new CubicVR.Texture(texName));
+ mat.setTexture(tex, enums.texture.map.COLOR);
+ }
+
+ if (melem.getElementsByTagName("texture_luminosity").length) {
+ texName = (prefix ? prefix : "") + melem.getElementsByTagName("texture_luminosity")[0].firstChild.nodeValue;
+ tex = (base.Textures_ref[texName] !== undef) ? base.Textures_obj[base.Textures_ref[texName]] : (new CubicVR.Texture(texName));
+ mat.setTexture(tex, enums.texture.map.AMBIENT);
+ }
+
+ if (melem.getElementsByTagName("texture_normal").length) {
+ texName = (prefix ? prefix : "") + melem.getElementsByTagName("texture_normal")[0].firstChild.nodeValue;
+ tex = (base.Textures_ref[texName] !== undef) ? base.Textures_obj[base.Textures_ref[texName]] : (new CubicVR.Texture(texName));
+ mat.setTexture(tex, enums.texture.map.NORMAL);
+ }
+
+ if (melem.getElementsByTagName("texture_specular").length) {
+ texName = (prefix ? prefix : "") + melem.getElementsByTagName("texture_specular")[0].firstChild.nodeValue;
+ tex = (base.Textures_ref[texName] !== undef) ? base.Textures_obj[base.Textures_ref[texName]] : (new CubicVR.Texture(texName));
+ mat.setTexture(tex, enums.texture.map.SPECULAR);
+ }
+
+ if (melem.getElementsByTagName("texture_bump").length) {
+ texName = (prefix ? prefix : "") + melem.getElementsByTagName("texture_bump")[0].firstChild.nodeValue;
+ tex = (base.Textures_ref[texName] !== undef) ? base.Textures_obj[base.Textures_ref[texName]] : (new CubicVR.Texture(texName));
+ mat.setTexture(tex, enums.texture.map.BUMP);
+ }
+
+ if (melem.getElementsByTagName("texture_envsphere").length) {
+ texName = (prefix ? prefix : "") + melem.getElementsByTagName("texture_envsphere")[0].firstChild.nodeValue;
+ tex = (base.Textures_ref[texName] !== undef) ? base.Textures_obj[base.Textures_ref[texName]] : (new CubicVR.Texture(texName));
+ mat.setTexture(tex, enums.texture.map.ENVSPHERE);
+ }
+
+ if (melem.getElementsByTagName("texture_alpha").length) {
+ texName = (prefix ? prefix : "") + melem.getElementsByTagName("texture_alpha")[0].firstChild.nodeValue;
+ tex = (base.Textures_ref[texName] !== undef) ? base.Textures_obj[base.Textures_ref[texName]] : (new CubicVR.Texture(texName));
+ mat.setTexture(tex, enums.texture.map.ALPHA);
+ }
+
+ var uvSet = null;
+
+ if (melem.getElementsByTagName("uvmapper").length) {
+ var uvm = new CubicVR.UVMapper();
+ var uvelem = melem.getElementsByTagName("uvmapper")[0];
+ var uvmType = "";
+
+ if (uvelem.getElementsByTagName("type").length) {
+ uvmType = melem.getElementsByTagName("type")[0].firstChild.nodeValue;
+
+ switch (uvmType) {
+ case "uv":
+ break;
+ case "planar":
+ uvm.projection_mode = enums.uv.projection.PLANAR;
+ break;
+ case "cylindrical":
+ uvm.projection_mode = enums.uv.projection.CYLINDRICAL;
+ break;
+ case "spherical":
+ uvm.projection_mode = enums.uv.projection.SPHERICAL;
+ break;
+ case "cubic":
+ uvm.projection_mode = enums.uv.projection.CUBIC;
+ break;
+ }
+ }
+
+ if (uvmType === "uv") {
+ if (uvelem.getElementsByTagName("uv").length) {
+ var uvText = util.collectTextNode(melem.getElementsByTagName("uv")[0]);
+
+ uvSet = uvText.split(" ");
+
+ for (j = 0, jMax = uvSet.length; j < jMax; j++) {
+ uvSet[j] = util.floatDelimArray(uvSet[j]);
+ }
+ }
+ }
+
+ if (uvelem.getElementsByTagName("axis").length) {
+ var uvmAxis = melem.getElementsByTagName("axis")[0].firstChild.nodeValue;
+
+ switch (uvmAxis) {
+ case "x":
+ uvm.projection_axis = enums.uv.axis.X;
+ break;
+ case "y":
+ uvm.projection_axis = enums.uv.axis.Y;
+ break;
+ case "z":
+ uvm.projection_axis = enums.uv.axis.Z;
+ break;
+ }
+
+ }
+
+ if (melem.getElementsByTagName("center").length) {
+ uvm.center = util.floatDelimArray(melem.getElementsByTagName("center")[0].firstChild.nodeValue);
+ }
+ if (melem.getElementsByTagName("rotation").length) {
+ uvm.rotation = util.floatDelimArray(melem.getElementsByTagName("rotation")[0].firstChild.nodeValue);
+ }
+ if (melem.getElementsByTagName("scale").length) {
+ uvm.scale = util.floatDelimArray(melem.getElementsByTagName("scale")[0].firstChild.nodeValue);
+ }
+
+ if (uvmType !== "" && uvmType !== "uv") {
+ mappers.push([uvm, mat]);
+ }
+ }
+
+
+ var seglist = null;
+ var triangles = null;
+
+ if (melem.getElementsByTagName("segments").length) {
+ seglist = util.intDelimArray(util.collectTextNode(melem.getElementsByTagName("segments")[0]), " ");
+ }
+ if (melem.getElementsByTagName("triangles").length) {
+ triangles = util.intDelimArray(util.collectTextNode(melem.getElementsByTagName("triangles")[0]), " ");
+ }
+
+
+ if (seglist === null) {
+ seglist = [0, parseInt((triangles.length) / 3, 10)];
+ }
+
+ var ofs = 0;
+
+ obj.setFaceMaterial(mat);
+
+ if (triangles.length) {
+ for (p = 0, pMax = seglist.length; p < pMax; p += 2) {
+ var currentSegment = seglist[p];
+ var totalPts = seglist[p + 1] * 3;
+
+ obj.setSegment(currentSegment);
+
+ for (j = ofs, jMax = ofs + totalPts; j < jMax; j += 3) {
+ var newFace = obj.addFace([triangles[j], triangles[j + 1], triangles[j + 2]]);
+ if (uvSet) {
+ obj.faces[newFace].setUV([uvSet[j], uvSet[j + 1], uvSet[j + 2]]);
+ }
+ }
+
+ ofs += totalPts;
+ }
+ }
+ }
+
+ obj.calcNormals();
+
+ for (i = 0, iMax = mappers.length; i < iMax; i++) {
+ mappers[i][0].apply(obj, mappers[i][1]);
+ }
+
+ obj.compile();
+
+ MeshPool[meshUrl] = obj;
+
+ return obj;
+ }
+
+
+
+
+
+
+
+ function cubicvr_loadScene(sceneUrl, model_prefix, image_prefix) {
+ var util = CubicVR.util;
+ if (model_prefix === undef) {
+ model_prefix = "";
+ }
+ if (image_prefix === undef) {
+ image_prefix = "";
+ }
+
+ var obj = new CubicVR.Mesh();
+ var scene = util.getXML(sceneUrl);
+
+ var sceneOut = new CubicVR.Scene();
+
+ var parentingSet = [];
+
+ var sceneobjs = scene.getElementsByTagName("sceneobjects");
+
+ var tempNode;
+
+ var position, rotation, scale;
+
+ // var pts_str = util.collectTextNode(pts_elem[0]);
+ for (var i = 0, iMax = sceneobjs[0].childNodes.length; i < iMax; i++) {
+ var sobj = sceneobjs[0].childNodes[i];
+
+ if (sobj.tagName === "sceneobject") {
+
+ var name = "unnamed";
+ var parent = "";
+ var model = "";
+
+ tempNode = sobj.getElementsByTagName("name");
+ if (tempNode.length) {
+ name = util.collectTextNode(tempNode[0]);
+ }
+
+ tempNode = sobj.getElementsByTagName("parent");
+ if (tempNode.length) {
+ parent = util.collectTextNode(tempNode[0]);
+ }
+
+ tempNode = sobj.getElementsByTagName("model");
+ if (tempNode.length) {
+ model = util.collectTextNode(tempNode[0]);
+ }
+
+ position = null;
+ rotation = null;
+ scale = null;
+
+ tempNode = sobj.getElementsByTagName("position");
+ if (tempNode.length) {
+ position = tempNode[0];
+ }
+
+ tempNode = sobj.getElementsByTagName("rotation");
+ if (tempNode.length) {
+ rotation = tempNode[0];
+ }
+
+ tempNode = sobj.getElementsByTagName("scale");
+ if (tempNode.length) {
+ scale = tempNode[0];
+ }
+
+ obj = null;
+
+ if (model !== "") {
+ obj = cubicvr_loadMesh(model_prefix + model, image_prefix);
+ }
+
+ var sceneObject = new CubicVR.SceneObject(obj, name);
+
+ if (cubicvr_isMotion(position)) {
+ if (!sceneObject.motion) {
+ sceneObject.motion = new CubicVR.Motion();
+ }
+ cubicvr_nodeToMotion(position, enums.motion.POS, sceneObject.motion);
+ } else if (position) {
+ sceneObject.position = util.floatDelimArray(util.collectTextNode(position));
+ }
+
+ if (cubicvr_isMotion(rotation)) {
+ if (!sceneObject.motion) {
+ sceneObject.motion = new CubicVR.Motion();
+ }
+ cubicvr_nodeToMotion(rotation, enums.motion.ROT, sceneObject.motion);
+ } else {
+ sceneObject.rotation = util.floatDelimArray(util.collectTextNode(rotation));
+ }
+
+ if (cubicvr_isMotion(scale)) {
+ if (!sceneObject.motion) {