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