three
Version:
JavaScript 3D library
2,498 lines (1,483 loc) • 106 kB
JavaScript
/**
* @author Tim Knip / http://www.floorplanner.com/ / tim at floorplanner.com
* @author Tony Parisi / http://www.tonyparisi.com/
*/
THREE.ColladaLoader = function () {
var COLLADA = null;
var scene = null;
var visualScene;
var kinematicsModel;
var readyCallbackFunc = null;
var sources = {};
var images = {};
var animations = {};
var controllers = {};
var geometries = {};
var materials = {};
var effects = {};
var cameras = {};
var lights = {};
var animData;
var kinematics;
var visualScenes;
var kinematicsModels;
var baseUrl;
var morphs;
var skins;
var flip_uv = true;
var preferredShading = THREE.SmoothShading;
var options = {
// Force Geometry to always be centered at the local origin of the
// containing Mesh.
centerGeometry: false,
// Axis conversion is done for geometries, animations, and controllers.
// If we ever pull cameras or lights out of the COLLADA file, they'll
// need extra work.
convertUpAxis: false,
subdivideFaces: true,
upAxis: 'Y',
// For reflective or refractive materials we'll use this cubemap
defaultEnvMap: null
};
var colladaUnit = 1.0;
var colladaUp = 'Y';
var upConversion = null;
function load ( url, readyCallback, progressCallback, failCallback ) {
var length = 0;
if ( document.implementation && document.implementation.createDocument ) {
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if ( request.readyState === 4 ) {
if ( request.status === 0 || request.status === 200 ) {
if ( request.response ) {
readyCallbackFunc = readyCallback;
parse( request.response, undefined, url );
} else {
if ( failCallback ) {
failCallback( { type: 'error', url: url } );
} else {
console.error( "ColladaLoader: Empty or non-existing file (" + url + ")" );
}
}
}else{
if( failCallback ){
failCallback( { type: 'error', url: url } );
}else{
console.error( 'ColladaLoader: Couldn\'t load "' + url + '" (' + request.status + ')' );
}
}
} else if ( request.readyState === 3 ) {
if ( progressCallback ) {
if ( length === 0 ) {
length = request.getResponseHeader( "Content-Length" );
}
progressCallback( { total: length, loaded: request.responseText.length } );
}
}
};
request.open( "GET", url, true );
request.send( null );
} else {
alert( "Don't know how to parse XML!" );
}
}
function parse( text, callBack, url ) {
COLLADA = new DOMParser().parseFromString( text, 'text/xml' );
callBack = callBack || readyCallbackFunc;
if ( url !== undefined ) {
var parts = url.split( '/' );
parts.pop();
baseUrl = ( parts.length < 1 ? '.' : parts.join( '/' ) ) + '/';
}
parseAsset();
setUpConversion();
images = parseLib( "library_images image", _Image, "image" );
materials = parseLib( "library_materials material", Material, "material" );
effects = parseLib( "library_effects effect", Effect, "effect" );
geometries = parseLib( "library_geometries geometry", Geometry, "geometry" );
cameras = parseLib( "library_cameras camera", Camera, "camera" );
lights = parseLib( "library_lights light", Light, "light" );
controllers = parseLib( "library_controllers controller", Controller, "controller" );
animations = parseLib( "library_animations animation", Animation, "animation" );
visualScenes = parseLib( "library_visual_scenes visual_scene", VisualScene, "visual_scene" );
kinematicsModels = parseLib( "library_kinematics_models kinematics_model", KinematicsModel, "kinematics_model" );
morphs = [];
skins = [];
visualScene = parseScene();
scene = new THREE.Group();
for ( var i = 0; i < visualScene.nodes.length; i ++ ) {
scene.add( createSceneGraph( visualScene.nodes[ i ] ) );
}
// unit conversion
scene.scale.multiplyScalar( colladaUnit );
createAnimations();
kinematicsModel = parseKinematicsModel();
createKinematics();
var result = {
scene: scene,
morphs: morphs,
skins: skins,
animations: animData,
kinematics: kinematics,
dae: {
images: images,
materials: materials,
cameras: cameras,
lights: lights,
effects: effects,
geometries: geometries,
controllers: controllers,
animations: animations,
visualScenes: visualScenes,
visualScene: visualScene,
scene: visualScene,
kinematicsModels: kinematicsModels,
kinematicsModel: kinematicsModel
}
};
if ( callBack ) {
callBack( result );
}
return result;
}
function setPreferredShading ( shading ) {
preferredShading = shading;
}
function parseAsset () {
var elements = COLLADA.querySelectorAll('asset');
var element = elements[0];
if ( element && element.childNodes ) {
for ( var i = 0; i < element.childNodes.length; i ++ ) {
var child = element.childNodes[ i ];
switch ( child.nodeName ) {
case 'unit':
var meter = child.getAttribute( 'meter' );
if ( meter ) {
colladaUnit = parseFloat( meter );
}
break;
case 'up_axis':
colladaUp = child.textContent.charAt(0);
break;
}
}
}
}
function parseLib ( q, classSpec, prefix ) {
var elements = COLLADA.querySelectorAll(q);
var lib = {};
var i = 0;
var elementsLength = elements.length;
for ( var j = 0; j < elementsLength; j ++ ) {
var element = elements[j];
var daeElement = ( new classSpec() ).parse( element );
if ( !daeElement.id || daeElement.id.length === 0 ) daeElement.id = prefix + ( i ++ );
lib[ daeElement.id ] = daeElement;
}
return lib;
}
function parseScene() {
var sceneElement = COLLADA.querySelectorAll('scene instance_visual_scene')[0];
if ( sceneElement ) {
var url = sceneElement.getAttribute( 'url' ).replace( /^#/, '' );
return visualScenes[ url.length > 0 ? url : 'visual_scene0' ];
} else {
return null;
}
}
function parseKinematicsModel() {
var kinematicsModelElement = COLLADA.querySelectorAll('instance_kinematics_model')[0];
if ( kinematicsModelElement ) {
var url = kinematicsModelElement.getAttribute( 'url' ).replace(/^#/, '');
return kinematicsModels[ url.length > 0 ? url : 'kinematics_model0' ];
} else {
return null;
}
}
function createAnimations() {
animData = [];
// fill in the keys
recurseHierarchy( scene );
}
function recurseHierarchy( node ) {
var n = visualScene.getChildById( node.colladaId, true ),
newData = null;
if ( n && n.keys ) {
newData = {
fps: 60,
hierarchy: [ {
node: n,
keys: n.keys,
sids: n.sids
} ],
node: node,
name: 'animation_' + node.name,
length: 0
};
animData.push(newData);
for ( var i = 0, il = n.keys.length; i < il; i ++ ) {
newData.length = Math.max( newData.length, n.keys[i].time );
}
} else {
newData = {
hierarchy: [ {
keys: [],
sids: []
} ]
}
}
for ( var i = 0, il = node.children.length; i < il; i ++ ) {
var d = recurseHierarchy( node.children[i] );
for ( var j = 0, jl = d.hierarchy.length; j < jl; j ++ ) {
newData.hierarchy.push( {
keys: [],
sids: []
} );
}
}
return newData;
}
function calcAnimationBounds () {
var start = 1000000;
var end = -start;
var frames = 0;
var ID;
for ( var id in animations ) {
var animation = animations[ id ];
ID = ID || animation.id;
for ( var i = 0; i < animation.sampler.length; i ++ ) {
var sampler = animation.sampler[ i ];
sampler.create();
start = Math.min( start, sampler.startTime );
end = Math.max( end, sampler.endTime );
frames = Math.max( frames, sampler.input.length );
}
}
return { start:start, end:end, frames:frames,ID:ID };
}
function createMorph ( geometry, ctrl ) {
var morphCtrl = ctrl instanceof InstanceController ? controllers[ ctrl.url ] : ctrl;
if ( !morphCtrl || !morphCtrl.morph ) {
console.log("could not find morph controller!");
return;
}
var morph = morphCtrl.morph;
for ( var i = 0; i < morph.targets.length; i ++ ) {
var target_id = morph.targets[ i ];
var daeGeometry = geometries[ target_id ];
if ( !daeGeometry.mesh ||
!daeGeometry.mesh.primitives ||
!daeGeometry.mesh.primitives.length ) {
continue;
}
var target = daeGeometry.mesh.primitives[ 0 ].geometry;
if ( target.vertices.length === geometry.vertices.length ) {
geometry.morphTargets.push( { name: "target_1", vertices: target.vertices } );
}
}
geometry.morphTargets.push( { name: "target_Z", vertices: geometry.vertices } );
}
function createSkin ( geometry, ctrl, applyBindShape ) {
var skinCtrl = controllers[ ctrl.url ];
if ( !skinCtrl || !skinCtrl.skin ) {
console.log( "could not find skin controller!" );
return;
}
if ( !ctrl.skeleton || !ctrl.skeleton.length ) {
console.log( "could not find the skeleton for the skin!" );
return;
}
var skin = skinCtrl.skin;
var skeleton = visualScene.getChildById( ctrl.skeleton[ 0 ] );
var hierarchy = [];
applyBindShape = applyBindShape !== undefined ? applyBindShape : true;
var bones = [];
geometry.skinWeights = [];
geometry.skinIndices = [];
//createBones( geometry.bones, skin, hierarchy, skeleton, null, -1 );
//createWeights( skin, geometry.bones, geometry.skinIndices, geometry.skinWeights );
/*
geometry.animation = {
name: 'take_001',
fps: 30,
length: 2,
JIT: true,
hierarchy: hierarchy
};
*/
if ( applyBindShape ) {
for ( var i = 0; i < geometry.vertices.length; i ++ ) {
geometry.vertices[ i ].applyMatrix4( skin.bindShapeMatrix );
}
}
}
function setupSkeleton ( node, bones, frame, parent ) {
node.world = node.world || new THREE.Matrix4();
node.localworld = node.localworld || new THREE.Matrix4();
node.world.copy( node.matrix );
node.localworld.copy( node.matrix );
if ( node.channels && node.channels.length ) {
var channel = node.channels[ 0 ];
var m = channel.sampler.output[ frame ];
if ( m instanceof THREE.Matrix4 ) {
node.world.copy( m );
node.localworld.copy(m);
if (frame === 0)
node.matrix.copy(m);
}
}
if ( parent ) {
node.world.multiplyMatrices( parent, node.world );
}
bones.push( node );
for ( var i = 0; i < node.nodes.length; i ++ ) {
setupSkeleton( node.nodes[ i ], bones, frame, node.world );
}
}
function setupSkinningMatrices ( bones, skin ) {
// FIXME: this is dumb...
for ( var i = 0; i < bones.length; i ++ ) {
var bone = bones[ i ];
var found = -1;
if ( bone.type != 'JOINT' ) continue;
for ( var j = 0; j < skin.joints.length; j ++ ) {
if ( bone.sid === skin.joints[ j ] ) {
found = j;
break;
}
}
if ( found >= 0 ) {
var inv = skin.invBindMatrices[ found ];
bone.invBindMatrix = inv;
bone.skinningMatrix = new THREE.Matrix4();
bone.skinningMatrix.multiplyMatrices(bone.world, inv); // (IBMi * JMi)
bone.animatrix = new THREE.Matrix4();
bone.animatrix.copy(bone.localworld);
bone.weights = [];
for ( var j = 0; j < skin.weights.length; j ++ ) {
for (var k = 0; k < skin.weights[ j ].length; k ++ ) {
var w = skin.weights[ j ][ k ];
if ( w.joint === found ) {
bone.weights.push( w );
}
}
}
} else {
console.warn( "ColladaLoader: Could not find joint '" + bone.sid + "'." );
bone.skinningMatrix = new THREE.Matrix4();
bone.weights = [];
}
}
}
//Walk the Collada tree and flatten the bones into a list, extract the position, quat and scale from the matrix
function flattenSkeleton(skeleton) {
var list = [];
var walk = function(parentid, node, list) {
var bone = {};
bone.name = node.sid;
bone.parent = parentid;
bone.matrix = node.matrix;
var data = [ new THREE.Vector3(),new THREE.Quaternion(),new THREE.Vector3() ];
bone.matrix.decompose(data[0], data[1], data[2]);
bone.pos = [ data[0].x,data[0].y,data[0].z ];
bone.scl = [ data[2].x,data[2].y,data[2].z ];
bone.rotq = [ data[1].x,data[1].y,data[1].z,data[1].w ];
list.push(bone);
for (var i in node.nodes) {
walk(node.sid, node.nodes[i], list);
}
};
walk(-1, skeleton, list);
return list;
}
//Move the vertices into the pose that is proper for the start of the animation
function skinToBindPose(geometry,skeleton,skinController) {
var bones = [];
setupSkeleton( skeleton, bones, -1 );
setupSkinningMatrices( bones, skinController.skin );
var v = new THREE.Vector3();
var skinned = [];
for (var i = 0; i < geometry.vertices.length; i ++) {
skinned.push(new THREE.Vector3());
}
for ( i = 0; i < bones.length; i ++ ) {
if ( bones[ i ].type != 'JOINT' ) continue;
for ( var j = 0; j < bones[ i ].weights.length; j ++ ) {
var w = bones[ i ].weights[ j ];
var vidx = w.index;
var weight = w.weight;
var o = geometry.vertices[vidx];
var s = skinned[vidx];
v.x = o.x;
v.y = o.y;
v.z = o.z;
v.applyMatrix4( bones[i].skinningMatrix );
s.x += (v.x * weight);
s.y += (v.y * weight);
s.z += (v.z * weight);
}
}
for (var i = 0; i < geometry.vertices.length; i ++) {
geometry.vertices[i] = skinned[i];
}
}
function applySkin ( geometry, instanceCtrl, frame ) {
var skinController = controllers[ instanceCtrl.url ];
frame = frame !== undefined ? frame : 40;
if ( !skinController || !skinController.skin ) {
console.log( 'ColladaLoader: Could not find skin controller.' );
return;
}
if ( !instanceCtrl.skeleton || !instanceCtrl.skeleton.length ) {
console.log( 'ColladaLoader: Could not find the skeleton for the skin. ' );
return;
}
var animationBounds = calcAnimationBounds();
var skeleton = visualScene.getChildById( instanceCtrl.skeleton[0], true ) || visualScene.getChildBySid( instanceCtrl.skeleton[0], true );
//flatten the skeleton into a list of bones
var bonelist = flattenSkeleton(skeleton);
var joints = skinController.skin.joints;
//sort that list so that the order reflects the order in the joint list
var sortedbones = [];
for (var i = 0; i < joints.length; i ++) {
for (var j = 0; j < bonelist.length; j ++) {
if (bonelist[j].name === joints[i]) {
sortedbones[i] = bonelist[j];
}
}
}
//hook up the parents by index instead of name
for (var i = 0; i < sortedbones.length; i ++) {
for (var j = 0; j < sortedbones.length; j ++) {
if (sortedbones[i].parent === sortedbones[j].name) {
sortedbones[i].parent = j;
}
}
}
var i, j, w, vidx, weight;
var v = new THREE.Vector3(), o, s;
// move vertices to bind shape
for ( i = 0; i < geometry.vertices.length; i ++ ) {
geometry.vertices[i].applyMatrix4( skinController.skin.bindShapeMatrix );
}
var skinIndices = [];
var skinWeights = [];
var weights = skinController.skin.weights;
// hook up the skin weights
// TODO - this might be a good place to choose greatest 4 weights
for ( var i =0; i < weights.length; i ++ ) {
var indicies = new THREE.Vector4(weights[i][0] ? weights[i][0].joint : 0,weights[i][1] ? weights[i][1].joint : 0,weights[i][2] ? weights[i][2].joint : 0,weights[i][3] ? weights[i][3].joint : 0);
var weight = new THREE.Vector4(weights[i][0] ? weights[i][0].weight : 0,weights[i][1] ? weights[i][1].weight : 0,weights[i][2] ? weights[i][2].weight : 0,weights[i][3] ? weights[i][3].weight : 0);
skinIndices.push(indicies);
skinWeights.push(weight);
}
geometry.skinIndices = skinIndices;
geometry.skinWeights = skinWeights;
geometry.bones = sortedbones;
// process animation, or simply pose the rig if no animation
//create an animation for the animated bones
//NOTE: this has no effect when using morphtargets
var animationdata = { "name":animationBounds.ID,"fps":30,"length":animationBounds.frames / 30,"hierarchy":[] };
for (var j = 0; j < sortedbones.length; j ++) {
animationdata.hierarchy.push({ parent:sortedbones[j].parent, name:sortedbones[j].name, keys:[] });
}
console.log( 'ColladaLoader:', animationBounds.ID + ' has ' + sortedbones.length + ' bones.' );
skinToBindPose(geometry, skeleton, skinController);
for ( frame = 0; frame < animationBounds.frames; frame ++ ) {
var bones = [];
var skinned = [];
// process the frame and setup the rig with a fresh
// transform, possibly from the bone's animation channel(s)
setupSkeleton( skeleton, bones, frame );
setupSkinningMatrices( bones, skinController.skin );
for (var i = 0; i < bones.length; i ++) {
for (var j = 0; j < animationdata.hierarchy.length; j ++) {
if (animationdata.hierarchy[j].name === bones[i].sid) {
var key = {};
key.time = (frame / 30);
key.matrix = bones[i].animatrix;
if (frame === 0)
bones[i].matrix = key.matrix;
var data = [ new THREE.Vector3(),new THREE.Quaternion(),new THREE.Vector3() ];
key.matrix.decompose(data[0], data[1], data[2]);
key.pos = [ data[0].x,data[0].y,data[0].z ];
key.scl = [ data[2].x,data[2].y,data[2].z ];
key.rot = data[1];
animationdata.hierarchy[j].keys.push(key);
}
}
}
geometry.animation = animationdata;
}
}
function createKinematics() {
if ( kinematicsModel && kinematicsModel.joints.length === 0 ) {
kinematics = undefined;
return;
}
var jointMap = {};
var _addToMap = function( jointIndex, parentVisualElement ) {
var parentVisualElementId = parentVisualElement.getAttribute( 'id' );
var colladaNode = visualScene.getChildById( parentVisualElementId, true );
var joint = kinematicsModel.joints[ jointIndex ];
scene.traverse(function( node ) {
if ( node.colladaId == parentVisualElementId ) {
jointMap[ jointIndex ] = {
node: node,
transforms: colladaNode.transforms,
joint: joint,
position: joint.zeroPosition
};
}
});
};
kinematics = {
joints: kinematicsModel && kinematicsModel.joints,
getJointValue: function( jointIndex ) {
var jointData = jointMap[ jointIndex ];
if ( jointData ) {
return jointData.position;
} else {
console.log( 'getJointValue: joint ' + jointIndex + ' doesn\'t exist' );
}
},
setJointValue: function( jointIndex, value ) {
var jointData = jointMap[ jointIndex ];
if ( jointData ) {
var joint = jointData.joint;
if ( value > joint.limits.max || value < joint.limits.min ) {
console.log( 'setJointValue: joint ' + jointIndex + ' value ' + value + ' outside of limits (min: ' + joint.limits.min + ', max: ' + joint.limits.max + ')' );
} else if ( joint.static ) {
console.log( 'setJointValue: joint ' + jointIndex + ' is static' );
} else {
var threejsNode = jointData.node;
var axis = joint.axis;
var transforms = jointData.transforms;
var matrix = new THREE.Matrix4();
for (i = 0; i < transforms.length; i ++ ) {
var transform = transforms[ i ];
// kinda ghetto joint detection
if ( transform.sid && transform.sid.indexOf( 'joint' + jointIndex ) !== -1 ) {
// apply actual joint value here
switch ( joint.type ) {
case 'revolute':
matrix.multiply( m1.makeRotationAxis( axis, THREE.Math.degToRad(value) ) );
break;
case 'prismatic':
matrix.multiply( m1.makeTranslation(axis.x * value, axis.y * value, axis.z * value ) );
break;
default:
console.warn( 'setJointValue: unknown joint type: ' + joint.type );
break;
}
} else {
var m1 = new THREE.Matrix4();
switch ( transform.type ) {
case 'matrix':
matrix.multiply( transform.obj );
break;
case 'translate':
matrix.multiply( m1.makeTranslation( transform.obj.x, transform.obj.y, transform.obj.z ) );
break;
case 'rotate':
matrix.multiply( m1.makeRotationAxis( transform.obj, transform.angle ) );
break;
}
}
}
// apply the matrix to the threejs node
var elementsFloat32Arr = matrix.elements;
var elements = Array.prototype.slice.call( elementsFloat32Arr );
var elementsRowMajor = [
elements[ 0 ],
elements[ 4 ],
elements[ 8 ],
elements[ 12 ],
elements[ 1 ],
elements[ 5 ],
elements[ 9 ],
elements[ 13 ],
elements[ 2 ],
elements[ 6 ],
elements[ 10 ],
elements[ 14 ],
elements[ 3 ],
elements[ 7 ],
elements[ 11 ],
elements[ 15 ]
];
threejsNode.matrix.set.apply( threejsNode.matrix, elementsRowMajor );
threejsNode.matrix.decompose( threejsNode.position, threejsNode.quaternion, threejsNode.scale );
}
} else {
console.log( 'setJointValue: joint ' + jointIndex + ' doesn\'t exist' );
}
}
};
var element = COLLADA.querySelector('scene instance_kinematics_scene');
if ( element ) {
for ( var i = 0; i < element.childNodes.length; i ++ ) {
var child = element.childNodes[ i ];
if ( child.nodeType != 1 ) continue;
switch ( child.nodeName ) {
case 'bind_joint_axis':
var visualTarget = child.getAttribute( 'target' ).split( '/' ).pop();
var axis = child.querySelector('axis param').textContent;
var jointIndex = parseInt( axis.split( 'joint' ).pop().split( '.' )[0] );
var visualTargetElement = COLLADA.querySelector( '[sid="' + visualTarget + '"]' );
if ( visualTargetElement ) {
var parentVisualElement = visualTargetElement.parentElement;
_addToMap(jointIndex, parentVisualElement);
}
break;
default:
break;
}
}
}
}
function createSceneGraph ( node, parent ) {
var obj = new THREE.Object3D();
var skinned = false;
var skinController;
var morphController;
var i, j;
// FIXME: controllers
for ( i = 0; i < node.controllers.length; i ++ ) {
var controller = controllers[ node.controllers[ i ].url ];
switch ( controller.type ) {
case 'skin':
if ( geometries[ controller.skin.source ] ) {
var inst_geom = new InstanceGeometry();
inst_geom.url = controller.skin.source;
inst_geom.instance_material = node.controllers[ i ].instance_material;
node.geometries.push( inst_geom );
skinned = true;
skinController = node.controllers[ i ];
} else if ( controllers[ controller.skin.source ] ) {
// urgh: controller can be chained
// handle the most basic case...
var second = controllers[ controller.skin.source ];
morphController = second;
// skinController = node.controllers[i];
if ( second.morph && geometries[ second.morph.source ] ) {
var inst_geom = new InstanceGeometry();
inst_geom.url = second.morph.source;
inst_geom.instance_material = node.controllers[ i ].instance_material;
node.geometries.push( inst_geom );
}
}
break;
case 'morph':
if ( geometries[ controller.morph.source ] ) {
var inst_geom = new InstanceGeometry();
inst_geom.url = controller.morph.source;
inst_geom.instance_material = node.controllers[ i ].instance_material;
node.geometries.push( inst_geom );
morphController = node.controllers[ i ];
}
console.log( 'ColladaLoader: Morph-controller partially supported.' );
default:
break;
}
}
// geometries
var double_sided_materials = {};
for ( i = 0; i < node.geometries.length; i ++ ) {
var instance_geometry = node.geometries[i];
var instance_materials = instance_geometry.instance_material;
var geometry = geometries[ instance_geometry.url ];
var used_materials = {};
var used_materials_array = [];
var num_materials = 0;
var first_material;
if ( geometry ) {
if ( !geometry.mesh || !geometry.mesh.primitives )
continue;
if ( obj.name.length === 0 ) {
obj.name = geometry.id;
}
// collect used fx for this geometry-instance
if ( instance_materials ) {
for ( j = 0; j < instance_materials.length; j ++ ) {
var instance_material = instance_materials[ j ];
var mat = materials[ instance_material.target ];
var effect_id = mat.instance_effect.url;
var shader = effects[ effect_id ].shader;
var material3js = shader.material;
if ( geometry.doubleSided ) {
if ( !( instance_material.symbol in double_sided_materials ) ) {
var _copied_material = material3js.clone();
_copied_material.side = THREE.DoubleSide;
double_sided_materials[ instance_material.symbol ] = _copied_material;
}
material3js = double_sided_materials[ instance_material.symbol ];
}
material3js.opacity = !material3js.opacity ? 1 : material3js.opacity;
used_materials[ instance_material.symbol ] = num_materials;
used_materials_array.push( material3js );
first_material = material3js;
first_material.name = mat.name === null || mat.name === '' ? mat.id : mat.name;
num_materials ++;
}
}
var mesh;
var material = first_material || new THREE.MeshLambertMaterial( { color: 0xdddddd, side: geometry.doubleSided ? THREE.DoubleSide : THREE.FrontSide } );
var geom = geometry.mesh.geometry3js;
if ( num_materials > 1 ) {
material = new THREE.MultiMaterial( used_materials_array );
for ( j = 0; j < geom.faces.length; j ++ ) {
var face = geom.faces[ j ];
face.materialIndex = used_materials[ face.daeMaterial ]
}
}
if ( skinController !== undefined ) {
applySkin( geom, skinController );
if ( geom.morphTargets.length > 0 ) {
material.morphTargets = true;
material.skinning = false;
} else {
material.morphTargets = false;
material.skinning = true;
}
mesh = new THREE.SkinnedMesh( geom, material, false );
//mesh.skeleton = skinController.skeleton;
//mesh.skinController = controllers[ skinController.url ];
//mesh.skinInstanceController = skinController;
mesh.name = 'skin_' + skins.length;
//mesh.animationHandle.setKey(0);
skins.push( mesh );
} else if ( morphController !== undefined ) {
createMorph( geom, morphController );
material.morphTargets = true;
mesh = new THREE.Mesh( geom, material );
mesh.name = 'morph_' + morphs.length;
morphs.push( mesh );
} else {
if ( geom.isLineStrip === true ) {
mesh = new THREE.Line( geom );
} else {
mesh = new THREE.Mesh( geom, material );
}
}
obj.add(mesh);
}
}
for ( i = 0; i < node.cameras.length; i ++ ) {
var instance_camera = node.cameras[i];
var cparams = cameras[instance_camera.url];
var cam = new THREE.PerspectiveCamera(cparams.yfov, parseFloat(cparams.aspect_ratio),
parseFloat(cparams.znear), parseFloat(cparams.zfar));
obj.add(cam);
}
for ( i = 0; i < node.lights.length; i ++ ) {
var light = null;
var instance_light = node.lights[i];
var lparams = lights[instance_light.url];
if ( lparams && lparams.technique ) {
var color = lparams.color.getHex();
var intensity = lparams.intensity;
var distance = lparams.distance;
var angle = lparams.falloff_angle;
switch ( lparams.technique ) {
case 'directional':
light = new THREE.DirectionalLight( color, intensity, distance );
light.position.set(0, 0, 1);
break;
case 'point':
light = new THREE.PointLight( color, intensity, distance );
break;
case 'spot':
light = new THREE.SpotLight( color, intensity, distance, angle );
light.position.set(0, 0, 1);
break;
case 'ambient':
light = new THREE.AmbientLight( color );
break;
}
}
if (light) {
obj.add(light);
}
}
obj.name = node.name || node.id || "";
obj.colladaId = node.id || "";
obj.layer = node.layer || "";
obj.matrix = node.matrix;
obj.matrix.decompose( obj.position, obj.quaternion, obj.scale );
if ( options.centerGeometry && obj.geometry ) {
var delta = obj.geometry.center();
delta.multiply( obj.scale );
delta.applyQuaternion( obj.quaternion );
obj.position.sub( delta );
}
for ( i = 0; i < node.nodes.length; i ++ ) {
obj.add( createSceneGraph( node.nodes[i], node ) );
}
return obj;
}
function getJointId( skin, id ) {
for ( var i = 0; i < skin.joints.length; i ++ ) {
if ( skin.joints[ i ] === id ) {
return i;
}
}
}
function getLibraryNode( id ) {
var nodes = COLLADA.querySelectorAll('library_nodes node');
for ( var i = 0; i < nodes.length; i++ ) {
var attObj = nodes[i].attributes.getNamedItem('id');
if ( attObj && attObj.value === id ) {
return nodes[i];
}
}
return undefined;
}
function getChannelsForNode ( node ) {
var channels = [];
var startTime = 1000000;
var endTime = -1000000;
for ( var id in animations ) {
var animation = animations[id];
for ( var i = 0; i < animation.channel.length; i ++ ) {
var channel = animation.channel[i];
var sampler = animation.sampler[i];
var id = channel.target.split('/')[0];
if ( id == node.id ) {
sampler.create();
channel.sampler = sampler;
startTime = Math.min(startTime, sampler.startTime);
endTime = Math.max(endTime, sampler.endTime);
channels.push(channel);
}
}
}
if ( channels.length ) {
node.startTime = startTime;
node.endTime = endTime;
}
return channels;
}
function calcFrameDuration( node ) {
var minT = 10000000;
for ( var i = 0; i < node.channels.length; i ++ ) {
var sampler = node.channels[i].sampler;
for ( var j = 0; j < sampler.input.length - 1; j ++ ) {
var t0 = sampler.input[ j ];
var t1 = sampler.input[ j + 1 ];
minT = Math.min( minT, t1 - t0 );
}
}
return minT;
}
function calcMatrixAt( node, t ) {
var animated = {};
var i, j;
for ( i = 0; i < node.channels.length; i ++ ) {
var channel = node.channels[ i ];
animated[ channel.sid ] = channel;
}
var matrix = new THREE.Matrix4();
for ( i = 0; i < node.transforms.length; i ++ ) {
var transform = node.transforms[ i ];
var channel = animated[ transform.sid ];
if ( channel !== undefined ) {
var sampler = channel.sampler;
var value;
for ( j = 0; j < sampler.input.length - 1; j ++ ) {
if ( sampler.input[ j + 1 ] > t ) {
value = sampler.output[ j ];
//console.log(value.flatten)
break;
}
}
if ( value !== undefined ) {
if ( value instanceof THREE.Matrix4 ) {
matrix.multiplyMatrices( matrix, value );
} else {
// FIXME: handle other types
matrix.multiplyMatrices( matrix, transform.matrix );
}
} else {
matrix.multiplyMatrices( matrix, transform.matrix );
}
} else {
matrix.multiplyMatrices( matrix, transform.matrix );
}
}
return matrix;
}
function bakeAnimations ( node ) {
if ( node.channels && node.channels.length ) {
var keys = [],
sids = [];
for ( var i = 0, il = node.channels.length; i < il; i ++ ) {
var channel = node.channels[i],
fullSid = channel.fullSid,
sampler = channel.sampler,
input = sampler.input,
transform = node.getTransformBySid( channel.sid ),
member;
if ( channel.arrIndices ) {
member = [];
for ( var j = 0, jl = channel.arrIndices.length; j < jl; j ++ ) {
member[ j ] = getConvertedIndex( channel.arrIndices[ j ] );
}
} else {
member = getConvertedMember( channel.member );
}
if ( transform ) {
if ( sids.indexOf( fullSid ) === -1 ) {
sids.push( fullSid );
}
for ( var j = 0, jl = input.length; j < jl; j ++ ) {
var time = input[j],
data = sampler.getData( transform.type, j, member ),
key = findKey( keys, time );
if ( !key ) {
key = new Key( time );
var timeNdx = findTimeNdx( keys, time );
keys.splice( timeNdx === -1 ? keys.length : timeNdx, 0, key );
}
key.addTarget( fullSid, transform, member, data );
}
} else {
console.log( 'Could not find transform "' + channel.sid + '" in node ' + node.id );
}
}
// post process
for ( var i = 0; i < sids.length; i ++ ) {
var sid = sids[ i ];
for ( var j = 0; j < keys.length; j ++ ) {
var key = keys[ j ];
if ( !key.hasTarget( sid ) ) {
interpolateKeys( keys, key, j, sid );
}
}
}
node.keys = keys;
node.sids = sids;
}
}
function findKey ( keys, time) {
var retVal = null;
for ( var i = 0, il = keys.length; i < il && retVal === null; i ++ ) {
var key = keys[i];
if ( key.time === time ) {
retVal = key;
} else if ( key.time > time ) {
break;
}
}
return retVal;
}
function findTimeNdx ( keys, time) {
var ndx = -1;
for ( var i = 0, il = keys.length; i < il && ndx === -1; i ++ ) {
var key = keys[i];
if ( key.time >= time ) {
ndx = i;
}
}
return ndx;
}
function interpolateKeys ( keys, key, ndx, fullSid ) {
var prevKey = getPrevKeyWith( keys, fullSid, ndx ? ndx - 1 : 0 ),
nextKey = getNextKeyWith( keys, fullSid, ndx + 1 );
if ( prevKey && nextKey ) {
var scale = (key.time - prevKey.time) / (nextKey.time - prevKey.time),
prevTarget = prevKey.getTarget( fullSid ),
nextData = nextKey.getTarget( fullSid ).data,
prevData = prevTarget.data,
data;
if ( prevTarget.type === 'matrix' ) {
data = prevData;
} else if ( prevData.length ) {
data = [];
for ( var i = 0; i < prevData.length; ++ i ) {
data[ i ] = prevData[ i ] + ( nextData[ i ] - prevData[ i ] ) * scale;
}
} else {
data = prevData + ( nextData - prevData ) * scale;
}
key.addTarget( fullSid, prevTarget.transform, prevTarget.member, data );
}
}
// Get next key with given sid
function getNextKeyWith( keys, fullSid, ndx ) {
for ( ; ndx < keys.length; ndx ++ ) {
var key = keys[ ndx ];
if ( key.hasTarget( fullSid ) ) {
return key;
}
}
return null;
}
// Get previous key with given sid
function getPrevKeyWith( keys, fullSid, ndx ) {
ndx = ndx >= 0 ? ndx : ndx + keys.length;
for ( ; ndx >= 0; ndx -- ) {
var key = keys[ ndx ];
if ( key.hasTarget( fullSid ) ) {
return key;
}
}
return null;
}
function _Image() {
this.id = "";
this.init_from = "";
}
_Image.prototype.parse = function(element) {
this.id = element.getAttribute('id');
for ( var i = 0; i < element.childNodes.length; i ++ ) {
var child = element.childNodes[ i ];
if ( child.nodeName === 'init_from' ) {
this.init_from = child.textContent;
}
}
return this;
};
function Controller() {
this.id = "";
this.name = "";
this.type = "";
this.skin = null;
this.morph = null;
}
Controller.prototype.parse = function( element ) {
this.id = element.getAttribute('id');
this.name = element.getAttribute('name');
this.type = "none";
for ( var i = 0; i < element.childNodes.length; i ++ ) {
var child = element.childNodes[ i ];
switch ( child.nodeName ) {
case 'skin':
this.skin = (new Skin()).parse(child);
this.type = child.nodeName;
break;
case 'morph':
this.morph = (new Morph()).parse(child);
this.type = child.nodeName;
break;
default:
break;
}
}
return this;
};
function Morph() {
this.method = null;
this.source = null;
this.targets = null;
this.weights = null;
}
Morph.prototype.parse = function( element ) {
var sources = {};
var inputs = [];
var i;
this.method = element.getAttribute( 'method' );
this.source = element.getAttribute( 'source' ).replace( /^#/, '' );
for ( i = 0; i < element.childNodes.length; i ++ ) {
var child = element.childNodes[ i ];
if ( child.nodeType != 1 ) continue;
switch ( child.nodeName ) {
case 'source':
var source = ( new Source() ).parse( child );
sources[ source.id ] = source;
break;
case 'targets':
inputs = this.parseInputs( child );
break;
default:
console.log( child.nodeName );
break;
}
}
for ( i = 0; i < inputs.length; i ++ ) {
var input = inputs[ i ];
var source = sources[ input.source ];
switch ( input.semantic ) {
case 'MORPH_TARGET':
this.targets = source.read();
break;
case 'MORPH_WEIGHT':
this.weights = source.read();
break;
default:
break;
}
}
return this;
};
Morph.prototype.parseInputs = function(element) {
var inputs = [];
for ( var i = 0; i < element.childNodes.length; i ++ ) {
var child = element.childNodes[i];
if ( child.nodeType != 1) continue;
switch ( child.nodeName ) {
case 'input':
inputs.push( (new Input()).parse(child) );
break;
default:
break;
}
}
return inputs;
};
function Skin() {
this.source = "";
this.bindShapeMatrix = null;
this.invBindMatrices = [];
this.joints = [];
this.weights = [];
}
Skin.prototype.parse = function( element ) {
var sources = {};
var joints, weights;
this.source = element.getAttribute( 'source' ).replace( /^#/, '' );
this.invBindMatrices = [];
this.joints = [];
this.weights = [];
for ( var i = 0; i < element.childNodes.length; i ++ ) {
var child = element.childNodes[i];
if ( child.nodeType != 1 ) continue;
switch ( child.nodeName ) {
case 'bind_shape_matrix':
var f = _floats(child.textContent);
this.bindShapeMatrix = getConvertedMat4( f );
break;
case 'source':
var src = new Source().parse(child);
sources[ src.id ] = src;
break;
case 'joints':
joints = child;
break;
case 'vertex_weights':
weights = child;
break;
default:
console.log( child.nodeName );
break;
}
}
this.parseJoints( joints, sources );
this.parseWeights( weights, sources );
return this;
};
Skin.prototype.parseJoints = function ( element, sources ) {
for ( var i = 0; i < element.childNodes.length; i ++ ) {
var child = element.childNodes[ i ];
if ( child.nodeType != 1 ) continue;
switch ( child.nodeName ) {
case 'input':
var input = ( new Input() ).parse( child );
var source = sources[ input.source ];
if ( input.semantic === 'JOINT' ) {
this.joints = source.read();
} else if ( input.semantic === 'INV_BIND_MATRIX' ) {
this.invBindMatrices = source.read();
}
break;
default:
break;
}
}
};
Skin.prototype.parseWeights = function ( element, sources ) {
var v, vcount, inputs = [];
for ( var i = 0; i < element.childNodes.length; i ++ ) {
var child = element.childNodes[ i ];
if ( child.nodeType != 1 ) continue;
switch ( child.nodeName ) {
case 'input':
inputs.push( ( new Input() ).parse( child ) );
break;
case 'v':
v = _ints( child.textContent );
break;
case 'vcount':
vcount = _ints( child.textContent );
break;
default:
break;
}
}
var index = 0;
for ( var i = 0; i < vcount.length; i ++ ) {
var numBones = vcount[i];
var vertex_weights = [];
for ( var j = 0; j < numBones; j ++ ) {
var influence = {};
for ( var k = 0; k < inputs.length; k ++ ) {
var input = inputs[ k ];
var value = v[ index + input.offset ];
switch ( input.semantic ) {
case 'JOINT':
influence.joint = value;//this.joints[value];
break;
case 'WEIGHT':
influence.weight = sources[ input.source ].data[ value ];
break;
default:
break;
}
}
vertex_weights.push( influence );
index += inputs.length;
}
for ( var j = 0; j < vertex_weights.length; j ++ ) {
vertex_weights[ j ].index = i;
}
this.weights.push( vertex_weights );
}
};
function VisualScene () {
this.id = "";
this.name = "";
this.nodes = [];
this.scene = new THREE.Group();
}
VisualScene.prototype.getChildById = function( id, recursive ) {
for ( var i = 0; i < this.nodes.length; i ++ ) {
var node = this.nodes[ i ].getChildById( id, recursive );
if ( node ) {
return node;
}
}
return null;
};
VisualScene.prototype.getChildBySid = function( sid, recursive ) {
for ( var i = 0; i < this.nodes.length; i ++ ) {
var node = this.nodes[ i ].getChildBySid( sid, recursive );
if ( node ) {
return node;
}
}
return null;
};
VisualScene.prototype.parse = function( element ) {
this.id = element.getAttribute( 'id' );
this.name = element.getAttribute( 'name' );
this.nodes = [];
for ( var i = 0; i < element.childNodes.length; i ++ ) {
var child = element.childNodes[ i ];
if ( child.nodeType != 1 ) continue;
switch ( child.nodeName ) {
case 'node':
this.nodes.push( ( new Node() ).parse( child ) );
break;
default:
break;
}
}
return this;
};
function Node() {
this.id = "";
this.name = "";
this.sid = "";
this.nodes = [];
this.controllers = [];
this.transforms = [];
this.geometries = [];
this.channels = [];
this.matrix = new THREE.Matrix4();
}
Node.prototype.getChannelForTransform = function( transformSid ) {
for ( var i = 0; i < this.channels.length; i ++ ) {
var channel = this.channels[i];
var parts = channel.target.split('/');
var id = parts.shift();
var sid = parts.shift();
var dotSyntax = (sid.indexOf(".") >= 0);
var arrSyntax = (sid.indexOf("(") >= 0);
var arrIndices;
var member;
if ( dotSyntax ) {
parts = sid.split(".");
sid = parts.shift();
member = parts.shift();
} else if ( arrSyntax ) {
arrIndices = sid.split("(");
sid = arrIndices.shift();
for ( var j = 0; j < arrIndices.length; j ++ ) {
arrIndices[ j ] = parseInt( arrIndices[ j ].replace( /\)/, '' ) );
}
}
if ( sid === transformSid ) {
channel.info = { sid: sid, dotSyntax: dotSyntax, arrSyntax: arrSyntax, arrIndices: arrIndices };
return channel;
}
}
return null;
};
Node.prototype.getChildById = function ( id, recursive ) {
if ( this.id === id ) {
return this;
}
if ( recursive ) {
for ( var i = 0; i < this.nodes.length; i ++ ) {
var n = this.nodes[ i ].getChildById( id, recursive );
if ( n ) {
return n;
}
}
}
return null;
};
Node.prototype.getChildBySid = function ( sid, recursive ) {
if ( this.sid === sid ) {
return this;
}
if ( recursive ) {
for ( var i = 0; i < this.nodes.length; i ++ ) {
var n = this.nodes[ i ].getChildBySid( sid, recursive );
if ( n ) {
return n;
}
}
}
return null;
};
Node.prototype.getTransformBySid = function ( sid ) {
for ( var i = 0; i < this.transforms.length; i ++ ) {
if ( this.transforms[ i ].sid === sid ) return this.transforms[ i ];
}
return null;
};
Node.prototype.parse = function( element ) {
var url;
this.id = element.getAttribute('id');
this.sid = element.getAttribute('sid');
this.name = element.getAttribute('name');
this.type = element.getAttribute('type');
this.layer = element.getAttribute('layer');
this.type = this.type === 'JOINT' ? this.type : 'NODE';
this.nodes = [];
this.transforms = [];
this.geometries = [];
this.cameras = [];
this.lights = [];
this.controllers = [];
this.matrix = new THREE.Matrix4();
for ( var i = 0; i < element.childNodes.length; i ++ ) {
var child = element.childNodes[ i ];
if ( child.nodeType != 1 ) continue;
switch ( child.nodeName ) {
case 'node':
this.nodes.push( ( new Node() ).parse( child ) );
break;
case 'instance_camera':
this.cameras.push( ( new InstanceCamera() ).parse( child ) );
break;
case 'instance_controller':
this.controllers.push( ( new InstanceController() ).parse( child ) );
break;
case 'instance_geometry':
this.geometries.push( ( new InstanceGeometry() ).parse( child ) );
break;
case 'instance_light':
this.lights.push( ( new InstanceLight() ).parse( child ) );
break;
case 'instance_node':
url = child.getAttribute( 'url' ).replace( /^#/, '' );
var iNode = getLibraryNode( url );
if ( iNode ) {
this.nodes.push( ( new Node() ).parse( iNode )) ;
}
break;
case 'rotate':
case 'translate':
case 'scale':
case 'matrix':
case 'lookat':
case 'skew':
this.transforms.push( ( new Transform() ).parse( child ) );
break;
case 'extra':
break;
default:
console.log( child.nodeName );
break;
}
}
this.channels = getChannelsForNode( this );
bakeAnimations( this );
this.updateMatrix();
return this;
};
Node.prototype.updateMatrix = function () {
this.matrix.identity();
for ( var i = 0; i < this.transforms.length; i ++ ) {
this.transforms[ i ].apply( this.matrix );
}
};
function Transform () {
this.sid = "";
this.type = "";
this.data = [];
this.obj = null;
}
Transform.prototype.parse = function ( element ) {
this.sid = element.getAttribute( 'sid' );
this.type = element.nodeName;
this.data = _floats( element.textContent );
this.convert();
return this;
};
Transform.prototype.convert = function () {
switch ( this.type ) {
case 'matrix':
this.obj = getConvertedMat4( this.data );
break;
case 'rotate':
this.angle = THREE.Math.degToRad( this.data[3] );
case 'translate':
fixCoords( this.data, -1 );
this.obj = new THREE.Vector3( this.data[ 0 ], this.data[ 1 ], this.data[ 2 ] );
break;
case 'scale':
fixCoords( this.data, 1 );
this.obj = new THREE.Vector3( this.data[ 0 ], this.data[ 1 ], this.data[ 2 ] );
break;
default:
console.log( 'Can not convert Transform of type ' + this.type );
break;
}
};
Transform.prototype.apply = function () {
var m1 = new THREE.Matrix4();
return function ( matrix ) {
switch ( this.type ) {
case 'matrix':
matrix.multiply( this.obj );
break;
case 'translate':
matrix.multiply( m1.makeTranslation( this.obj.x, this.obj.y, this.obj.z ) );
break;
case 'rotate':
matrix.multiply( m1.makeRotationAxis( this.obj, this.angle ) );
break;
case 'scale':
matrix.scale( this.obj );
break;
}
};
}();
Transform.prototype.update = function ( data, member ) {
var members = [ 'X', 'Y', 'Z', 'ANGLE' ];
switch ( this.type ) {
case 'matrix':
if ( ! member ) {
this.obj.copy( data );
} else if ( member.length === 1 ) {
switch ( member[ 0 ] ) {
case 0:
this.obj.n11 = data[ 0 ];
this.obj.n21 = data[ 1 ];
this.obj.n31 = data[ 2 ];
this.obj.n41 = data[ 3 ];
break;
case 1: