Player.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import * as THREE from "three";
  2. import FloorplanControls from "../controls/FloorplanControls.js";
  3. import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
  4. import Line from "../box/object/Line";
  5. import LinePoints from "../box/object/LinePoints.js";
  6. import { LineMaterial } from "three/examples/jsm/lines/LineMaterial.js";
  7. const convertScreenToNDC = function (event, domElement) {
  8. let x = (event.offsetX / domElement.clientWidth) * 2 - 1;
  9. let y = -(event.offsetY / domElement.clientHeight) * 2 + 1;
  10. return new THREE.Vector2(x, y);
  11. };
  12. export default class Player {
  13. constructor(scene) {
  14. this.scene = scene;
  15. this.orthCamera = scene.orthCamera;
  16. this.floorplanControls = null;
  17. this.raycaster = null;
  18. this.position = new THREE.Vector3();
  19. this.pointerdown = new THREE.Vector2();
  20. this.pointerup = new THREE.Vector2();
  21. this.pointer = new THREE.Vector2();
  22. this.touchImg = null;
  23. this.activeEdge = null;
  24. this.drawLine = null;
  25. this.startObj = null;
  26. this.allowDrawing = false;
  27. this.drawing = false;
  28. this.inited = false;
  29. this.renderLines = [];
  30. this.activeEdges = [];
  31. this.matLine = null;
  32. this.lineColor = 0xe44d54;
  33. this.init();
  34. }
  35. init = () => {
  36. // //floorplanControls
  37. // this.floorplanControls = new FloorplanControls(this.orthCamera, this.scene.domElement, this);
  38. this.floorplanControls = new OrbitControls(
  39. this.orthCamera,
  40. this.scene.domElement
  41. );
  42. this.floorplanControls.enablePan = true;
  43. // this.floorplanControls.target.set(0, 1, 0);
  44. // this.floorplanControls.rotateSpeed = 0.5;
  45. // this.floorplanControls.panSpeed = 0.75
  46. // this.floorplanControls.maxDistance = 100
  47. // this.floorplanControls.minDistance = 3.5
  48. this.floorplanControls.maxZoom = 500;
  49. this.floorplanControls.minZoom = 100;
  50. this.floorplanControls.enableRotate = false;
  51. this.raycaster = new THREE.Raycaster();
  52. this.onBindEvent();
  53. this.inited = true;
  54. console.log("this.floorplanControls", this.floorplanControls);
  55. this.matLine = new LineMaterial({
  56. color: this.lineColor,
  57. linewidth: 3, // in world units with size attenuation, pixels otherwise
  58. dashed: false,
  59. alphaToCoverage: true,
  60. });
  61. this.matLine.resolution = new THREE.Vector2(
  62. this.scene.width,
  63. this.scene.height
  64. );
  65. };
  66. onPointerMove = (e) => {
  67. if (!this.drawing) return;
  68. this.pointermove = convertScreenToNDC(e, this.scene.domElement);
  69. this.raycaster.setFromCamera(this.pointermove, this.orthCamera);
  70. let intersectArr = this.scene.boxManager.imgList;
  71. // if(this.startObj) {
  72. // let i = intersectArr.indexOf(this.startObj)
  73. // intersectArr.splice(i, 1)
  74. // }
  75. const intersects = this.raycaster.intersectObjects(intersectArr, false);
  76. if (intersects[0] && intersects[0].object !== this.startObj) {
  77. this.touchImg = intersects[0];
  78. this.setActiveLine(this.touchImg);
  79. }
  80. };
  81. onPointerDown = (e) => {
  82. console.log("start draw");
  83. this.pointerdown = convertScreenToNDC(e, this.scene.domElement);
  84. this.raycaster.setFromCamera(this.pointerdown, this.orthCamera);
  85. let intersectArr = this.scene.boxManager.imgList;
  86. const intersects = this.raycaster.intersectObjects(intersectArr, false);
  87. console.log("intersects", intersects);
  88. if (intersects[0]) {
  89. this.startObj = intersects[0].object;
  90. this.drawing = true;
  91. } else {
  92. this.startObj = null;
  93. this.drawing = false;
  94. }
  95. // this.floorplanControls.enabled = false;
  96. };
  97. onPointerUp = (e) => {
  98. // console.log("last Line-wPos", points);
  99. this.pointerup = convertScreenToNDC(e, this.scene.domElement);
  100. this.drawing = false;
  101. this.floorplanControls.enabled = true;
  102. this.startObj = null;
  103. if (this.drawLine) {
  104. const points = this.drawLine.userData.points;
  105. const dir = this.drawLine.userData.dir;
  106. const finishLine = new LinePoints(points, 0, this.matLine);
  107. this.renderLines.push(points);
  108. this.scene.scene.add(finishLine);
  109. const imageId = this.touchImg.object.userData;
  110. console.log("this.touchImg", dir, imageId, points);
  111. }
  112. };
  113. Listener = {
  114. onPointerDown: this.onPointerDown.bind(this),
  115. onPointerMove: this.onPointerMove.bind(this),
  116. onPointerUp: this.onPointerUp.bind(this),
  117. };
  118. onBindEvent = () => {
  119. this.scene.domElement.addEventListener(
  120. "pointerdown",
  121. this.Listener.onPointerDown
  122. );
  123. this.scene.domElement.addEventListener(
  124. "pointermove",
  125. this.Listener.onPointerMove,
  126. false
  127. );
  128. this.scene.domElement.addEventListener(
  129. "pointerup",
  130. this.Listener.onPointerUp
  131. );
  132. };
  133. unbindEvent = () => {
  134. this.scene.domElement.removeEventListener(
  135. "pointerdown",
  136. this.Listener.onPointerDown
  137. );
  138. this.scene.domElement.removeEventListener(
  139. "pointermove",
  140. this.Listener.onPointerMove
  141. );
  142. this.scene.domElement.removeEventListener(
  143. "pointerup",
  144. this.Listener.onPointerUp
  145. );
  146. };
  147. buildLine = () => {
  148. if (this.drawLine) {
  149. this.drawLine.removeFromParent();
  150. }
  151. // console.log("this.drawLine", this.drawLine);
  152. let s = new THREE.Vector3(this.pointerdown.x, this.pointerdown.y, -1);
  153. let e = new THREE.Vector3(this.pointermove.x, this.pointermove.y, -1);
  154. s.unproject(this.orthCamera);
  155. e.unproject(this.orthCamera);
  156. s.y = 5;
  157. e.y = 5;
  158. const matLine = new LineMaterial({
  159. color: this.lineColor,
  160. linewidth: 3, // in world units with size attenuation, pixels otherwise
  161. dashed: false,
  162. alphaToCoverage: true,
  163. });
  164. matLine.resolution = new THREE.Vector2(this.scene.width, this.scene.height);
  165. this.drawLine = new Line(s, e, this.activeEdge, matLine);
  166. this.scene.scene.add(this.drawLine);
  167. };
  168. setActiveLine = (obj) => {
  169. function getTouchLine(x, y) {
  170. // [0 - 1]
  171. x -= 0.5;
  172. y -= 0.5;
  173. // console.log(x, y);
  174. if (x >= 0 && y >= 0) {
  175. if (x > y) {
  176. return 3;
  177. } else {
  178. return 0;
  179. }
  180. } else if (x >= 0 && y <= 0) {
  181. if (x > Math.abs(y)) {
  182. return 3;
  183. } else {
  184. return 2;
  185. }
  186. } else if (x <= 0 && y >= 0) {
  187. if (Math.abs(x) > y) {
  188. return 1;
  189. } else {
  190. return 0;
  191. }
  192. } else if (x <= 0 && y <= 0) {
  193. if (-x > -y) {
  194. return 1;
  195. } else {
  196. return 2;
  197. }
  198. }
  199. }
  200. if (this.activeEdge) {
  201. this.activeEdge.visible = false;
  202. this.activeEdge = null;
  203. }
  204. let num = getTouchLine(obj.uv.x, obj.uv.y);
  205. this.activeEdge = obj.object.touchLines.getObjectByName(num);
  206. this.activeEdge.visible = true;
  207. this.buildLine();
  208. };
  209. update = () => {
  210. if (this.floorplanControls.enabled) {
  211. this.floorplanControls && this.floorplanControls.update();
  212. }
  213. };
  214. }