TransformControls.js 58 KB

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