TransformControls.js 54 KB

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