TransformControls.js 61 KB

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