TransformControls.js 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034
  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, 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, 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, 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. for ( var i = 0; i < handles.length; i ++ ) {
  978. var handle = handles[ i ];
  979. //add
  980. if(this.hideAxis[this.mode]?.some(e=>handle.name.includes(e.toUpperCase())) && !this.showAxis[this.mode]?.some(e=>handle.name == e.toUpperCase() )){
  981. Potree.Utils.updateVisible(handle, 'hidden', false)
  982. if(!handle.visible) continue //XYZE还是要作为装饰显示
  983. }
  984. let visible = true
  985. // hide aligned to camera
  986. handle.rotation.set( 0, 0, 0 );
  987. handle.position.copy( this.worldPosition );
  988. if(camera.type == "OrthographicCamera"){
  989. var eyeDistance = 800 / camera.zoom
  990. }else{
  991. var eyeDistance = this.worldPosition.distanceTo( this.cameraPosition );
  992. }
  993. handle.scale.set( 1, 1, 1 ).multiplyScalar( eyeDistance * this.size / 7 );
  994. // TODO: simplify helpers and consider decoupling from gizmo
  995. if ( handle.tag === 'helper' ) {
  996. visible = false;
  997. if ( handle.name === 'AXIS' ) {
  998. handle.position.copy( this.worldPositionStart );
  999. visible = !!this.axis;
  1000. if ( this.axis === 'X' ) {
  1001. tempQuaternion.setFromEuler( tempEuler.set( 0, 0, 0 ) );
  1002. handle.quaternion.copy( quaternion ).multiply( tempQuaternion );
  1003. if ( Math.abs( alignVector.copy( unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  1004. visible = false;
  1005. }
  1006. }
  1007. if ( this.axis === 'Y' ) {
  1008. tempQuaternion.setFromEuler( tempEuler.set( 0, 0, Math.PI / 2 ) );
  1009. handle.quaternion.copy( quaternion ).multiply( tempQuaternion );
  1010. if ( Math.abs( alignVector.copy( unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  1011. visible = false;
  1012. }
  1013. }
  1014. if ( this.axis === 'Z' ) {
  1015. tempQuaternion.setFromEuler( tempEuler.set( 0, Math.PI / 2, 0 ) );
  1016. handle.quaternion.copy( quaternion ).multiply( tempQuaternion );
  1017. if ( Math.abs( alignVector.copy( unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  1018. visible = false;
  1019. }
  1020. }
  1021. if ( this.axis === 'XYZE' ) {
  1022. tempQuaternion.setFromEuler( tempEuler.set( 0, Math.PI / 2, 0 ) );
  1023. alignVector.copy( this.rotationAxis );
  1024. handle.quaternion.setFromRotationMatrix( lookAtMatrix.lookAt( zeroVector, alignVector, unitY ) );
  1025. handle.quaternion.multiply( tempQuaternion );
  1026. visible = this.dragging;
  1027. }
  1028. if ( this.axis === 'E' ) {
  1029. visible = false;
  1030. }
  1031. } else if ( handle.name === 'START' ) {
  1032. handle.position.copy( this.worldPositionStart );
  1033. visible = this.dragging;
  1034. } else if ( handle.name === 'END' ) {
  1035. handle.position.copy( this.worldPosition );
  1036. visible = this.dragging;
  1037. } else if ( handle.name === 'DELTA' ) {
  1038. handle.position.copy( this.worldPositionStart );
  1039. handle.quaternion.copy( this.worldQuaternionStart );
  1040. tempVector.set( 1e-10, 1e-10, 1e-10 ).add( this.worldPositionStart ).sub( this.worldPosition ).multiplyScalar( - 1 );
  1041. tempVector.applyQuaternion( this.worldQuaternionStart.clone().invert() );
  1042. handle.scale.copy( tempVector );
  1043. visible = this.dragging;
  1044. } else {
  1045. handle.quaternion.copy( quaternion );
  1046. if ( this.dragging ) {
  1047. handle.position.copy( this.worldPositionStart );
  1048. } else {
  1049. handle.position.copy( this.worldPosition );
  1050. }
  1051. if ( this.axis ) {
  1052. visible = this.axis.search( handle.name ) !== - 1;
  1053. }
  1054. }
  1055. // If updating helper, skip rest of the loop
  1056. Potree.Utils.updateVisible(handle, 'hidden', !!visible)
  1057. continue;
  1058. }
  1059. // Align handles to current local or world rotation
  1060. handle.quaternion.copy( quaternion );
  1061. if ( this.mode === 'translate' || this.mode === 'scale' ) {
  1062. // Hide translate and scale axis facing the camera
  1063. var AXIS_HIDE_TRESHOLD = 0.99;
  1064. var PLANE_HIDE_TRESHOLD = 0.2;
  1065. var AXIS_FLIP_TRESHOLD = 0.0;
  1066. if(options.dontHideWhenFaceCamera){//xzw add
  1067. //正对镜头时不隐藏箭头
  1068. }else{
  1069. if ( handle.name === 'X' || handle.name === 'XYZX' ) {
  1070. if ( Math.abs( alignVector.copy( unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_TRESHOLD ) {
  1071. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  1072. visible = false;
  1073. }
  1074. }
  1075. if ( handle.name === 'Y' || handle.name === 'XYZY' ) {
  1076. if ( Math.abs( alignVector.copy( unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_TRESHOLD ) {
  1077. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  1078. visible = false;
  1079. }
  1080. }
  1081. if ( handle.name === 'Z' || handle.name === 'XYZZ' ) {
  1082. if ( Math.abs( alignVector.copy( unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_TRESHOLD ) {
  1083. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  1084. visible = false;
  1085. }
  1086. }
  1087. if ( handle.name === 'XY' ) {
  1088. if ( Math.abs( alignVector.copy( unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_TRESHOLD ) {
  1089. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  1090. visible = false;
  1091. }
  1092. }
  1093. if ( handle.name === 'YZ' ) {
  1094. if ( Math.abs( alignVector.copy( unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_TRESHOLD ) {
  1095. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  1096. visible = false;
  1097. }
  1098. }
  1099. if ( handle.name === 'XZ' ) {
  1100. if ( Math.abs( alignVector.copy( unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_TRESHOLD ) {
  1101. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  1102. visible = false;
  1103. }
  1104. }
  1105. }
  1106. // Flip translate and scale axis ocluded behind another axis
  1107. //xzw 改 去掉反向箭头
  1108. if ( handle.name.search( 'X' ) !== - 1 ) {
  1109. if ( alignVector.copy( unitX ).applyQuaternion( quaternion ).dot( this.eye ) < AXIS_FLIP_TRESHOLD ) {
  1110. /* if ( handle.tag === 'fwd' ) {
  1111. handle.visible = false;
  1112. } else {
  1113. handle.scale.x *= - 1;
  1114. }
  1115. } else if ( handle.tag === 'bwd' ) {
  1116. handle.visible = false;*/
  1117. handle.scale.x *= - 1;
  1118. }
  1119. }
  1120. if ( handle.name.search( 'Y' ) !== - 1 ) {
  1121. if ( alignVector.copy( unitY ).applyQuaternion( quaternion ).dot( this.eye ) < AXIS_FLIP_TRESHOLD ) {
  1122. /* if ( handle.tag === 'fwd' ) {
  1123. handle.visible = false;
  1124. } else {
  1125. handle.scale.y *= - 1;
  1126. }
  1127. } else if ( handle.tag === 'bwd' ) {
  1128. handle.visible = false;
  1129. */
  1130. handle.scale.y *= - 1;
  1131. }
  1132. }
  1133. if ( handle.name.search( 'Z' ) !== - 1 ) {
  1134. if ( alignVector.copy( unitZ ).applyQuaternion( quaternion ).dot( this.eye ) < AXIS_FLIP_TRESHOLD ) {
  1135. /* if ( handle.tag === 'fwd' ) {
  1136. handle.visible = false;
  1137. } else {
  1138. handle.scale.z *= - 1;
  1139. }
  1140. } else if ( handle.tag === 'bwd' ) {
  1141. handle.visible = false; */
  1142. handle.scale.z *= - 1;
  1143. }
  1144. }
  1145. } else if ( this.mode === 'rotate' ) {
  1146. // Align handles to current local or world rotation
  1147. tempQuaternion2.copy( quaternion );
  1148. alignVector.copy( this.eye ).applyQuaternion( tempQuaternion.copy( quaternion ).invert() );
  1149. if ( handle.name.search( "E" ) !== - 1 ) {
  1150. handle.quaternion.setFromRotationMatrix( lookAtMatrix.lookAt( this.eye, zeroVector, unitY ) );
  1151. }
  1152. if ( handle.name === 'X' ) {
  1153. tempQuaternion.setFromAxisAngle( unitX, Math.atan2( - alignVector.y, alignVector.z ) );
  1154. tempQuaternion.multiplyQuaternions( tempQuaternion2, tempQuaternion );
  1155. handle.quaternion.copy( tempQuaternion );
  1156. }
  1157. if ( handle.name === 'Y' ) {
  1158. tempQuaternion.setFromAxisAngle( unitY, Math.atan2( alignVector.x, alignVector.z ) );
  1159. tempQuaternion.multiplyQuaternions( tempQuaternion2, tempQuaternion );
  1160. handle.quaternion.copy( tempQuaternion );
  1161. }
  1162. if ( handle.name === 'Z' ) {
  1163. tempQuaternion.setFromAxisAngle( unitZ, Math.atan2( alignVector.y, alignVector.x ) );
  1164. tempQuaternion.multiplyQuaternions( tempQuaternion2, tempQuaternion );
  1165. handle.quaternion.copy( tempQuaternion );
  1166. }
  1167. }
  1168. // Hide disabled axes
  1169. visible = visible && ( handle.name.indexOf( "X" ) === - 1 || this.showX );
  1170. visible = visible && ( handle.name.indexOf( "Y" ) === - 1 || this.showY );
  1171. visible = visible && ( handle.name.indexOf( "Z" ) === - 1 || this.showZ );
  1172. visible = visible && ( handle.name.indexOf( "E" ) === - 1 || ( this.showX && this.showY && this.showZ ) );
  1173. Potree.Utils.updateVisible(handle, 'hidden', !!visible)
  1174. // highlight selected axis
  1175. handle.material._opacity = handle.material._opacity || handle.material.opacity;
  1176. handle.material._color = handle.material._color || handle.material.color.clone();
  1177. handle.material.color.copy( handle.material._color );
  1178. handle.material.opacity = handle.material._opacity;
  1179. if ( ! this.enabled ) {
  1180. handle.material.opacity *= 0.5;
  1181. handle.material.color.lerp( new THREE.Color( 1, 1, 1 ), 0.5 );
  1182. } else if ( this.axis ) {
  1183. if ( handle.name === this.axis ) {
  1184. handle.material.opacity = 1.0;
  1185. handle.material.color.lerp( new THREE.Color( 1, 1, 1 ), 0.5 );
  1186. } else if ( this.axis.split( '' ).some( function ( a ) {
  1187. return handle.name === a;
  1188. } ) ) {
  1189. handle.material.opacity = 1.0;
  1190. handle.material.color.lerp( new THREE.Color( 1, 1, 1 ), 0.5 );
  1191. } else {
  1192. handle.material.opacity *= 0.25;
  1193. handle.material.color.lerp( new THREE.Color( 1, 1, 1 ), 0.5 );
  1194. }
  1195. }
  1196. }
  1197. THREE.Object3D.prototype.updateMatrixWorld.call( this );
  1198. };
  1199. };
  1200. /* var TransformControlsLines = function (options) {
  1201. THREE.Object3D.call( this)
  1202. let label1 = new Label2D({ innerHTML:`<div>与相机水平距离</div>` , domElement:$("#otherLabels")[0] ,autoUpdate:true })
  1203. let label2 = new Label2D({ innerHTML:`<div>与相机高度差</div>` , domElement:$("#otherLabels")[0] ,autoUpdate:true })
  1204. let css = {'opacity': 0.7, 'color': '#07fceb', transform:'translate(-50%, -50%)'}
  1205. label1.elem.css(css);
  1206. label2.elem.css(css)
  1207. //for(let i=0;i<2;i++){
  1208. // let line = LineDraw.createLine([new THREE.THREE.Vector3, new THREE.THREE.Vector3],{color:'#07fceb', opacity:0.7 })
  1209. // this.add(line)
  1210. //}
  1211. this.updateTransform = function(object){
  1212. object = object || this.parent.object;
  1213. let A = player.position.clone(); //当前相机位置
  1214. let B = object.position //物体位置
  1215. let C = B.clone().setY(A.y); //物体在相机的高度的位置
  1216. let D = A.clone().setY(B.y); //相机在物体的高度的位置
  1217. //LineDraw.moveLine(this.children[0], [B, C]) // 垂直线
  1218. //LineDraw.moveLine(this.children[1], [D, B]) // 水平线
  1219. let dis1 = toPrecision(D.distanceTo(B),1)
  1220. let dis2 = C.y - B.y
  1221. label1.elem.text('与相机水平距离: '+dis1+'米')
  1222. label2.elem.text('在相机之'+ (dis2>0?'下' : '上') +' :'+ toPrecision(dis2 , 1) +'米')
  1223. label1.setPos(new THREE.THREE.Vector3().addVectors(D,B).multiplyScalar(0.5))
  1224. label2.setPos(new THREE.THREE.Vector3().addVectors(B,C).multiplyScalar(0.5))
  1225. }
  1226. this.setVisible = (v,reason)=>{
  1227. label1.setVisible(v, reason || 'unvisi')
  1228. label2.setVisible(v, reason || 'unvisi')
  1229. this.visible = label1.visible
  1230. if(this.visible){
  1231. this.updateTransform()
  1232. }
  1233. }
  1234. this.setVisible(false)
  1235. player.on("mode.changing",(currentMode, mode, pano, duration)=>{
  1236. if(mode != 'panorama'){
  1237. this.setVisible(false, 'isPanorama')
  1238. }else{
  1239. this.setVisible(true, 'isPanorama')
  1240. }
  1241. })
  1242. player.on("flying.started",( )=>{
  1243. this.setVisible(false, 'flying')
  1244. })
  1245. player.on("flying.ended",( )=>{
  1246. this.setVisible(true, 'flying')
  1247. })
  1248. }
  1249. */
  1250. var TransformControlsPlane = function (options) {
  1251. 'use strict';
  1252. THREE.Mesh.call( this,
  1253. new THREE.PlaneBufferGeometry( 100000, 100000, 2, 2 ),
  1254. new THREE.MeshBasicMaterial( { color: "#ff0000", visible: false, wireframe: false, side: THREE.DoubleSide, transparent: true, opacity: 0.2 } )
  1255. );
  1256. this.type = 'TransformControlsPlane';
  1257. var unitX = new THREE.Vector3( 1, 0, 0 );
  1258. var unitY = new THREE.Vector3( 0, 1, 0 );
  1259. var unitZ = new THREE.Vector3( 0, 0, 1 );
  1260. var tempVector = new THREE.Vector3();
  1261. var dirVector = new THREE.Vector3();
  1262. var alignVector = new THREE.Vector3();
  1263. var tempMatrix = new THREE.Matrix4();
  1264. var identityQuaternion = new THREE.Quaternion();
  1265. this.updateMatrixWorld = function () {//TransformControlsPlane
  1266. if(!this.visible)return//add
  1267. var space = this.space;
  1268. this.position.copy( this.worldPosition );
  1269. if ( this.mode === 'scale' ) space = 'local'; // scale always oriented to local rotation
  1270. unitX.set( 1, 0, 0 ).applyQuaternion( space === "local" ? this.worldQuaternion : identityQuaternion );
  1271. unitY.set( 0, 1, 0 ).applyQuaternion( space === "local" ? this.worldQuaternion : identityQuaternion );
  1272. unitZ.set( 0, 0, 1 ).applyQuaternion( space === "local" ? this.worldQuaternion : identityQuaternion );
  1273. // Align the plane for current transform mode, axis and space.
  1274. alignVector.copy( unitY );
  1275. switch ( this.mode ) {
  1276. case 'translate':
  1277. case 'scale':
  1278. switch ( this.axis ) {
  1279. case 'X':
  1280. alignVector.copy( this.eye ).cross( unitX );
  1281. dirVector.copy( unitX ).cross( alignVector );
  1282. break;
  1283. case 'Y':
  1284. alignVector.copy( this.eye ).cross( unitY );
  1285. dirVector.copy( unitY ).cross( alignVector );
  1286. break;
  1287. case 'Z':
  1288. alignVector.copy( this.eye ).cross( unitZ );
  1289. dirVector.copy( unitZ ).cross( alignVector );
  1290. break;
  1291. case 'XY':
  1292. dirVector.copy( unitZ );
  1293. break;
  1294. case 'YZ':
  1295. dirVector.copy( unitX );
  1296. break;
  1297. case 'XZ':
  1298. alignVector.copy( unitZ );
  1299. dirVector.copy( unitY );
  1300. break;
  1301. case 'XYZ':
  1302. case 'E':
  1303. default: //xzw add for scale xyzz
  1304. dirVector.set( 0, 0, 0 );
  1305. break;
  1306. }
  1307. break;
  1308. case 'rotate':
  1309. default:
  1310. // special case for rotate
  1311. dirVector.set( 0, 0, 0 );
  1312. }
  1313. if ( dirVector.length() === 0 ) {//使物体在视线中平移
  1314. // If in rotate mode, make the plane parallel to camera
  1315. this.quaternion.copy( this.cameraQuaternion );
  1316. } else {//方向滑动所在面
  1317. tempMatrix.lookAt( tempVector.set( 0, 0, 0 ), dirVector, alignVector );
  1318. this.quaternion.setFromRotationMatrix( tempMatrix );
  1319. }
  1320. THREE.Object3D.prototype.updateMatrixWorld.call( this );
  1321. };
  1322. };
  1323. TransformControls.prototype = Object.assign( Object.create( THREE.Object3D.prototype ), {
  1324. constructor: TransformControls,
  1325. isTransformControls: true
  1326. } );
  1327. TransformControlsGizmo.prototype = Object.assign( Object.create( THREE.Object3D.prototype ), {
  1328. constructor: TransformControlsGizmo,
  1329. isTransformControlsGizmo: true
  1330. } );
  1331. TransformControlsPlane.prototype = Object.assign( Object.create( THREE.Mesh.prototype ), {
  1332. constructor: TransformControlsPlane,
  1333. isTransformControlsPlane: true
  1334. } );
  1335. export { TransformControls, TransformControlsGizmo, TransformControlsPlane };
  1336. /*
  1337. 备注:
  1338. //这里虽然 使坐标轴在boundingBox中心
  1339. boundingBox && this.object.boundingBox.getCenter(worldPosition).applyMatrix4(this.object.matrixWorld);
  1340. 但是旋转中心并不是这个坐标轴显示的位置,需要再执行 MergeEditor.maintainBoundXY()才能维持在这个中心
  1341. */