TransformControls.js 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113
  1. !function(){
  2. var BoxBufferGeometry,
  3. BufferGeometry,
  4. Color,
  5. CylinderBufferGeometry,
  6. DoubleSide,
  7. Euler,
  8. Float32BufferAttribute ,
  9. Line ,
  10. LineBasicMaterial ,
  11. Matrix4 ,
  12. Mesh ,
  13. MeshBasicMaterial ,
  14. Object3D ,
  15. OctahedronBufferGeometry ,
  16. PlaneBufferGeometry ,
  17. Quaternion ,
  18. Raycaster ,
  19. SphereBufferGeometry ,
  20. TorusBufferGeometry ,
  21. Vector2,
  22. Vector3 ;
  23. var TransformControls = function ( camera, domElement, options ) {
  24. if ( domElement === undefined ) {
  25. console.warn( 'THREE.TransformControls: The second parameter "domElement" is now mandatory.' );
  26. domElement = document;
  27. }
  28. Object3D.call( this );
  29. this.visible = false;
  30. this.domElement = domElement;
  31. var _gizmo = new TransformControlsGizmo(options);
  32. this.add( _gizmo );
  33. var _plane = new TransformControlsPlane(options);
  34. this.add( _plane );
  35. var scope = this;
  36. this.player = options.player;//xzw add
  37. this.options = options//xzw add
  38. // Define properties with getters/setter
  39. // Setting the defined property will automatically trigger change event
  40. // Defined properties are passed down to gizmo and plane
  41. defineProperty( "camera", camera );
  42. defineProperty( "object", undefined );
  43. defineProperty( "enabled", true );
  44. defineProperty( "axis", null );
  45. defineProperty( "mode", "translate" );
  46. defineProperty( "translationSnap", null );
  47. defineProperty( "rotationSnap", null );
  48. defineProperty( "scaleSnap", null );
  49. defineProperty( "space", "world" );
  50. defineProperty( "size", 1 );
  51. defineProperty( "dragging", false );
  52. defineProperty( "showX", true );
  53. defineProperty( "showY", true );
  54. defineProperty( "showZ", true );
  55. var changeEvent = { type: "change" };
  56. var mouseDownEvent = { type: "mouseDown" };
  57. var mouseUpEvent = { type: "mouseUp", mode: scope.mode };
  58. var objectChangeEvent = { type: "objectChange" };
  59. // Reusable utility variables
  60. var ray = new Raycaster();
  61. var _tempVector = new Vector3();
  62. var _tempVector2 = new Vector3();
  63. var _tempQuaternion = new Quaternion();
  64. var _unit = {
  65. X: new Vector3( 1, 0, 0 ),
  66. Y: new Vector3( 0, 1, 0 ),
  67. Z: new Vector3( 0, 0, 1 )
  68. };
  69. var pointStart = new Vector3();
  70. var pointEnd = new Vector3();
  71. var offset = new Vector3();
  72. var rotationAxis = new Vector3();
  73. var startNorm = new Vector3();
  74. var endNorm = new Vector3();
  75. var rotationAngle = 0;
  76. var cameraPosition = new Vector3();
  77. var cameraQuaternion = new Quaternion();
  78. var cameraScale = new Vector3();
  79. var parentPosition = new Vector3();
  80. var parentQuaternion = new Quaternion();
  81. var parentQuaternionInv = new Quaternion();
  82. var parentScale = new Vector3();
  83. var worldPositionStart = new Vector3();
  84. var worldQuaternionStart = new Quaternion();
  85. var worldScaleStart = new Vector3();
  86. var worldPosition = new Vector3();
  87. var worldQuaternion = new Quaternion();
  88. var worldQuaternionInv = new Quaternion();
  89. var worldScale = new Vector3();
  90. var eye = new Vector3();
  91. var positionStart = new Vector3();
  92. var quaternionStart = new Quaternion();
  93. var scaleStart = new Vector3();
  94. // TODO: remove properties unused in plane and gizmo
  95. defineProperty( "worldPosition", worldPosition );
  96. defineProperty( "worldPositionStart", worldPositionStart );
  97. defineProperty( "worldQuaternion", worldQuaternion );
  98. defineProperty( "worldQuaternionStart", worldQuaternionStart );
  99. defineProperty( "cameraPosition", cameraPosition );
  100. defineProperty( "cameraQuaternion", cameraQuaternion );
  101. defineProperty( "pointStart", pointStart );
  102. defineProperty( "pointEnd", pointEnd );
  103. defineProperty( "rotationAxis", rotationAxis );
  104. defineProperty( "rotationAngle", rotationAngle );
  105. defineProperty( "eye", eye );
  106. {
  107. domElement.addEventListener( "mousedown", onPointerDown, false );
  108. domElement.addEventListener( "touchstart", onPointerDown, false );
  109. domElement.addEventListener( "mousemove", onPointerHover, false );
  110. domElement.addEventListener( "touchmove", onPointerHover, false );
  111. domElement.addEventListener( "touchmove", onPointerMove, false );
  112. document.addEventListener( "mouseup", onPointerUp, false );
  113. domElement.addEventListener( "touchend", onPointerUp, false );
  114. domElement.addEventListener( "touchcancel", onPointerUp, false );
  115. domElement.addEventListener( "touchleave", onPointerUp, false );
  116. }
  117. this.dispose = function () {
  118. domElement.removeEventListener( "mousedown", onPointerDown );
  119. domElement.removeEventListener( "touchstart", onPointerDown );
  120. domElement.removeEventListener( "mousemove", onPointerHover );
  121. domElement.removeEventListener( "mousemove", onPointerMove );
  122. domElement.removeEventListener( "touchmove", onPointerHover );
  123. domElement.removeEventListener( "touchmove", onPointerMove );
  124. document.removeEventListener( "mouseup", onPointerUp );
  125. domElement.removeEventListener( "touchend", onPointerUp );
  126. domElement.removeEventListener( "touchcancel", onPointerUp );
  127. domElement.removeEventListener( "touchleave", onPointerUp );
  128. this.traverse( function ( child ) {
  129. if ( child.geometry ) child.geometry.dispose();
  130. if ( child.material ) child.material.dispose();
  131. } );
  132. };
  133. // Set current object
  134. this.attach = function ( object ) {
  135. this.object = object;
  136. this.visible = true;
  137. //Config.keyCon = false;//add
  138. return this;
  139. };
  140. // Detatch from object
  141. this.detach = function () {
  142. this.object = undefined;
  143. this.visible = false;
  144. this.axis = null;
  145. //Config.keyCon = true;//add
  146. return this;
  147. };
  148. // Defined getter, setter and store for a property
  149. function defineProperty( propName, defaultValue ) {
  150. var propValue = defaultValue;
  151. Object.defineProperty( scope, propName, {
  152. get: function () {
  153. return propValue !== undefined ? propValue : defaultValue;
  154. },
  155. set: function ( value ) {
  156. if ( propValue !== value ) {
  157. propValue = value;
  158. _plane[ propName ] = value;
  159. _gizmo[ propName ] = value;
  160. scope.dispatchEvent( { type: propName + "-changed", value: value } );
  161. scope.dispatchEvent( changeEvent );
  162. }
  163. }
  164. } );
  165. scope[ propName ] = defaultValue;
  166. _plane[ propName ] = defaultValue;
  167. _gizmo[ propName ] = defaultValue;
  168. }
  169. // updateMatrixWorld updates key transformation variables
  170. this.updateMatrixWorld = function () {
  171. if ( this.object !== undefined ) {
  172. this.object.updateMatrixWorld();
  173. this.object.parent.matrixWorld.decompose( parentPosition, parentQuaternion, parentScale );
  174. this.object.matrixWorld.decompose( worldPosition, worldQuaternion, worldScale );
  175. parentQuaternionInv.copy( parentQuaternion ).inverse();
  176. worldQuaternionInv.copy( worldQuaternion ).inverse();
  177. }
  178. this.camera.updateMatrixWorld();
  179. this.camera.matrixWorld.decompose( cameraPosition, cameraQuaternion, cameraScale );
  180. eye.copy( cameraPosition ).sub( worldPosition ).normalize();
  181. Object3D.prototype.updateMatrixWorld.call( this );
  182. };
  183. this.pointerHover = function ( pointer ) {
  184. if ( this.object === undefined || this.dragging === true || ( pointer.button !== undefined && pointer.button !== 0 ) ) return;
  185. //ray.setFromCamera( pointer, this.camera ); //这句会在floorplan模式get不到intersect
  186. var origin = new Vector3(pointer.x, pointer.y, -1).unproject(this.camera);
  187. ray.set(origin, player.getMouseDirection(pointer));
  188. var intersect = ray.intersectObjects( _gizmo.picker[ this.mode ].children, true )[ 0 ] || false;
  189. if ( intersect ) {
  190. this.axis = intersect.object.name;
  191. } else {
  192. this.axis = null;
  193. }
  194. };
  195. this.pointerDown = function ( pointer ) {
  196. if ( this.object === undefined || this.dragging === true || ( pointer.button !== undefined && pointer.button !== 0 ) ) return;
  197. if ( ( pointer.button === 0 || pointer.button === undefined ) && this.axis !== null ) {
  198. //ray.setFromCamera( pointer, this.camera ); //这句会在floorplan模式get不到intersect
  199. var origin = new Vector3(pointer.x, pointer.y, -1).unproject(this.camera);
  200. ray.set(origin, player.getMouseDirection(pointer));
  201. var planeIntersect = ray.intersectObjects( [ _plane ], true )[ 0 ] || false;
  202. if ( planeIntersect ) {
  203. var space = this.space;
  204. if ( this.mode === 'scale' ) {
  205. space = 'local';
  206. } else if ( this.axis === 'E' || this.axis === 'XYZE' || this.axis === 'XYZ' ) {
  207. space = 'world';
  208. }
  209. if ( space === 'local' && this.mode === 'rotate' ) {
  210. var snap = this.rotationSnap;
  211. if ( this.axis === 'X' && snap ) this.object.rotation.x = Math.round( this.object.rotation.x / snap ) * snap;
  212. if ( this.axis === 'Y' && snap ) this.object.rotation.y = Math.round( this.object.rotation.y / snap ) * snap;
  213. if ( this.axis === 'Z' && snap ) this.object.rotation.z = Math.round( this.object.rotation.z / snap ) * snap;
  214. }
  215. this.object.updateMatrixWorld();
  216. this.object.parent.updateMatrixWorld();
  217. positionStart.copy( this.object.position );
  218. quaternionStart.copy( this.object.quaternion );
  219. scaleStart.copy( this.object.scale );
  220. this.object.matrixWorld.decompose( worldPositionStart, worldQuaternionStart, worldScaleStart );
  221. pointStart.copy( planeIntersect.point ).sub( worldPositionStart );
  222. if(this.player.cameraControls.activeControl){
  223. //this.player.cameraControls.activeControl.locked = true; //add
  224. this.player.cameraControls.activeControl.enabled = false; //add
  225. }
  226. }
  227. this.dragging = true;
  228. mouseDownEvent.mode = this.mode;
  229. this.dispatchEvent( mouseDownEvent );
  230. }
  231. };
  232. this.pointerMove = function ( pointer ) {
  233. var axis = this.axis;
  234. var mode = this.mode;
  235. var object = this.object;
  236. var space = this.space;
  237. if ( mode === 'scale' ) {
  238. space = 'local';
  239. } else if ( axis === 'E' || axis === 'XYZE' || axis === 'XYZ' ) {
  240. space = 'world';
  241. }
  242. if ( object === undefined || axis === null || this.dragging === false || ( pointer.button !== undefined && pointer.button !== 0 ) ) return;
  243. //ray.setFromCamera( pointer, this.camera ); //这句会在floorplan模式get不到intersect
  244. var origin = new Vector3(pointer.x, pointer.y, -1).unproject(this.camera);
  245. ray.set(origin, player.getMouseDirection(pointer));
  246. var planeIntersect = ray.intersectObjects( [ _plane ], true )[ 0 ] || false;
  247. if ( planeIntersect === false ) return;
  248. pointEnd.copy( planeIntersect.point ).sub( worldPositionStart );
  249. if ( mode === 'translate' ) {
  250. // Apply translate
  251. offset.copy( pointEnd ).sub( pointStart );
  252. if ( space === 'local' && axis !== 'XYZ' ) {
  253. offset.applyQuaternion( worldQuaternionInv );
  254. }
  255. if ( axis.indexOf( 'X' ) === - 1 ) offset.x = 0;
  256. if ( axis.indexOf( 'Y' ) === - 1 ) offset.y = 0;
  257. if ( axis.indexOf( 'Z' ) === - 1 ) offset.z = 0;
  258. if ( space === 'local' && axis !== 'XYZ' ) {
  259. offset.applyQuaternion( quaternionStart ).divide( parentScale );
  260. } else {
  261. offset.applyQuaternion( parentQuaternionInv ).divide( parentScale );
  262. }
  263. object.position.copy( offset ).add( positionStart );
  264. // Apply translation snap
  265. if ( this.translationSnap ) {
  266. if ( space === 'local' ) {
  267. object.position.applyQuaternion( _tempQuaternion.copy( quaternionStart ).inverse() );
  268. if ( axis.search( 'X' ) !== - 1 ) {
  269. object.position.x = Math.round( object.position.x / this.translationSnap ) * this.translationSnap;
  270. }
  271. if ( axis.search( 'Y' ) !== - 1 ) {
  272. object.position.y = Math.round( object.position.y / this.translationSnap ) * this.translationSnap;
  273. }
  274. if ( axis.search( 'Z' ) !== - 1 ) {
  275. object.position.z = Math.round( object.position.z / this.translationSnap ) * this.translationSnap;
  276. }
  277. object.position.applyQuaternion( quaternionStart );
  278. }
  279. if ( space === 'world' ) {
  280. if ( object.parent ) {
  281. object.position.add( _tempVector.setFromMatrixPosition( object.parent.matrixWorld ) );
  282. }
  283. if ( axis.search( 'X' ) !== - 1 ) {
  284. object.position.x = Math.round( object.position.x / this.translationSnap ) * this.translationSnap;
  285. }
  286. if ( axis.search( 'Y' ) !== - 1 ) {
  287. object.position.y = Math.round( object.position.y / this.translationSnap ) * this.translationSnap;
  288. }
  289. if ( axis.search( 'Z' ) !== - 1 ) {
  290. object.position.z = Math.round( object.position.z / this.translationSnap ) * this.translationSnap;
  291. }
  292. if ( object.parent ) {
  293. object.position.sub( _tempVector.setFromMatrixPosition( object.parent.matrixWorld ) );
  294. }
  295. }
  296. }
  297. //add:
  298. /* if(EditOverlay.editing){
  299. EditOverlay.updatePosition();
  300. }else */ if(editTool.hotpoint.editSpot){
  301. editTool.hotpoint.updateTransform('position');
  302. }
  303. } else if ( mode === 'scale' ) {
  304. if ( axis.search( 'XYZ' ) !== - 1 ) {
  305. var d = pointEnd.length() / pointStart.length();
  306. if ( pointEnd.dot( pointStart ) < 0 ) d *= - 1;
  307. if(options.NoScaleZ){//xzw add
  308. _tempVector2.set( d, d, 1 );
  309. }else{
  310. _tempVector2.set( d, d, d );
  311. }
  312. }else if ( axis.search( 'XY' ) !== - 1 ) { //add 等比例for plane
  313. var d = pointEnd.length() / pointStart.length();
  314. if ( pointEnd.dot( pointStart ) < 0 ) d *= - 1;
  315. _tempVector2.set( d, d, 1 );
  316. }else {
  317. _tempVector.copy( pointStart );
  318. _tempVector2.copy( pointEnd );
  319. _tempVector.applyQuaternion( worldQuaternionInv );
  320. _tempVector2.applyQuaternion( worldQuaternionInv );
  321. _tempVector2.divide( _tempVector );
  322. if ( axis.search( 'X' ) === - 1 ) {
  323. _tempVector2.x = 1;
  324. }
  325. if ( axis.search( 'Y' ) === - 1 ) {
  326. _tempVector2.y = 1;
  327. }
  328. if ( axis.search( 'Z' ) === - 1 ) {
  329. _tempVector2.z = 1;
  330. }
  331. }
  332. // Apply scale
  333. object.scale.copy( scaleStart ).multiply( _tempVector2 );
  334. if ( this.scaleSnap ) {
  335. if ( axis.search( 'X' ) !== - 1 ) {
  336. object.scale.x = Math.round( object.scale.x / this.scaleSnap ) * this.scaleSnap || this.scaleSnap;
  337. }
  338. if ( axis.search( 'Y' ) !== - 1 ) {
  339. object.scale.y = Math.round( object.scale.y / this.scaleSnap ) * this.scaleSnap || this.scaleSnap;
  340. }
  341. if ( axis.search( 'Z' ) !== - 1 ) {
  342. object.scale.z = Math.round( object.scale.z / this.scaleSnap ) * this.scaleSnap || this.scaleSnap;
  343. }
  344. }
  345. //add:
  346. /* if(EditOverlay.editing){
  347. EditOverlay.updateScale();
  348. } */
  349. if(editTool.hotpoint.editSpot){
  350. editTool.hotpoint.updateTransform('scale');
  351. }
  352. } else if ( mode === 'rotate' ) {
  353. offset.copy( pointEnd ).sub( pointStart );
  354. var ROTATION_SPEED = 2 / worldPosition.distanceTo( _tempVector.setFromMatrixPosition( this.camera.matrixWorld ) );
  355. if ( axis === 'E' ) {
  356. rotationAxis.copy( eye );
  357. rotationAngle = pointEnd.angleTo( pointStart );
  358. startNorm.copy( pointStart ).normalize();
  359. endNorm.copy( pointEnd ).normalize();
  360. rotationAngle *= ( endNorm.cross( startNorm ).dot( eye ) < 0 ? 1 : - 1 );
  361. } else if ( axis === 'XYZE' ) {
  362. rotationAxis.copy( offset ).cross( eye ).normalize();
  363. rotationAngle = offset.dot( _tempVector.copy( rotationAxis ).cross( this.eye ) ) * ROTATION_SPEED;
  364. } else if ( axis === 'X' || axis === 'Y' || axis === 'Z' ) {
  365. rotationAxis.copy( _unit[ axis ] );
  366. _tempVector.copy( _unit[ axis ] );
  367. if ( space === 'local' ) {
  368. _tempVector.applyQuaternion( worldQuaternion );
  369. }
  370. rotationAngle = offset.dot( _tempVector.cross( eye ).normalize() ) * ROTATION_SPEED;
  371. }
  372. // Apply rotation snap
  373. if ( this.rotationSnap ) rotationAngle = Math.round( rotationAngle / this.rotationSnap ) * this.rotationSnap;
  374. this.rotationAngle = rotationAngle;
  375. // Apply rotate
  376. if ( space === 'local' && axis !== 'E' && axis !== 'XYZE' ) {
  377. object.quaternion.copy( quaternionStart );
  378. object.quaternion.multiply( _tempQuaternion.setFromAxisAngle( rotationAxis, rotationAngle ) ).normalize();
  379. } else {
  380. rotationAxis.applyQuaternion( parentQuaternionInv );
  381. object.quaternion.copy( _tempQuaternion.setFromAxisAngle( rotationAxis, rotationAngle ) );
  382. object.quaternion.multiply( quaternionStart ).normalize();
  383. }
  384. //add:
  385. /*
  386. if(EditOverlay.editing){
  387. EditOverlay.updateQua();
  388. } */
  389. if(editTool.hotpoint.editSpot){
  390. editTool.hotpoint.updateTransform('quaternion');
  391. }
  392. }
  393. this.dispatchEvent( changeEvent );
  394. this.dispatchEvent( objectChangeEvent );
  395. };
  396. this.pointerUp = function ( pointer ) {
  397. //if ( pointer.button !== undefined && pointer.button !== 0 ) return;
  398. if ( this.dragging && ( this.axis !== null ) ) {
  399. mouseUpEvent.mode = this.mode;
  400. this.dispatchEvent( mouseUpEvent );
  401. }
  402. this.dragging = false;
  403. if ( pointer.button === undefined ) this.axis = null;
  404. if(this.player.cameraControls.activeControl){
  405. //this.player.cameraControls.activeControl.locked = false; //add
  406. this.player.cameraControls.activeControl.pointerDragOn = false //add
  407. this.player.cameraControls.activeControl.enabled = true
  408. }
  409. };
  410. // normalize mouse / touch pointer and remap {x,y} to view space.
  411. function getPointer( event ) {
  412. if(!event){
  413. console.log('hhahhhahah')
  414. return;
  415. }
  416. if ( document.pointerLockElement ) {
  417. return {
  418. x: 0,
  419. y: 0,
  420. button: event.button
  421. };
  422. } else {
  423. var pointer = event.changedTouches ? event.changedTouches[ 0 ] : event;
  424. var rect = domElement.getBoundingClientRect();
  425. return {
  426. x: ( pointer.clientX - rect.left ) / rect.width * 2 - 1,
  427. y: - ( pointer.clientY - rect.top ) / rect.height * 2 + 1,
  428. button: event.button
  429. };
  430. }
  431. }
  432. // mouse / touch event handlers
  433. function onPointerHover( event ) {
  434. if ( ! scope.enabled ) return;
  435. //scope.pointerHover( getPointer( event ) );
  436. scope.pointerHover( scope.player.mouse );
  437. }
  438. function onPointerDown( event ) {
  439. if ( ! scope.enabled ) return;
  440. //document.addEventListener( "mousemove", onPointerMove, false );
  441. /* scope.pointerHover( getPointer( event ) );
  442. scope.pointerDown( getPointer( event ) ); */
  443. scope.pointerHover( scope.player.mouse );
  444. scope.pointerDown( scope.player.mouse );
  445. }
  446. this.onPointerDown = onPointerDown;
  447. function onPointerMove( event ) {
  448. if ( ! scope.enabled || !this.dragging) return; //xzw change
  449. //scope.pointerMove( getPointer( event ) );
  450. scope.pointerMove( scope.player.mouse );
  451. }
  452. this.onPointerMove = onPointerMove;
  453. function onPointerUp( event ) {
  454. if ( ! scope.enabled ) return;
  455. //document.removeEventListener( "mousemove", onPointerMove, false );
  456. //scope.pointerUp( getPointer( event ) );
  457. scope.pointerUp( scope.player.mouse );
  458. }
  459. this.onPointerUp = onPointerUp;
  460. // TODO: deprecate
  461. this.getMode = function () {
  462. return scope.mode;
  463. };
  464. this.setMode = function ( mode ) {
  465. scope.mode = mode;
  466. };
  467. this.setTranslationSnap = function ( translationSnap ) {
  468. scope.translationSnap = translationSnap;
  469. };
  470. this.setRotationSnap = function ( rotationSnap ) {
  471. scope.rotationSnap = rotationSnap;
  472. };
  473. this.setScaleSnap = function ( scaleSnap ) {
  474. scope.scaleSnap = scaleSnap;
  475. };
  476. this.setSize = function ( size ) {
  477. scope.size = size;
  478. };
  479. this.setSpace = function ( space ) {
  480. scope.space = space;
  481. };
  482. this.update = function () {
  483. console.warn( 'THREE.TransformControls: update function has no more functionality and therefore has been deprecated.' );
  484. };
  485. };
  486. var TransformControlsGizmo = function (options) {
  487. 'use strict';
  488. Object3D.call( this );
  489. this.type = 'TransformControlsGizmo';
  490. // shared materials
  491. var gizmoMaterial = new MeshBasicMaterial( {
  492. depthTest: false,
  493. depthWrite: false,
  494. transparent: true,
  495. side: DoubleSide,
  496. fog: false
  497. } );
  498. var gizmoLineMaterial = new LineBasicMaterial( {
  499. depthTest: false,
  500. depthWrite: false,
  501. transparent: true,
  502. linewidth: 1,
  503. fog: false
  504. } );
  505. // Make unique material for each axis/color
  506. var matInvisible = gizmoMaterial.clone();
  507. matInvisible.opacity = 0.15;
  508. var matHelper = gizmoMaterial.clone();
  509. matHelper.opacity = 0.33;
  510. var matRed = gizmoMaterial.clone();
  511. matRed.color.set( 0xff0000 );
  512. var matGreen = gizmoMaterial.clone();
  513. matGreen.color.set( 0x00ff00 );
  514. var matBlue = gizmoMaterial.clone();
  515. matBlue.color.set( 0x0000ff );
  516. var matWhiteTransparent = gizmoMaterial.clone();
  517. matWhiteTransparent.opacity = 0.35;
  518. matWhiteTransparent.color.set( 0x00d0fd );//xzw add
  519. var matYellowTransparent = matWhiteTransparent.clone();
  520. matYellowTransparent.color.set( 0xffff00 );
  521. var matCyanTransparent = matWhiteTransparent.clone();
  522. matCyanTransparent.color.set( 0x00ffff );
  523. var matMagentaTransparent = matWhiteTransparent.clone();
  524. matMagentaTransparent.color.set( 0xff00ff );
  525. var matYellow = gizmoMaterial.clone();
  526. matYellow.color.set( 0xffff00 );
  527. var matLineRed = gizmoLineMaterial.clone();
  528. matLineRed.color.set( 0xff0000 );
  529. var matLineGreen = gizmoLineMaterial.clone();
  530. matLineGreen.color.set( 0x00ff00 );
  531. var matLineBlue = gizmoLineMaterial.clone();
  532. matLineBlue.color.set( 0x0000ff );
  533. var matLineCyan = gizmoLineMaterial.clone();
  534. matLineCyan.color.set( 0x00ffff );
  535. var matLineMagenta = gizmoLineMaterial.clone();
  536. matLineMagenta.color.set( 0xff00ff );
  537. var matLineYellow = gizmoLineMaterial.clone();
  538. matLineYellow.color.set( 0xffff00 );
  539. var matLineGray = gizmoLineMaterial.clone();
  540. matLineGray.color.set( 0x787878 );
  541. var matLineYellowTransparent = matLineYellow.clone();
  542. matLineYellowTransparent.opacity = 0.25;
  543. // reusable geometry
  544. var arrowGeometry = new CylinderBufferGeometry( 0, 0.05, 0.2, 12, 1, false );
  545. var scaleHandleGeometry = new BoxBufferGeometry( 0.125, 0.125, 0.125 );
  546. var lineGeometry = new BufferGeometry( );
  547. //lineGeometry.addAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 1, 0, 0 ], 3 ) );
  548. lineGeometry.addAttribute("position", new THREE.BufferAttribute(new Float32Array([ 0, 0, 0, 1, 0, 0]), 3 ))
  549. var CircleGeometry = function ( radius, arc ) {
  550. var geometry = new BufferGeometry( );
  551. var vertices = [];
  552. for ( var i = 0; i <= 64 * arc; ++ i ) {
  553. vertices.push( 0, Math.cos( i / 32 * Math.PI ) * radius, Math.sin( i / 32 * Math.PI ) * radius );
  554. }
  555. //geometry.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  556. geometry.addAttribute("position", new THREE.BufferAttribute(new Float32Array(vertices), 3 ))
  557. return geometry;
  558. };
  559. // Special geometry for transform helper. If scaled with position vector it spans from [0,0,0] to position
  560. var TranslateHelperGeometry = function () {
  561. var geometry = new BufferGeometry();
  562. //geometry.addAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 1, 1, 1 ], 3 ) );
  563. geometry.addAttribute("position", new THREE.BufferAttribute(new Float32Array([ 0, 0, 0, 1, 1, 1 ]), 3 ))
  564. return geometry;
  565. };
  566. // Gizmo definitions - custom hierarchy definitions for setupGizmo() function
  567. var gizmoTranslate = {
  568. X: [
  569. [ new Mesh( arrowGeometry, matRed ), [ 1, 0, 0 ], [ 0, 0, - Math.PI / 2 ], null, 'fwd' ],
  570. //[ new Mesh( arrowGeometry, matRed ), [ 1, 0, 0 ], [ 0, 0, Math.PI / 2 ], null, 'bwd' ],
  571. [ new Line( lineGeometry, matLineRed ) ]
  572. ],
  573. Y: [
  574. [ new Mesh( arrowGeometry, matGreen ), [ 0, 1, 0 ], null, null, 'fwd' ],
  575. //[ new Mesh( arrowGeometry, matGreen ), [ 0, 1, 0 ], [ Math.PI, 0, 0 ], null, 'bwd' ],
  576. [ new Line( lineGeometry, matLineGreen ), null, [ 0, 0, Math.PI / 2 ]]
  577. ],
  578. Z: [
  579. [ new Mesh( arrowGeometry, matBlue ), [ 0, 0, 1 ], [ Math.PI / 2, 0, 0 ], null, 'fwd' ],
  580. //[ new Mesh( arrowGeometry, matBlue ), [ 0, 0, 1 ], [ - Math.PI / 2, 0, 0 ], null, 'bwd' ],
  581. [ new Line( lineGeometry, matLineBlue ), null, [ 0, - Math.PI / 2, 0 ]]
  582. ],
  583. /* XYZ: [
  584. [ new Mesh( new OctahedronBufferGeometry( 0.1, 0 ), matWhiteTransparent.clone() ), [ 0, 0, 0 ], [ 0, 0, 0 ]]
  585. ], */
  586. XY: [
  587. [ new Mesh( new PlaneBufferGeometry( 0.295, 0.295 ), matYellowTransparent.clone() ), [ 0.15, 0.15, 0 ]],
  588. [ new Line( lineGeometry, matLineYellow ), [ 0.18, 0.3, 0 ], null, [ 0.125, 1, 1 ]],
  589. [ new Line( lineGeometry, matLineYellow ), [ 0.3, 0.18, 0 ], [ 0, 0, Math.PI / 2 ], [ 0.125, 1, 1 ]]
  590. ],
  591. YZ: [
  592. [ new Mesh( new PlaneBufferGeometry( 0.295, 0.295 ), matCyanTransparent.clone() ), [ 0, 0.15, 0.15 ], [ 0, Math.PI / 2, 0 ]],
  593. [ new Line( lineGeometry, matLineCyan ), [ 0, 0.18, 0.3 ], [ 0, 0, Math.PI / 2 ], [ 0.125, 1, 1 ]],
  594. [ new Line( lineGeometry, matLineCyan ), [ 0, 0.3, 0.18 ], [ 0, - Math.PI / 2, 0 ], [ 0.125, 1, 1 ]]
  595. ],
  596. XZ: [
  597. [ new Mesh( new PlaneBufferGeometry( 0.295, 0.295 ), matMagentaTransparent.clone() ), [ 0.15, 0, 0.15 ], [ - Math.PI / 2, 0, 0 ]],
  598. [ new Line( lineGeometry, matLineMagenta ), [ 0.18, 0, 0.3 ], null, [ 0.125, 1, 1 ]],
  599. [ new Line( lineGeometry, matLineMagenta ), [ 0.3, 0, 0.18 ], [ 0, - Math.PI / 2, 0 ], [ 0.125, 1, 1 ]]
  600. ]
  601. };
  602. var pickerTranslate = {
  603. X: [
  604. [ new Mesh( new CylinderBufferGeometry( 0.2, 0, 1, 4, 1, false ), matInvisible ), [ 0.6, 0, 0 ], [ 0, 0, - Math.PI / 2 ]]
  605. ],
  606. Y: [
  607. [ new Mesh( new CylinderBufferGeometry( 0.2, 0, 1, 4, 1, false ), matInvisible ), [ 0, 0.6, 0 ]]
  608. ],
  609. Z: [
  610. [ new Mesh( new CylinderBufferGeometry( 0.2, 0, 1, 4, 1, false ), matInvisible ), [ 0, 0, 0.6 ], [ Math.PI / 2, 0, 0 ]]
  611. ],
  612. /* XYZ: [
  613. [ new Mesh( new OctahedronBufferGeometry( 0.2, 0 ), matInvisible ) ]
  614. ], */
  615. XY: [
  616. [ new Mesh( new PlaneBufferGeometry( 0.4, 0.4 ), matInvisible ), [ 0.2, 0.2, 0 ]]
  617. ],
  618. YZ: [
  619. [ new Mesh( new PlaneBufferGeometry( 0.4, 0.4 ), matInvisible ), [ 0, 0.2, 0.2 ], [ 0, Math.PI / 2, 0 ]]
  620. ],
  621. XZ: [
  622. [ new Mesh( new PlaneBufferGeometry( 0.4, 0.4 ), matInvisible ), [ 0.2, 0, 0.2 ], [ - Math.PI / 2, 0, 0 ]]
  623. ]
  624. };
  625. var helperTranslate = {
  626. START: [
  627. [ new Mesh( new OctahedronBufferGeometry( 0.01, 2 ), matHelper ), null, null, null, 'helper' ]
  628. ],
  629. END: [
  630. [ new Mesh( new OctahedronBufferGeometry( 0.01, 2 ), matHelper ), null, null, null, 'helper' ]
  631. ],
  632. DELTA: [
  633. [ new Line( TranslateHelperGeometry(), matHelper ), null, null, null, 'helper' ]
  634. ],
  635. X: [
  636. [ new Line( lineGeometry, matHelper.clone() ), [ - 1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ]
  637. ],
  638. Y: [
  639. [ new Line( lineGeometry, matHelper.clone() ), [ 0, - 1e3, 0 ], [ 0, 0, Math.PI / 2 ], [ 1e6, 1, 1 ], 'helper' ]
  640. ],
  641. Z: [
  642. [ new Line( lineGeometry, matHelper.clone() ), [ 0, 0, - 1e3 ], [ 0, - Math.PI / 2, 0 ], [ 1e6, 1, 1 ], 'helper' ]
  643. ]
  644. };
  645. var gizmoRotate = {
  646. X: [
  647. [ new Line( CircleGeometry( 1, 0.5 ), matLineRed ) ],
  648. [ new Mesh( new OctahedronBufferGeometry( 0.04, 0 ), matRed ), [ 0, 0, 0.99 ], null, [ 1, 3, 1 ]],
  649. ],
  650. Y: [
  651. [ new Line( CircleGeometry( 1, 0.5 ), matLineGreen ), null, [ 0, 0, - Math.PI / 2 ]],
  652. [ new Mesh( new OctahedronBufferGeometry( 0.04, 0 ), matGreen ), [ 0, 0, 0.99 ], null, [ 3, 1, 1 ]],
  653. ],
  654. Z: [
  655. [ new Line( CircleGeometry( 1, 0.5 ), matLineBlue ), null, [ 0, Math.PI / 2, 0 ]],
  656. [ new Mesh( new OctahedronBufferGeometry( 0.04, 0 ), matBlue ), [ 0.99, 0, 0 ], null, [ 1, 3, 1 ]],
  657. ],
  658. E: [
  659. [ new Line( CircleGeometry( 1.25, 1 ), matLineYellowTransparent ), null, [ 0, Math.PI / 2, 0 ]],
  660. [ new Mesh( new CylinderBufferGeometry( 0.03, 0, 0.15, 4, 1, false ), matLineYellowTransparent ), [ 1.17, 0, 0 ], [ 0, 0, - Math.PI / 2 ], [ 1, 1, 0.001 ]],
  661. [ new Mesh( new CylinderBufferGeometry( 0.03, 0, 0.15, 4, 1, false ), matLineYellowTransparent ), [ - 1.17, 0, 0 ], [ 0, 0, Math.PI / 2 ], [ 1, 1, 0.001 ]],
  662. [ new Mesh( new CylinderBufferGeometry( 0.03, 0, 0.15, 4, 1, false ), matLineYellowTransparent ), [ 0, - 1.17, 0 ], [ Math.PI, 0, 0 ], [ 1, 1, 0.001 ]],
  663. [ new Mesh( new CylinderBufferGeometry( 0.03, 0, 0.15, 4, 1, false ), matLineYellowTransparent ), [ 0, 1.17, 0 ], [ 0, 0, 0 ], [ 1, 1, 0.001 ]],
  664. ],
  665. XYZE: [
  666. [ new Line( CircleGeometry( 1, 1 ), matLineGray ), null, [ 0, Math.PI / 2, 0 ]]
  667. ]
  668. };
  669. var helperRotate = {
  670. AXIS: [
  671. [ new Line( lineGeometry, matHelper.clone() ), [ - 1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ]
  672. ]
  673. };
  674. var pickerRotate = {
  675. X: [
  676. [ new Mesh( new TorusBufferGeometry( 1, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ 0, - Math.PI / 2, - Math.PI / 2 ]],
  677. ],
  678. Y: [
  679. [ new Mesh( new TorusBufferGeometry( 1, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ Math.PI / 2, 0, 0 ]],
  680. ],
  681. Z: [
  682. [ new Mesh( new TorusBufferGeometry( 1, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  683. ],
  684. E: [
  685. [ new Mesh( new TorusBufferGeometry( 1.25, 0.1, 2, 24 ), matInvisible ) ]
  686. ],
  687. XYZE: [
  688. [ new Mesh( new SphereBufferGeometry( 0.7, 10, 8 ), matInvisible ) ]
  689. ]
  690. };
  691. var gizmoScale = {
  692. X: [
  693. [ new Mesh( scaleHandleGeometry, matRed ), [ 0.8, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  694. [ new Line( lineGeometry, matLineRed ), null, null, [ 0.8, 1, 1 ]]
  695. ],
  696. Y: [
  697. [ new Mesh( scaleHandleGeometry, matGreen ), [ 0, 0.8, 0 ]],
  698. [ new Line( lineGeometry, matLineGreen ), null, [ 0, 0, Math.PI / 2 ], [ 0.8, 1, 1 ]]
  699. ],
  700. Z: [
  701. [ new Mesh( scaleHandleGeometry, matBlue ), [ 0, 0, 0.8 ], [ Math.PI / 2, 0, 0 ]],
  702. [ new Line( lineGeometry, matLineBlue ), null, [ 0, - Math.PI / 2, 0 ], [ 0.8, 1, 1 ]]
  703. ],
  704. XY: [
  705. [ new Mesh( scaleHandleGeometry, matYellowTransparent ), [ 0.85, 0.85, 0 ], null, [ 2, 2, 0.2 ]],
  706. [ new Line( lineGeometry, matLineYellow ), [ 0.855, 0.98, 0 ], null, [ 0.125, 1, 1 ]],
  707. [ new Line( lineGeometry, matLineYellow ), [ 0.98, 0.855, 0 ], [ 0, 0, Math.PI / 2 ], [ 0.125, 1, 1 ]]
  708. ],
  709. /* YZ: [
  710. [ new Mesh( scaleHandleGeometry, matCyanTransparent ), [ 0, 0.85, 0.85 ], null, [ 0.2, 2, 2 ]],
  711. [ new Line( lineGeometry, matLineCyan ), [ 0, 0.855, 0.98 ], [ 0, 0, Math.PI / 2 ], [ 0.125, 1, 1 ]],
  712. [ new Line( lineGeometry, matLineCyan ), [ 0, 0.98, 0.855 ], [ 0, - Math.PI / 2, 0 ], [ 0.125, 1, 1 ]]
  713. ],
  714. XZ: [
  715. [ new Mesh( scaleHandleGeometry, matMagentaTransparent ), [ 0.85, 0, 0.85 ], null, [ 2, 0.2, 2 ]],
  716. [ new Line( lineGeometry, matLineMagenta ), [ 0.855, 0, 0.98 ], null, [ 0.125, 1, 1 ]],
  717. [ new Line( lineGeometry, matLineMagenta ), [ 0.98, 0, 0.855 ], [ 0, - Math.PI / 2, 0 ], [ 0.125, 1, 1 ]]
  718. ],
  719. XYZX: [
  720. [ new Mesh( new BoxBufferGeometry( 0.125, 0.125, 0.125 ), matWhiteTransparent.clone() ), [ 1.1, 0, 0 ]],
  721. ],*/
  722. XYZY: [
  723. [ new Mesh( new BoxBufferGeometry( 0.125, 0.125, 0.125 ), matWhiteTransparent.clone() ), [ 0, 1.1, 0 ]],
  724. ]/* ,
  725. XYZZ: [
  726. [ new Mesh( new BoxBufferGeometry( 0.125, 0.125, 0.125 ), matWhiteTransparent.clone() ), [ 0, 0, 1.1 ]],
  727. ] */
  728. };
  729. var pickerScale = {
  730. X: [
  731. [ new Mesh( new CylinderBufferGeometry( 0.2, 0, 0.8, 4, 1, false ), matInvisible ), [ 0.5, 0, 0 ], [ 0, 0, - Math.PI / 2 ]]
  732. ],
  733. Y: [
  734. [ new Mesh( new CylinderBufferGeometry( 0.2, 0, 0.8, 4, 1, false ), matInvisible ), [ 0, 0.5, 0 ]]
  735. ],
  736. Z: [
  737. [ new Mesh( new CylinderBufferGeometry( 0.2, 0, 0.8, 4, 1, false ), matInvisible ), [ 0, 0, 0.5 ], [ Math.PI / 2, 0, 0 ]]
  738. ],
  739. XY: [
  740. [ new Mesh( scaleHandleGeometry, matInvisible ), [ 0.85, 0.85, 0 ], null, [ 3, 3, 0.2 ]],
  741. ],
  742. /*YZ: [
  743. [ new Mesh( scaleHandleGeometry, matInvisible ), [ 0, 0.85, 0.85 ], null, [ 0.2, 3, 3 ]],
  744. ],
  745. XZ: [
  746. [ new Mesh( scaleHandleGeometry, matInvisible ), [ 0.85, 0, 0.85 ], null, [ 3, 0.2, 3 ]],
  747. ],
  748. XYZX: [
  749. [ new Mesh( new BoxBufferGeometry( 0.2, 0.2, 0.2 ), matInvisible ), [ 1.1, 0, 0 ]],
  750. ],*/
  751. XYZY: [
  752. [ new Mesh( new BoxBufferGeometry( 0.2, 0.2, 0.2 ), matInvisible ), [ 0, 1.1, 0 ]],
  753. ]/* ,
  754. XYZZ: [
  755. [ new Mesh( new BoxBufferGeometry( 0.2, 0.2, 0.2 ), matInvisible ), [ 0, 0, 1.1 ]],
  756. ] */
  757. };
  758. var helperScale = {
  759. X: [
  760. [ new Line( lineGeometry, matHelper.clone() ), [ - 1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ]
  761. ],
  762. Y: [
  763. [ new Line( lineGeometry, matHelper.clone() ), [ 0, - 1e3, 0 ], [ 0, 0, Math.PI / 2 ], [ 1e6, 1, 1 ], 'helper' ]
  764. ],
  765. Z: [
  766. [ new Line( lineGeometry, matHelper.clone() ), [ 0, 0, - 1e3 ], [ 0, - Math.PI / 2, 0 ], [ 1e6, 1, 1 ], 'helper' ]
  767. ]
  768. };
  769. // Creates an Object3D with gizmos described in custom hierarchy definition.
  770. var setupGizmo = function ( gizmoMap ) {
  771. var gizmo = new Object3D();
  772. for ( var name in gizmoMap ) {
  773. for ( var i = gizmoMap[ name ].length; i --; ) {
  774. var object = gizmoMap[ name ][ i ][ 0 ].clone();
  775. var position = gizmoMap[ name ][ i ][ 1 ];
  776. var rotation = gizmoMap[ name ][ i ][ 2 ];
  777. var scale = gizmoMap[ name ][ i ][ 3 ];
  778. var tag = gizmoMap[ name ][ i ][ 4 ];
  779. // name and tag properties are essential for picking and updating logic.
  780. object.name = name;
  781. object.tag = tag;
  782. if ( position ) {
  783. object.position.set( position[ 0 ], position[ 1 ], position[ 2 ] );
  784. }
  785. if ( rotation ) {
  786. object.rotation.set( rotation[ 0 ], rotation[ 1 ], rotation[ 2 ] );
  787. }
  788. if ( scale ) {
  789. object.scale.set( scale[ 0 ], scale[ 1 ], scale[ 2 ] );
  790. }
  791. object.updateMatrix();
  792. var tempGeometry = object.geometry.clone();
  793. tempGeometry.applyMatrix( object.matrix );
  794. object.geometry = tempGeometry;
  795. object.renderOrder = Infinity;
  796. object.position.set( 0, 0, 0 );
  797. object.rotation.set( 0, 0, 0 );
  798. object.scale.set( 1, 1, 1 );
  799. gizmo.add( object );
  800. }
  801. }
  802. return gizmo;
  803. };
  804. // Reusable utility variables
  805. var tempVector = new Vector3( 0, 0, 0 );
  806. var tempEuler = new Euler();
  807. var alignVector = new Vector3( 0, 1, 0 );
  808. var zeroVector = new Vector3( 0, 0, 0 );
  809. var lookAtMatrix = new Matrix4();
  810. var tempQuaternion = new Quaternion();
  811. var tempQuaternion2 = new Quaternion();
  812. var identityQuaternion = new Quaternion();
  813. var unitX = new Vector3( 1, 0, 0 );
  814. var unitY = new Vector3( 0, 1, 0 );
  815. var unitZ = new Vector3( 0, 0, 1 );
  816. // Gizmo creation
  817. this.gizmo = {};
  818. this.picker = {};
  819. this.helper = {};
  820. this.add( this.gizmo[ "translate" ] = setupGizmo( gizmoTranslate ) );
  821. this.add( this.gizmo[ "rotate" ] = setupGizmo( gizmoRotate ) );
  822. this.add( this.gizmo[ "scale" ] = setupGizmo( gizmoScale ) );
  823. this.add( this.picker[ "translate" ] = setupGizmo( pickerTranslate ) );
  824. this.add( this.picker[ "rotate" ] = setupGizmo( pickerRotate ) );
  825. this.add( this.picker[ "scale" ] = setupGizmo( pickerScale ) );
  826. this.add( this.helper[ "translate" ] = setupGizmo( helperTranslate ) );
  827. this.add( this.helper[ "rotate" ] = setupGizmo( helperRotate ) );
  828. this.add( this.helper[ "scale" ] = setupGizmo( helperScale ) );
  829. // Pickers should be hidden always
  830. this.picker[ "translate" ].visible = false;
  831. this.picker[ "rotate" ].visible = false;
  832. this.picker[ "scale" ].visible = false;
  833. // updateMatrixWorld will update transformations and appearance of individual handles
  834. this.updateMatrixWorld = function () {
  835. var space = this.space;
  836. if ( this.mode === 'scale' ) space = 'local'; // scale always oriented to local rotation
  837. var quaternion = space === "local" ? this.worldQuaternion : identityQuaternion;
  838. // Show only gizmos for current transform mode
  839. this.gizmo[ "translate" ].visible = this.mode === "translate";
  840. this.gizmo[ "rotate" ].visible = this.mode === "rotate";
  841. this.gizmo[ "scale" ].visible = this.mode === "scale";
  842. this.helper[ "translate" ].visible = this.mode === "translate";
  843. this.helper[ "rotate" ].visible = this.mode === "rotate";
  844. this.helper[ "scale" ].visible = this.mode === "scale";
  845. var handles = [];
  846. handles = handles.concat( this.picker[ this.mode ].children );
  847. handles = handles.concat( this.gizmo[ this.mode ].children );
  848. handles = handles.concat( this.helper[ this.mode ].children );
  849. for ( var i = 0; i < handles.length; i ++ ) {
  850. var handle = handles[ i ];
  851. // hide aligned to camera
  852. handle.visible = true;
  853. handle.rotation.set( 0, 0, 0 );
  854. handle.position.copy( this.worldPosition );
  855. var eyeDistance = this.worldPosition.distanceTo( this.cameraPosition );
  856. //俯视图的透视和距离无关,因此在两种相机切换切换时大小过渡比较困难
  857. if(player.mode == "transitioning" && (player.modeTran.split('-')[0] == "floorplan" || player.modeTran.split('-')[1] == "floorplan")){
  858. eyeDistance = Math.min(eyeDistance, 6.5)//从一开始很小的距离过渡到俯视图50的高度,会变得很大。
  859. }else if(player.mode == "floorplan"){
  860. var flcamera = player.cameraControls.cameras.floorplan;
  861. eyeDistance = (flcamera.right - flcamera.left ) / flcamera.aspect ;
  862. }
  863. handle.scale.set( 1, 1, 1 ).multiplyScalar( eyeDistance * this.size / 7 );
  864. // TODO: simplify helpers and consider decoupling from gizmo
  865. if ( handle.tag === 'helper' ) {
  866. handle.visible = false;
  867. if ( handle.name === 'AXIS' ) {
  868. handle.position.copy( this.worldPositionStart );
  869. handle.visible = !! this.axis;
  870. if ( this.axis === 'X' ) {
  871. tempQuaternion.setFromEuler( tempEuler.set( 0, 0, 0 ) );
  872. handle.quaternion.copy( quaternion ).multiply( tempQuaternion );
  873. if ( Math.abs( alignVector.copy( unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  874. handle.visible = false;
  875. }
  876. }
  877. if ( this.axis === 'Y' ) {
  878. tempQuaternion.setFromEuler( tempEuler.set( 0, 0, Math.PI / 2 ) );
  879. handle.quaternion.copy( quaternion ).multiply( tempQuaternion );
  880. if ( Math.abs( alignVector.copy( unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  881. handle.visible = false;
  882. }
  883. }
  884. if ( this.axis === 'Z' ) {
  885. tempQuaternion.setFromEuler( tempEuler.set( 0, Math.PI / 2, 0 ) );
  886. handle.quaternion.copy( quaternion ).multiply( tempQuaternion );
  887. if ( Math.abs( alignVector.copy( unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  888. handle.visible = false;
  889. }
  890. }
  891. if ( this.axis === 'XYZE' ) {
  892. tempQuaternion.setFromEuler( tempEuler.set( 0, Math.PI / 2, 0 ) );
  893. alignVector.copy( this.rotationAxis );
  894. handle.quaternion.setFromRotationMatrix( lookAtMatrix.lookAt( zeroVector, alignVector, unitY ) );
  895. handle.quaternion.multiply( tempQuaternion );
  896. handle.visible = this.dragging;
  897. }
  898. if ( this.axis === 'E' ) {
  899. handle.visible = false;
  900. }
  901. } else if ( handle.name === 'START' ) {
  902. handle.position.copy( this.worldPositionStart );
  903. handle.visible = this.dragging;
  904. } else if ( handle.name === 'END' ) {
  905. handle.position.copy( this.worldPosition );
  906. handle.visible = this.dragging;
  907. } else if ( handle.name === 'DELTA' ) {
  908. handle.position.copy( this.worldPositionStart );
  909. handle.quaternion.copy( this.worldQuaternionStart );
  910. tempVector.set( 1e-10, 1e-10, 1e-10 ).add( this.worldPositionStart ).sub( this.worldPosition ).multiplyScalar( - 1 );
  911. tempVector.applyQuaternion( this.worldQuaternionStart.clone().inverse() );
  912. handle.scale.copy( tempVector );
  913. handle.visible = this.dragging;
  914. } else {
  915. handle.quaternion.copy( quaternion );
  916. if ( this.dragging ) {
  917. handle.position.copy( this.worldPositionStart );
  918. } else {
  919. handle.position.copy( this.worldPosition );
  920. }
  921. if ( this.axis ) {
  922. handle.visible = this.axis.search( handle.name ) !== - 1;
  923. }
  924. }
  925. // If updating helper, skip rest of the loop
  926. continue;
  927. }
  928. // Align handles to current local or world rotation
  929. handle.quaternion.copy( quaternion );
  930. if ( this.mode === 'translate' || this.mode === 'scale' ) {
  931. // Hide translate and scale axis facing the camera
  932. var AXIS_HIDE_TRESHOLD = 0.99;
  933. var PLANE_HIDE_TRESHOLD = 0.2;
  934. var AXIS_FLIP_TRESHOLD = 0.0;
  935. if(options.dontHideWhenFaceCamera){//xzw add
  936. //正对镜头时不隐藏箭头
  937. }else{
  938. if ( handle.name === 'X' || handle.name === 'XYZX' ) {
  939. if ( Math.abs( alignVector.copy( unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_TRESHOLD ) {
  940. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  941. handle.visible = false;
  942. }
  943. }
  944. if ( handle.name === 'Y' || handle.name === 'XYZY' ) {
  945. if ( Math.abs( alignVector.copy( unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_TRESHOLD ) {
  946. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  947. handle.visible = false;
  948. }
  949. }
  950. if ( handle.name === 'Z' || handle.name === 'XYZZ' ) {
  951. if ( Math.abs( alignVector.copy( unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_TRESHOLD ) {
  952. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  953. handle.visible = false;
  954. }
  955. }
  956. if ( handle.name === 'XY' ) {
  957. if ( Math.abs( alignVector.copy( unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_TRESHOLD ) {
  958. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  959. handle.visible = false;
  960. }
  961. }
  962. if ( handle.name === 'YZ' ) {
  963. if ( Math.abs( alignVector.copy( unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_TRESHOLD ) {
  964. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  965. handle.visible = false;
  966. }
  967. }
  968. if ( handle.name === 'XZ' ) {
  969. if ( Math.abs( alignVector.copy( unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_TRESHOLD ) {
  970. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  971. handle.visible = false;
  972. }
  973. }
  974. }
  975. // Flip translate and scale axis ocluded behind another axis
  976. //xzw 改 去掉反向箭头
  977. if ( handle.name.search( 'X' ) !== - 1 ) {
  978. if ( alignVector.copy( unitX ).applyQuaternion( quaternion ).dot( this.eye ) < AXIS_FLIP_TRESHOLD ) {
  979. /* if ( handle.tag === 'fwd' ) {
  980. handle.visible = false;
  981. } else {
  982. handle.scale.x *= - 1;
  983. }
  984. } else if ( handle.tag === 'bwd' ) {
  985. handle.visible = false;*/
  986. handle.scale.x *= - 1;
  987. }
  988. }
  989. if ( handle.name.search( 'Y' ) !== - 1 ) {
  990. if ( alignVector.copy( unitY ).applyQuaternion( quaternion ).dot( this.eye ) < AXIS_FLIP_TRESHOLD ) {
  991. /* if ( handle.tag === 'fwd' ) {
  992. handle.visible = false;
  993. } else {
  994. handle.scale.y *= - 1;
  995. }
  996. } else if ( handle.tag === 'bwd' ) {
  997. handle.visible = false;
  998. */
  999. handle.scale.y *= - 1;
  1000. }
  1001. }
  1002. if ( handle.name.search( 'Z' ) !== - 1 ) {
  1003. if ( alignVector.copy( unitZ ).applyQuaternion( quaternion ).dot( this.eye ) < AXIS_FLIP_TRESHOLD ) {
  1004. /* if ( handle.tag === 'fwd' ) {
  1005. handle.visible = false;
  1006. } else {
  1007. handle.scale.z *= - 1;
  1008. }
  1009. } else if ( handle.tag === 'bwd' ) {
  1010. handle.visible = false; */
  1011. handle.scale.z *= - 1;
  1012. }
  1013. }
  1014. } else if ( this.mode === 'rotate' ) {
  1015. // Align handles to current local or world rotation
  1016. tempQuaternion2.copy( quaternion );
  1017. alignVector.copy( this.eye ).applyQuaternion( tempQuaternion.copy( quaternion ).inverse() );
  1018. if ( handle.name.search( "E" ) !== - 1 ) {
  1019. handle.quaternion.setFromRotationMatrix( lookAtMatrix.lookAt( this.eye, zeroVector, unitY ) );
  1020. }
  1021. if ( handle.name === 'X' ) {
  1022. tempQuaternion.setFromAxisAngle( unitX, Math.atan2( - alignVector.y, alignVector.z ) );
  1023. tempQuaternion.multiplyQuaternions( tempQuaternion2, tempQuaternion );
  1024. handle.quaternion.copy( tempQuaternion );
  1025. }
  1026. if ( handle.name === 'Y' ) {
  1027. tempQuaternion.setFromAxisAngle( unitY, Math.atan2( alignVector.x, alignVector.z ) );
  1028. tempQuaternion.multiplyQuaternions( tempQuaternion2, tempQuaternion );
  1029. handle.quaternion.copy( tempQuaternion );
  1030. }
  1031. if ( handle.name === 'Z' ) {
  1032. tempQuaternion.setFromAxisAngle( unitZ, Math.atan2( alignVector.y, alignVector.x ) );
  1033. tempQuaternion.multiplyQuaternions( tempQuaternion2, tempQuaternion );
  1034. handle.quaternion.copy( tempQuaternion );
  1035. }
  1036. }
  1037. // Hide disabled axes
  1038. handle.visible = handle.visible && ( handle.name.indexOf( "X" ) === - 1 || this.showX );
  1039. handle.visible = handle.visible && ( handle.name.indexOf( "Y" ) === - 1 || this.showY );
  1040. handle.visible = handle.visible && ( handle.name.indexOf( "Z" ) === - 1 || this.showZ );
  1041. handle.visible = handle.visible && ( handle.name.indexOf( "E" ) === - 1 || ( this.showX && this.showY && this.showZ ) );
  1042. // highlight selected axis
  1043. handle.material._opacity = handle.material._opacity || handle.material.opacity;
  1044. handle.material._color = handle.material._color || handle.material.color.clone();
  1045. handle.material.color.copy( handle.material._color );
  1046. handle.material.opacity = handle.material._opacity;
  1047. if ( ! this.enabled ) {
  1048. handle.material.opacity *= 0.5;
  1049. handle.material.color.lerp( new Color( 1, 1, 1 ), 0.5 );
  1050. } else if ( this.axis ) {
  1051. if ( handle.name === this.axis ) {
  1052. handle.material.opacity = 1.0;
  1053. handle.material.color.lerp( new Color( 1, 1, 1 ), 0.5 );
  1054. } else if ( this.axis.split( '' ).some( function ( a ) {
  1055. return handle.name === a;
  1056. } ) ) {
  1057. handle.material.opacity = 1.0;
  1058. handle.material.color.lerp( new Color( 1, 1, 1 ), 0.5 );
  1059. } else {
  1060. handle.material.opacity *= 0.25;
  1061. handle.material.color.lerp( new Color( 1, 1, 1 ), 0.5 );
  1062. }
  1063. }
  1064. }
  1065. Object3D.prototype.updateMatrixWorld.call( this );
  1066. };
  1067. };
  1068. var TransformControlsPlane = function (options) {
  1069. 'use strict';
  1070. Mesh.call( this,
  1071. new PlaneBufferGeometry( 100000, 100000, 2, 2 ),
  1072. new MeshBasicMaterial( { color: "#ff0000", visible: false, wireframe: false, side: DoubleSide, transparent: true, opacity: 0.2 } )
  1073. );
  1074. this.type = 'TransformControlsPlane';
  1075. var unitX = new Vector3( 1, 0, 0 );
  1076. var unitY = new Vector3( 0, 1, 0 );
  1077. var unitZ = new Vector3( 0, 0, 1 );
  1078. var tempVector = new Vector3();
  1079. var dirVector = new Vector3();
  1080. var alignVector = new Vector3();
  1081. var tempMatrix = new Matrix4();
  1082. var identityQuaternion = new Quaternion();
  1083. this.updateMatrixWorld = function () {
  1084. var space = this.space;
  1085. this.position.copy( this.worldPosition );
  1086. if ( this.mode === 'scale' ) space = 'local'; // scale always oriented to local rotation
  1087. unitX.set( 1, 0, 0 ).applyQuaternion( space === "local" ? this.worldQuaternion : identityQuaternion );
  1088. unitY.set( 0, 1, 0 ).applyQuaternion( space === "local" ? this.worldQuaternion : identityQuaternion );
  1089. unitZ.set( 0, 0, 1 ).applyQuaternion( space === "local" ? this.worldQuaternion : identityQuaternion );
  1090. // Align the plane for current transform mode, axis and space.
  1091. alignVector.copy( unitY );
  1092. switch ( this.mode ) {
  1093. case 'translate':
  1094. case 'scale':
  1095. switch ( this.axis ) {
  1096. case 'X':
  1097. alignVector.copy( this.eye ).cross( unitX );
  1098. dirVector.copy( unitX ).cross( alignVector );
  1099. break;
  1100. case 'Y':
  1101. alignVector.copy( this.eye ).cross( unitY );
  1102. dirVector.copy( unitY ).cross( alignVector );
  1103. break;
  1104. case 'Z':
  1105. alignVector.copy( this.eye ).cross( unitZ );
  1106. dirVector.copy( unitZ ).cross( alignVector );
  1107. break;
  1108. case 'XY':
  1109. dirVector.copy( unitZ );
  1110. break;
  1111. case 'YZ':
  1112. dirVector.copy( unitX );
  1113. break;
  1114. case 'XZ':
  1115. alignVector.copy( unitZ );
  1116. dirVector.copy( unitY );
  1117. break;
  1118. case 'XYZ':
  1119. case 'E':
  1120. default: //xzw add for scale xyzz
  1121. dirVector.set( 0, 0, 0 );
  1122. break;
  1123. }
  1124. break;
  1125. case 'rotate':
  1126. default:
  1127. // special case for rotate
  1128. dirVector.set( 0, 0, 0 );
  1129. }
  1130. if ( dirVector.length() === 0 ) {
  1131. // If in rotate mode, make the plane parallel to camera
  1132. this.quaternion.copy( this.cameraQuaternion );
  1133. } else {
  1134. tempMatrix.lookAt( tempVector.set( 0, 0, 0 ), dirVector, alignVector );
  1135. this.quaternion.setFromRotationMatrix( tempMatrix );
  1136. }
  1137. Object3D.prototype.updateMatrixWorld.call( this );
  1138. };
  1139. };
  1140. //export { TransformControls, TransformControlsGizmo, TransformControlsPlane };
  1141. TransformControls.init = function(THREE){
  1142. BoxBufferGeometry = THREE.BoxBufferGeometry,
  1143. BufferGeometry = THREE.BufferGeometry,
  1144. Color = THREE.Color,
  1145. CylinderBufferGeometry = THREE.CylinderBufferGeometry,
  1146. DoubleSide = THREE.DoubleSide,
  1147. Euler = THREE.Euler,
  1148. Float32BufferAttribute = THREE.Float32BufferAttribute,
  1149. Line = THREE.Line,
  1150. LineBasicMaterial = THREE.LineBasicMaterial,
  1151. Matrix4 = THREE.Matrix4,
  1152. Mesh = THREE.Mesh,
  1153. MeshBasicMaterial = THREE.MeshBasicMaterial,
  1154. Object3D = THREE.Object3D,
  1155. //OctahedronBufferGeometry = THREE.OctahedronBufferGeometry,
  1156. PlaneBufferGeometry = THREE.PlaneBufferGeometry,
  1157. Quaternion = THREE.Quaternion,
  1158. Raycaster = THREE.Raycaster,
  1159. SphereBufferGeometry = THREE.SphereBufferGeometry,
  1160. TorusBufferGeometry = THREE.TorusBufferGeometry,
  1161. Vector3 = THREE.Vector3;
  1162. Vector2 = THREE.Vector2;
  1163. TransformControls.prototype = Object.assign( Object.create( Object3D.prototype ), {
  1164. constructor: TransformControls,
  1165. isTransformControls: true
  1166. } );
  1167. TransformControlsGizmo.prototype = Object.assign( Object.create( Object3D.prototype ), {
  1168. constructor: TransformControlsGizmo,
  1169. isTransformControlsGizmo: true
  1170. } );
  1171. TransformControlsPlane.prototype = Object.assign( Object.create( Mesh.prototype ), {
  1172. constructor: TransformControlsPlane,
  1173. isTransformControlsPlane: true
  1174. } );
  1175. //----------
  1176. function PolyhedronBufferGeometry( vertices, indices, radius, detail ) {
  1177. BufferGeometry.call( this );
  1178. this.type = 'PolyhedronBufferGeometry';
  1179. this.parameters = {
  1180. vertices: vertices,
  1181. indices: indices,
  1182. radius: radius,
  1183. detail: detail
  1184. };
  1185. radius = radius || 1;
  1186. detail = detail || 0;
  1187. // default buffer data
  1188. var vertexBuffer = [];
  1189. var uvBuffer = [];
  1190. // the subdivision creates the vertex buffer data
  1191. subdivide( detail );
  1192. // all vertices should lie on a conceptual sphere with a given radius
  1193. appplyRadius( radius );
  1194. // finally, create the uv data
  1195. generateUVs();
  1196. // build non-indexed geometry
  1197. this.addAttribute("position", new THREE.BufferAttribute(new Float32Array(vertexBuffer), 3 ))
  1198. this.addAttribute( 'normal', new THREE.BufferAttribute( new Float32Array(vertexBuffer), 3 ) );
  1199. this.addAttribute( 'uv', new THREE.BufferAttribute( new Float32Array(uvBuffer), 2 ) );
  1200. if ( detail === 0 ) {
  1201. this.computeVertexNormals(); // flat normals
  1202. } else {
  1203. this.normalizeNormals(); // smooth normals
  1204. }
  1205. // helper functions
  1206. function subdivide( detail ) {
  1207. var a = new Vector3();
  1208. var b = new Vector3();
  1209. var c = new Vector3();
  1210. // iterate over all faces and apply a subdivison with the given detail value
  1211. for ( var i = 0; i < indices.length; i += 3 ) {
  1212. // get the vertices of the face
  1213. getVertexByIndex( indices[ i + 0 ], a );
  1214. getVertexByIndex( indices[ i + 1 ], b );
  1215. getVertexByIndex( indices[ i + 2 ], c );
  1216. // perform subdivision
  1217. subdivideFace( a, b, c, detail );
  1218. }
  1219. }
  1220. function subdivideFace( a, b, c, detail ) {
  1221. var cols = Math.pow( 2, detail );
  1222. // we use this multidimensional array as a data structure for creating the subdivision
  1223. var v = [];
  1224. var i, j;
  1225. // construct all of the vertices for this subdivision
  1226. for ( i = 0; i <= cols; i ++ ) {
  1227. v[ i ] = [];
  1228. var aj = a.clone().lerp( c, i / cols );
  1229. var bj = b.clone().lerp( c, i / cols );
  1230. var rows = cols - i;
  1231. for ( j = 0; j <= rows; j ++ ) {
  1232. if ( j === 0 && i === cols ) {
  1233. v[ i ][ j ] = aj;
  1234. } else {
  1235. v[ i ][ j ] = aj.clone().lerp( bj, j / rows );
  1236. }
  1237. }
  1238. }
  1239. // construct all of the faces
  1240. for ( i = 0; i < cols; i ++ ) {
  1241. for ( j = 0; j < 2 * ( cols - i ) - 1; j ++ ) {
  1242. var k = Math.floor( j / 2 );
  1243. if ( j % 2 === 0 ) {
  1244. pushVertex( v[ i ][ k + 1 ] );
  1245. pushVertex( v[ i + 1 ][ k ] );
  1246. pushVertex( v[ i ][ k ] );
  1247. } else {
  1248. pushVertex( v[ i ][ k + 1 ] );
  1249. pushVertex( v[ i + 1 ][ k + 1 ] );
  1250. pushVertex( v[ i + 1 ][ k ] );
  1251. }
  1252. }
  1253. }
  1254. }
  1255. function appplyRadius( radius ) {
  1256. var vertex = new Vector3();
  1257. // iterate over the entire buffer and apply the radius to each vertex
  1258. for ( var i = 0; i < vertexBuffer.length; i += 3 ) {
  1259. vertex.x = vertexBuffer[ i + 0 ];
  1260. vertex.y = vertexBuffer[ i + 1 ];
  1261. vertex.z = vertexBuffer[ i + 2 ];
  1262. vertex.normalize().multiplyScalar( radius );
  1263. vertexBuffer[ i + 0 ] = vertex.x;
  1264. vertexBuffer[ i + 1 ] = vertex.y;
  1265. vertexBuffer[ i + 2 ] = vertex.z;
  1266. }
  1267. }
  1268. function generateUVs() {
  1269. var vertex = new Vector3();
  1270. for ( var i = 0; i < vertexBuffer.length; i += 3 ) {
  1271. vertex.x = vertexBuffer[ i + 0 ];
  1272. vertex.y = vertexBuffer[ i + 1 ];
  1273. vertex.z = vertexBuffer[ i + 2 ];
  1274. var u = azimuth( vertex ) / 2 / Math.PI + 0.5;
  1275. var v = inclination( vertex ) / Math.PI + 0.5;
  1276. uvBuffer.push( u, 1 - v );
  1277. }
  1278. correctUVs();
  1279. correctSeam();
  1280. }
  1281. function correctSeam() {
  1282. // handle case when face straddles the seam, see #3269
  1283. for ( var i = 0; i < uvBuffer.length; i += 6 ) {
  1284. // uv data of a single face
  1285. var x0 = uvBuffer[ i + 0 ];
  1286. var x1 = uvBuffer[ i + 2 ];
  1287. var x2 = uvBuffer[ i + 4 ];
  1288. var max = Math.max( x0, x1, x2 );
  1289. var min = Math.min( x0, x1, x2 );
  1290. // 0.9 is somewhat arbitrary
  1291. if ( max > 0.9 && min < 0.1 ) {
  1292. if ( x0 < 0.2 ) uvBuffer[ i + 0 ] += 1;
  1293. if ( x1 < 0.2 ) uvBuffer[ i + 2 ] += 1;
  1294. if ( x2 < 0.2 ) uvBuffer[ i + 4 ] += 1;
  1295. }
  1296. }
  1297. }
  1298. function pushVertex( vertex ) {
  1299. vertexBuffer.push( vertex.x, vertex.y, vertex.z );
  1300. }
  1301. function getVertexByIndex( index, vertex ) {
  1302. var stride = index * 3;
  1303. vertex.x = vertices[ stride + 0 ];
  1304. vertex.y = vertices[ stride + 1 ];
  1305. vertex.z = vertices[ stride + 2 ];
  1306. }
  1307. function correctUVs() {
  1308. var a = new Vector3();
  1309. var b = new Vector3();
  1310. var c = new Vector3();
  1311. var centroid = new Vector3();
  1312. var uvA = new Vector2();
  1313. var uvB = new Vector2();
  1314. var uvC = new Vector2();
  1315. for ( var i = 0, j = 0; i < vertexBuffer.length; i += 9, j += 6 ) {
  1316. a.set( vertexBuffer[ i + 0 ], vertexBuffer[ i + 1 ], vertexBuffer[ i + 2 ] );
  1317. b.set( vertexBuffer[ i + 3 ], vertexBuffer[ i + 4 ], vertexBuffer[ i + 5 ] );
  1318. c.set( vertexBuffer[ i + 6 ], vertexBuffer[ i + 7 ], vertexBuffer[ i + 8 ] );
  1319. uvA.set( uvBuffer[ j + 0 ], uvBuffer[ j + 1 ] );
  1320. uvB.set( uvBuffer[ j + 2 ], uvBuffer[ j + 3 ] );
  1321. uvC.set( uvBuffer[ j + 4 ], uvBuffer[ j + 5 ] );
  1322. centroid.copy( a ).add( b ).add( c ).divideScalar( 3 );
  1323. var azi = azimuth( centroid );
  1324. correctUV( uvA, j + 0, a, azi );
  1325. correctUV( uvB, j + 2, b, azi );
  1326. correctUV( uvC, j + 4, c, azi );
  1327. }
  1328. }
  1329. function correctUV( uv, stride, vector, azimuth ) {
  1330. if ( ( azimuth < 0 ) && ( uv.x === 1 ) ) {
  1331. uvBuffer[ stride ] = uv.x - 1;
  1332. }
  1333. if ( ( vector.x === 0 ) && ( vector.z === 0 ) ) {
  1334. uvBuffer[ stride ] = azimuth / 2 / Math.PI + 0.5;
  1335. }
  1336. }
  1337. // Angle around the Y axis, counter-clockwise when looking from above.
  1338. function azimuth( vector ) {
  1339. return Math.atan2( vector.z, - vector.x );
  1340. }
  1341. // Angle above the XZ plane.
  1342. function inclination( vector ) {
  1343. return Math.atan2( - vector.y, Math.sqrt( ( vector.x * vector.x ) + ( vector.z * vector.z ) ) );
  1344. }
  1345. }
  1346. PolyhedronBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
  1347. PolyhedronBufferGeometry.prototype.constructor = PolyhedronBufferGeometry;
  1348. OctahedronBufferGeometry = function( radius, detail ) {
  1349. var vertices = [
  1350. 1, 0, 0, - 1, 0, 0, 0, 1, 0,
  1351. 0, - 1, 0, 0, 0, 1, 0, 0, - 1
  1352. ];
  1353. var indices = [
  1354. 0, 2, 4, 0, 4, 3, 0, 3, 5,
  1355. 0, 5, 2, 1, 2, 5, 1, 5, 3,
  1356. 1, 3, 4, 1, 4, 2
  1357. ];
  1358. PolyhedronBufferGeometry.call( this, vertices, indices, radius, detail );
  1359. this.type = 'OctahedronBufferGeometry';
  1360. this.parameters = {
  1361. radius: radius,
  1362. detail: detail
  1363. };
  1364. }
  1365. OctahedronBufferGeometry.prototype = Object.create( PolyhedronBufferGeometry.prototype );
  1366. OctahedronBufferGeometry.prototype.constructor = OctahedronBufferGeometry;
  1367. }
  1368. window.TransformControls = TransformControls;
  1369. }()