TransformControls.js 58 KB

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