// Eisung Sohn : essohn@gmail.com //----------------------------------------------------------------------------------- function SDCSConstraint (node) { this.node = node; this.focussed = false; this.active = false; this.init_position = new THREE.Vector3(); this.init_position.set(node.init_pos.x, node.init_pos.y, node.init_pos.z); this.position = new THREE.Vector3(); this.position.set(node.k_pos.x, node.k_pos.y, node.k_pos.z); this.fixed = true; this.rotation = new THREE.Vector3(); this.pos_weight = 10.0; this.rot_weight = 10.0; this.render_obj = null; } function SDCSConstraint_manager () { } SDCSConstraint_manager.prototype = { init: function (scene_manager) { this.scene_manager = scene_manager; this.scene = scene_manager.scene_selectable; this.model = scene_manager.model; }, load: function (init_pos, rotation, fixed, pos_weight, rot_weight) { var min_length = 10000000; var min_index = -1; for (var i=0; i< this.model.sdcs_data.cnodes.length; i++) { var node = this.model.sdcs_data.cnodes[i]; var len = (new THREE.Vector3( node.init_pos.x - init_pos.x, node.init_pos.y - init_pos.y, node.init_pos.z - init_pos.z)).length(); if (len < min_length) { min_index = i; min_length = len; } } if (min_index >= 0) { var c = this.create( this.model.sdcs_data.cnodes[min_index], fixed ); c.rotation.set( rotation.x, rotation.y, rotation.z ); c.pos_weight = pos_weight; c.rot_weight = rot_weight; } }, add_helper: function (constraint) { if ( constraint.render_object_circle ) { constraint.render_object.remove( constraint.render_object_circle ); constraint.render_object_circle = null; } if ( constraint.render_object_cross ) { constraint.render_object.remove( constraint.render_object_cross ); constraint.render_object_cross = null; } // add circle object var arcShape = new THREE.Shape(); arcShape.absarc( 0, 0, constraint.pos_weight / 2.0, 0, Math.PI*2, false ); var points = arcShape.createPointsGeometry(); var circle = new THREE.Line( points, new THREE.LineBasicMaterial( { color: 0xffff00, transparent: true, opacity: 0.5 } ) ); constraint.render_object.add( circle ); constraint.render_object_circle = circle; // add cross object var cross_len = constraint.rot_weight; var crossShape = new THREE.Shape(); crossShape.moveTo( 0, - cross_len/2 ); crossShape.lineTo( 0, cross_len/2 ); crossShape.moveTo( - cross_len/2, 0 ); crossShape.lineTo( cross_len/2, 0 ); var points = crossShape.createPointsGeometry(); var cross = new THREE.Line( points, new THREE.LineBasicMaterial( { color: 0x00ff00, transparent: true, opacity: 0.5 } ) ); constraint.render_object.add( cross ); constraint.render_object_cross = cross; }, create: function (node, fixed) { if (node) { var model = this.model; // check duplicate for (var i in model.sdcs_constraints) { if ( model.sdcs_constraints[i].node == node ) return; } model.sdcs_deformer.precomputed = false; var constraint = new SDCSConstraint( node ); if (fixed) { constraint.fixed = fixed; constraint.rot_weight = 10.0; } else { constraint.rot_weight = 0.0; } // add render object var geometry = new THREE.SphereGeometry( 1.0, 12, 12 ); var material = new THREE.MeshBasicMaterial( {transparent: true, color: 0xffff00} ); material.color.setHSL( 0.15, 1.0, 0.5 ); var render_obj = new THREE.Mesh( geometry, material ); render_obj.position.set(constraint.position.x, constraint.position.y, constraint.position.z); // user data render_obj.class = this.scene_manager.object_type.CONSTRAINT; render_obj.ref_object = constraint; constraint.render_object = render_obj; this.add_helper(constraint); // add constraint this.model.sdcs_constraints.push(constraint); // add to scene this.scene.add( render_obj ); console.log(this.model.sdcs_constraints); return constraint; } return null; }, delete: function (constraint) { if (constraint) { this.model.sdcs_deformer.precomputed = false; var index = -1; for (var i in this.model.sdcs_constraints) { if (this.model.sdcs_constraints[i] == constraint) { // remove from scene this.scene.remove( this.model.sdcs_constraints[i].render_object ); // remove from array this.model.sdcs_constraints.splice(i,1); break; } } console.log(this.model.sdcs_constraints); } }, clear: function () { this.model.sdcs_deformer.precomputed = false; while( this.model.sdcs_constraints.length > 0) { // remove from scene this.scene.remove( this.model.sdcs_constraints[0].render_object ); // remove from array this.model.sdcs_constraints.splice(0,1); } } }