TransformControls.js 32 KB

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