TransformControls.js 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212
  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 加,否则会反向-----three官网也有问题----2025.4又改了一下(之前改完没测试吗=,=),经测试没问题---
  265. //object.scale.x < 0 && (offset.x *= -1)
  266. object.scale.y < 0 && (offset.x *= -1, offset.y *= -1)
  267. object.scale.z < 0 && (offset.x *= -1, offset.z *= -1)
  268. offset.applyQuaternion( quaternionStart ).divide( parentScale );
  269. } else {
  270. offset.applyQuaternion( parentQuaternionInv ).divide( parentScale );
  271. }
  272. object.position.copy( offset ).add( positionStart );
  273. // Apply translation snap
  274. if ( this.translationSnap ) {
  275. if ( space === 'local' ) {
  276. object.position.applyQuaternion( _tempQuaternion.copy( quaternionStart ).inverse() );
  277. if ( axis.search( 'X' ) !== - 1 ) {
  278. object.position.x = Math.round( object.position.x / this.translationSnap ) * this.translationSnap;
  279. }
  280. if ( axis.search( 'Y' ) !== - 1 ) {
  281. object.position.y = Math.round( object.position.y / this.translationSnap ) * this.translationSnap;
  282. }
  283. if ( axis.search( 'Z' ) !== - 1 ) {
  284. object.position.z = Math.round( object.position.z / this.translationSnap ) * this.translationSnap;
  285. }
  286. object.position.applyQuaternion( quaternionStart );
  287. }
  288. if ( space === 'world' ) {
  289. if ( object.parent ) {
  290. object.position.add( _tempVector.setFromMatrixPosition( object.parent.matrixWorld ) );
  291. }
  292. if ( axis.search( 'X' ) !== - 1 ) {
  293. object.position.x = Math.round( object.position.x / this.translationSnap ) * this.translationSnap;
  294. }
  295. if ( axis.search( 'Y' ) !== - 1 ) {
  296. object.position.y = Math.round( object.position.y / this.translationSnap ) * this.translationSnap;
  297. }
  298. if ( axis.search( 'Z' ) !== - 1 ) {
  299. object.position.z = Math.round( object.position.z / this.translationSnap ) * this.translationSnap;
  300. }
  301. if ( object.parent ) {
  302. object.position.sub( _tempVector.setFromMatrixPosition( object.parent.matrixWorld ) );
  303. }
  304. }
  305. }
  306. //add:
  307. this.dispatchEvent({type:'positionChanged', object })
  308. //this.linesAssistance.updateTransform(this.object)
  309. } else if ( mode === 'scale' ) {
  310. if ( axis.search( 'XYZ' ) !== - 1 ) {
  311. var d = pointEnd.length() / pointStart.length();
  312. if ( pointEnd.dot( pointStart ) < 0 ) d *= - 1;
  313. if(options.NoScaleZ){//xzw add
  314. _tempVector2.set( d, d, 1 );
  315. }else{
  316. _tempVector2.set( d, d, d );
  317. }
  318. }else if ( axis.search( 'XY' ) !== - 1 ) { //add 等比例for plane
  319. var d = pointEnd.length() / pointStart.length();
  320. if ( pointEnd.dot( pointStart ) < 0 ) d *= - 1;
  321. _tempVector2.set( d, d, 1 );
  322. }else {
  323. _tempVector.copy( pointStart );
  324. _tempVector2.copy( pointEnd );
  325. _tempVector.applyQuaternion( worldQuaternionInv );
  326. _tempVector2.applyQuaternion( worldQuaternionInv );
  327. _tempVector2.divide( _tempVector );
  328. if ( axis.search( 'X' ) === - 1 ) {
  329. _tempVector2.x = 1;
  330. }
  331. if ( axis.search( 'Y' ) === - 1 ) {
  332. _tempVector2.y = 1;
  333. }
  334. if ( axis.search( 'Z' ) === - 1 ) {
  335. _tempVector2.z = 1;
  336. }
  337. }
  338. // Apply scale
  339. object.scale.copy( scaleStart ).multiply( _tempVector2 );
  340. if ( this.scaleSnap ) {
  341. if ( axis.search( 'X' ) !== - 1 ) {
  342. object.scale.x = Math.round( object.scale.x / this.scaleSnap ) * this.scaleSnap || this.scaleSnap;
  343. }
  344. if ( axis.search( 'Y' ) !== - 1 ) {
  345. object.scale.y = Math.round( object.scale.y / this.scaleSnap ) * this.scaleSnap || this.scaleSnap;
  346. }
  347. if ( axis.search( 'Z' ) !== - 1 ) {
  348. object.scale.z = Math.round( object.scale.z / this.scaleSnap ) * this.scaleSnap || this.scaleSnap;
  349. }
  350. }
  351. //add:
  352. this.dispatchEvent({type:'scaleChanged', object })
  353. } else if ( mode === 'rotate' ) {
  354. offset.copy( pointEnd ).sub( pointStart );
  355. var ROTATION_SPEED = 2 / worldPosition.distanceTo( _tempVector.setFromMatrixPosition( this.camera.matrixWorld ) );
  356. if ( axis === 'E' ) {
  357. rotationAxis.copy( eye );
  358. rotationAngle = pointEnd.angleTo( pointStart );
  359. startNorm.copy( pointStart ).normalize();
  360. endNorm.copy( pointEnd ).normalize();
  361. rotationAngle *= ( endNorm.cross( startNorm ).dot( eye ) < 0 ? 1 : - 1 );
  362. } else if ( axis === 'XYZE' ) {
  363. rotationAxis.copy( offset ).cross( eye ).normalize();
  364. rotationAngle = offset.dot( _tempVector.copy( rotationAxis ).cross( this.eye ) ) * ROTATION_SPEED;
  365. } else if ( axis === 'X' || axis === 'Y' || axis === 'Z' ) {
  366. rotationAxis.copy( _unit[ axis ] );
  367. _tempVector.copy( _unit[ axis ] );
  368. if ( space === 'local' ) {
  369. _tempVector.applyQuaternion( worldQuaternion );
  370. }
  371. rotationAngle = offset.dot( _tempVector.cross( eye ).normalize() ) * ROTATION_SPEED;
  372. }
  373. // Apply rotation snap
  374. if ( this.rotationSnap ) rotationAngle = Math.round( rotationAngle / this.rotationSnap ) * this.rotationSnap;
  375. this.rotationAngle = rotationAngle;
  376. // Apply rotate
  377. if ( space === 'local' && axis !== 'E' && axis !== 'XYZE' ) {
  378. object.scale[axis.toLowerCase()] < 0 && (rotationAngle *= -1) //xzw 加,否则会反向
  379. object.quaternion.copy( quaternionStart );
  380. object.quaternion.multiply( _tempQuaternion.setFromAxisAngle( rotationAxis, rotationAngle ) ).normalize();
  381. } else {
  382. rotationAxis.applyQuaternion( parentQuaternionInv );
  383. object.quaternion.copy( _tempQuaternion.setFromAxisAngle( rotationAxis, rotationAngle ) );
  384. object.quaternion.multiply( quaternionStart ).normalize();
  385. }
  386. //add:
  387. /*
  388. if(EditOverlay.editing){
  389. EditOverlay.updateQua();
  390. } */
  391. this.dispatchEvent({type:'quaternionChanged', object })
  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. if(this.player.cameraControls.activeControl){
  402. //this.player.cameraControls.activeControl.locked = false; //add
  403. this.player.cameraControls.activeControl.pointerDragOn = false //add
  404. this.player.cameraControls.activeControl.enabled = true
  405. }
  406. }
  407. this.dragging = false;
  408. if ( pointer.button === undefined ) this.axis = null;
  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. this.hideAxis = {} //add
  491. // shared materials
  492. var gizmoMaterial = new MeshBasicMaterial( {
  493. depthTest: false,
  494. depthWrite: false,
  495. transparent: true,
  496. side: DoubleSide,
  497. fog: false
  498. } );
  499. var gizmoLineMaterial = new LineBasicMaterial( {
  500. depthTest: false,
  501. depthWrite: false,
  502. transparent: true,
  503. linewidth: 1,
  504. fog: false
  505. } );
  506. // Make unique material for each axis/color
  507. var matInvisible = gizmoMaterial.clone();
  508. matInvisible.opacity = 0.15;
  509. var matHelper = gizmoMaterial.clone();
  510. matHelper.opacity = 0.33;
  511. var matRed = gizmoMaterial.clone();
  512. matRed.color.set( 0xff0000 );
  513. var matGreen = gizmoMaterial.clone();
  514. matGreen.color.set( 0x00ff00 );
  515. var matBlue = gizmoMaterial.clone();
  516. matBlue.color.set( 0x0000ff );
  517. var matWhiteTransparent = gizmoMaterial.clone();
  518. matWhiteTransparent.opacity = 0.35;
  519. matWhiteTransparent.color.set( 0x00d0fd );//xzw add
  520. var matYellowTransparent = matWhiteTransparent.clone();
  521. matYellowTransparent.color.set( 0xffff00 );
  522. var matCyanTransparent = matWhiteTransparent.clone();
  523. matCyanTransparent.color.set( 0x00ffff );
  524. var matMagentaTransparent = matWhiteTransparent.clone();
  525. matMagentaTransparent.color.set( 0xff00ff );
  526. var matYellow = gizmoMaterial.clone();
  527. matYellow.color.set( 0xffff00 );
  528. var matLineRed = gizmoLineMaterial.clone();
  529. matLineRed.color.set( 0xff0000 );
  530. var matLineGreen = gizmoLineMaterial.clone();
  531. matLineGreen.color.set( 0x00ff00 );
  532. var matLineBlue = gizmoLineMaterial.clone();
  533. matLineBlue.color.set( 0x0000ff );
  534. var matLineCyan = gizmoLineMaterial.clone();
  535. matLineCyan.color.set( 0x00ffff );
  536. var matLineMagenta = gizmoLineMaterial.clone();
  537. matLineMagenta.color.set( 0xff00ff );
  538. var matLineYellow = gizmoLineMaterial.clone();
  539. matLineYellow.color.set( 0xffff00 );
  540. var matLineGray = gizmoLineMaterial.clone();
  541. matLineGray.color.set( 0x787878 );
  542. var matLineYellowTransparent = matLineYellow.clone();
  543. matLineYellowTransparent.opacity = 0.25;
  544. // reusable geometry
  545. var arrowGeometry = new CylinderBufferGeometry( 0, 0.05, 0.2, 12, 1, false );
  546. var scaleHandleGeometry = new BoxBufferGeometry( 0.125, 0.125, 0.125 );
  547. var lineGeometry = new BufferGeometry( );
  548. //lineGeometry.addAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 1, 0, 0 ], 3 ) );
  549. lineGeometry.addAttribute("position", new THREE.BufferAttribute(new Float32Array([ 0, 0, 0, 1, 0, 0]), 3 ))
  550. var CircleGeometry = function ( radius, arc ) {
  551. var geometry = new BufferGeometry( );
  552. var vertices = [];
  553. for ( var i = 0; i <= 64 * arc; ++ i ) {
  554. vertices.push( 0, Math.cos( i / 32 * Math.PI ) * radius, Math.sin( i / 32 * Math.PI ) * radius );
  555. }
  556. //geometry.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  557. geometry.addAttribute("position", new THREE.BufferAttribute(new Float32Array(vertices), 3 ))
  558. return geometry;
  559. };
  560. // Special geometry for transform helper. If scaled with position vector it spans from [0,0,0] to position
  561. var TranslateHelperGeometry = function () {
  562. var geometry = new BufferGeometry();
  563. //geometry.addAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 1, 1, 1 ], 3 ) );
  564. geometry.addAttribute("position", new THREE.BufferAttribute(new Float32Array([ 0, 0, 0, 1, 1, 1 ]), 3 ))
  565. return geometry;
  566. };
  567. // Gizmo definitions - custom hierarchy definitions for setupGizmo() function
  568. var gizmoTranslate = {
  569. X: [
  570. [ new Mesh( arrowGeometry, matRed ), [ 1, 0, 0 ], [ 0, 0, - Math.PI / 2 ], null, 'fwd' ],
  571. //[ new Mesh( arrowGeometry, matRed ), [ 1, 0, 0 ], [ 0, 0, Math.PI / 2 ], null, 'bwd' ],
  572. [ new Line( lineGeometry, matLineRed ) ]
  573. ],
  574. Y: [
  575. [ new Mesh( arrowGeometry, matGreen ), [ 0, 1, 0 ], null, null, 'fwd' ],
  576. //[ new Mesh( arrowGeometry, matGreen ), [ 0, 1, 0 ], [ Math.PI, 0, 0 ], null, 'bwd' ],
  577. [ new Line( lineGeometry, matLineGreen ), null, [ 0, 0, Math.PI / 2 ]]
  578. ],
  579. Z: [
  580. [ new Mesh( arrowGeometry, matBlue ), [ 0, 0, 1 ], [ Math.PI / 2, 0, 0 ], null, 'fwd' ],
  581. //[ new Mesh( arrowGeometry, matBlue ), [ 0, 0, 1 ], [ - Math.PI / 2, 0, 0 ], null, 'bwd' ],
  582. [ new Line( lineGeometry, matLineBlue ), null, [ 0, - Math.PI / 2, 0 ]]
  583. ],
  584. /* XYZ: [
  585. [ new Mesh( new OctahedronBufferGeometry( 0.1, 0 ), matWhiteTransparent.clone() ), [ 0, 0, 0 ], [ 0, 0, 0 ]]
  586. ], */
  587. XY: [
  588. [ new Mesh( new PlaneBufferGeometry( 0.295, 0.295 ), matYellowTransparent.clone() ), [ 0.15, 0.15, 0 ]],
  589. [ new Line( lineGeometry, matLineYellow ), [ 0.18, 0.3, 0 ], null, [ 0.125, 1, 1 ]],
  590. [ new Line( lineGeometry, matLineYellow ), [ 0.3, 0.18, 0 ], [ 0, 0, Math.PI / 2 ], [ 0.125, 1, 1 ]]
  591. ],
  592. YZ: [
  593. [ new Mesh( new PlaneBufferGeometry( 0.295, 0.295 ), matCyanTransparent.clone() ), [ 0, 0.15, 0.15 ], [ 0, Math.PI / 2, 0 ]],
  594. [ new Line( lineGeometry, matLineCyan ), [ 0, 0.18, 0.3 ], [ 0, 0, Math.PI / 2 ], [ 0.125, 1, 1 ]],
  595. [ new Line( lineGeometry, matLineCyan ), [ 0, 0.3, 0.18 ], [ 0, - Math.PI / 2, 0 ], [ 0.125, 1, 1 ]]
  596. ],
  597. XZ: [
  598. [ new Mesh( new PlaneBufferGeometry( 0.295, 0.295 ), matMagentaTransparent.clone() ), [ 0.15, 0, 0.15 ], [ - Math.PI / 2, 0, 0 ]],
  599. [ new Line( lineGeometry, matLineMagenta ), [ 0.18, 0, 0.3 ], null, [ 0.125, 1, 1 ]],
  600. [ new Line( lineGeometry, matLineMagenta ), [ 0.3, 0, 0.18 ], [ 0, - Math.PI / 2, 0 ], [ 0.125, 1, 1 ]]
  601. ]
  602. };
  603. var pickerTranslate = {
  604. X: [
  605. [ new Mesh( new CylinderBufferGeometry( 0.2, 0, 1, 4, 1, false ), matInvisible ), [ 0.6, 0, 0 ], [ 0, 0, - Math.PI / 2 ]]
  606. ],
  607. Y: [
  608. [ new Mesh( new CylinderBufferGeometry( 0.2, 0, 1, 4, 1, false ), matInvisible ), [ 0, 0.6, 0 ]]
  609. ],
  610. Z: [
  611. [ new Mesh( new CylinderBufferGeometry( 0.2, 0, 1, 4, 1, false ), matInvisible ), [ 0, 0, 0.6 ], [ Math.PI / 2, 0, 0 ]]
  612. ],
  613. /* XYZ: [
  614. [ new Mesh( new OctahedronBufferGeometry( 0.2, 0 ), matInvisible ) ]
  615. ], */
  616. XY: [
  617. [ new Mesh( new PlaneBufferGeometry( 0.4, 0.4 ), matInvisible ), [ 0.2, 0.2, 0 ]]
  618. ],
  619. YZ: [
  620. [ new Mesh( new PlaneBufferGeometry( 0.4, 0.4 ), matInvisible ), [ 0, 0.2, 0.2 ], [ 0, Math.PI / 2, 0 ]]
  621. ],
  622. XZ: [
  623. [ new Mesh( new PlaneBufferGeometry( 0.4, 0.4 ), matInvisible ), [ 0.2, 0, 0.2 ], [ - Math.PI / 2, 0, 0 ]]
  624. ]
  625. };
  626. var helperTranslate = {
  627. START: [
  628. [ new Mesh( new OctahedronBufferGeometry( 0.01, 2 ), matHelper ), null, null, null, 'helper' ]
  629. ],
  630. END: [
  631. [ new Mesh( new OctahedronBufferGeometry( 0.01, 2 ), matHelper ), null, null, null, 'helper' ]
  632. ],
  633. DELTA: [
  634. [ new Line( TranslateHelperGeometry(), matHelper ), null, null, null, 'helper' ]
  635. ],
  636. X: [
  637. [ new Line( lineGeometry, matHelper.clone() ), [ - 1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ]
  638. ],
  639. Y: [
  640. [ new Line( lineGeometry, matHelper.clone() ), [ 0, - 1e3, 0 ], [ 0, 0, Math.PI / 2 ], [ 1e6, 1, 1 ], 'helper' ]
  641. ],
  642. Z: [
  643. [ new Line( lineGeometry, matHelper.clone() ), [ 0, 0, - 1e3 ], [ 0, - Math.PI / 2, 0 ], [ 1e6, 1, 1 ], 'helper' ]
  644. ]
  645. };
  646. var gizmoRotate = {
  647. X: [
  648. [ new Line( CircleGeometry( 1, 0.5 ), matLineRed ) ],
  649. [ new Mesh( new OctahedronBufferGeometry( 0.04, 0 ), matRed ), [ 0, 0, 0.99 ], null, [ 1, 3, 1 ]],
  650. ],
  651. Y: [
  652. [ new Line( CircleGeometry( 1, 0.5 ), matLineGreen ), null, [ 0, 0, - Math.PI / 2 ]],
  653. [ new Mesh( new OctahedronBufferGeometry( 0.04, 0 ), matGreen ), [ 0, 0, 0.99 ], null, [ 3, 1, 1 ]],
  654. ],
  655. Z: [
  656. [ new Line( CircleGeometry( 1, 0.5 ), matLineBlue ), null, [ 0, Math.PI / 2, 0 ]],
  657. [ new Mesh( new OctahedronBufferGeometry( 0.04, 0 ), matBlue ), [ 0.99, 0, 0 ], null, [ 1, 3, 1 ]],
  658. ],
  659. E: [
  660. [ new Line( CircleGeometry( 1.25, 1 ), matLineYellowTransparent ), null, [ 0, Math.PI / 2, 0 ]],
  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 ), [ - 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 ), [ 0, - 1.17, 0 ], [ Math.PI, 0, 0 ], [ 1, 1, 0.001 ]],
  664. [ new Mesh( new CylinderBufferGeometry( 0.03, 0, 0.15, 4, 1, false ), matLineYellowTransparent ), [ 0, 1.17, 0 ], [ 0, 0, 0 ], [ 1, 1, 0.001 ]],
  665. ],
  666. XYZE: [
  667. [ new Line( CircleGeometry( 1, 1 ), matLineGray ), null, [ 0, Math.PI / 2, 0 ]]
  668. ]
  669. };
  670. var helperRotate = {
  671. AXIS: [
  672. [ new Line( lineGeometry, matHelper.clone() ), [ - 1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ]
  673. ]
  674. };
  675. var pickerRotate = {
  676. X: [
  677. [ new Mesh( new TorusBufferGeometry( 1, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ 0, - Math.PI / 2, - Math.PI / 2 ]],
  678. ],
  679. Y: [
  680. [ new Mesh( new TorusBufferGeometry( 1, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ Math.PI / 2, 0, 0 ]],
  681. ],
  682. Z: [
  683. [ new Mesh( new TorusBufferGeometry( 1, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  684. ],
  685. E: [
  686. [ new Mesh( new TorusBufferGeometry( 1.25, 0.1, 2, 24 ), matInvisible ) ]
  687. ],
  688. XYZE: [
  689. [ new Mesh( new SphereBufferGeometry( 0.7, 10, 8 ), matInvisible ) ]
  690. ]
  691. };
  692. var gizmoScale = {
  693. X: [
  694. [ new Mesh( scaleHandleGeometry, matRed ), [ 0.8, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  695. [ new Line( lineGeometry, matLineRed ), null, null, [ 0.8, 1, 1 ]]
  696. ],
  697. Y: [
  698. [ new Mesh( scaleHandleGeometry, matGreen ), [ 0, 0.8, 0 ]],
  699. [ new Line( lineGeometry, matLineGreen ), null, [ 0, 0, Math.PI / 2 ], [ 0.8, 1, 1 ]]
  700. ],
  701. Z: [
  702. [ new Mesh( scaleHandleGeometry, matBlue ), [ 0, 0, 0.8 ], [ Math.PI / 2, 0, 0 ]],
  703. [ new Line( lineGeometry, matLineBlue ), null, [ 0, - Math.PI / 2, 0 ], [ 0.8, 1, 1 ]]
  704. ],
  705. XY: [
  706. [ new Mesh( scaleHandleGeometry, matYellowTransparent ), [ 0.85, 0.85, 0 ], null, [ 2, 2, 0.2 ]],
  707. [ new Line( lineGeometry, matLineYellow ), [ 0.855, 0.98, 0 ], null, [ 0.125, 1, 1 ]],
  708. [ new Line( lineGeometry, matLineYellow ), [ 0.98, 0.855, 0 ], [ 0, 0, Math.PI / 2 ], [ 0.125, 1, 1 ]]
  709. ],
  710. /* YZ: [
  711. [ new Mesh( scaleHandleGeometry, matCyanTransparent ), [ 0, 0.85, 0.85 ], null, [ 0.2, 2, 2 ]],
  712. [ new Line( lineGeometry, matLineCyan ), [ 0, 0.855, 0.98 ], [ 0, 0, Math.PI / 2 ], [ 0.125, 1, 1 ]],
  713. [ new Line( lineGeometry, matLineCyan ), [ 0, 0.98, 0.855 ], [ 0, - Math.PI / 2, 0 ], [ 0.125, 1, 1 ]]
  714. ],
  715. XZ: [
  716. [ new Mesh( scaleHandleGeometry, matMagentaTransparent ), [ 0.85, 0, 0.85 ], null, [ 2, 0.2, 2 ]],
  717. [ new Line( lineGeometry, matLineMagenta ), [ 0.855, 0, 0.98 ], null, [ 0.125, 1, 1 ]],
  718. [ new Line( lineGeometry, matLineMagenta ), [ 0.98, 0, 0.855 ], [ 0, - Math.PI / 2, 0 ], [ 0.125, 1, 1 ]]
  719. ],
  720. XYZX: [
  721. [ new Mesh( new BoxBufferGeometry( 0.125, 0.125, 0.125 ), matWhiteTransparent.clone() ), [ 1.1, 0, 0 ]],
  722. ],*/
  723. XYZY: [
  724. [ new Mesh( new BoxBufferGeometry( 0.125, 0.125, 0.125 ), matWhiteTransparent.clone() ), [ 0, 1.1, 0 ]],
  725. ]/* ,
  726. XYZZ: [
  727. [ new Mesh( new BoxBufferGeometry( 0.125, 0.125, 0.125 ), matWhiteTransparent.clone() ), [ 0, 0, 1.1 ]],
  728. ] */
  729. };
  730. var pickerScale = {
  731. X: [
  732. [ new Mesh( new CylinderBufferGeometry( 0.2, 0, 0.8, 4, 1, false ), matInvisible ), [ 0.5, 0, 0 ], [ 0, 0, - Math.PI / 2 ]]
  733. ],
  734. Y: [
  735. [ new Mesh( new CylinderBufferGeometry( 0.2, 0, 0.8, 4, 1, false ), matInvisible ), [ 0, 0.5, 0 ]]
  736. ],
  737. Z: [
  738. [ new Mesh( new CylinderBufferGeometry( 0.2, 0, 0.8, 4, 1, false ), matInvisible ), [ 0, 0, 0.5 ], [ Math.PI / 2, 0, 0 ]]
  739. ],
  740. XY: [
  741. [ new Mesh( scaleHandleGeometry, matInvisible ), [ 0.85, 0.85, 0 ], null, [ 3, 3, 0.2 ]],
  742. ],
  743. /*YZ: [
  744. [ new Mesh( scaleHandleGeometry, matInvisible ), [ 0, 0.85, 0.85 ], null, [ 0.2, 3, 3 ]],
  745. ],
  746. XZ: [
  747. [ new Mesh( scaleHandleGeometry, matInvisible ), [ 0.85, 0, 0.85 ], null, [ 3, 0.2, 3 ]],
  748. ],
  749. XYZX: [
  750. [ new Mesh( new BoxBufferGeometry( 0.2, 0.2, 0.2 ), matInvisible ), [ 1.1, 0, 0 ]],
  751. ],*/
  752. XYZY: [
  753. [ new Mesh( new BoxBufferGeometry( 0.2, 0.2, 0.2 ), matInvisible ), [ 0, 1.1, 0 ]],
  754. ]/* ,
  755. XYZZ: [
  756. [ new Mesh( new BoxBufferGeometry( 0.2, 0.2, 0.2 ), matInvisible ), [ 0, 0, 1.1 ]],
  757. ] */
  758. };
  759. var helperScale = {
  760. X: [
  761. [ new Line( lineGeometry, matHelper.clone() ), [ - 1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ]
  762. ],
  763. Y: [
  764. [ new Line( lineGeometry, matHelper.clone() ), [ 0, - 1e3, 0 ], [ 0, 0, Math.PI / 2 ], [ 1e6, 1, 1 ], 'helper' ]
  765. ],
  766. Z: [
  767. [ new Line( lineGeometry, matHelper.clone() ), [ 0, 0, - 1e3 ], [ 0, - Math.PI / 2, 0 ], [ 1e6, 1, 1 ], 'helper' ]
  768. ]
  769. };
  770. // Creates an Object3D with gizmos described in custom hierarchy definition.
  771. var setupGizmo = function ( gizmoMap ) {
  772. var gizmo = new Object3D();
  773. for ( var name in gizmoMap ) {
  774. for ( var i = gizmoMap[ name ].length; i --; ) {
  775. var object = gizmoMap[ name ][ i ][ 0 ].clone();
  776. var position = gizmoMap[ name ][ i ][ 1 ];
  777. var rotation = gizmoMap[ name ][ i ][ 2 ];
  778. var scale = gizmoMap[ name ][ i ][ 3 ];
  779. var tag = gizmoMap[ name ][ i ][ 4 ];
  780. // name and tag properties are essential for picking and updating logic.
  781. object.name = name;
  782. object.tag = tag;
  783. if ( position ) {
  784. object.position.set( position[ 0 ], position[ 1 ], position[ 2 ] );
  785. }
  786. if ( rotation ) {
  787. object.rotation.set( rotation[ 0 ], rotation[ 1 ], rotation[ 2 ] );
  788. }
  789. if ( scale ) {
  790. object.scale.set( scale[ 0 ], scale[ 1 ], scale[ 2 ] );
  791. }
  792. object.updateMatrix();
  793. var tempGeometry = object.geometry.clone();
  794. tempGeometry.applyMatrix( object.matrix );
  795. object.geometry = tempGeometry;
  796. object.renderOrder = Infinity;
  797. object.position.set( 0, 0, 0 );
  798. object.rotation.set( 0, 0, 0 );
  799. object.scale.set( 1, 1, 1 );
  800. gizmo.add( object );
  801. }
  802. }
  803. return gizmo;
  804. };
  805. // Reusable utility variables
  806. var tempVector = new Vector3( 0, 0, 0 );
  807. var tempEuler = new Euler();
  808. var alignVector = new Vector3( 0, 1, 0 );
  809. var zeroVector = new Vector3( 0, 0, 0 );
  810. var lookAtMatrix = new Matrix4();
  811. var tempQuaternion = new Quaternion();
  812. var tempQuaternion2 = new Quaternion();
  813. var identityQuaternion = new Quaternion();
  814. var unitX = new Vector3( 1, 0, 0 );
  815. var unitY = new Vector3( 0, 1, 0 );
  816. var unitZ = new Vector3( 0, 0, 1 );
  817. // Gizmo creation
  818. this.gizmo = {};
  819. this.picker = {};
  820. this.helper = {};
  821. this.add( this.gizmo[ "translate" ] = setupGizmo( gizmoTranslate ) );
  822. this.add( this.gizmo[ "rotate" ] = setupGizmo( gizmoRotate ) );
  823. this.add( this.gizmo[ "scale" ] = setupGizmo( gizmoScale ) );
  824. this.add( this.picker[ "translate" ] = setupGizmo( pickerTranslate ) );
  825. this.add( this.picker[ "rotate" ] = setupGizmo( pickerRotate ) );
  826. this.add( this.picker[ "scale" ] = setupGizmo( pickerScale ) );
  827. this.add( this.helper[ "translate" ] = setupGizmo( helperTranslate ) );
  828. this.add( this.helper[ "rotate" ] = setupGizmo( helperRotate ) );
  829. this.add( this.helper[ "scale" ] = setupGizmo( helperScale ) );
  830. // Pickers should be hidden always
  831. this.picker[ "translate" ].visible = false;
  832. this.picker[ "rotate" ].visible = false;
  833. this.picker[ "scale" ].visible = false;
  834. // updateMatrixWorld will update transformations and appearance of individual handles
  835. this.updateMatrixWorld = function () {
  836. var space = this.space;
  837. if ( this.mode === 'scale' ) space = 'local'; // scale always oriented to local rotation
  838. var quaternion = space === "local" ? this.worldQuaternion : identityQuaternion;
  839. // Show only gizmos for current transform mode
  840. this.gizmo[ "translate" ].visible = this.mode === "translate";
  841. this.gizmo[ "rotate" ].visible = this.mode === "rotate";
  842. this.gizmo[ "scale" ].visible = this.mode === "scale";
  843. this.helper[ "translate" ].visible = this.mode === "translate";
  844. this.helper[ "rotate" ].visible = this.mode === "rotate";
  845. this.helper[ "scale" ].visible = this.mode === "scale";
  846. var handles = [];
  847. handles = handles.concat( this.picker[ this.mode ].children );
  848. handles = handles.concat( this.gizmo[ this.mode ].children );
  849. handles = handles.concat( this.helper[ this.mode ].children );
  850. var eyeDistance = this.worldPosition.distanceTo( this.cameraPosition );
  851. //俯视图的透视和距离无关,因此在两种相机切换切换时大小过渡比较困难
  852. if(player.mode == "transitioning" && (player.modeTran.split('-')[0] == "floorplan" || player.modeTran.split('-')[1] == "floorplan")){
  853. eyeDistance = Math.min(eyeDistance, 6.5)//从一开始很小的距离过渡到俯视图50的高度,会变得很大。
  854. }else if(player.mode == "floorplan"){
  855. var flcamera = player.cameraControls.cameras.floorplan;
  856. eyeDistance = (flcamera.right - flcamera.left ) / flcamera.aspect ;
  857. }
  858. let scale = eyeDistance * this.size / 7
  859. for ( var i = 0; i < handles.length; i ++ ) {
  860. var handle = handles[ i ];
  861. //add
  862. if(this.hideAxis[this.mode] && this.hideAxis[this.mode].some(e=>handle.name.includes(e.toUpperCase()))){
  863. convertTool.updateVisible(handle, 'hidden', false)
  864. continue
  865. }
  866. // hide aligned to camera
  867. let visible = true //handle.visible = true;
  868. handle.rotation.set( 0, 0, 0 );
  869. handle.position.copy( this.worldPosition );
  870. handle.scale.set( 1, 1, 1 ).multiplyScalar( scale );
  871. // TODO: simplify helpers and consider decoupling from gizmo
  872. if ( handle.tag === 'helper' ) {
  873. visible = false;
  874. if ( handle.name === 'AXIS' ) {
  875. handle.position.copy( this.worldPositionStart );
  876. visible = !! this.axis;
  877. if ( this.axis === 'X' ) {
  878. tempQuaternion.setFromEuler( tempEuler.set( 0, 0, 0 ) );
  879. handle.quaternion.copy( quaternion ).multiply( tempQuaternion );
  880. if ( Math.abs( alignVector.copy( unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  881. visible = false;
  882. }
  883. }
  884. if ( this.axis === 'Y' ) {
  885. tempQuaternion.setFromEuler( tempEuler.set( 0, 0, Math.PI / 2 ) );
  886. handle.quaternion.copy( quaternion ).multiply( tempQuaternion );
  887. if ( Math.abs( alignVector.copy( unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  888. visible = false;
  889. }
  890. }
  891. if ( this.axis === 'Z' ) {
  892. tempQuaternion.setFromEuler( tempEuler.set( 0, Math.PI / 2, 0 ) );
  893. handle.quaternion.copy( quaternion ).multiply( tempQuaternion );
  894. if ( Math.abs( alignVector.copy( unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  895. visible = false;
  896. }
  897. }
  898. if ( this.axis === 'XYZE' ) {
  899. tempQuaternion.setFromEuler( tempEuler.set( 0, Math.PI / 2, 0 ) );
  900. alignVector.copy( this.rotationAxis );
  901. handle.quaternion.setFromRotationMatrix( lookAtMatrix.lookAt( zeroVector, alignVector, unitY ) );
  902. handle.quaternion.multiply( tempQuaternion );
  903. visible = this.dragging;
  904. }
  905. if ( this.axis === 'E' ) {
  906. visible = false;
  907. }
  908. } else if ( handle.name === 'START' ) {
  909. handle.position.copy( this.worldPositionStart );
  910. visible = this.dragging;
  911. } else if ( handle.name === 'END' ) {
  912. handle.position.copy( this.worldPosition );
  913. visible = this.dragging;
  914. } else if ( handle.name === 'DELTA' ) {
  915. handle.position.copy( this.worldPositionStart );
  916. handle.quaternion.copy( this.worldQuaternionStart );
  917. tempVector.set( 1e-10, 1e-10, 1e-10 ).add( this.worldPositionStart ).sub( this.worldPosition ).multiplyScalar( - 1 );
  918. tempVector.applyQuaternion( this.worldQuaternionStart.clone().inverse() );
  919. handle.scale.copy( tempVector );
  920. visible = this.dragging;
  921. } else {
  922. handle.quaternion.copy( quaternion );
  923. if ( this.dragging ) {
  924. handle.position.copy( this.worldPositionStart );
  925. } else {
  926. handle.position.copy( this.worldPosition );
  927. }
  928. if ( this.axis ) {
  929. visible = this.axis.search( handle.name ) !== - 1;
  930. }
  931. }
  932. // If updating helper, skip rest of the loop
  933. convertTool.updateVisible(handle, 'hidden', !!visible)
  934. continue;
  935. }
  936. // Align handles to current local or world rotation
  937. handle.quaternion.copy( quaternion );
  938. if ( this.mode === 'translate' || this.mode === 'scale' ) {
  939. // Hide translate and scale axis facing the camera
  940. var AXIS_HIDE_TRESHOLD = 0.99;
  941. var PLANE_HIDE_TRESHOLD = 0.2;
  942. var AXIS_FLIP_TRESHOLD = 0.0;
  943. if(options.dontHideWhenFaceCamera){//xzw add
  944. //正对镜头时不隐藏箭头
  945. }else{
  946. if ( handle.name === 'X' || handle.name === 'XYZX' ) {
  947. if ( Math.abs( alignVector.copy( unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_TRESHOLD ) {
  948. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  949. visible = false;
  950. }
  951. }
  952. if ( handle.name === 'Y' || handle.name === 'XYZY' ) {
  953. if ( Math.abs( alignVector.copy( unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_TRESHOLD ) {
  954. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  955. visible = false;
  956. }
  957. }
  958. if ( handle.name === 'Z' || handle.name === 'XYZZ' ) {
  959. if ( Math.abs( alignVector.copy( unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_TRESHOLD ) {
  960. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  961. visible = false;
  962. }
  963. }
  964. if ( handle.name === 'XY' ) {
  965. if ( Math.abs( alignVector.copy( unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_TRESHOLD ) {
  966. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  967. visible = false;
  968. }
  969. }
  970. if ( handle.name === 'YZ' ) {
  971. if ( Math.abs( alignVector.copy( unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_TRESHOLD ) {
  972. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  973. visible = false;
  974. }
  975. }
  976. if ( handle.name === 'XZ' ) {
  977. if ( Math.abs( alignVector.copy( unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_TRESHOLD ) {
  978. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  979. visible = false;
  980. }
  981. }
  982. }
  983. // Flip translate and scale axis ocluded behind another axis
  984. //xzw 改 去掉反向箭头
  985. if ( handle.name.search( 'X' ) !== - 1 ) {
  986. if ( alignVector.copy( unitX ).applyQuaternion( quaternion ).dot( this.eye ) < AXIS_FLIP_TRESHOLD ) {
  987. /* if ( handle.tag === 'fwd' ) {
  988. visible = false;
  989. } else {
  990. handle.scale.x *= - 1;
  991. }
  992. } else if ( handle.tag === 'bwd' ) {
  993. visible = false;*/
  994. handle.scale.x *= - 1;
  995. }
  996. }
  997. if ( handle.name.search( 'Y' ) !== - 1 ) {
  998. if ( alignVector.copy( unitY ).applyQuaternion( quaternion ).dot( this.eye ) < AXIS_FLIP_TRESHOLD ) {
  999. /* if ( handle.tag === 'fwd' ) {
  1000. visible = false;
  1001. } else {
  1002. handle.scale.y *= - 1;
  1003. }
  1004. } else if ( handle.tag === 'bwd' ) {
  1005. visible = false;
  1006. */
  1007. handle.scale.y *= - 1;
  1008. }
  1009. }
  1010. if ( handle.name.search( 'Z' ) !== - 1 ) {
  1011. if ( alignVector.copy( unitZ ).applyQuaternion( quaternion ).dot( this.eye ) < AXIS_FLIP_TRESHOLD ) {
  1012. /* if ( handle.tag === 'fwd' ) {
  1013. visible = false;
  1014. } else {
  1015. handle.scale.z *= - 1;
  1016. }
  1017. } else if ( handle.tag === 'bwd' ) {
  1018. visible = false; */
  1019. handle.scale.z *= - 1;
  1020. }
  1021. }
  1022. } else if ( this.mode === 'rotate' ) {
  1023. // Align handles to current local or world rotation
  1024. tempQuaternion2.copy( quaternion );
  1025. alignVector.copy( this.eye ).applyQuaternion( tempQuaternion.copy( quaternion ).inverse() );
  1026. if ( handle.name.search( "E" ) !== - 1 ) {
  1027. handle.quaternion.setFromRotationMatrix( lookAtMatrix.lookAt( this.eye, zeroVector, unitY ) );
  1028. }
  1029. if ( handle.name === 'X' ) {
  1030. tempQuaternion.setFromAxisAngle( unitX, Math.atan2( - alignVector.y, alignVector.z ) );
  1031. tempQuaternion.multiplyQuaternions( tempQuaternion2, tempQuaternion );
  1032. handle.quaternion.copy( tempQuaternion );
  1033. }
  1034. if ( handle.name === 'Y' ) {
  1035. tempQuaternion.setFromAxisAngle( unitY, Math.atan2( alignVector.x, alignVector.z ) );
  1036. tempQuaternion.multiplyQuaternions( tempQuaternion2, tempQuaternion );
  1037. handle.quaternion.copy( tempQuaternion );
  1038. }
  1039. if ( handle.name === 'Z' ) {
  1040. tempQuaternion.setFromAxisAngle( unitZ, Math.atan2( alignVector.y, alignVector.x ) );
  1041. tempQuaternion.multiplyQuaternions( tempQuaternion2, tempQuaternion );
  1042. handle.quaternion.copy( tempQuaternion );
  1043. }
  1044. }
  1045. // Hide disabled axes
  1046. visible = visible && ( handle.name.indexOf( "X" ) === - 1 || this.showX );
  1047. visible = visible && ( handle.name.indexOf( "Y" ) === - 1 || this.showY );
  1048. visible = visible && ( handle.name.indexOf( "Z" ) === - 1 || this.showZ );
  1049. visible = visible && ( handle.name.indexOf( "E" ) === - 1 || ( this.showX && this.showY && this.showZ ) );
  1050. convertTool.updateVisible(handle, 'hidden', !!visible)
  1051. // highlight selected axis
  1052. handle.material._opacity = handle.material._opacity || handle.material.opacity;
  1053. handle.material._color = handle.material._color || handle.material.color.clone();
  1054. handle.material.color.copy( handle.material._color );
  1055. handle.material.opacity = handle.material._opacity;
  1056. if ( ! this.enabled ) {
  1057. handle.material.opacity *= 0.5;
  1058. handle.material.color.lerp( new Color( 1, 1, 1 ), 0.5 );
  1059. } else if ( this.axis ) {
  1060. if ( handle.name === this.axis ) {
  1061. handle.material.opacity = 1.0;
  1062. handle.material.color.lerp( new Color( 1, 1, 1 ), 0.5 );
  1063. } else if ( this.axis.split( '' ).some( function ( a ) {
  1064. return handle.name === a;
  1065. } ) ) {
  1066. handle.material.opacity = 1.0;
  1067. handle.material.color.lerp( new Color( 1, 1, 1 ), 0.5 );
  1068. } else {
  1069. handle.material.opacity *= 0.25;
  1070. handle.material.color.lerp( new Color( 1, 1, 1 ), 0.5 );
  1071. }
  1072. }
  1073. }
  1074. Object3D.prototype.updateMatrixWorld.call( this );
  1075. };
  1076. };
  1077. /* var TransformControlsLines = function (options) {
  1078. Object3D.call( this)
  1079. let label1 = new Label2D({ innerHTML:`<div>与相机水平距离</div>` , domElement:$("#otherLabels")[0] ,autoUpdate:true })
  1080. let label2 = new Label2D({ innerHTML:`<div>与相机高度差</div>` , domElement:$("#otherLabels")[0] ,autoUpdate:true })
  1081. let css = {'opacity': 0.7, 'color': '#07fceb', transform:'translate(-50%, -50%)'}
  1082. label1.elem.css(css);
  1083. label2.elem.css(css)
  1084. //for(let i=0;i<2;i++){
  1085. // let line = LineDraw.createLine([new THREE.Vector3, new THREE.Vector3],{color:'#07fceb', opacity:0.7 })
  1086. // this.add(line)
  1087. //}
  1088. this.updateTransform = function(object){
  1089. object = object || this.parent.object;
  1090. let A = player.position.clone(); //当前相机位置
  1091. let B = object.position //物体位置
  1092. let C = B.clone().setY(A.y); //物体在相机的高度的位置
  1093. let D = A.clone().setY(B.y); //相机在物体的高度的位置
  1094. //LineDraw.moveLine(this.children[0], [B, C]) // 垂直线
  1095. //LineDraw.moveLine(this.children[1], [D, B]) // 水平线
  1096. let dis1 = toPrecision(D.distanceTo(B),1)
  1097. let dis2 = C.y - B.y
  1098. label1.elem.text('与相机水平距离: '+dis1+'米')
  1099. label2.elem.text('在相机之'+ (dis2>0?'下' : '上') +' :'+ toPrecision(dis2 , 1) +'米')
  1100. label1.setPos(new THREE.Vector3().addVectors(D,B).multiplyScalar(0.5))
  1101. label2.setPos(new THREE.Vector3().addVectors(B,C).multiplyScalar(0.5))
  1102. }
  1103. this.setVisible = (v,reason)=>{
  1104. label1.setVisible(v, reason || 'unvisi')
  1105. label2.setVisible(v, reason || 'unvisi')
  1106. this.visible = label1.visible
  1107. if(this.visible){
  1108. this.updateTransform()
  1109. }
  1110. }
  1111. this.setVisible(false)
  1112. player.on("mode.changing",(currentMode, mode, pano, duration)=>{
  1113. if(mode != 'panorama'){
  1114. this.setVisible(false, 'isPanorama')
  1115. }else{
  1116. this.setVisible(true, 'isPanorama')
  1117. }
  1118. })
  1119. player.on("flying.started",( )=>{
  1120. this.setVisible(false, 'flying')
  1121. })
  1122. player.on("flying.ended",( )=>{
  1123. this.setVisible(true, 'flying')
  1124. })
  1125. }
  1126. */
  1127. var TransformControlsPlane = function (options) {
  1128. 'use strict';
  1129. Mesh.call( this,
  1130. new PlaneBufferGeometry( 100000, 100000, 2, 2 ),
  1131. new MeshBasicMaterial( { color: "#ff0000", visible: false, wireframe: false, side: DoubleSide, transparent: true, opacity: 0.2 } )
  1132. );
  1133. this.type = 'TransformControlsPlane';
  1134. var unitX = new Vector3( 1, 0, 0 );
  1135. var unitY = new Vector3( 0, 1, 0 );
  1136. var unitZ = new Vector3( 0, 0, 1 );
  1137. var tempVector = new Vector3();
  1138. var dirVector = new Vector3();
  1139. var alignVector = new Vector3();
  1140. var tempMatrix = new Matrix4();
  1141. var identityQuaternion = new Quaternion();
  1142. this.updateMatrixWorld = function () {
  1143. var space = this.space;
  1144. this.position.copy( this.worldPosition );
  1145. if ( this.mode === 'scale' ) space = 'local'; // scale always oriented to local rotation
  1146. unitX.set( 1, 0, 0 ).applyQuaternion( space === "local" ? this.worldQuaternion : identityQuaternion );
  1147. unitY.set( 0, 1, 0 ).applyQuaternion( space === "local" ? this.worldQuaternion : identityQuaternion );
  1148. unitZ.set( 0, 0, 1 ).applyQuaternion( space === "local" ? this.worldQuaternion : identityQuaternion );
  1149. // Align the plane for current transform mode, axis and space.
  1150. alignVector.copy( unitY );
  1151. switch ( this.mode ) {
  1152. case 'translate':
  1153. case 'scale':
  1154. switch ( this.axis ) {
  1155. case 'X':
  1156. alignVector.copy( this.eye ).cross( unitX );
  1157. dirVector.copy( unitX ).cross( alignVector );
  1158. break;
  1159. case 'Y':
  1160. alignVector.copy( this.eye ).cross( unitY );
  1161. dirVector.copy( unitY ).cross( alignVector );
  1162. break;
  1163. case 'Z':
  1164. alignVector.copy( this.eye ).cross( unitZ );
  1165. dirVector.copy( unitZ ).cross( alignVector );
  1166. break;
  1167. case 'XY':
  1168. dirVector.copy( unitZ );
  1169. break;
  1170. case 'YZ':
  1171. dirVector.copy( unitX );
  1172. break;
  1173. case 'XZ':
  1174. alignVector.copy( unitZ );
  1175. dirVector.copy( unitY );
  1176. break;
  1177. case 'XYZ':
  1178. case 'E':
  1179. default: //xzw add for scale xyzz
  1180. dirVector.set( 0, 0, 0 );
  1181. break;
  1182. }
  1183. break;
  1184. case 'rotate':
  1185. default:
  1186. // special case for rotate
  1187. dirVector.set( 0, 0, 0 );
  1188. }
  1189. if ( dirVector.length() === 0 ) {
  1190. // If in rotate mode, make the plane parallel to camera
  1191. this.quaternion.copy( this.cameraQuaternion );
  1192. } else {
  1193. tempMatrix.lookAt( tempVector.set( 0, 0, 0 ), dirVector, alignVector );
  1194. this.quaternion.setFromRotationMatrix( tempMatrix );
  1195. }
  1196. Object3D.prototype.updateMatrixWorld.call( this );
  1197. };
  1198. };
  1199. //export { TransformControls, TransformControlsGizmo, TransformControlsPlane };
  1200. TransformControls.init = function(THREE){
  1201. BoxBufferGeometry = THREE.BoxBufferGeometry,
  1202. BufferGeometry = THREE.BufferGeometry,
  1203. Color = THREE.Color,
  1204. CylinderBufferGeometry = THREE.CylinderBufferGeometry,
  1205. DoubleSide = THREE.DoubleSide,
  1206. Euler = THREE.Euler,
  1207. Float32BufferAttribute = THREE.Float32BufferAttribute,
  1208. Line = THREE.Line,
  1209. LineBasicMaterial = THREE.LineBasicMaterial,
  1210. Matrix4 = THREE.Matrix4,
  1211. Mesh = THREE.Mesh,
  1212. MeshBasicMaterial = THREE.MeshBasicMaterial,
  1213. Object3D = THREE.Object3D,
  1214. //OctahedronBufferGeometry = THREE.OctahedronBufferGeometry,
  1215. PlaneBufferGeometry = THREE.PlaneBufferGeometry,
  1216. Quaternion = THREE.Quaternion,
  1217. Raycaster = THREE.Raycaster,
  1218. SphereBufferGeometry = THREE.SphereBufferGeometry,
  1219. TorusBufferGeometry = THREE.TorusBufferGeometry,
  1220. Vector3 = THREE.Vector3;
  1221. Vector2 = THREE.Vector2;
  1222. TransformControls.prototype = Object.assign( Object.create( Object3D.prototype ), {
  1223. constructor: TransformControls,
  1224. isTransformControls: true
  1225. } );
  1226. TransformControlsGizmo.prototype = Object.assign( Object.create( Object3D.prototype ), {
  1227. constructor: TransformControlsGizmo,
  1228. isTransformControlsGizmo: true
  1229. } );
  1230. TransformControlsPlane.prototype = Object.assign( Object.create( Mesh.prototype ), {
  1231. constructor: TransformControlsPlane,
  1232. isTransformControlsPlane: true
  1233. } );
  1234. /* TransformControlsLines.prototype = Object.assign( Object.create( Object3D.prototype ), {
  1235. constructor: TransformControlsLines,
  1236. isTransformControlsLines: true
  1237. } ); */
  1238. //----------
  1239. function PolyhedronBufferGeometry( vertices, indices, radius, detail ) {
  1240. BufferGeometry.call( this );
  1241. this.type = 'PolyhedronBufferGeometry';
  1242. this.parameters = {
  1243. vertices: vertices,
  1244. indices: indices,
  1245. radius: radius,
  1246. detail: detail
  1247. };
  1248. radius = radius || 1;
  1249. detail = detail || 0;
  1250. // default buffer data
  1251. var vertexBuffer = [];
  1252. var uvBuffer = [];
  1253. // the subdivision creates the vertex buffer data
  1254. subdivide( detail );
  1255. // all vertices should lie on a conceptual sphere with a given radius
  1256. appplyRadius( radius );
  1257. // finally, create the uv data
  1258. generateUVs();
  1259. // build non-indexed geometry
  1260. this.addAttribute("position", new THREE.BufferAttribute(new Float32Array(vertexBuffer), 3 ))
  1261. this.addAttribute( 'normal', new THREE.BufferAttribute( new Float32Array(vertexBuffer), 3 ) );
  1262. this.addAttribute( 'uv', new THREE.BufferAttribute( new Float32Array(uvBuffer), 2 ) );
  1263. if ( detail === 0 ) {
  1264. this.computeVertexNormals(); // flat normals
  1265. } else {
  1266. this.normalizeNormals(); // smooth normals
  1267. }
  1268. // helper functions
  1269. function subdivide( detail ) {
  1270. var a = new Vector3();
  1271. var b = new Vector3();
  1272. var c = new Vector3();
  1273. // iterate over all faces and apply a subdivison with the given detail value
  1274. for ( var i = 0; i < indices.length; i += 3 ) {
  1275. // get the vertices of the face
  1276. getVertexByIndex( indices[ i + 0 ], a );
  1277. getVertexByIndex( indices[ i + 1 ], b );
  1278. getVertexByIndex( indices[ i + 2 ], c );
  1279. // perform subdivision
  1280. subdivideFace( a, b, c, detail );
  1281. }
  1282. }
  1283. function subdivideFace( a, b, c, detail ) {
  1284. var cols = Math.pow( 2, detail );
  1285. // we use this multidimensional array as a data structure for creating the subdivision
  1286. var v = [];
  1287. var i, j;
  1288. // construct all of the vertices for this subdivision
  1289. for ( i = 0; i <= cols; i ++ ) {
  1290. v[ i ] = [];
  1291. var aj = a.clone().lerp( c, i / cols );
  1292. var bj = b.clone().lerp( c, i / cols );
  1293. var rows = cols - i;
  1294. for ( j = 0; j <= rows; j ++ ) {
  1295. if ( j === 0 && i === cols ) {
  1296. v[ i ][ j ] = aj;
  1297. } else {
  1298. v[ i ][ j ] = aj.clone().lerp( bj, j / rows );
  1299. }
  1300. }
  1301. }
  1302. // construct all of the faces
  1303. for ( i = 0; i < cols; i ++ ) {
  1304. for ( j = 0; j < 2 * ( cols - i ) - 1; j ++ ) {
  1305. var k = Math.floor( j / 2 );
  1306. if ( j % 2 === 0 ) {
  1307. pushVertex( v[ i ][ k + 1 ] );
  1308. pushVertex( v[ i + 1 ][ k ] );
  1309. pushVertex( v[ i ][ k ] );
  1310. } else {
  1311. pushVertex( v[ i ][ k + 1 ] );
  1312. pushVertex( v[ i + 1 ][ k + 1 ] );
  1313. pushVertex( v[ i + 1 ][ k ] );
  1314. }
  1315. }
  1316. }
  1317. }
  1318. function appplyRadius( radius ) {
  1319. var vertex = new Vector3();
  1320. // iterate over the entire buffer and apply the radius to each vertex
  1321. for ( var i = 0; i < vertexBuffer.length; i += 3 ) {
  1322. vertex.x = vertexBuffer[ i + 0 ];
  1323. vertex.y = vertexBuffer[ i + 1 ];
  1324. vertex.z = vertexBuffer[ i + 2 ];
  1325. vertex.normalize().multiplyScalar( radius );
  1326. vertexBuffer[ i + 0 ] = vertex.x;
  1327. vertexBuffer[ i + 1 ] = vertex.y;
  1328. vertexBuffer[ i + 2 ] = vertex.z;
  1329. }
  1330. }
  1331. function generateUVs() {
  1332. var vertex = new Vector3();
  1333. for ( var i = 0; i < vertexBuffer.length; i += 3 ) {
  1334. vertex.x = vertexBuffer[ i + 0 ];
  1335. vertex.y = vertexBuffer[ i + 1 ];
  1336. vertex.z = vertexBuffer[ i + 2 ];
  1337. var u = azimuth( vertex ) / 2 / Math.PI + 0.5;
  1338. var v = inclination( vertex ) / Math.PI + 0.5;
  1339. uvBuffer.push( u, 1 - v );
  1340. }
  1341. correctUVs();
  1342. correctSeam();
  1343. }
  1344. function correctSeam() {
  1345. // handle case when face straddles the seam, see #3269
  1346. for ( var i = 0; i < uvBuffer.length; i += 6 ) {
  1347. // uv data of a single face
  1348. var x0 = uvBuffer[ i + 0 ];
  1349. var x1 = uvBuffer[ i + 2 ];
  1350. var x2 = uvBuffer[ i + 4 ];
  1351. var max = Math.max( x0, x1, x2 );
  1352. var min = Math.min( x0, x1, x2 );
  1353. // 0.9 is somewhat arbitrary
  1354. if ( max > 0.9 && min < 0.1 ) {
  1355. if ( x0 < 0.2 ) uvBuffer[ i + 0 ] += 1;
  1356. if ( x1 < 0.2 ) uvBuffer[ i + 2 ] += 1;
  1357. if ( x2 < 0.2 ) uvBuffer[ i + 4 ] += 1;
  1358. }
  1359. }
  1360. }
  1361. function pushVertex( vertex ) {
  1362. vertexBuffer.push( vertex.x, vertex.y, vertex.z );
  1363. }
  1364. function getVertexByIndex( index, vertex ) {
  1365. var stride = index * 3;
  1366. vertex.x = vertices[ stride + 0 ];
  1367. vertex.y = vertices[ stride + 1 ];
  1368. vertex.z = vertices[ stride + 2 ];
  1369. }
  1370. function correctUVs() {
  1371. var a = new Vector3();
  1372. var b = new Vector3();
  1373. var c = new Vector3();
  1374. var centroid = new Vector3();
  1375. var uvA = new Vector2();
  1376. var uvB = new Vector2();
  1377. var uvC = new Vector2();
  1378. for ( var i = 0, j = 0; i < vertexBuffer.length; i += 9, j += 6 ) {
  1379. a.set( vertexBuffer[ i + 0 ], vertexBuffer[ i + 1 ], vertexBuffer[ i + 2 ] );
  1380. b.set( vertexBuffer[ i + 3 ], vertexBuffer[ i + 4 ], vertexBuffer[ i + 5 ] );
  1381. c.set( vertexBuffer[ i + 6 ], vertexBuffer[ i + 7 ], vertexBuffer[ i + 8 ] );
  1382. uvA.set( uvBuffer[ j + 0 ], uvBuffer[ j + 1 ] );
  1383. uvB.set( uvBuffer[ j + 2 ], uvBuffer[ j + 3 ] );
  1384. uvC.set( uvBuffer[ j + 4 ], uvBuffer[ j + 5 ] );
  1385. centroid.copy( a ).add( b ).add( c ).divideScalar( 3 );
  1386. var azi = azimuth( centroid );
  1387. correctUV( uvA, j + 0, a, azi );
  1388. correctUV( uvB, j + 2, b, azi );
  1389. correctUV( uvC, j + 4, c, azi );
  1390. }
  1391. }
  1392. function correctUV( uv, stride, vector, azimuth ) {
  1393. if ( ( azimuth < 0 ) && ( uv.x === 1 ) ) {
  1394. uvBuffer[ stride ] = uv.x - 1;
  1395. }
  1396. if ( ( vector.x === 0 ) && ( vector.z === 0 ) ) {
  1397. uvBuffer[ stride ] = azimuth / 2 / Math.PI + 0.5;
  1398. }
  1399. }
  1400. // Angle around the Y axis, counter-clockwise when looking from above.
  1401. function azimuth( vector ) {
  1402. return Math.atan2( vector.z, - vector.x );
  1403. }
  1404. // Angle above the XZ plane.
  1405. function inclination( vector ) {
  1406. return Math.atan2( - vector.y, Math.sqrt( ( vector.x * vector.x ) + ( vector.z * vector.z ) ) );
  1407. }
  1408. }
  1409. PolyhedronBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
  1410. PolyhedronBufferGeometry.prototype.constructor = PolyhedronBufferGeometry;
  1411. OctahedronBufferGeometry = function( radius, detail ) {
  1412. var vertices = [
  1413. 1, 0, 0, - 1, 0, 0, 0, 1, 0,
  1414. 0, - 1, 0, 0, 0, 1, 0, 0, - 1
  1415. ];
  1416. var indices = [
  1417. 0, 2, 4, 0, 4, 3, 0, 3, 5,
  1418. 0, 5, 2, 1, 2, 5, 1, 5, 3,
  1419. 1, 3, 4, 1, 4, 2
  1420. ];
  1421. PolyhedronBufferGeometry.call( this, vertices, indices, radius, detail );
  1422. this.type = 'OctahedronBufferGeometry';
  1423. this.parameters = {
  1424. radius: radius,
  1425. detail: detail
  1426. };
  1427. }
  1428. OctahedronBufferGeometry.prototype = Object.create( PolyhedronBufferGeometry.prototype );
  1429. OctahedronBufferGeometry.prototype.constructor = OctahedronBufferGeometry;
  1430. }
  1431. window.TransformControls = TransformControls;
  1432. }()