TransformControls.js 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  1. /**
  2. * @author arodic / https://github.com/arodic
  3. */
  4. ( function () {
  5. 'use strict';
  6. var GizmoMaterial = function ( parameters ) {
  7. THREE.MeshBasicMaterial.call( this );
  8. this.depthTest = false;
  9. this.depthWrite = false;
  10. this.side = THREE.FrontSide;
  11. this.transparent = true;
  12. this.setValues( parameters );
  13. this.oldColor = this.color.clone();
  14. this.oldOpacity = this.opacity;
  15. this.highlight = function( highlighted ) {
  16. if ( highlighted ) {
  17. this.color.setRGB( 1, 1, 0 );
  18. this.opacity = 1;
  19. } else {
  20. this.color.copy( this.oldColor );
  21. this.opacity = this.oldOpacity;
  22. }
  23. };
  24. };
  25. GizmoMaterial.prototype = Object.create( THREE.MeshBasicMaterial.prototype );
  26. GizmoMaterial.prototype.constructor = GizmoMaterial;
  27. var GizmoLineMaterial = function ( parameters ) {
  28. THREE.LineBasicMaterial.call( this );
  29. this.depthTest = false;
  30. this.depthWrite = false;
  31. this.transparent = true;
  32. this.linewidth = 1;
  33. this.setValues( parameters );
  34. this.oldColor = this.color.clone();
  35. this.oldOpacity = this.opacity;
  36. this.highlight = function( highlighted ) {
  37. if ( highlighted ) {
  38. this.color.setRGB( 1, 1, 0 );
  39. this.opacity = 1;
  40. } else {
  41. this.color.copy( this.oldColor );
  42. this.opacity = this.oldOpacity;
  43. }
  44. };
  45. };
  46. GizmoLineMaterial.prototype = Object.create( THREE.LineBasicMaterial.prototype );
  47. GizmoLineMaterial.prototype.constructor = GizmoLineMaterial;
  48. var pickerMaterial = new GizmoMaterial( { visible: false, transparent: false } );
  49. THREE.TransformGizmo = function () {
  50. this.init = function () {
  51. THREE.Object3D.call( this );
  52. this.handles = new THREE.Object3D();
  53. this.pickers = new THREE.Object3D();
  54. this.planes = new THREE.Object3D();
  55. this.add( this.handles );
  56. this.add( this.pickers );
  57. this.add( this.planes );
  58. //// PLANES
  59. var planeGeometry = new THREE.PlaneBufferGeometry( 50, 50, 2, 2 );
  60. var planeMaterial = new THREE.MeshBasicMaterial( { visible: false, side: THREE.DoubleSide } );
  61. var planes = {
  62. "XY": new THREE.Mesh( planeGeometry, planeMaterial ),
  63. "YZ": new THREE.Mesh( planeGeometry, planeMaterial ),
  64. "XZ": new THREE.Mesh( planeGeometry, planeMaterial ),
  65. "XYZE": new THREE.Mesh( planeGeometry, planeMaterial )
  66. };
  67. this.activePlane = planes[ "XYZE" ];
  68. planes[ "YZ" ].rotation.set( 0, Math.PI / 2, 0 );
  69. planes[ "XZ" ].rotation.set( - Math.PI / 2, 0, 0 );
  70. for ( var i in planes ) {
  71. planes[ i ].name = i;
  72. this.planes.add( planes[ i ] );
  73. this.planes[ i ] = planes[ i ];
  74. }
  75. //// HANDLES AND PICKERS
  76. var setupGizmos = function( gizmoMap, parent ) {
  77. for ( var name in gizmoMap ) {
  78. for ( i = gizmoMap[ name ].length; i --; ) {
  79. var object = gizmoMap[ name ][ i ][ 0 ];
  80. var position = gizmoMap[ name ][ i ][ 1 ];
  81. var rotation = gizmoMap[ name ][ i ][ 2 ];
  82. object.name = name;
  83. if ( position ) object.position.set( position[ 0 ], position[ 1 ], position[ 2 ] );
  84. if ( rotation ) object.rotation.set( rotation[ 0 ], rotation[ 1 ], rotation[ 2 ] );
  85. parent.add( object );
  86. }
  87. }
  88. };
  89. setupGizmos( this.handleGizmos, this.handles );
  90. setupGizmos( this.pickerGizmos, this.pickers );
  91. // reset Transformations
  92. this.traverse( function ( child ) {
  93. if ( child instanceof THREE.Mesh ) {
  94. child.updateMatrix();
  95. var tempGeometry = child.geometry.clone();
  96. tempGeometry.applyMatrix( child.matrix );
  97. child.geometry = tempGeometry;
  98. child.position.set( 0, 0, 0 );
  99. child.rotation.set( 0, 0, 0 );
  100. child.scale.set( 1, 1, 1 );
  101. }
  102. } );
  103. };
  104. this.highlight = function ( axis ) {
  105. this.traverse( function( child ) {
  106. if ( child.material && child.material.highlight ) {
  107. if ( child.name === axis ) {
  108. child.material.highlight( true );
  109. } else {
  110. child.material.highlight( false );
  111. }
  112. }
  113. } );
  114. };
  115. };
  116. THREE.TransformGizmo.prototype = Object.create( THREE.Object3D.prototype );
  117. THREE.TransformGizmo.prototype.constructor = THREE.TransformGizmo;
  118. THREE.TransformGizmo.prototype.update = function ( rotation, eye ) {
  119. var vec1 = new THREE.Vector3( 0, 0, 0 );
  120. var vec2 = new THREE.Vector3( 0, 1, 0 );
  121. var lookAtMatrix = new THREE.Matrix4();
  122. this.traverse( function( child ) {
  123. if ( child.name.search( "E" ) !== - 1 ) {
  124. child.quaternion.setFromRotationMatrix( lookAtMatrix.lookAt( eye, vec1, vec2 ) );
  125. } else if ( child.name.search( "X" ) !== - 1 || child.name.search( "Y" ) !== - 1 || child.name.search( "Z" ) !== - 1 ) {
  126. child.quaternion.setFromEuler( rotation );
  127. }
  128. } );
  129. };
  130. THREE.TransformGizmoTranslate = function () {
  131. THREE.TransformGizmo.call( this );
  132. var arrowGeometry = new THREE.Geometry();
  133. var mesh = new THREE.Mesh( new THREE.CylinderGeometry( 0, 0.05, 0.2, 12, 1, false ) );
  134. mesh.position.y = 0.5;
  135. mesh.updateMatrix();
  136. arrowGeometry.merge( mesh.geometry, mesh.matrix );
  137. var lineXGeometry = new THREE.BufferGeometry();
  138. lineXGeometry.addAttribute( 'position', new THREE.Float32BufferAttribute( [ 0, 0, 0, 1, 0, 0 ], 3 ) );
  139. var lineYGeometry = new THREE.BufferGeometry();
  140. lineYGeometry.addAttribute( 'position', new THREE.Float32BufferAttribute( [ 0, 0, 0, 0, 1, 0 ], 3 ) );
  141. var lineZGeometry = new THREE.BufferGeometry();
  142. lineZGeometry.addAttribute( 'position', new THREE.Float32BufferAttribute( [ 0, 0, 0, 0, 0, 1 ], 3 ) );
  143. this.handleGizmos = {
  144. X: [
  145. [ new THREE.Mesh( arrowGeometry, new GizmoMaterial( { color: 0xff0000 } ) ), [ 0.5, 0, 0 ], [ 0, 0, - Math.PI / 2 ] ],
  146. [ new THREE.Line( lineXGeometry, new GizmoLineMaterial( { color: 0xff0000 } ) ) ]
  147. ],
  148. Y: [
  149. [ new THREE.Mesh( arrowGeometry, new GizmoMaterial( { color: 0x00ff00 } ) ), [ 0, 0.5, 0 ] ],
  150. [ new THREE.Line( lineYGeometry, new GizmoLineMaterial( { color: 0x00ff00 } ) ) ]
  151. ],
  152. Z: [
  153. [ new THREE.Mesh( arrowGeometry, new GizmoMaterial( { color: 0x0000ff } ) ), [ 0, 0, 0.5 ], [ Math.PI / 2, 0, 0 ] ],
  154. [ new THREE.Line( lineZGeometry, new GizmoLineMaterial( { color: 0x0000ff } ) ) ]
  155. ],
  156. XYZ: [
  157. [ new THREE.Mesh( new THREE.OctahedronGeometry( 0.1, 0 ), new GizmoMaterial( { color: 0xffffff, opacity: 0.25 } ) ), [ 0, 0, 0 ], [ 0, 0, 0 ] ]
  158. ],
  159. XY: [
  160. [ new THREE.Mesh( new THREE.PlaneBufferGeometry( 0.29, 0.29 ), new GizmoMaterial( { color: 0xffff00, opacity: 0.25 } ) ), [ 0.15, 0.15, 0 ] ]
  161. ],
  162. YZ: [
  163. [ new THREE.Mesh( new THREE.PlaneBufferGeometry( 0.29, 0.29 ), new GizmoMaterial( { color: 0x00ffff, opacity: 0.25 } ) ), [ 0, 0.15, 0.15 ], [ 0, Math.PI / 2, 0 ] ]
  164. ],
  165. XZ: [
  166. [ new THREE.Mesh( new THREE.PlaneBufferGeometry( 0.29, 0.29 ), new GizmoMaterial( { color: 0xff00ff, opacity: 0.25 } ) ), [ 0.15, 0, 0.15 ], [ - Math.PI / 2, 0, 0 ] ]
  167. ]
  168. };
  169. this.pickerGizmos = {
  170. X: [
  171. [ new THREE.Mesh( new THREE.CylinderBufferGeometry( 0.2, 0, 1, 4, 1, false ), pickerMaterial ), [ 0.6, 0, 0 ], [ 0, 0, - Math.PI / 2 ] ]
  172. ],
  173. Y: [
  174. [ new THREE.Mesh( new THREE.CylinderBufferGeometry( 0.2, 0, 1, 4, 1, false ), pickerMaterial ), [ 0, 0.6, 0 ] ]
  175. ],
  176. Z: [
  177. [ new THREE.Mesh( new THREE.CylinderBufferGeometry( 0.2, 0, 1, 4, 1, false ), pickerMaterial ), [ 0, 0, 0.6 ], [ Math.PI / 2, 0, 0 ] ]
  178. ],
  179. XYZ: [
  180. [ new THREE.Mesh( new THREE.OctahedronGeometry( 0.2, 0 ), pickerMaterial ) ]
  181. ],
  182. XY: [
  183. [ new THREE.Mesh( new THREE.PlaneBufferGeometry( 0.4, 0.4 ), pickerMaterial ), [ 0.2, 0.2, 0 ] ]
  184. ],
  185. YZ: [
  186. [ new THREE.Mesh( new THREE.PlaneBufferGeometry( 0.4, 0.4 ), pickerMaterial ), [ 0, 0.2, 0.2 ], [ 0, Math.PI / 2, 0 ] ]
  187. ],
  188. XZ: [
  189. [ new THREE.Mesh( new THREE.PlaneBufferGeometry( 0.4, 0.4 ), pickerMaterial ), [ 0.2, 0, 0.2 ], [ - Math.PI / 2, 0, 0 ] ]
  190. ]
  191. };
  192. this.setActivePlane = function ( axis, eye ) {
  193. var tempMatrix = new THREE.Matrix4();
  194. eye.applyMatrix4( tempMatrix.getInverse( tempMatrix.extractRotation( this.planes[ "XY" ].matrixWorld ) ) );
  195. if ( axis === "X" ) {
  196. this.activePlane = this.planes[ "XY" ];
  197. if ( Math.abs( eye.y ) > Math.abs( eye.z ) ) this.activePlane = this.planes[ "XZ" ];
  198. }
  199. if ( axis === "Y" ) {
  200. this.activePlane = this.planes[ "XY" ];
  201. if ( Math.abs( eye.x ) > Math.abs( eye.z ) ) this.activePlane = this.planes[ "YZ" ];
  202. }
  203. if ( axis === "Z" ) {
  204. this.activePlane = this.planes[ "XZ" ];
  205. if ( Math.abs( eye.x ) > Math.abs( eye.y ) ) this.activePlane = this.planes[ "YZ" ];
  206. }
  207. if ( axis === "XYZ" ) this.activePlane = this.planes[ "XYZE" ];
  208. if ( axis === "XY" ) this.activePlane = this.planes[ "XY" ];
  209. if ( axis === "YZ" ) this.activePlane = this.planes[ "YZ" ];
  210. if ( axis === "XZ" ) this.activePlane = this.planes[ "XZ" ];
  211. };
  212. this.init();
  213. };
  214. THREE.TransformGizmoTranslate.prototype = Object.create( THREE.TransformGizmo.prototype );
  215. THREE.TransformGizmoTranslate.prototype.constructor = THREE.TransformGizmoTranslate;
  216. THREE.TransformGizmoRotate = function () {
  217. THREE.TransformGizmo.call( this );
  218. var CircleGeometry = function ( radius, facing, arc ) {
  219. var geometry = new THREE.BufferGeometry();
  220. var vertices = [];
  221. arc = arc ? arc : 1;
  222. for ( var i = 0; i <= 64 * arc; ++ i ) {
  223. if ( facing === 'x' ) vertices.push( 0, Math.cos( i / 32 * Math.PI ) * radius, Math.sin( i / 32 * Math.PI ) * radius );
  224. if ( facing === 'y' ) vertices.push( Math.cos( i / 32 * Math.PI ) * radius, 0, Math.sin( i / 32 * Math.PI ) * radius );
  225. if ( facing === 'z' ) vertices.push( Math.sin( i / 32 * Math.PI ) * radius, Math.cos( i / 32 * Math.PI ) * radius, 0 );
  226. }
  227. geometry.addAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  228. return geometry;
  229. };
  230. this.handleGizmos = {
  231. X: [
  232. [ new THREE.Line( new CircleGeometry( 1, 'x', 0.5 ), new GizmoLineMaterial( { color: 0xff0000 } ) ) ]
  233. ],
  234. Y: [
  235. [ new THREE.Line( new CircleGeometry( 1, 'y', 0.5 ), new GizmoLineMaterial( { color: 0x00ff00 } ) ) ]
  236. ],
  237. Z: [
  238. [ new THREE.Line( new CircleGeometry( 1, 'z', 0.5 ), new GizmoLineMaterial( { color: 0x0000ff } ) ) ]
  239. ],
  240. E: [
  241. [ new THREE.Line( new CircleGeometry( 1.25, 'z', 1 ), new GizmoLineMaterial( { color: 0xcccc00 } ) ) ]
  242. ],
  243. XYZE: [
  244. [ new THREE.Line( new CircleGeometry( 1, 'z', 1 ), new GizmoLineMaterial( { color: 0x787878 } ) ) ]
  245. ]
  246. };
  247. this.pickerGizmos = {
  248. X: [
  249. [ new THREE.Mesh( new THREE.TorusBufferGeometry( 1, 0.12, 4, 12, Math.PI ), pickerMaterial ), [ 0, 0, 0 ], [ 0, - Math.PI / 2, - Math.PI / 2 ] ]
  250. ],
  251. Y: [
  252. [ new THREE.Mesh( new THREE.TorusBufferGeometry( 1, 0.12, 4, 12, Math.PI ), pickerMaterial ), [ 0, 0, 0 ], [ Math.PI / 2, 0, 0 ] ]
  253. ],
  254. Z: [
  255. [ new THREE.Mesh( new THREE.TorusBufferGeometry( 1, 0.12, 4, 12, Math.PI ), pickerMaterial ), [ 0, 0, 0 ], [ 0, 0, - Math.PI / 2 ] ]
  256. ],
  257. E: [
  258. [ new THREE.Mesh( new THREE.TorusBufferGeometry( 1.25, 0.12, 2, 24 ), pickerMaterial ) ]
  259. ],
  260. XYZE: [
  261. [ new THREE.Mesh() ]// TODO
  262. ]
  263. };
  264. this.setActivePlane = function ( axis ) {
  265. if ( axis === "E" ) this.activePlane = this.planes[ "XYZE" ];
  266. if ( axis === "X" ) this.activePlane = this.planes[ "YZ" ];
  267. if ( axis === "Y" ) this.activePlane = this.planes[ "XZ" ];
  268. if ( axis === "Z" ) this.activePlane = this.planes[ "XY" ];
  269. };
  270. this.update = function ( rotation, eye2 ) {
  271. THREE.TransformGizmo.prototype.update.apply( this, arguments );
  272. var group = {
  273. handles: this[ "handles" ],
  274. pickers: this[ "pickers" ]
  275. };
  276. var tempMatrix = new THREE.Matrix4();
  277. var worldRotation = new THREE.Euler( 0, 0, 1 );
  278. var tempQuaternion = new THREE.Quaternion();
  279. var unitX = new THREE.Vector3( 1, 0, 0 );
  280. var unitY = new THREE.Vector3( 0, 1, 0 );
  281. var unitZ = new THREE.Vector3( 0, 0, 1 );
  282. var quaternionX = new THREE.Quaternion();
  283. var quaternionY = new THREE.Quaternion();
  284. var quaternionZ = new THREE.Quaternion();
  285. var eye = eye2.clone();
  286. worldRotation.copy( this.planes[ "XY" ].rotation );
  287. tempQuaternion.setFromEuler( worldRotation );
  288. tempMatrix.makeRotationFromQuaternion( tempQuaternion ).getInverse( tempMatrix );
  289. eye.applyMatrix4( tempMatrix );
  290. this.traverse( function( child ) {
  291. tempQuaternion.setFromEuler( worldRotation );
  292. if ( child.name === "X" ) {
  293. quaternionX.setFromAxisAngle( unitX, Math.atan2( - eye.y, eye.z ) );
  294. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionX );
  295. child.quaternion.copy( tempQuaternion );
  296. }
  297. if ( child.name === "Y" ) {
  298. quaternionY.setFromAxisAngle( unitY, Math.atan2( eye.x, eye.z ) );
  299. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionY );
  300. child.quaternion.copy( tempQuaternion );
  301. }
  302. if ( child.name === "Z" ) {
  303. quaternionZ.setFromAxisAngle( unitZ, Math.atan2( eye.y, eye.x ) );
  304. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionZ );
  305. child.quaternion.copy( tempQuaternion );
  306. }
  307. } );
  308. };
  309. this.init();
  310. };
  311. THREE.TransformGizmoRotate.prototype = Object.create( THREE.TransformGizmo.prototype );
  312. THREE.TransformGizmoRotate.prototype.constructor = THREE.TransformGizmoRotate;
  313. THREE.TransformGizmoScale = function () {
  314. THREE.TransformGizmo.call( this );
  315. var arrowGeometry = new THREE.Geometry();
  316. var mesh = new THREE.Mesh( new THREE.BoxGeometry( 0.125, 0.125, 0.125 ) );
  317. mesh.position.y = 0.5;
  318. mesh.updateMatrix();
  319. arrowGeometry.merge( mesh.geometry, mesh.matrix );
  320. var lineXGeometry = new THREE.BufferGeometry();
  321. lineXGeometry.addAttribute( 'position', new THREE.Float32BufferAttribute( [ 0, 0, 0, 1, 0, 0 ], 3 ) );
  322. var lineYGeometry = new THREE.BufferGeometry();
  323. lineYGeometry.addAttribute( 'position', new THREE.Float32BufferAttribute( [ 0, 0, 0, 0, 1, 0 ], 3 ) );
  324. var lineZGeometry = new THREE.BufferGeometry();
  325. lineZGeometry.addAttribute( 'position', new THREE.Float32BufferAttribute( [ 0, 0, 0, 0, 0, 1 ], 3 ) );
  326. this.handleGizmos = {
  327. X: [
  328. [ new THREE.Mesh( arrowGeometry, new GizmoMaterial( { color: 0xff0000 } ) ), [ 0.5, 0, 0 ], [ 0, 0, - Math.PI / 2 ] ],
  329. [ new THREE.Line( lineXGeometry, new GizmoLineMaterial( { color: 0xff0000 } ) ) ]
  330. ],
  331. Y: [
  332. [ new THREE.Mesh( arrowGeometry, new GizmoMaterial( { color: 0x00ff00 } ) ), [ 0, 0.5, 0 ] ],
  333. [ new THREE.Line( lineYGeometry, new GizmoLineMaterial( { color: 0x00ff00 } ) ) ]
  334. ],
  335. Z: [
  336. [ new THREE.Mesh( arrowGeometry, new GizmoMaterial( { color: 0x0000ff } ) ), [ 0, 0, 0.5 ], [ Math.PI / 2, 0, 0 ] ],
  337. [ new THREE.Line( lineZGeometry, new GizmoLineMaterial( { color: 0x0000ff } ) ) ]
  338. ],
  339. XYZ: [
  340. [ new THREE.Mesh( new THREE.BoxBufferGeometry( 0.125, 0.125, 0.125 ), new GizmoMaterial( { color: 0xffffff, opacity: 0.25 } ) ) ]
  341. ]
  342. };
  343. this.pickerGizmos = {
  344. X: [
  345. [ new THREE.Mesh( new THREE.CylinderBufferGeometry( 0.2, 0, 1, 4, 1, false ), pickerMaterial ), [ 0.6, 0, 0 ], [ 0, 0, - Math.PI / 2 ] ]
  346. ],
  347. Y: [
  348. [ new THREE.Mesh( new THREE.CylinderBufferGeometry( 0.2, 0, 1, 4, 1, false ), pickerMaterial ), [ 0, 0.6, 0 ] ]
  349. ],
  350. Z: [
  351. [ new THREE.Mesh( new THREE.CylinderBufferGeometry( 0.2, 0, 1, 4, 1, false ), pickerMaterial ), [ 0, 0, 0.6 ], [ Math.PI / 2, 0, 0 ] ]
  352. ],
  353. XYZ: [
  354. [ new THREE.Mesh( new THREE.BoxBufferGeometry( 0.4, 0.4, 0.4 ), pickerMaterial ) ]
  355. ]
  356. };
  357. this.setActivePlane = function ( axis, eye ) {
  358. var tempMatrix = new THREE.Matrix4();
  359. eye.applyMatrix4( tempMatrix.getInverse( tempMatrix.extractRotation( this.planes[ "XY" ].matrixWorld ) ) );
  360. if ( axis === "X" ) {
  361. this.activePlane = this.planes[ "XY" ];
  362. if ( Math.abs( eye.y ) > Math.abs( eye.z ) ) this.activePlane = this.planes[ "XZ" ];
  363. }
  364. if ( axis === "Y" ) {
  365. this.activePlane = this.planes[ "XY" ];
  366. if ( Math.abs( eye.x ) > Math.abs( eye.z ) ) this.activePlane = this.planes[ "YZ" ];
  367. }
  368. if ( axis === "Z" ) {
  369. this.activePlane = this.planes[ "XZ" ];
  370. if ( Math.abs( eye.x ) > Math.abs( eye.y ) ) this.activePlane = this.planes[ "YZ" ];
  371. }
  372. if ( axis === "XYZ" ) this.activePlane = this.planes[ "XYZE" ];
  373. };
  374. this.init();
  375. };
  376. THREE.TransformGizmoScale.prototype = Object.create( THREE.TransformGizmo.prototype );
  377. THREE.TransformGizmoScale.prototype.constructor = THREE.TransformGizmoScale;
  378. THREE.TransformControls = function ( camera, domElement ) {
  379. // TODO: Make non-uniform scale and rotate play nice in hierarchies
  380. // TODO: ADD RXYZ contol
  381. THREE.Object3D.call( this );
  382. domElement = ( domElement !== undefined ) ? domElement : document;
  383. this.object = undefined;
  384. this.visible = false;
  385. this.translationSnap = null;
  386. this.rotationSnap = null;
  387. this.space = "world";
  388. this.size = 1;
  389. this.axis = null;
  390. var scope = this;
  391. var _mode = "translate";
  392. var _dragging = false;
  393. var _plane = "XY";
  394. var _gizmo = {
  395. "translate": new THREE.TransformGizmoTranslate(),
  396. "rotate": new THREE.TransformGizmoRotate(),
  397. "scale": new THREE.TransformGizmoScale()
  398. };
  399. for ( var type in _gizmo ) {
  400. var gizmoObj = _gizmo[ type ];
  401. gizmoObj.visible = ( type === _mode );
  402. this.add( gizmoObj );
  403. }
  404. var changeEvent = { type: "change" };
  405. var mouseDownEvent = { type: "mouseDown" };
  406. var mouseUpEvent = { type: "mouseUp", mode: _mode };
  407. var objectChangeEvent = { type: "objectChange" };
  408. var ray = new THREE.Raycaster();
  409. var pointerVector = new THREE.Vector2();
  410. var point = new THREE.Vector3();
  411. var offset = new THREE.Vector3();
  412. var rotation = new THREE.Vector3();
  413. var offsetRotation = new THREE.Vector3();
  414. var scale = 1;
  415. var lookAtMatrix = new THREE.Matrix4();
  416. var eye = new THREE.Vector3();
  417. var tempMatrix = new THREE.Matrix4();
  418. var tempVector = new THREE.Vector3();
  419. var tempQuaternion = new THREE.Quaternion();
  420. var unitX = new THREE.Vector3( 1, 0, 0 );
  421. var unitY = new THREE.Vector3( 0, 1, 0 );
  422. var unitZ = new THREE.Vector3( 0, 0, 1 );
  423. var quaternionXYZ = new THREE.Quaternion();
  424. var quaternionX = new THREE.Quaternion();
  425. var quaternionY = new THREE.Quaternion();
  426. var quaternionZ = new THREE.Quaternion();
  427. var quaternionE = new THREE.Quaternion();
  428. var oldPosition = new THREE.Vector3();
  429. var oldScale = new THREE.Vector3();
  430. var oldRotationMatrix = new THREE.Matrix4();
  431. var parentRotationMatrix = new THREE.Matrix4();
  432. var parentScale = new THREE.Vector3();
  433. var worldPosition = new THREE.Vector3();
  434. var worldRotation = new THREE.Euler();
  435. var worldRotationMatrix = new THREE.Matrix4();
  436. var camPosition = new THREE.Vector3();
  437. var camRotation = new THREE.Euler();
  438. domElement.addEventListener( "mousedown", onPointerDown, false );
  439. domElement.addEventListener( "touchstart", onPointerDown, false );
  440. domElement.addEventListener( "mousemove", onPointerHover, false );
  441. domElement.addEventListener( "touchmove", onPointerHover, false );
  442. domElement.addEventListener( "mousemove", onPointerMove, false );
  443. domElement.addEventListener( "touchmove", onPointerMove, false );
  444. domElement.addEventListener( "mouseup", onPointerUp, false );
  445. domElement.addEventListener( "mouseout", onPointerUp, false );
  446. domElement.addEventListener( "touchend", onPointerUp, false );
  447. domElement.addEventListener( "touchcancel", onPointerUp, false );
  448. domElement.addEventListener( "touchleave", onPointerUp, false );
  449. this.dispose = function () {
  450. domElement.removeEventListener( "mousedown", onPointerDown );
  451. domElement.removeEventListener( "touchstart", onPointerDown );
  452. domElement.removeEventListener( "mousemove", onPointerHover );
  453. domElement.removeEventListener( "touchmove", onPointerHover );
  454. domElement.removeEventListener( "mousemove", onPointerMove );
  455. domElement.removeEventListener( "touchmove", onPointerMove );
  456. domElement.removeEventListener( "mouseup", onPointerUp );
  457. domElement.removeEventListener( "mouseout", onPointerUp );
  458. domElement.removeEventListener( "touchend", onPointerUp );
  459. domElement.removeEventListener( "touchcancel", onPointerUp );
  460. domElement.removeEventListener( "touchleave", onPointerUp );
  461. };
  462. this.attach = function ( object ) {
  463. this.object = object;
  464. this.visible = true;
  465. this.update();
  466. };
  467. this.detach = function () {
  468. this.object = undefined;
  469. this.visible = false;
  470. this.axis = null;
  471. };
  472. this.getMode = function () {
  473. return _mode;
  474. };
  475. this.setMode = function ( mode ) {
  476. _mode = mode ? mode : _mode;
  477. if ( _mode === "scale" ) scope.space = "local";
  478. for ( var type in _gizmo ) _gizmo[ type ].visible = ( type === _mode );
  479. this.update();
  480. scope.dispatchEvent( changeEvent );
  481. };
  482. this.setTranslationSnap = function ( translationSnap ) {
  483. scope.translationSnap = translationSnap;
  484. };
  485. this.setRotationSnap = function ( rotationSnap ) {
  486. scope.rotationSnap = rotationSnap;
  487. };
  488. this.setSize = function ( size ) {
  489. scope.size = size;
  490. this.update();
  491. scope.dispatchEvent( changeEvent );
  492. };
  493. this.setSpace = function ( space ) {
  494. scope.space = space;
  495. this.update();
  496. scope.dispatchEvent( changeEvent );
  497. };
  498. this.update = function () {
  499. if ( scope.object === undefined ) return;
  500. scope.object.updateMatrixWorld();
  501. worldPosition.setFromMatrixPosition( scope.object.matrixWorld );
  502. worldRotation.setFromRotationMatrix( tempMatrix.extractRotation( scope.object.matrixWorld ) );
  503. camera.updateMatrixWorld();
  504. camPosition.setFromMatrixPosition( camera.matrixWorld );
  505. camRotation.setFromRotationMatrix( tempMatrix.extractRotation( camera.matrixWorld ) );
  506. scale = worldPosition.distanceTo( camPosition ) / 6 * scope.size;
  507. this.position.copy( worldPosition );
  508. this.scale.set( scale, scale, scale );
  509. if ( camera instanceof THREE.PerspectiveCamera ) {
  510. eye.copy( camPosition ).sub( worldPosition ).normalize();
  511. } else if ( camera instanceof THREE.OrthographicCamera ) {
  512. eye.copy( camPosition ).normalize();
  513. }
  514. if ( scope.space === "local" ) {
  515. _gizmo[ _mode ].update( worldRotation, eye );
  516. } else if ( scope.space === "world" ) {
  517. _gizmo[ _mode ].update( new THREE.Euler(), eye );
  518. }
  519. _gizmo[ _mode ].highlight( scope.axis );
  520. };
  521. function onPointerHover( event ) {
  522. if ( scope.object === undefined || _dragging === true || ( event.button !== undefined && event.button !== 0 ) ) return;
  523. var pointer = event.changedTouches ? event.changedTouches[ 0 ] : event;
  524. var intersect = intersectObjects( pointer, _gizmo[ _mode ].pickers.children );
  525. var axis = null;
  526. if ( intersect ) {
  527. axis = intersect.object.name;
  528. event.preventDefault();
  529. }
  530. if ( scope.axis !== axis ) {
  531. scope.axis = axis;
  532. scope.update();
  533. scope.dispatchEvent( changeEvent );
  534. }
  535. }
  536. function onPointerDown( event ) {
  537. if ( scope.object === undefined || _dragging === true || ( event.button !== undefined && event.button !== 0 ) ) return;
  538. var pointer = event.changedTouches ? event.changedTouches[ 0 ] : event;
  539. if ( pointer.button === 0 || pointer.button === undefined ) {
  540. var intersect = intersectObjects( pointer, _gizmo[ _mode ].pickers.children );
  541. if ( intersect ) {
  542. event.preventDefault();
  543. event.stopPropagation();
  544. scope.dispatchEvent( mouseDownEvent );
  545. scope.axis = intersect.object.name;
  546. scope.update();
  547. eye.copy( camPosition ).sub( worldPosition ).normalize();
  548. _gizmo[ _mode ].setActivePlane( scope.axis, eye );
  549. var planeIntersect = intersectObjects( pointer, [ _gizmo[ _mode ].activePlane ] );
  550. if ( planeIntersect ) {
  551. oldPosition.copy( scope.object.position );
  552. oldScale.copy( scope.object.scale );
  553. oldRotationMatrix.extractRotation( scope.object.matrix );
  554. worldRotationMatrix.extractRotation( scope.object.matrixWorld );
  555. parentRotationMatrix.extractRotation( scope.object.parent.matrixWorld );
  556. parentScale.setFromMatrixScale( tempMatrix.getInverse( scope.object.parent.matrixWorld ) );
  557. offset.copy( planeIntersect.point );
  558. }
  559. }
  560. }
  561. _dragging = true;
  562. }
  563. function onPointerMove( event ) {
  564. if ( scope.object === undefined || scope.axis === null || _dragging === false || ( event.button !== undefined && event.button !== 0 ) ) return;
  565. var pointer = event.changedTouches ? event.changedTouches[ 0 ] : event;
  566. var planeIntersect = intersectObjects( pointer, [ _gizmo[ _mode ].activePlane ] );
  567. if ( planeIntersect === false ) return;
  568. event.preventDefault();
  569. event.stopPropagation();
  570. point.copy( planeIntersect.point );
  571. if ( _mode === "translate" ) {
  572. point.sub( offset );
  573. point.multiply( parentScale );
  574. if ( scope.space === "local" ) {
  575. point.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
  576. if ( scope.axis.search( "X" ) === - 1 ) point.x = 0;
  577. if ( scope.axis.search( "Y" ) === - 1 ) point.y = 0;
  578. if ( scope.axis.search( "Z" ) === - 1 ) point.z = 0;
  579. point.applyMatrix4( oldRotationMatrix );
  580. scope.object.position.copy( oldPosition );
  581. scope.object.position.add( point );
  582. }
  583. if ( scope.space === "world" || scope.axis.search( "XYZ" ) !== - 1 ) {
  584. if ( scope.axis.search( "X" ) === - 1 ) point.x = 0;
  585. if ( scope.axis.search( "Y" ) === - 1 ) point.y = 0;
  586. if ( scope.axis.search( "Z" ) === - 1 ) point.z = 0;
  587. point.applyMatrix4( tempMatrix.getInverse( parentRotationMatrix ) );
  588. scope.object.position.copy( oldPosition );
  589. scope.object.position.add( point );
  590. }
  591. if ( scope.translationSnap !== null ) {
  592. if ( scope.space === "local" ) {
  593. scope.object.position.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
  594. }
  595. if ( scope.axis.search( "X" ) !== - 1 ) scope.object.position.x = Math.round( scope.object.position.x / scope.translationSnap ) * scope.translationSnap;
  596. if ( scope.axis.search( "Y" ) !== - 1 ) scope.object.position.y = Math.round( scope.object.position.y / scope.translationSnap ) * scope.translationSnap;
  597. if ( scope.axis.search( "Z" ) !== - 1 ) scope.object.position.z = Math.round( scope.object.position.z / scope.translationSnap ) * scope.translationSnap;
  598. if ( scope.space === "local" ) {
  599. scope.object.position.applyMatrix4( worldRotationMatrix );
  600. }
  601. }
  602. } else if ( _mode === "scale" ) {
  603. point.sub( offset );
  604. point.multiply( parentScale );
  605. if ( scope.space === "local" ) {
  606. if ( scope.axis === "XYZ" ) {
  607. scale = 1 + ( ( point.y ) / Math.max( oldScale.x, oldScale.y, oldScale.z ) );
  608. scope.object.scale.x = oldScale.x * scale;
  609. scope.object.scale.y = oldScale.y * scale;
  610. scope.object.scale.z = oldScale.z * scale;
  611. } else {
  612. point.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
  613. if ( scope.axis === "X" ) scope.object.scale.x = oldScale.x * ( 1 + point.x / oldScale.x );
  614. if ( scope.axis === "Y" ) scope.object.scale.y = oldScale.y * ( 1 + point.y / oldScale.y );
  615. if ( scope.axis === "Z" ) scope.object.scale.z = oldScale.z * ( 1 + point.z / oldScale.z );
  616. }
  617. }
  618. } else if ( _mode === "rotate" ) {
  619. point.sub( worldPosition );
  620. point.multiply( parentScale );
  621. tempVector.copy( offset ).sub( worldPosition );
  622. tempVector.multiply( parentScale );
  623. if ( scope.axis === "E" ) {
  624. point.applyMatrix4( tempMatrix.getInverse( lookAtMatrix ) );
  625. tempVector.applyMatrix4( tempMatrix.getInverse( lookAtMatrix ) );
  626. rotation.set( Math.atan2( point.z, point.y ), Math.atan2( point.x, point.z ), Math.atan2( point.y, point.x ) );
  627. offsetRotation.set( Math.atan2( tempVector.z, tempVector.y ), Math.atan2( tempVector.x, tempVector.z ), Math.atan2( tempVector.y, tempVector.x ) );
  628. tempQuaternion.setFromRotationMatrix( tempMatrix.getInverse( parentRotationMatrix ) );
  629. quaternionE.setFromAxisAngle( eye, rotation.z - offsetRotation.z );
  630. quaternionXYZ.setFromRotationMatrix( worldRotationMatrix );
  631. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionE );
  632. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionXYZ );
  633. scope.object.quaternion.copy( tempQuaternion );
  634. } else if ( scope.axis === "XYZE" ) {
  635. quaternionE.setFromEuler( point.clone().cross( tempVector ).normalize() ); // rotation axis
  636. tempQuaternion.setFromRotationMatrix( tempMatrix.getInverse( parentRotationMatrix ) );
  637. quaternionX.setFromAxisAngle( quaternionE, - point.clone().angleTo( tempVector ) );
  638. quaternionXYZ.setFromRotationMatrix( worldRotationMatrix );
  639. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionX );
  640. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionXYZ );
  641. scope.object.quaternion.copy( tempQuaternion );
  642. } else if ( scope.space === "local" ) {
  643. point.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
  644. tempVector.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
  645. rotation.set( Math.atan2( point.z, point.y ), Math.atan2( point.x, point.z ), Math.atan2( point.y, point.x ) );
  646. offsetRotation.set( Math.atan2( tempVector.z, tempVector.y ), Math.atan2( tempVector.x, tempVector.z ), Math.atan2( tempVector.y, tempVector.x ) );
  647. quaternionXYZ.setFromRotationMatrix( oldRotationMatrix );
  648. if ( scope.rotationSnap !== null ) {
  649. quaternionX.setFromAxisAngle( unitX, Math.round( ( rotation.x - offsetRotation.x ) / scope.rotationSnap ) * scope.rotationSnap );
  650. quaternionY.setFromAxisAngle( unitY, Math.round( ( rotation.y - offsetRotation.y ) / scope.rotationSnap ) * scope.rotationSnap );
  651. quaternionZ.setFromAxisAngle( unitZ, Math.round( ( rotation.z - offsetRotation.z ) / scope.rotationSnap ) * scope.rotationSnap );
  652. } else {
  653. quaternionX.setFromAxisAngle( unitX, rotation.x - offsetRotation.x );
  654. quaternionY.setFromAxisAngle( unitY, rotation.y - offsetRotation.y );
  655. quaternionZ.setFromAxisAngle( unitZ, rotation.z - offsetRotation.z );
  656. }
  657. if ( scope.axis === "X" ) quaternionXYZ.multiplyQuaternions( quaternionXYZ, quaternionX );
  658. if ( scope.axis === "Y" ) quaternionXYZ.multiplyQuaternions( quaternionXYZ, quaternionY );
  659. if ( scope.axis === "Z" ) quaternionXYZ.multiplyQuaternions( quaternionXYZ, quaternionZ );
  660. scope.object.quaternion.copy( quaternionXYZ );
  661. } else if ( scope.space === "world" ) {
  662. rotation.set( Math.atan2( point.z, point.y ), Math.atan2( point.x, point.z ), Math.atan2( point.y, point.x ) );
  663. offsetRotation.set( Math.atan2( tempVector.z, tempVector.y ), Math.atan2( tempVector.x, tempVector.z ), Math.atan2( tempVector.y, tempVector.x ) );
  664. tempQuaternion.setFromRotationMatrix( tempMatrix.getInverse( parentRotationMatrix ) );
  665. if ( scope.rotationSnap !== null ) {
  666. quaternionX.setFromAxisAngle( unitX, Math.round( ( rotation.x - offsetRotation.x ) / scope.rotationSnap ) * scope.rotationSnap );
  667. quaternionY.setFromAxisAngle( unitY, Math.round( ( rotation.y - offsetRotation.y ) / scope.rotationSnap ) * scope.rotationSnap );
  668. quaternionZ.setFromAxisAngle( unitZ, Math.round( ( rotation.z - offsetRotation.z ) / scope.rotationSnap ) * scope.rotationSnap );
  669. } else {
  670. quaternionX.setFromAxisAngle( unitX, rotation.x - offsetRotation.x );
  671. quaternionY.setFromAxisAngle( unitY, rotation.y - offsetRotation.y );
  672. quaternionZ.setFromAxisAngle( unitZ, rotation.z - offsetRotation.z );
  673. }
  674. quaternionXYZ.setFromRotationMatrix( worldRotationMatrix );
  675. if ( scope.axis === "X" ) tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionX );
  676. if ( scope.axis === "Y" ) tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionY );
  677. if ( scope.axis === "Z" ) tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionZ );
  678. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionXYZ );
  679. scope.object.quaternion.copy( tempQuaternion );
  680. }
  681. }
  682. scope.update();
  683. scope.dispatchEvent( changeEvent );
  684. scope.dispatchEvent( objectChangeEvent );
  685. }
  686. function onPointerUp( event ) {
  687. event.preventDefault(); // Prevent MouseEvent on mobile
  688. if ( event.button !== undefined && event.button !== 0 ) return;
  689. if ( _dragging && ( scope.axis !== null ) ) {
  690. mouseUpEvent.mode = _mode;
  691. scope.dispatchEvent( mouseUpEvent );
  692. }
  693. _dragging = false;
  694. if ( 'TouchEvent' in window && event instanceof TouchEvent ) {
  695. // Force "rollover"
  696. scope.axis = null;
  697. scope.update();
  698. scope.dispatchEvent( changeEvent );
  699. } else {
  700. onPointerHover( event );
  701. }
  702. }
  703. function intersectObjects( pointer, objects ) {
  704. var rect = domElement.getBoundingClientRect();
  705. var x = ( pointer.clientX - rect.left ) / rect.width;
  706. var y = ( pointer.clientY - rect.top ) / rect.height;
  707. pointerVector.set( ( x * 2 ) - 1, - ( y * 2 ) + 1 );
  708. ray.setFromCamera( pointerVector, camera );
  709. var intersections = ray.intersectObjects( objects, true );
  710. return intersections[ 0 ] ? intersections[ 0 ] : false;
  711. }
  712. };
  713. THREE.TransformControls.prototype = Object.create( THREE.Object3D.prototype );
  714. THREE.TransformControls.prototype.constructor = THREE.TransformControls;
  715. }() );