TransformControls.js 54 KB

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