TransformControls.js 58 KB

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