1 /** 2 * @fileOverview Plugins for tQuery.Object3D to play with .position/.rotation/.scale 3 */ 4 5 ////////////////////////////////////////////////////////////////////////////////// 6 // position // 7 ////////////////////////////////////////////////////////////////////////////////// 8 9 tQuery.Object3D.registerInstance('position', function(vector3){ 10 // handle the getter 11 if( vector3 === undefined ) return this.get(0).position; 12 // handle parameters 13 if( typeof vector3 === "number" && arguments.length === 3 ){ 14 vector3 = new THREE.Vector3(arguments[0], arguments[1], arguments[2]); 15 } 16 console.assert(vector3 instanceof THREE.Vector3, "Object3D.position parameter error"); 17 // do the operation on each node 18 this.each(function(object3d){ 19 object3d.position.copy(vector3); 20 }); 21 // return this, to get chained API 22 return this; 23 }); 24 25 tQuery.Object3D.registerInstance('positionX', function(scalar){ 26 if( scalar === undefined ) return object.get(0).position.x; 27 console.assert(typeof scalar === "number" && arguments.length === 1); 28 this.each(function(object3d){ object3d.position.x = scalar; }); 29 return this; 30 }); 31 tQuery.Object3D.registerInstance('positionY', function(scalar){ 32 if( scalar === undefined ) return object.get(0).position.y; 33 console.assert(typeof scalar === "number" && arguments.length === 1); 34 this.each(function(object3d){ object3d.position.y = scalar; }); 35 return this; 36 }); 37 tQuery.Object3D.registerInstance('positionZ', function(scalar){ 38 if( scalar === undefined ) return object.get(0).position.z; 39 console.assert(typeof scalar === "number" && arguments.length === 1); 40 this.each(function(object3d){ object3d.position.z = scalar; }); 41 return this; 42 }); 43 44 tQuery.Object3D.registerInstance('translate', function(delta){ 45 // handle parameters 46 if( typeof delta === "number" && arguments.length === 3 ){ 47 delta = new THREE.Vector3(arguments[0], arguments[1], arguments[2]); 48 } 49 // sanity check 50 console.assert(delta instanceof THREE.Vector3, "Object3D.translate parameter error"); 51 // do the operation on each node 52 this.each(function(object3d){ 53 object3d.position.addSelf(delta); 54 }); 55 // return this, to get chained API 56 return this; 57 }); 58 59 // some shortcuts 60 tQuery.Object3D.registerInstance('translateX' , function(delta){ return this.translate(delta, 0, 0); }); 61 tQuery.Object3D.registerInstance('translateY' , function(delta){ return this.translate(0, delta, 0); }); 62 tQuery.Object3D.registerInstance('translateZ' , function(delta){ return this.translate(0, 0, delta); }); 63 64 65 ////////////////////////////////////////////////////////////////////////////////// 66 // rotation // 67 ////////////////////////////////////////////////////////////////////////////////// 68 69 tQuery.Object3D.registerInstance('rotation', function(vector3){ 70 // handle the getter 71 if( vector3 === undefined ) return this.get(0).rotation; 72 // handle parameters polymorphism 73 if( typeof vector3 === "number" && arguments.length === 3 ){ 74 vector3 = new THREE.Vector3(arguments[0], arguments[1], arguments[2]); 75 } 76 // sanity check 77 console.assert(vector3 instanceof THREE.Vector3, "Object3D.rotation parameter error"); 78 // do the operation on each node 79 this.each(function(object3d){ 80 object3d.rotation.copy(vector3); 81 }) 82 // return this, to get chained API 83 return this; 84 }); 85 86 tQuery.Object3D.registerInstance('rotationX', function(scalar){ 87 if( scalar === undefined ) return object.get(0).rotation.x; 88 console.assert(typeof scalar === "number" && arguments.length === 1); 89 this.each(function(object3d){ object3d.rotation.x = scalar; }); 90 return this; 91 }); 92 tQuery.Object3D.registerInstance('rotationY', function(scalar){ 93 if( scalar === undefined ) return object.get(0).rotation.y; 94 console.assert(typeof scalar === "number" && arguments.length === 1); 95 this.each(function(object3d){ object3d.rotation.y = scalar; }); 96 return this; 97 }); 98 tQuery.Object3D.registerInstance('rotationZ', function(scalar){ 99 if( scalar === undefined ) return object.get(0).rotation.z; 100 console.assert(typeof scalar === "number" && arguments.length === 1); 101 this.each(function(object3d){ object3d.rotation.z = scalar; }); 102 return this; 103 }); 104 105 tQuery.Object3D.registerInstance('rotate', function(angles){ 106 // handle parameter polymorphism 107 if( typeof angles === "number" && arguments.length === 3 ){ 108 angles = new THREE.Vector3(arguments[0], arguments[1], arguments[2]); 109 } 110 // sanity check 111 console.assert(angles instanceof THREE.Vector3, "Object3D.rotate parameter error"); 112 // do the operation on each node 113 this.each(function(object3d){ 114 object3d.rotation.addSelf(angles); 115 }) 116 // return this, to get chained API 117 return this; 118 }); 119 120 121 // some shortcuts 122 tQuery.Object3D.registerInstance('rotateX' , function(angle){ return this.rotate(angle, 0, 0); }); 123 tQuery.Object3D.registerInstance('rotateY' , function(angle){ return this.rotate(0, angle, 0); }); 124 tQuery.Object3D.registerInstance('rotateZ' , function(angle){ return this.rotate(0, 0, angle); }); 125 126 ////////////////////////////////////////////////////////////////////////////////// 127 // scale // 128 ////////////////////////////////////////////////////////////////////////////////// 129 130 tQuery.Object3D.registerInstance('scale', function(vector3){ 131 // handle the getter 132 if( vector3 === undefined ) return this.get(0).scale; 133 // handle parameters 134 if( typeof vector3 === "number" && arguments.length === 1 ){ 135 vector3 = new THREE.Vector3(vector3, vector3, vector3); 136 }else if( typeof vector3 === "number" && arguments.length === 3 ){ 137 vector3 = new THREE.Vector3(arguments[0], arguments[1], arguments[2]); 138 } 139 // sanity check 140 console.assert(vector3 instanceof THREE.Vector3, "Object3D.scale parameter error"); 141 // do the operation on each node 142 this.each(function(object3d){ 143 object3d.scale.copy(vector3); 144 }); 145 // return this, to get chained API 146 return this; 147 }); 148 149 tQuery.Object3D.registerInstance('scaleX', function(scalar){ 150 if( scalar === undefined ) return object.get(0).scale.x; 151 console.assert(typeof scalar === "number" && arguments.length === 1); 152 this.each(function(object3d){ object3d.scale.x = scalar; }); 153 return this; 154 }); 155 tQuery.Object3D.registerInstance('scaleY', function(scalar){ 156 if( scalar === undefined ) return object.get(0).scale.y; 157 console.assert(typeof scalar === "number" && arguments.length === 1); 158 this.each(function(object3d){ object3d.scale.y = scalar; }); 159 return this; 160 }); 161 tQuery.Object3D.registerInstance('scaleZ', function(scalar){ 162 if( scalar === undefined ) return object.get(0).scale.z; 163 console.assert(typeof scalar === "number" && arguments.length === 1); 164 this.each(function(object3d){ object3d.scale.z = scalar; }); 165 return this; 166 }); 167 168 tQuery.Object3D.registerInstance('scaleBy', function(ratio){ 169 // handle parameters 170 if( typeof ratio === "number" && arguments.length === 1 ){ 171 ratio = new THREE.Vector3(ratio, ratio, ratio); 172 }else if( typeof ratio === "number" && arguments.length === 3 ){ 173 ratio = new THREE.Vector3(arguments[0], arguments[1], arguments[2]); 174 } 175 // sanity check 176 console.assert(ratio instanceof THREE.Vector3, "Object3D.rotate parameter error"); 177 // do the operation on each node 178 this.each(function(object3d){ 179 object3d.scale.multiplySelf(ratio); 180 }) 181 // return this, to get chained API 182 return this; 183 }); 184 185 // some shortcuts 186 tQuery.Object3D.registerInstance('scaleXBy' , function(ratio){ return this.scaleBy(ratio, 1, 1); }); 187 tQuery.Object3D.registerInstance('scaleYBy' , function(ratio){ return this.scaleBy(1, ratio, 1); }); 188 tQuery.Object3D.registerInstance('scaleZBy' , function(ratio){ return this.scaleBy(1, 1, ratio); }); 189