TransformControls.js 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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. //always render on top of the scene (avoid being hidden by transparent objects)
  86. object.renderOrder = Infinity;
  87. if ( position ) object.position.set( position[ 0 ], position[ 1 ], position[ 2 ] );
  88. if ( rotation ) object.rotation.set( rotation[ 0 ], rotation[ 1 ], rotation[ 2 ] );
  89. parent.add( object );
  90. }
  91. }
  92. };
  93. setupGizmos( this.handleGizmos, this.handles );
  94. setupGizmos( this.pickerGizmos, this.pickers );
  95. // reset Transformations
  96. this.traverse( function ( child ) {
  97. if ( child instanceof THREE.Mesh ) {
  98. child.updateMatrix();
  99. var tempGeometry = child.geometry.clone();
  100. tempGeometry.applyMatrix( child.matrix );
  101. child.geometry = tempGeometry;
  102. child.position.set( 0, 0, 0 );
  103. child.rotation.set( 0, 0, 0 );
  104. child.scale.set( 1, 1, 1 );
  105. }
  106. } );
  107. };
  108. this.highlight = function ( axis ) {
  109. this.traverse( function( child ) {
  110. if ( child.material && child.material.highlight ) {
  111. if ( child.name === axis ) {
  112. child.material.highlight( true );
  113. } else {
  114. child.material.highlight( false );
  115. }
  116. }
  117. } );
  118. };
  119. };
  120. THREE.TransformGizmo.prototype = Object.create( THREE.Object3D.prototype );
  121. THREE.TransformGizmo.prototype.constructor = THREE.TransformGizmo;
  122. THREE.TransformGizmo.prototype.update = function ( rotation, eye ) {
  123. var vec1 = new THREE.Vector3( 0, 0, 0 );
  124. var vec2 = new THREE.Vector3( 0, 1, 0 );
  125. var lookAtMatrix = new THREE.Matrix4();
  126. this.traverse( function( child ) {
  127. if ( child.name.search( "E" ) !== - 1 ) {
  128. child.quaternion.setFromRotationMatrix( lookAtMatrix.lookAt( eye, vec1, vec2 ) );
  129. } else if ( child.name.search( "X" ) !== - 1 || child.name.search( "Y" ) !== - 1 || child.name.search( "Z" ) !== - 1 ) {
  130. child.quaternion.setFromEuler( rotation );
  131. }
  132. } );
  133. };
  134. THREE.TransformGizmoTranslate = function () {
  135. THREE.TransformGizmo.call( this );
  136. var arrowGeometry = new THREE.Geometry();
  137. var mesh = new THREE.Mesh( new THREE.CylinderGeometry( 0, 0.05, 0.2, 12, 1, false ) );
  138. mesh.position.y = 0.5;
  139. mesh.updateMatrix();
  140. arrowGeometry.merge( mesh.geometry, mesh.matrix );
  141. var lineXGeometry = new THREE.BufferGeometry();
  142. lineXGeometry.addAttribute( 'position', new THREE.Float32BufferAttribute( [ 0, 0, 0, 1, 0, 0 ], 3 ) );
  143. var lineYGeometry = new THREE.BufferGeometry();
  144. lineYGeometry.addAttribute( 'position', new THREE.Float32BufferAttribute( [ 0, 0, 0, 0, 1, 0 ], 3 ) );
  145. var lineZGeometry = new THREE.BufferGeometry();
  146. lineZGeometry.addAttribute( 'position', new THREE.Float32BufferAttribute( [ 0, 0, 0, 0, 0, 1 ], 3 ) );
  147. this.handleGizmos = {
  148. X: [
  149. [ new THREE.Mesh( arrowGeometry, new GizmoMaterial( { color: 0xff0000 } ) ), [ 0.5, 0, 0 ], [ 0, 0, - Math.PI / 2 ] ],
  150. [ new THREE.Line( lineXGeometry, new GizmoLineMaterial( { color: 0xff0000 } ) ) ]
  151. ],
  152. Y: [
  153. [ new THREE.Mesh( arrowGeometry, new GizmoMaterial( { color: 0x00ff00 } ) ), [ 0, 0.5, 0 ] ],
  154. [ new THREE.Line( lineYGeometry, new GizmoLineMaterial( { color: 0x00ff00 } ) ) ]
  155. ],
  156. Z: [
  157. [ new THREE.Mesh( arrowGeometry, new GizmoMaterial( { color: 0x0000ff } ) ), [ 0, 0, 0.5 ], [ Math.PI / 2, 0, 0 ] ],
  158. [ new THREE.Line( lineZGeometry, new GizmoLineMaterial( { color: 0x0000ff } ) ) ]
  159. ],
  160. XYZ: [
  161. [ new THREE.Mesh( new THREE.OctahedronGeometry( 0.1, 0 ), new GizmoMaterial( { color: 0xffffff, opacity: 0.25 } ) ), [ 0, 0, 0 ], [ 0, 0, 0 ] ]
  162. ],
  163. XY: [
  164. [ new THREE.Mesh( new THREE.PlaneBufferGeometry( 0.29, 0.29 ), new GizmoMaterial( { color: 0xffff00, opacity: 0.25 } ) ), [ 0.15, 0.15, 0 ] ]
  165. ],
  166. YZ: [
  167. [ 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 ] ]
  168. ],
  169. XZ: [
  170. [ 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 ] ]
  171. ]
  172. };
  173. this.pickerGizmos = {
  174. X: [
  175. [ new THREE.Mesh( new THREE.CylinderBufferGeometry( 0.2, 0, 1, 4, 1, false ), pickerMaterial ), [ 0.6, 0, 0 ], [ 0, 0, - Math.PI / 2 ] ]
  176. ],
  177. Y: [
  178. [ new THREE.Mesh( new THREE.CylinderBufferGeometry( 0.2, 0, 1, 4, 1, false ), pickerMaterial ), [ 0, 0.6, 0 ] ]
  179. ],
  180. Z: [
  181. [ new THREE.Mesh( new THREE.CylinderBufferGeometry( 0.2, 0, 1, 4, 1, false ), pickerMaterial ), [ 0, 0, 0.6 ], [ Math.PI / 2, 0, 0 ] ]
  182. ],
  183. XYZ: [
  184. [ new THREE.Mesh( new THREE.OctahedronGeometry( 0.2, 0 ), pickerMaterial ) ]
  185. ],
  186. XY: [
  187. [ new THREE.Mesh( new THREE.PlaneBufferGeometry( 0.4, 0.4 ), pickerMaterial ), [ 0.2, 0.2, 0 ] ]
  188. ],
  189. YZ: [
  190. [ new THREE.Mesh( new THREE.PlaneBufferGeometry( 0.4, 0.4 ), pickerMaterial ), [ 0, 0.2, 0.2 ], [ 0, Math.PI / 2, 0 ] ]
  191. ],
  192. XZ: [
  193. [ new THREE.Mesh( new THREE.PlaneBufferGeometry( 0.4, 0.4 ), pickerMaterial ), [ 0.2, 0, 0.2 ], [ - Math.PI / 2, 0, 0 ] ]
  194. ]
  195. };
  196. this.setActivePlane = function ( axis, eye ) {
  197. var tempMatrix = new THREE.Matrix4();
  198. eye.applyMatrix4( tempMatrix.getInverse( tempMatrix.extractRotation( this.planes[ "XY" ].matrixWorld ) ) );
  199. if ( axis === "X" ) {
  200. this.activePlane = this.planes[ "XY" ];
  201. if ( Math.abs( eye.y ) > Math.abs( eye.z ) ) this.activePlane = this.planes[ "XZ" ];
  202. }
  203. if ( axis === "Y" ) {
  204. this.activePlane = this.planes[ "XY" ];
  205. if ( Math.abs( eye.x ) > Math.abs( eye.z ) ) this.activePlane = this.planes[ "YZ" ];
  206. }
  207. if ( axis === "Z" ) {
  208. this.activePlane = this.planes[ "XZ" ];
  209. if ( Math.abs( eye.x ) > Math.abs( eye.y ) ) this.activePlane = this.planes[ "YZ" ];
  210. }
  211. if ( axis === "XYZ" ) this.activePlane = this.planes[ "XYZE" ];
  212. if ( axis === "XY" ) this.activePlane = this.planes[ "XY" ];
  213. if ( axis === "YZ" ) this.activePlane = this.planes[ "YZ" ];
  214. if ( axis === "XZ" ) this.activePlane = this.planes[ "XZ" ];
  215. };
  216. this.init();
  217. };
  218. THREE.TransformGizmoTranslate.prototype = Object.create( THREE.TransformGizmo.prototype );
  219. THREE.TransformGizmoTranslate.prototype.constructor = THREE.TransformGizmoTranslate;
  220. THREE.TransformGizmoRotate = function () {
  221. THREE.TransformGizmo.call( this );
  222. var CircleGeometry = function ( radius, facing, arc ) {
  223. var geometry = new THREE.BufferGeometry();
  224. var vertices = [];
  225. arc = arc ? arc : 1;
  226. for ( var i = 0; i <= 64 * arc; ++ i ) {
  227. if ( facing === 'x' ) vertices.push( 0, Math.cos( i / 32 * Math.PI ) * radius, Math.sin( i / 32 * Math.PI ) * radius );
  228. if ( facing === 'y' ) vertices.push( Math.cos( i / 32 * Math.PI ) * radius, 0, Math.sin( i / 32 * Math.PI ) * radius );
  229. if ( facing === 'z' ) vertices.push( Math.sin( i / 32 * Math.PI ) * radius, Math.cos( i / 32 * Math.PI ) * radius, 0 );
  230. }
  231. geometry.addAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  232. return geometry;
  233. };
  234. this.handleGizmos = {
  235. X: [
  236. [ new THREE.Line( new CircleGeometry( 1, 'x', 0.5 ), new GizmoLineMaterial( { color: 0xff0000 } ) ) ]
  237. ],
  238. Y: [
  239. [ new THREE.Line( new CircleGeometry( 1, 'y', 0.5 ), new GizmoLineMaterial( { color: 0x00ff00 } ) ) ]
  240. ],
  241. Z: [
  242. [ new THREE.Line( new CircleGeometry( 1, 'z', 0.5 ), new GizmoLineMaterial( { color: 0x0000ff } ) ) ]
  243. ],
  244. E: [
  245. [ new THREE.Line( new CircleGeometry( 1.25, 'z', 1 ), new GizmoLineMaterial( { color: 0xcccc00 } ) ) ]
  246. ],
  247. XYZE: [
  248. [ new THREE.Line( new CircleGeometry( 1, 'z', 1 ), new GizmoLineMaterial( { color: 0x787878 } ) ) ]
  249. ]
  250. };
  251. this.pickerGizmos = {
  252. X: [
  253. [ new THREE.Mesh( new THREE.TorusBufferGeometry( 1, 0.12, 4, 12, Math.PI ), pickerMaterial ), [ 0, 0, 0 ], [ 0, - Math.PI / 2, - Math.PI / 2 ] ]
  254. ],
  255. Y: [
  256. [ new THREE.Mesh( new THREE.TorusBufferGeometry( 1, 0.12, 4, 12, Math.PI ), pickerMaterial ), [ 0, 0, 0 ], [ Math.PI / 2, 0, 0 ] ]
  257. ],
  258. Z: [
  259. [ new THREE.Mesh( new THREE.TorusBufferGeometry( 1, 0.12, 4, 12, Math.PI ), pickerMaterial ), [ 0, 0, 0 ], [ 0, 0, - Math.PI / 2 ] ]
  260. ],
  261. E: [
  262. [ new THREE.Mesh( new THREE.TorusBufferGeometry( 1.25, 0.12, 2, 24 ), pickerMaterial ) ]
  263. ],
  264. XYZE: [
  265. [ new THREE.Mesh() ]// TODO
  266. ]
  267. };
  268. this.setActivePlane = function ( axis ) {
  269. if ( axis === "E" ) this.activePlane = this.planes[ "XYZE" ];
  270. if ( axis === "X" ) this.activePlane = this.planes[ "YZ" ];
  271. if ( axis === "Y" ) this.activePlane = this.planes[ "XZ" ];
  272. if ( axis === "Z" ) this.activePlane = this.planes[ "XY" ];
  273. };
  274. this.update = function ( rotation, eye2 ) {
  275. THREE.TransformGizmo.prototype.update.apply( this, arguments );
  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 _gizmo = {
  394. "translate": new THREE.TransformGizmoTranslate(),
  395. "rotate": new THREE.TransformGizmoRotate(),
  396. "scale": new THREE.TransformGizmoScale()
  397. };
  398. for ( var type in _gizmo ) {
  399. var gizmoObj = _gizmo[ type ];
  400. gizmoObj.visible = ( type === _mode );
  401. this.add( gizmoObj );
  402. }
  403. var changeEvent = { type: "change" };
  404. var mouseDownEvent = { type: "mouseDown" };
  405. var mouseUpEvent = { type: "mouseUp", mode: _mode };
  406. var objectChangeEvent = { type: "objectChange" };
  407. var ray = new THREE.Raycaster();
  408. var pointerVector = new THREE.Vector2();
  409. var point = new THREE.Vector3();
  410. var offset = new THREE.Vector3();
  411. var rotation = new THREE.Vector3();
  412. var offsetRotation = new THREE.Vector3();
  413. var scale = 1;
  414. var lookAtMatrix = new THREE.Matrix4();
  415. var eye = new THREE.Vector3();
  416. var tempMatrix = new THREE.Matrix4();
  417. var tempVector = new THREE.Vector3();
  418. var tempQuaternion = new THREE.Quaternion();
  419. var unitX = new THREE.Vector3( 1, 0, 0 );
  420. var unitY = new THREE.Vector3( 0, 1, 0 );
  421. var unitZ = new THREE.Vector3( 0, 0, 1 );
  422. var quaternionXYZ = new THREE.Quaternion();
  423. var quaternionX = new THREE.Quaternion();
  424. var quaternionY = new THREE.Quaternion();
  425. var quaternionZ = new THREE.Quaternion();
  426. var quaternionE = new THREE.Quaternion();
  427. var oldPosition = new THREE.Vector3();
  428. var oldScale = new THREE.Vector3();
  429. var oldRotationMatrix = new THREE.Matrix4();
  430. var parentRotationMatrix = new THREE.Matrix4();
  431. var parentScale = new THREE.Vector3();
  432. var worldPosition = new THREE.Vector3();
  433. var worldRotation = new THREE.Euler();
  434. var worldRotationMatrix = new THREE.Matrix4();
  435. var camPosition = new THREE.Vector3();
  436. var camRotation = new THREE.Euler();
  437. domElement.addEventListener( "mousedown", onPointerDown, false );
  438. domElement.addEventListener( "touchstart", onPointerDown, false );
  439. domElement.addEventListener( "mousemove", onPointerHover, false );
  440. domElement.addEventListener( "touchmove", onPointerHover, false );
  441. domElement.addEventListener( "mousemove", onPointerMove, false );
  442. domElement.addEventListener( "touchmove", onPointerMove, false );
  443. domElement.addEventListener( "mouseup", onPointerUp, false );
  444. domElement.addEventListener( "mouseout", onPointerUp, false );
  445. domElement.addEventListener( "touchend", onPointerUp, false );
  446. domElement.addEventListener( "touchcancel", onPointerUp, false );
  447. domElement.addEventListener( "touchleave", onPointerUp, false );
  448. this.dispose = function () {
  449. domElement.removeEventListener( "mousedown", onPointerDown );
  450. domElement.removeEventListener( "touchstart", onPointerDown );
  451. domElement.removeEventListener( "mousemove", onPointerHover );
  452. domElement.removeEventListener( "touchmove", onPointerHover );
  453. domElement.removeEventListener( "mousemove", onPointerMove );
  454. domElement.removeEventListener( "touchmove", onPointerMove );
  455. domElement.removeEventListener( "mouseup", onPointerUp );
  456. domElement.removeEventListener( "mouseout", onPointerUp );
  457. domElement.removeEventListener( "touchend", onPointerUp );
  458. domElement.removeEventListener( "touchcancel", onPointerUp );
  459. domElement.removeEventListener( "touchleave", onPointerUp );
  460. };
  461. this.attach = function ( object ) {
  462. this.object = object;
  463. this.visible = true;
  464. this.update();
  465. };
  466. this.detach = function () {
  467. this.object = undefined;
  468. this.visible = false;
  469. this.axis = null;
  470. };
  471. this.getMode = function () {
  472. return _mode;
  473. };
  474. this.setMode = function ( mode ) {
  475. _mode = mode ? mode : _mode;
  476. if ( _mode === "scale" ) scope.space = "local";
  477. for ( var type in _gizmo ) _gizmo[ type ].visible = ( type === _mode );
  478. this.update();
  479. scope.dispatchEvent( changeEvent );
  480. };
  481. this.setTranslationSnap = function ( translationSnap ) {
  482. scope.translationSnap = translationSnap;
  483. };
  484. this.setRotationSnap = function ( rotationSnap ) {
  485. scope.rotationSnap = rotationSnap;
  486. };
  487. this.setSize = function ( size ) {
  488. scope.size = size;
  489. this.update();
  490. scope.dispatchEvent( changeEvent );
  491. };
  492. this.setSpace = function ( space ) {
  493. scope.space = space;
  494. this.update();
  495. scope.dispatchEvent( changeEvent );
  496. };
  497. this.update = function () {
  498. if ( scope.object === undefined ) return;
  499. scope.object.updateMatrixWorld();
  500. worldPosition.setFromMatrixPosition( scope.object.matrixWorld );
  501. worldRotation.setFromRotationMatrix( tempMatrix.extractRotation( scope.object.matrixWorld ) );
  502. camera.updateMatrixWorld();
  503. camPosition.setFromMatrixPosition( camera.matrixWorld );
  504. camRotation.setFromRotationMatrix( tempMatrix.extractRotation( camera.matrixWorld ) );
  505. scale = worldPosition.distanceTo( camPosition ) / 6 * scope.size;
  506. this.position.copy( worldPosition );
  507. this.scale.set( scale, scale, scale );
  508. if ( camera instanceof THREE.PerspectiveCamera ) {
  509. eye.copy( camPosition ).sub( worldPosition ).normalize();
  510. } else if ( camera instanceof THREE.OrthographicCamera ) {
  511. eye.copy( camPosition ).normalize();
  512. }
  513. if ( scope.space === "local" ) {
  514. _gizmo[ _mode ].update( worldRotation, eye );
  515. } else if ( scope.space === "world" ) {
  516. _gizmo[ _mode ].update( new THREE.Euler(), eye );
  517. }
  518. _gizmo[ _mode ].highlight( scope.axis );
  519. };
  520. function onPointerHover( event ) {
  521. if ( scope.object === undefined || _dragging === true || ( event.button !== undefined && event.button !== 0 ) ) return;
  522. var pointer = event.changedTouches ? event.changedTouches[ 0 ] : event;
  523. var intersect = intersectObjects( pointer, _gizmo[ _mode ].pickers.children );
  524. var axis = null;
  525. if ( intersect ) {
  526. axis = intersect.object.name;
  527. event.preventDefault();
  528. }
  529. if ( scope.axis !== axis ) {
  530. scope.axis = axis;
  531. scope.update();
  532. scope.dispatchEvent( changeEvent );
  533. }
  534. }
  535. function onPointerDown( event ) {
  536. if ( scope.object === undefined || _dragging === true || ( event.button !== undefined && event.button !== 0 ) ) return;
  537. var pointer = event.changedTouches ? event.changedTouches[ 0 ] : event;
  538. if ( pointer.button === 0 || pointer.button === undefined ) {
  539. var intersect = intersectObjects( pointer, _gizmo[ _mode ].pickers.children );
  540. if ( intersect ) {
  541. event.preventDefault();
  542. event.stopPropagation();
  543. scope.axis = intersect.object.name;
  544. scope.dispatchEvent( mouseDownEvent );
  545. scope.update();
  546. eye.copy( camPosition ).sub( worldPosition ).normalize();
  547. _gizmo[ _mode ].setActivePlane( scope.axis, eye );
  548. var planeIntersect = intersectObjects( pointer, [ _gizmo[ _mode ].activePlane ] );
  549. if ( planeIntersect ) {
  550. oldPosition.copy( scope.object.position );
  551. oldScale.copy( scope.object.scale );
  552. oldRotationMatrix.extractRotation( scope.object.matrix );
  553. worldRotationMatrix.extractRotation( scope.object.matrixWorld );
  554. parentRotationMatrix.extractRotation( scope.object.parent.matrixWorld );
  555. parentScale.setFromMatrixScale( tempMatrix.getInverse( scope.object.parent.matrixWorld ) );
  556. offset.copy( planeIntersect.point );
  557. }
  558. }
  559. }
  560. _dragging = true;
  561. }
  562. function onPointerMove( event ) {
  563. if ( scope.object === undefined || scope.axis === null || _dragging === false || ( event.button !== undefined && event.button !== 0 ) ) return;
  564. var pointer = event.changedTouches ? event.changedTouches[ 0 ] : event;
  565. var planeIntersect = intersectObjects( pointer, [ _gizmo[ _mode ].activePlane ] );
  566. if ( planeIntersect === false ) return;
  567. event.preventDefault();
  568. event.stopPropagation();
  569. point.copy( planeIntersect.point );
  570. if ( _mode === "translate" ) {
  571. point.sub( offset );
  572. point.multiply( parentScale );
  573. if ( scope.space === "local" ) {
  574. point.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
  575. if ( scope.axis.search( "X" ) === - 1 ) point.x = 0;
  576. if ( scope.axis.search( "Y" ) === - 1 ) point.y = 0;
  577. if ( scope.axis.search( "Z" ) === - 1 ) point.z = 0;
  578. point.applyMatrix4( oldRotationMatrix );
  579. scope.object.position.copy( oldPosition );
  580. scope.object.position.add( point );
  581. }
  582. if ( scope.space === "world" || scope.axis.search( "XYZ" ) !== - 1 ) {
  583. if ( scope.axis.search( "X" ) === - 1 ) point.x = 0;
  584. if ( scope.axis.search( "Y" ) === - 1 ) point.y = 0;
  585. if ( scope.axis.search( "Z" ) === - 1 ) point.z = 0;
  586. point.applyMatrix4( tempMatrix.getInverse( parentRotationMatrix ) );
  587. scope.object.position.copy( oldPosition );
  588. scope.object.position.add( point );
  589. }
  590. if ( scope.translationSnap !== null ) {
  591. if ( scope.space === "local" ) {
  592. scope.object.position.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
  593. }
  594. if ( scope.axis.search( "X" ) !== - 1 ) scope.object.position.x = Math.round( scope.object.position.x / scope.translationSnap ) * scope.translationSnap;
  595. if ( scope.axis.search( "Y" ) !== - 1 ) scope.object.position.y = Math.round( scope.object.position.y / scope.translationSnap ) * scope.translationSnap;
  596. if ( scope.axis.search( "Z" ) !== - 1 ) scope.object.position.z = Math.round( scope.object.position.z / scope.translationSnap ) * scope.translationSnap;
  597. if ( scope.space === "local" ) {
  598. scope.object.position.applyMatrix4( worldRotationMatrix );
  599. }
  600. }
  601. } else if ( _mode === "scale" ) {
  602. point.sub( offset );
  603. point.multiply( parentScale );
  604. if ( scope.space === "local" ) {
  605. if ( scope.axis === "XYZ" ) {
  606. scale = 1 + ( ( point.y ) / Math.max( oldScale.x, oldScale.y, oldScale.z ) );
  607. scope.object.scale.x = oldScale.x * scale;
  608. scope.object.scale.y = oldScale.y * scale;
  609. scope.object.scale.z = oldScale.z * scale;
  610. } else {
  611. point.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
  612. if ( scope.axis === "X" ) scope.object.scale.x = oldScale.x * ( 1 + point.x / oldScale.x );
  613. if ( scope.axis === "Y" ) scope.object.scale.y = oldScale.y * ( 1 + point.y / oldScale.y );
  614. if ( scope.axis === "Z" ) scope.object.scale.z = oldScale.z * ( 1 + point.z / oldScale.z );
  615. }
  616. }
  617. } else if ( _mode === "rotate" ) {
  618. point.sub( worldPosition );
  619. point.multiply( parentScale );
  620. tempVector.copy( offset ).sub( worldPosition );
  621. tempVector.multiply( parentScale );
  622. if ( scope.axis === "E" ) {
  623. point.applyMatrix4( tempMatrix.getInverse( lookAtMatrix ) );
  624. tempVector.applyMatrix4( tempMatrix.getInverse( lookAtMatrix ) );
  625. rotation.set( Math.atan2( point.z, point.y ), Math.atan2( point.x, point.z ), Math.atan2( point.y, point.x ) );
  626. offsetRotation.set( Math.atan2( tempVector.z, tempVector.y ), Math.atan2( tempVector.x, tempVector.z ), Math.atan2( tempVector.y, tempVector.x ) );
  627. tempQuaternion.setFromRotationMatrix( tempMatrix.getInverse( parentRotationMatrix ) );
  628. quaternionE.setFromAxisAngle( eye, rotation.z - offsetRotation.z );
  629. quaternionXYZ.setFromRotationMatrix( worldRotationMatrix );
  630. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionE );
  631. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionXYZ );
  632. scope.object.quaternion.copy( tempQuaternion );
  633. } else if ( scope.axis === "XYZE" ) {
  634. quaternionE.setFromEuler( point.clone().cross( tempVector ).normalize() ); // rotation axis
  635. tempQuaternion.setFromRotationMatrix( tempMatrix.getInverse( parentRotationMatrix ) );
  636. quaternionX.setFromAxisAngle( quaternionE, - point.clone().angleTo( tempVector ) );
  637. quaternionXYZ.setFromRotationMatrix( worldRotationMatrix );
  638. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionX );
  639. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionXYZ );
  640. scope.object.quaternion.copy( tempQuaternion );
  641. } else if ( scope.space === "local" ) {
  642. point.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
  643. tempVector.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
  644. rotation.set( Math.atan2( point.z, point.y ), Math.atan2( point.x, point.z ), Math.atan2( point.y, point.x ) );
  645. offsetRotation.set( Math.atan2( tempVector.z, tempVector.y ), Math.atan2( tempVector.x, tempVector.z ), Math.atan2( tempVector.y, tempVector.x ) );
  646. quaternionXYZ.setFromRotationMatrix( oldRotationMatrix );
  647. if ( scope.rotationSnap !== null ) {
  648. quaternionX.setFromAxisAngle( unitX, Math.round( ( rotation.x - offsetRotation.x ) / scope.rotationSnap ) * scope.rotationSnap );
  649. quaternionY.setFromAxisAngle( unitY, Math.round( ( rotation.y - offsetRotation.y ) / scope.rotationSnap ) * scope.rotationSnap );
  650. quaternionZ.setFromAxisAngle( unitZ, Math.round( ( rotation.z - offsetRotation.z ) / scope.rotationSnap ) * scope.rotationSnap );
  651. } else {
  652. quaternionX.setFromAxisAngle( unitX, rotation.x - offsetRotation.x );
  653. quaternionY.setFromAxisAngle( unitY, rotation.y - offsetRotation.y );
  654. quaternionZ.setFromAxisAngle( unitZ, rotation.z - offsetRotation.z );
  655. }
  656. if ( scope.axis === "X" ) quaternionXYZ.multiplyQuaternions( quaternionXYZ, quaternionX );
  657. if ( scope.axis === "Y" ) quaternionXYZ.multiplyQuaternions( quaternionXYZ, quaternionY );
  658. if ( scope.axis === "Z" ) quaternionXYZ.multiplyQuaternions( quaternionXYZ, quaternionZ );
  659. scope.object.quaternion.copy( quaternionXYZ );
  660. } else if ( scope.space === "world" ) {
  661. rotation.set( Math.atan2( point.z, point.y ), Math.atan2( point.x, point.z ), Math.atan2( point.y, point.x ) );
  662. offsetRotation.set( Math.atan2( tempVector.z, tempVector.y ), Math.atan2( tempVector.x, tempVector.z ), Math.atan2( tempVector.y, tempVector.x ) );
  663. tempQuaternion.setFromRotationMatrix( tempMatrix.getInverse( parentRotationMatrix ) );
  664. if ( scope.rotationSnap !== null ) {
  665. quaternionX.setFromAxisAngle( unitX, Math.round( ( rotation.x - offsetRotation.x ) / scope.rotationSnap ) * scope.rotationSnap );
  666. quaternionY.setFromAxisAngle( unitY, Math.round( ( rotation.y - offsetRotation.y ) / scope.rotationSnap ) * scope.rotationSnap );
  667. quaternionZ.setFromAxisAngle( unitZ, Math.round( ( rotation.z - offsetRotation.z ) / scope.rotationSnap ) * scope.rotationSnap );
  668. } else {
  669. quaternionX.setFromAxisAngle( unitX, rotation.x - offsetRotation.x );
  670. quaternionY.setFromAxisAngle( unitY, rotation.y - offsetRotation.y );
  671. quaternionZ.setFromAxisAngle( unitZ, rotation.z - offsetRotation.z );
  672. }
  673. quaternionXYZ.setFromRotationMatrix( worldRotationMatrix );
  674. if ( scope.axis === "X" ) tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionX );
  675. if ( scope.axis === "Y" ) tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionY );
  676. if ( scope.axis === "Z" ) tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionZ );
  677. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionXYZ );
  678. scope.object.quaternion.copy( tempQuaternion );
  679. }
  680. }
  681. scope.update();
  682. scope.dispatchEvent( changeEvent );
  683. scope.dispatchEvent( objectChangeEvent );
  684. }
  685. function onPointerUp( event ) {
  686. event.preventDefault(); // Prevent MouseEvent on mobile
  687. if ( event.button !== undefined && event.button !== 0 ) return;
  688. if ( _dragging && ( scope.axis !== null ) ) {
  689. mouseUpEvent.mode = _mode;
  690. scope.dispatchEvent( mouseUpEvent );
  691. }
  692. _dragging = false;
  693. if ( 'TouchEvent' in window && event instanceof TouchEvent ) {
  694. // Force "rollover"
  695. scope.axis = null;
  696. scope.update();
  697. scope.dispatchEvent( changeEvent );
  698. } else {
  699. onPointerHover( event );
  700. }
  701. }
  702. function intersectObjects( pointer, objects ) {
  703. var rect = domElement.getBoundingClientRect();
  704. var x = ( pointer.clientX - rect.left ) / rect.width;
  705. var y = ( pointer.clientY - rect.top ) / rect.height;
  706. pointerVector.set( ( x * 2 ) - 1, - ( y * 2 ) + 1 );
  707. ray.setFromCamera( pointerVector, camera );
  708. var intersections = ray.intersectObjects( objects, true );
  709. return intersections[ 0 ] ? intersections[ 0 ] : false;
  710. }
  711. };
  712. THREE.TransformControls.prototype = Object.create( THREE.Object3D.prototype );
  713. THREE.TransformControls.prototype.constructor = THREE.TransformControls;
  714. }() );