TransformControls.js 59 KB

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