TransformControls.js 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997
  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,{importance:10})
  106. viewer.addEventListener('global_drag',drag,{importance: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) //center设置为boundingbox的中心。但center一直更改会导致虽然在按某个时针旋转,但v2相对v1可能会时不时逆向,导致闪烁。所以固定住center,虽然这样计算的角度和绕模型中心的感觉偏颇
  419. }else{ */
  420. center.copy(worldPositionStart)
  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. //console.log('rot', rotationAngle, center.toArray())
  438. if (Number.isNaN(rotationAngle)) {
  439. return;
  440. }
  441. this.rotateStart.v1 = v2
  442. }
  443. }
  444. }else{
  445. //pointStart 是起始intersect - object中心的向量
  446. //pointEnd 是当前intersect - object中心的向量
  447. offset.copy( pointEnd ).sub( pointStart );
  448. var ROTATION_SPEED = 2 / worldPosition.distanceTo( _tempVector.setFromMatrixPosition( this.camera.matrixWorld ) );
  449. if ( axis === 'E' ) {//绕着视线转
  450. rotationAxis.copy( eye );//旋转轴
  451. rotationAngle = pointEnd.angleTo( pointStart );
  452. startNorm.copy( pointStart ).normalize();
  453. endNorm.copy( pointEnd ).normalize();
  454. rotationAngle *= ( endNorm.cross( startNorm ).dot( eye ) < 0 ? 1 : - 1 ); //角度
  455. } else if ( axis === 'XYZE' ) {//像滚球一样拨动,鼠标滑动方向为拨动方向,在plane面上滚动
  456. rotationAxis.copy( offset ).cross( eye ).normalize();
  457. rotationAngle = offset.dot( _tempVector.copy( rotationAxis ).cross( this.eye ) ) * ROTATION_SPEED;
  458. } else if ( axis === 'X' || axis === 'Y' || axis === 'Z' ) {
  459. rotationAxis.copy( _unit[ axis ] );
  460. _tempVector.copy( _unit[ axis ] );
  461. if ( space === 'local' ) {
  462. _tempVector.applyQuaternion( worldQuaternion );
  463. }
  464. rotationAngle = offset.dot( _tempVector.cross( eye ).normalize() ) * ROTATION_SPEED;
  465. }
  466. // Apply rotation snap
  467. if ( this.rotationSnap ) rotationAngle = Math.round( rotationAngle / this.rotationSnap ) * this.rotationSnap;
  468. this.rotationAngle = rotationAngle;
  469. }
  470. // Apply rotate
  471. if ( space === 'local' && axis !== 'E' && axis !== 'XYZE' ) {
  472. object.scale[axis.toLowerCase()] < 0 && (rotationAngle *= -1) //xzw 加,否则会反向
  473. object.quaternion.copy( quaternionStart );
  474. object.quaternion.multiply( _tempQuaternion.setFromAxisAngle( rotationAxis, rotationAngle ) ).normalize();
  475. } else {
  476. rotationAxis.applyQuaternion( parentQuaternionInv );
  477. object.quaternion.copy( _tempQuaternion.setFromAxisAngle( rotationAxis, rotationAngle ) );
  478. object.quaternion.multiply( quaternionStart ).normalize();
  479. }
  480. if ( this.rotateMethod == 2 && axis != 'E' ) {
  481. quaternionStart.copy(object.quaternion)
  482. }
  483. //add:
  484. object.dispatchEvent({
  485. type: "rotation_changed"
  486. });
  487. }
  488. this.dispatchEvent( changeEvent );
  489. this.dispatchEvent( objectChangeEvent );
  490. viewer.dispatchEvent('content_changed')
  491. };
  492. this.pointerUp = function ( pointer ) {
  493. //if ( pointer.button !== undefined && pointer.button !== 0 ) return;
  494. if ( this.dragging && ( this.axis !== null ) ) {
  495. mouseUpEvent.mode = this.mode;
  496. this.dispatchEvent( mouseUpEvent );
  497. /* if(this.player.cameraControls.activeControl){
  498. //this.player.cameraControls.activeControl.locked = false; //add
  499. this.player.cameraControls.activeControl.pointerDragOn = false //add
  500. this.player.cameraControls.activeControl.enabled = true
  501. } */
  502. this.rotateStart = null//add
  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. */