TransformationTool.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. import * as THREE from "../../libs/three.js/build/three.module.js";
  2. import {Utils} from "../utils.js";
  3. //add-------------------------------------
  4. const OpaWhenNotSelect = 0.75
  5. const ScaleRatio = 4
  6. const OutlineColor = 0x666666
  7. //----------------------------------------
  8. export class TransformationTool {
  9. constructor(viewer) {
  10. this.viewer = viewer;
  11. this.scene = new THREE.Scene();
  12. this.selection = [];
  13. this.pivot = new THREE.Vector3();
  14. this.dragging = false;
  15. this.showPickVolumes = false;
  16. this.viewer.inputHandler.registerInteractiveScene(this.scene);
  17. this.viewer.inputHandler.addEventListener('selection_changed', (e) => {
  18. for(let selected of this.selection){
  19. this.viewer.inputHandler.blacklist.delete(selected);
  20. }
  21. this.selection = e.selection;
  22. for(let selected of this.selection){
  23. this.viewer.inputHandler.blacklist.add(selected);
  24. }
  25. });
  26. let red = Potree.config.axis.x.color
  27. let green = Potree.config.axis.y.color
  28. let blue = Potree.config.axis.z.color
  29. this.activeHandle = null;
  30. this.scaleHandles = {
  31. "scale.x+": {name: "scale.x+", node: new THREE.Object3D(), color: red, alignment: [+1, +0, +0]},
  32. "scale.x-": {name: "scale.x-", node: new THREE.Object3D(), color: red, alignment: [-1, +0, +0]},
  33. "scale.y+": {name: "scale.y+", node: new THREE.Object3D(), color: green, alignment: [+0, +1, +0]},
  34. "scale.y-": {name: "scale.y-", node: new THREE.Object3D(), color: green, alignment: [+0, -1, +0]},
  35. "scale.z+": {name: "scale.z+", node: new THREE.Object3D(), color: blue, alignment: [+0, +0, +1]},
  36. "scale.z-": {name: "scale.z-", node: new THREE.Object3D(), color: blue, alignment: [+0, +0, -1]},
  37. };
  38. this.focusHandles = {
  39. "focus.x+": {name: "focus.x+", node: new THREE.Object3D(), color: red, alignment: [+1, +0, +0]},
  40. "focus.x-": {name: "focus.x-", node: new THREE.Object3D(), color: red, alignment: [-1, +0, +0]},
  41. "focus.y+": {name: "focus.y+", node: new THREE.Object3D(), color: green, alignment: [+0, +1, +0]},
  42. "focus.y-": {name: "focus.y-", node: new THREE.Object3D(), color: green, alignment: [+0, -1, +0]},
  43. "focus.z+": {name: "focus.z+", node: new THREE.Object3D(), color: blue, alignment: [+0, +0, +1]},
  44. "focus.z-": {name: "focus.z-", node: new THREE.Object3D(), color: blue, alignment: [+0, +0, -1]},
  45. };
  46. this.translationHandles = {
  47. "translation.x": {name: "translation.x", node: new THREE.Object3D(), color: red, alignment: [1, 0, 0]},
  48. "translation.y": {name: "translation.y", node: new THREE.Object3D(), color: green, alignment: [0, 1, 0]},
  49. "translation.z": {name: "translation.z", node: new THREE.Object3D(), color: blue, alignment: [0, 0, 1]},
  50. };
  51. this.rotationHandles = {
  52. "rotation.x": {name: "rotation.x", node: new THREE.Object3D(), color: red, alignment: [1, 0, 0]},
  53. "rotation.y": {name: "rotation.y", node: new THREE.Object3D(), color: green, alignment: [0, 1, 0]},
  54. "rotation.z": {name: "rotation.z", node: new THREE.Object3D(), color: blue, alignment: [0, 0, 1]},
  55. };
  56. this.handles = Object.assign({}, this.scaleHandles, this.focusHandles, this.translationHandles, this.rotationHandles);
  57. this.pickVolumes = [];
  58. this.initializeScaleHandles();
  59. this.initializeFocusHandles();
  60. this.initializeTranslationHandles();
  61. this.initializeRotationHandles();
  62. let boxFrameGeometry = new THREE.Geometry();
  63. {
  64. // bottom
  65. boxFrameGeometry.vertices.push(new THREE.Vector3(-0.5, -0.5, 0.5));
  66. boxFrameGeometry.vertices.push(new THREE.Vector3(0.5, -0.5, 0.5));
  67. boxFrameGeometry.vertices.push(new THREE.Vector3(0.5, -0.5, 0.5));
  68. boxFrameGeometry.vertices.push(new THREE.Vector3(0.5, -0.5, -0.5));
  69. boxFrameGeometry.vertices.push(new THREE.Vector3(0.5, -0.5, -0.5));
  70. boxFrameGeometry.vertices.push(new THREE.Vector3(-0.5, -0.5, -0.5));
  71. boxFrameGeometry.vertices.push(new THREE.Vector3(-0.5, -0.5, -0.5));
  72. boxFrameGeometry.vertices.push(new THREE.Vector3(-0.5, -0.5, 0.5));
  73. // top
  74. boxFrameGeometry.vertices.push(new THREE.Vector3(-0.5, 0.5, 0.5));
  75. boxFrameGeometry.vertices.push(new THREE.Vector3(0.5, 0.5, 0.5));
  76. boxFrameGeometry.vertices.push(new THREE.Vector3(0.5, 0.5, 0.5));
  77. boxFrameGeometry.vertices.push(new THREE.Vector3(0.5, 0.5, -0.5));
  78. boxFrameGeometry.vertices.push(new THREE.Vector3(0.5, 0.5, -0.5));
  79. boxFrameGeometry.vertices.push(new THREE.Vector3(-0.5, 0.5, -0.5));
  80. boxFrameGeometry.vertices.push(new THREE.Vector3(-0.5, 0.5, -0.5));
  81. boxFrameGeometry.vertices.push(new THREE.Vector3(-0.5, 0.5, 0.5));
  82. // sides
  83. boxFrameGeometry.vertices.push(new THREE.Vector3(-0.5, -0.5, 0.5));
  84. boxFrameGeometry.vertices.push(new THREE.Vector3(-0.5, 0.5, 0.5));
  85. boxFrameGeometry.vertices.push(new THREE.Vector3(0.5, -0.5, 0.5));
  86. boxFrameGeometry.vertices.push(new THREE.Vector3(0.5, 0.5, 0.5));
  87. boxFrameGeometry.vertices.push(new THREE.Vector3(0.5, -0.5, -0.5));
  88. boxFrameGeometry.vertices.push(new THREE.Vector3(0.5, 0.5, -0.5));
  89. boxFrameGeometry.vertices.push(new THREE.Vector3(-0.5, -0.5, -0.5));
  90. boxFrameGeometry.vertices.push(new THREE.Vector3(-0.5, 0.5, -0.5));
  91. }
  92. this.frame = new THREE.LineSegments(boxFrameGeometry, new THREE.LineBasicMaterial({color: 0xffff00}));
  93. this.scene.add(this.frame);
  94. viewer.setObjectLayers(this.scene, 'transformationTool' )
  95. }
  96. initializeScaleHandles(){
  97. let sgSphere = new THREE.SphereGeometry(1, 32, 32);
  98. let sgLowPolySphere = new THREE.SphereGeometry(1, 16, 16);
  99. for(let handleName of Object.keys(this.scaleHandles)){
  100. let handle = this.scaleHandles[handleName];
  101. let node = handle.node;
  102. this.scene.add(node);
  103. node.position.set(...handle.alignment).multiplyScalar(0.5);
  104. let material = new THREE.MeshBasicMaterial({
  105. color: handle.color,
  106. opacity: OpaWhenNotSelect,
  107. transparent: true
  108. });
  109. let outlineMaterial = new THREE.MeshBasicMaterial({
  110. color: OutlineColor,
  111. side: THREE.BackSide,
  112. opacity: OpaWhenNotSelect,
  113. transparent: true});
  114. let pickMaterial = new THREE.MeshNormalMaterial({
  115. opacity: 0.2,
  116. transparent: true,
  117. visible: this.showPickVolumes});
  118. let sphere = new THREE.Mesh(sgSphere, material);
  119. sphere.scale.set(2, 2, 2 );
  120. sphere.name = `${handleName}.handle`;
  121. node.add(sphere);
  122. let outline = new THREE.Mesh(sgSphere, outlineMaterial);
  123. outline.scale.set(1.1, 1.1, 1.1);
  124. outline.name = `${handleName}.outline`;
  125. sphere.add(outline);
  126. let pickSphere = new THREE.Mesh(sgLowPolySphere, pickMaterial);
  127. pickSphere.name = `${handleName}.pick_volume`;
  128. pickSphere.scale.set(1.5, 1.5, 1.5);
  129. sphere.add(pickSphere);
  130. pickSphere.handle = handleName;
  131. this.pickVolumes.push(pickSphere);
  132. node.setOpacity = (target) => {
  133. let opacity = {x: material.opacity};
  134. let t = new TWEEN.Tween(opacity).to({x: target}, 100);
  135. t.onUpdate(() => {
  136. sphere.visible = opacity.x > 0;
  137. pickSphere.visible = opacity.x > 0;
  138. material.opacity = opacity.x;
  139. outlineMaterial.opacity = opacity.x;
  140. pickSphere.material.opacity = opacity.x * 0.5;
  141. });
  142. t.start();
  143. };
  144. pickSphere.addEventListener("drag", (e) => this.dragScaleHandle(e));
  145. pickSphere.addEventListener("drop", (e) => this.dropScaleHandle(e));
  146. pickSphere.addEventListener("mouseover", e => {
  147. //node.setOpacity(1);
  148. });
  149. pickSphere.addEventListener("click", e => {
  150. e.consume();
  151. });
  152. pickSphere.addEventListener("mouseleave", e => {
  153. //node.setOpacity(OpaWhenNotSelect);
  154. });
  155. }
  156. }
  157. initializeFocusHandles(){
  158. //let sgBox = new THREE.BoxGeometry(1, 1, 1);
  159. let sgPlane = new THREE.PlaneGeometry(4, 4, 1, 1);
  160. let sgLowPolySphere = new THREE.SphereGeometry(1, 16, 16);
  161. let texture = new THREE.TextureLoader().load(`${exports.resourcePath}/icons/eye_2.png`);
  162. for(let handleName of Object.keys(this.focusHandles)){
  163. let handle = this.focusHandles[handleName];
  164. let node = handle.node;
  165. this.scene.add(node);
  166. let align = handle.alignment;
  167. //node.lookAt(new THREE.Vector3().addVectors(node.position, new THREE.Vector3(...align)));
  168. node.lookAt(new THREE.Vector3(...align));
  169. let off = 0.8;
  170. if(align[0] === 1){
  171. node.position.set(1, off, -off).multiplyScalar(0.5);
  172. node.rotation.z = Math.PI / 2;
  173. }else if(align[0] === -1){
  174. node.position.set(-1, -off, -off).multiplyScalar(0.5);
  175. node.rotation.z = Math.PI / 2;
  176. }else if(align[1] === 1){
  177. node.position.set(-off, 1, -off).multiplyScalar(0.5);
  178. node.rotation.set(Math.PI / 2, Math.PI, 0.0);
  179. }else if(align[1] === -1){
  180. node.position.set(off, -1, -off).multiplyScalar(0.5);
  181. node.rotation.set(Math.PI / 2, 0.0, 0.0);
  182. }else if(align[2] === 1){
  183. node.position.set(off, off, 1).multiplyScalar(0.5);
  184. }else if(align[2] === -1){
  185. node.position.set(-off, off, -1).multiplyScalar(0.5);
  186. }
  187. let material = new THREE.MeshBasicMaterial({
  188. color: handle.color,
  189. opacity: 0,
  190. transparent: true,
  191. map: texture
  192. });
  193. //let outlineMaterial = new THREE.MeshBasicMaterial({
  194. // color: 0x000000,
  195. // side: THREE.BackSide,
  196. // opacity: 0,
  197. // transparent: true});
  198. let pickMaterial = new THREE.MeshNormalMaterial({
  199. //opacity: 0,
  200. transparent: true,
  201. visible: this.showPickVolumes});
  202. let box = new THREE.Mesh(sgPlane, material);
  203. box.name = `${handleName}.handle`;
  204. box.scale.set(1.5, 1.5, 1.5);
  205. box.position.set(0, 0, 0);
  206. box.visible = false;
  207. node.add(box);
  208. //handle.focusNode = box;
  209. //let outline = new THREE.Mesh(sgPlane, outlineMaterial);
  210. //outline.scale.set(1.4, 1.4, 1.4);
  211. //outline.name = `${handleName}.outline`;
  212. //box.add(outline);
  213. let pickSphere = new THREE.Mesh(sgLowPolySphere, pickMaterial);
  214. pickSphere.name = `${handleName}.pick_volume`;
  215. pickSphere.scale.set(2, 2, 2);
  216. box.add(pickSphere);
  217. pickSphere.handle = handleName;
  218. this.pickVolumes.push(pickSphere);
  219. node.setOpacity = (target) => {
  220. let opacity = {x: material.opacity};
  221. let t = new TWEEN.Tween(opacity).to({x: target}, 100);
  222. t.onUpdate(() => {
  223. pickSphere.visible = opacity.x > 0;
  224. box.visible = opacity.x > 0;
  225. material.opacity = opacity.x;
  226. //outlineMaterial.opacity = opacity.x;
  227. pickSphere.material.opacity = opacity.x * 0.5;
  228. });
  229. t.start();
  230. };
  231. //pickSphere.addEventListener("drag", e => {});
  232. pickSphere.addEventListener("mouseup", e => {
  233. e.consume();
  234. });
  235. pickSphere.addEventListener("mousedown", e => {
  236. e.consume();
  237. });
  238. pickSphere.addEventListener("click", e => {
  239. e.consume();
  240. let selected = this.selection[0];
  241. let maxScale = Math.max(...selected.scale.toArray());
  242. let minScale = Math.min(...selected.scale.toArray());
  243. let handleLength = Math.abs(selected.scale.dot(new THREE.Vector3(...handle.alignment)));
  244. let alignment = new THREE.Vector3(...handle.alignment).multiplyScalar(2 * maxScale / handleLength);
  245. alignment.applyMatrix4(selected.matrixWorld);
  246. let newCamPos = alignment;
  247. let newCamTarget = selected.getWorldPosition(new THREE.Vector3());
  248. Utils.moveTo(this.viewer.scene, newCamPos, newCamTarget);
  249. });
  250. pickSphere.addEventListener("mouseover", e => {
  251. //box.setOpacity(1);
  252. });
  253. pickSphere.addEventListener("mouseleave", e => {
  254. //box.setOpacity(OpaWhenNotSelect);
  255. });
  256. }
  257. }
  258. initializeTranslationHandles(){
  259. let boxGeometry = new THREE.BoxGeometry(1, 1, 1);
  260. for(let handleName of Object.keys(this.translationHandles)){
  261. let handle = this.handles[handleName];
  262. let node = handle.node;
  263. this.scene.add(node);
  264. let material = new THREE.MeshBasicMaterial({
  265. color: handle.color,
  266. opacity: OpaWhenNotSelect,
  267. transparent: true});
  268. let outlineMaterial = new THREE.MeshBasicMaterial({
  269. color: OutlineColor,
  270. side: THREE.BackSide,
  271. opacity: OpaWhenNotSelect,
  272. transparent: true});
  273. let pickMaterial = new THREE.MeshNormalMaterial({
  274. opacity: 0.2,
  275. transparent: true,
  276. visible: this.showPickVolumes
  277. });
  278. let box = new THREE.Mesh(boxGeometry, material);
  279. box.name = `${handleName}.handle`;
  280. box.scale.set(1, 1, 36);
  281. box.lookAt(new THREE.Vector3(...handle.alignment));
  282. box.renderOrder = 10;
  283. node.add(box);
  284. handle.translateNode = box;
  285. let outline = new THREE.Mesh(boxGeometry, outlineMaterial);
  286. outline.name = `${handleName}.outline`;
  287. outline.scale.set(1.3, 1.3, 1.01);
  288. outline.renderOrder = 0;
  289. box.add(outline);
  290. let pickVolume = new THREE.Mesh(boxGeometry, pickMaterial);
  291. pickVolume.name = `${handleName}.pick_volume`;
  292. pickVolume.scale.set(4, 4, 1.1);
  293. pickVolume.handle = handleName;
  294. box.add(pickVolume);
  295. this.pickVolumes.push(pickVolume);
  296. node.setOpacity = (target) => {
  297. let opacity = {x: material.opacity};
  298. let t = new TWEEN.Tween(opacity).to({x: target}, 100);
  299. t.onUpdate(() => {
  300. box.visible = opacity.x > 0;
  301. pickVolume.visible = opacity.x > 0;
  302. material.opacity = opacity.x;
  303. outlineMaterial.opacity = opacity.x;
  304. pickMaterial.opacity = opacity.x * 0.5;
  305. });
  306. t.start();
  307. };
  308. pickVolume.addEventListener("drag", (e) => {this.dragTranslationHandle(e)});
  309. pickVolume.addEventListener("drop", (e) => {this.dropTranslationHandle(e)});
  310. }
  311. }
  312. initializeRotationHandles(){
  313. let adjust = 1.5;
  314. let torusGeometry = new THREE.TorusGeometry(1, adjust * 0.015, 8, 64, Math.PI / 2);
  315. let outlineGeometry = new THREE.TorusGeometry(1, adjust * 0.018, 8, 64, Math.PI / 2);
  316. let pickGeometry = new THREE.TorusGeometry(1, adjust * 0.07, 6, 4, Math.PI / 2);
  317. for(let handleName of Object.keys(this.rotationHandles)){
  318. let handle = this.handles[handleName];
  319. let node = handle.node;
  320. this.scene.add(node);
  321. let material = new THREE.MeshBasicMaterial({
  322. color: handle.color,
  323. opacity: OpaWhenNotSelect,
  324. transparent: true
  325. });
  326. let outlineMaterial = new THREE.MeshBasicMaterial({
  327. color: OutlineColor,
  328. side: THREE.BackSide,
  329. opacity: OpaWhenNotSelect,
  330. transparent: true
  331. });
  332. let pickMaterial = new THREE.MeshNormalMaterial({
  333. opacity: 0.2,
  334. transparent: true,
  335. visible: this.showPickVolumes
  336. });
  337. let box = new THREE.Mesh(torusGeometry, material);
  338. box.name = `${handleName}.handle`;
  339. box.scale.set(30, 30, 30);
  340. box.lookAt(new THREE.Vector3(...handle.alignment));
  341. node.add(box);
  342. handle.translateNode = box;
  343. let outline = new THREE.Mesh(outlineGeometry, outlineMaterial);
  344. outline.name = `${handleName}.outline`;
  345. outline.scale.set(1, 1, 1);
  346. outline.renderOrder = 0;
  347. box.add(outline);
  348. let pickVolume = new THREE.Mesh(pickGeometry, pickMaterial);
  349. pickVolume.name = `${handleName}.pick_volume`;
  350. pickVolume.scale.set(1, 1, 1);
  351. pickVolume.handle = handleName;
  352. box.add(pickVolume);
  353. this.pickVolumes.push(pickVolume);
  354. node.setOpacity = (target) => {
  355. let opacity = {x: material.opacity};
  356. let t = new TWEEN.Tween(opacity).to({x: target}, 100);
  357. t.onUpdate(() => {
  358. box.visible = opacity.x > 0;
  359. pickVolume.visible = opacity.x > 0;
  360. material.opacity = opacity.x;
  361. outlineMaterial.opacity = opacity.x;
  362. pickMaterial.opacity = opacity.x * 0.5;
  363. });
  364. t.start();
  365. };
  366. //pickVolume.addEventListener("mouseover", (e) => {
  367. // //let a = this.viewer.scene.getActiveCamera().getWorldDirection(new THREE.Vector3()).dot(pickVolume.getWorldDirection(new THREE.Vector3()));
  368. // console.log(pickVolume.getWorldDirection(new THREE.Vector3()));
  369. //});
  370. pickVolume.addEventListener("drag", (e) => {this.dragRotationHandle(e)});
  371. pickVolume.addEventListener("drop", (e) => {this.dropRotationHandle(e)});
  372. }
  373. }
  374. dragRotationHandle(e){
  375. let drag = e.drag;
  376. let handle = this.activeHandle;
  377. let camera = this.viewer.scene.getActiveCamera();
  378. if(!handle){
  379. return
  380. };
  381. let localNormal = new THREE.Vector3(...handle.alignment);
  382. let n = new THREE.Vector3();
  383. n.copy(new THREE.Vector4(...localNormal.toArray(), 0).applyMatrix4(handle.node.matrixWorld));
  384. n.normalize();
  385. if (!drag.intersectionStart){
  386. //this.viewer.scene.scene.remove(this.debug);
  387. //this.debug = new THREE.Object3D();
  388. //this.viewer.scene.scene.add(this.debug);
  389. //Utils.debugSphere(this.debug, drag.location, 3, 0xaaaaaa);
  390. //let debugEnd = drag.location.clone().add(n.clone().multiplyScalar(20));
  391. //Utils.debugLine(this.debug, drag.location, debugEnd, 0xff0000);
  392. drag.intersectionStart = drag.location;
  393. drag.objectStart = drag.object.getWorldPosition(new THREE.Vector3());
  394. drag.handle = handle;
  395. let plane = new THREE.Plane().setFromNormalAndCoplanarPoint(n, drag.intersectionStart);
  396. drag.dragPlane = plane;
  397. drag.pivot = drag.intersectionStart;
  398. }else{
  399. handle = drag.handle;
  400. }
  401. this.dragging = true;
  402. let pointer = this.viewer.inputHandler.pointer
  403. let domElement = this.viewer.renderer.domElement;
  404. let ray = Utils.mouseToRay(pointer, camera, domElement.clientWidth, domElement.clientHeight);
  405. let I = ray.intersectPlane(drag.dragPlane, new THREE.Vector3());
  406. if (I) {
  407. let center = this.scene.getWorldPosition(new THREE.Vector3());
  408. let from = drag.pivot;
  409. let to = I;
  410. let v1 = from.clone().sub(center).normalize();
  411. let v2 = to.clone().sub(center).normalize();
  412. let angle = Math.acos(v1.dot(v2));
  413. let sign = Math.sign(v1.cross(v2).dot(n));
  414. angle = angle * sign;
  415. if (Number.isNaN(angle)) {
  416. return;
  417. }
  418. let normal = new THREE.Vector3(...handle.alignment);
  419. for (let selection of this.selection) {
  420. selection.rotateOnAxis(normal, angle);
  421. selection.dispatchEvent({
  422. type: "orientation_changed",
  423. object: selection
  424. });
  425. }
  426. drag.pivot = I;
  427. }
  428. }
  429. dropRotationHandle(e){
  430. this.dragging = false;
  431. this.setActiveHandle(null);
  432. }
  433. dragTranslationHandle(e){
  434. let drag = e.drag;
  435. let handle = this.activeHandle;
  436. let camera = this.viewer.scene.getActiveCamera();
  437. if(!drag.intersectionStart && handle){
  438. drag.intersectionStart = drag.location;
  439. drag.objectStart = drag.object.getWorldPosition(new THREE.Vector3());
  440. let start = drag.intersectionStart;
  441. let dir = new THREE.Vector4(...handle.alignment, 0).applyMatrix4(this.scene.matrixWorld);
  442. let end = new THREE.Vector3().addVectors(start, dir);
  443. let line = new THREE.Line3(start.clone(), end.clone());
  444. drag.line = line;
  445. let camOnLine = line.closestPointToPoint(camera.position, false, new THREE.Vector3());
  446. let normal = new THREE.Vector3().subVectors(camera.position, camOnLine);
  447. let plane = new THREE.Plane().setFromNormalAndCoplanarPoint(normal, drag.intersectionStart);
  448. drag.dragPlane = plane;
  449. drag.pivot = drag.intersectionStart;
  450. }else{
  451. handle = drag.handle;
  452. }
  453. this.dragging = true;
  454. {
  455. let pointer = this.viewer.inputHandler.pointer
  456. let domElement = this.viewer.renderer.domElement;
  457. let ray = Utils.mouseToRay(pointer, camera, domElement.clientWidth, domElement.clientHeight);
  458. let I = ray.intersectPlane(drag.dragPlane, new THREE.Vector3());
  459. if (I) {
  460. let iOnLine = drag.line.closestPointToPoint(I, false, new THREE.Vector3());
  461. let diff = new THREE.Vector3().subVectors(iOnLine, drag.pivot);
  462. for (let selection of this.selection) {
  463. selection.position.add(diff);
  464. selection.dispatchEvent({
  465. type: "position_changed",
  466. object: selection
  467. });
  468. }
  469. drag.pivot = drag.pivot.add(diff);
  470. }
  471. }
  472. }
  473. dropTranslationHandle(e){
  474. this.dragging = false;
  475. this.setActiveHandle(null);
  476. }
  477. dropScaleHandle(e){
  478. this.dragging = false;
  479. this.setActiveHandle(null);
  480. }
  481. dragScaleHandle(e){
  482. let drag = e.drag;
  483. let handle = this.activeHandle;
  484. let camera = this.viewer.scene.getActiveCamera();
  485. if(!drag.intersectionStart){
  486. drag.intersectionStart = drag.location;
  487. drag.objectStart = drag.object.getWorldPosition(new THREE.Vector3());
  488. drag.handle = handle;
  489. let start = drag.intersectionStart;
  490. let dir = new THREE.Vector4(...handle.alignment, 0).applyMatrix4(this.scene.matrixWorld);
  491. let end = new THREE.Vector3().addVectors(start, dir);
  492. let line = new THREE.Line3(start.clone(), end.clone());
  493. drag.line = line;
  494. let camOnLine = line.closestPointToPoint(camera.position, false, new THREE.Vector3());
  495. let normal = new THREE.Vector3().subVectors(camera.position, camOnLine);
  496. let plane = new THREE.Plane().setFromNormalAndCoplanarPoint(normal, drag.intersectionStart);
  497. drag.dragPlane = plane;
  498. drag.pivot = drag.intersectionStart;
  499. //Utils.debugSphere(viewer.scene.scene, drag.pivot, 0.05);
  500. }else{
  501. handle = drag.handle;
  502. }
  503. this.dragging = true;
  504. {
  505. let pointer = this.viewer.inputHandler.pointer
  506. let domElement = this.viewer.renderer.domElement;
  507. let ray = Utils.mouseToRay(pointer, camera, domElement.clientWidth, domElement.clientHeight);
  508. let I = ray.intersectPlane(drag.dragPlane, new THREE.Vector3());
  509. if (I) {
  510. let iOnLine = drag.line.closestPointToPoint(I, false, new THREE.Vector3());
  511. let direction = handle.alignment.reduce( (a, v) => a + v, 0);
  512. let toObjectSpace = this.selection[0].matrixWorld.clone().invert();
  513. let iOnLineOS = iOnLine.clone().applyMatrix4(toObjectSpace);
  514. let pivotOS = drag.pivot.clone().applyMatrix4(toObjectSpace);
  515. let diffOS = new THREE.Vector3().subVectors(iOnLineOS, pivotOS);
  516. let dragDirectionOS = diffOS.clone().normalize();
  517. if(iOnLine.distanceTo(drag.pivot) === 0){
  518. dragDirectionOS.set(0, 0, 0);
  519. }
  520. let dragDirection = dragDirectionOS.dot(new THREE.Vector3(...handle.alignment));
  521. let diff = new THREE.Vector3().subVectors(iOnLine, drag.pivot);
  522. let diffScale = new THREE.Vector3(...handle.alignment).multiplyScalar(diff.length() * direction * dragDirection);
  523. let diffPosition = diff.clone().multiplyScalar(0.5);
  524. for (let selection of this.selection) {
  525. selection.scale.add(diffScale);
  526. selection.scale.x = Math.max(0.1, selection.scale.x);
  527. selection.scale.y = Math.max(0.1, selection.scale.y);
  528. selection.scale.z = Math.max(0.1, selection.scale.z);
  529. selection.position.add(diffPosition);
  530. selection.dispatchEvent({
  531. type: "position_changed",
  532. object: selection
  533. });
  534. selection.dispatchEvent({
  535. type: "scale_changed",
  536. object: selection
  537. });
  538. }
  539. drag.pivot.copy(iOnLine);
  540. //Utils.debugSphere(viewer.scene.scene, drag.pivot, 0.05);
  541. }
  542. }
  543. }
  544. setActiveHandle(handle){
  545. if(this.dragging){
  546. return;
  547. }
  548. if(this.activeHandle === handle){
  549. return;
  550. }
  551. this.activeHandle = handle;
  552. if(handle === null){
  553. for(let handleName of Object.keys(this.handles)){
  554. let handle = this.handles[handleName];
  555. handle.node.setOpacity(0);
  556. }
  557. }
  558. for(let handleName of Object.keys(this.focusHandles)){
  559. let handle = this.focusHandles[handleName];
  560. if(this.activeHandle === handle){
  561. handle.node.setOpacity(1.0);
  562. }else{
  563. handle.node.setOpacity(OpaWhenNotSelect)
  564. }
  565. }
  566. for(let handleName of Object.keys(this.translationHandles)){
  567. let handle = this.translationHandles[handleName];
  568. if(this.activeHandle === handle){
  569. handle.node.setOpacity(1.0);
  570. }else{
  571. handle.node.setOpacity(OpaWhenNotSelect)
  572. }
  573. }
  574. for(let handleName of Object.keys(this.rotationHandles)){
  575. let handle = this.rotationHandles[handleName];
  576. //if(this.activeHandle === handle){
  577. // handle.node.setOpacity(1.0);
  578. //}else{
  579. // handle.node.setOpacity(OpaWhenNotSelect)
  580. //}
  581. handle.node.setOpacity(OpaWhenNotSelect);
  582. }
  583. for(let handleName of Object.keys(this.scaleHandles)){
  584. let handle = this.scaleHandles[handleName];
  585. if(this.activeHandle === handle){
  586. handle.node.setOpacity(1.0);
  587. let relatedFocusHandle = this.focusHandles[handle.name.replace("scale", "focus")];
  588. let relatedFocusNode = relatedFocusHandle.node;
  589. relatedFocusNode.setOpacity(OpaWhenNotSelect);
  590. for(let translationHandleName of Object.keys(this.translationHandles)){
  591. let translationHandle = this.translationHandles[translationHandleName];
  592. translationHandle.node.setOpacity(OpaWhenNotSelect);
  593. }
  594. //let relatedTranslationHandle = this.translationHandles[
  595. // handle.name.replace("scale", "translation").replace(/[+-]/g, "")];
  596. //let relatedTranslationNode = relatedTranslationHandle.node;
  597. //relatedTranslationNode.setOpacity(OpaWhenNotSelect);
  598. }else{
  599. handle.node.setOpacity(OpaWhenNotSelect)
  600. }
  601. }
  602. if(handle){
  603. handle.node.setOpacity(1.0);
  604. }
  605. }
  606. update () {
  607. if(this.selection.length === 1){
  608. this.scene.visible = true;
  609. this.scene.updateMatrix();
  610. this.scene.updateMatrixWorld();
  611. let selected = this.selection[0];
  612. let world = selected.matrixWorld;
  613. let camera = this.viewer.scene.getActiveCamera();
  614. let domElement = this.viewer.renderer.domElement;
  615. let pointer = this.viewer.inputHandler.pointer;
  616. let center = selected.boundingBox.getCenter(new THREE.Vector3()).clone().applyMatrix4(selected.matrixWorld);
  617. this.scene.scale.copy(selected.boundingBox.getSize(new THREE.Vector3()).multiply(selected.scale));
  618. this.scene.position.copy(center);
  619. this.scene.rotation.copy(selected.rotation);
  620. this.scene.updateMatrixWorld();
  621. {
  622. // adjust scale of components
  623. for(let handleName of Object.keys(this.handles)){
  624. let handle = this.handles[handleName];
  625. let node = handle.node;
  626. let handlePos = node.getWorldPosition(new THREE.Vector3());
  627. let distance = handlePos.distanceTo(camera.position);
  628. let pr = Utils.projectedRadius(1, camera, distance, domElement.clientWidth, domElement.clientHeight);
  629. let ws = node.parent.getWorldScale(new THREE.Vector3());
  630. let s = (ScaleRatio / pr);
  631. let scale = new THREE.Vector3(s, s, s).divide(ws);
  632. let rot = new THREE.Matrix4().makeRotationFromEuler(node.rotation);
  633. let rotInv = rot.clone().invert();
  634. scale.applyMatrix4(rotInv);
  635. scale.x = Math.abs(scale.x);
  636. scale.y = Math.abs(scale.y);
  637. scale.z = Math.abs(scale.z);
  638. node.scale.copy(scale);
  639. }
  640. // adjust rotation handles
  641. if(!this.dragging){
  642. let tWorld = this.scene.matrixWorld;
  643. let tObject = tWorld.clone().invert();
  644. let camObjectPos = camera.getWorldPosition(new THREE.Vector3()).applyMatrix4(tObject);
  645. let x = this.rotationHandles["rotation.x"].node.rotation;
  646. let y = this.rotationHandles["rotation.y"].node.rotation;
  647. let z = this.rotationHandles["rotation.z"].node.rotation;
  648. x.order = "ZYX";
  649. y.order = "ZYX";
  650. let above = camObjectPos.z > 0;
  651. let below = !above;
  652. let PI_HALF = Math.PI / 2;
  653. if(above){
  654. if(camObjectPos.x > 0 && camObjectPos.y > 0){
  655. x.x = 1 * PI_HALF;
  656. y.y = 3 * PI_HALF;
  657. z.z = 0 * PI_HALF;
  658. }else if(camObjectPos.x < 0 && camObjectPos.y > 0){
  659. x.x = 1 * PI_HALF;
  660. y.y = 2 * PI_HALF;
  661. z.z = 1 * PI_HALF;
  662. }else if(camObjectPos.x < 0 && camObjectPos.y < 0){
  663. x.x = 2 * PI_HALF;
  664. y.y = 2 * PI_HALF;
  665. z.z = 2 * PI_HALF;
  666. }else if(camObjectPos.x > 0 && camObjectPos.y < 0){
  667. x.x = 2 * PI_HALF;
  668. y.y = 3 * PI_HALF;
  669. z.z = 3 * PI_HALF;
  670. }
  671. }else if(below){
  672. if(camObjectPos.x > 0 && camObjectPos.y > 0){
  673. x.x = 0 * PI_HALF;
  674. y.y = 0 * PI_HALF;
  675. z.z = 0 * PI_HALF;
  676. }else if(camObjectPos.x < 0 && camObjectPos.y > 0){
  677. x.x = 0 * PI_HALF;
  678. y.y = 1 * PI_HALF;
  679. z.z = 1 * PI_HALF;
  680. }else if(camObjectPos.x < 0 && camObjectPos.y < 0){
  681. x.x = 3 * PI_HALF;
  682. y.y = 1 * PI_HALF;
  683. z.z = 2 * PI_HALF;
  684. }else if(camObjectPos.x > 0 && camObjectPos.y < 0){
  685. x.x = 3 * PI_HALF;
  686. y.y = 0 * PI_HALF;
  687. z.z = 3 * PI_HALF;
  688. }
  689. }
  690. }
  691. {
  692. let ray = Utils.mouseToRay(pointer, camera, domElement.clientWidth, domElement.clientHeight);
  693. let raycaster = new THREE.Raycaster(ray.origin, ray.direction);
  694. raycaster.layers.enableAll()//add
  695. let intersects = raycaster.intersectObjects(this.pickVolumes.filter(v => v.visible), true);
  696. if(intersects.length > 0){
  697. let I = intersects[0];
  698. let handleName = I.object.handle;
  699. this.setActiveHandle(this.handles[handleName]);
  700. }else{
  701. this.setActiveHandle(null);
  702. }
  703. }
  704. //
  705. for(let handleName of Object.keys(this.scaleHandles)){
  706. let handle = this.handles[handleName];
  707. let node = handle.node;
  708. let alignment = handle.alignment;
  709. }
  710. }
  711. }else{
  712. this.scene.visible = false;
  713. }
  714. }
  715. };