TransformControls.js 59 KB

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