drawGeometry.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. let scene, camera, renderer, controls;
  2. let stats = initStats();
  3. let key = 'points', id = 'id';
  4. let verticalLineAry = [];
  5. /* 初始加载 */
  6. (function () {
  7. console.log("three start...");
  8. init();
  9. animate();
  10. console.log("three end...");
  11. })();
  12. /**
  13. * 初始化函数
  14. */
  15. function init() {
  16. initRenderer();
  17. initScene();
  18. initCamera();
  19. initLight();
  20. initControls();
  21. drawGeometry();
  22. }
  23. function drawGeometry() {
  24. if (!panorama_line_3d) {
  25. let geometry = new THREE.geometry
  26. console.log('panorama_line_3d未定义, 请检查');
  27. return;
  28. }
  29. let material = new THREE.LineBasicMaterial({
  30. color: 'red'
  31. })
  32. let geometry = new THREE.Geometry();
  33. // 第一层的面循环
  34. for (let i = 0; i < panorama_line_3d.length; i++) {
  35. let face = panorama_line_3d[i];
  36. if (face.length < 4) {
  37. console.log('构成面的线段数小于4, 请检查');
  38. return;
  39. }
  40. let sta = false; // 用于控制寻找相同点还是不同点
  41. // 第二层面中的线循环
  42. for (let j = 0; j < face.length; j++) {
  43. if (!face[j][key] || face[j][key].length < 2) {
  44. console.log('线段' + face[j][id] + '的点数据有误, 请检查');
  45. return;
  46. }
  47. // 绘制第一条线段
  48. if (j === 0) {
  49. // 第一条线段, 先寻找不同点
  50. let firstPoint = findPoint(face[j][key], face[j + 1][key], false);
  51. verticalLineAry.push(findverticalLine(firstPoint));
  52. firstPoint && geometry.vertices.push(new THREE.Vector3(firstPoint[0], firstPoint[1], firstPoint[2]));
  53. // 再寻找相同点
  54. let secondPoint = findPoint(face[j][key], face[j + 1][key], true);
  55. verticalLineAry.push(findverticalLine(secondPoint));
  56. secondPoint && geometry.vertices.push(new THREE.Vector3(secondPoint[0], secondPoint[1], secondPoint[2]));
  57. } else if (j === face.length - 1) { // 最后一条线段, 寻找与第一条线段相交的点
  58. let lastPoint = findPoint(face[j][key], face[0][key], true);
  59. verticalLineAry.push(findverticalLine(lastPoint));
  60. lastPoint && geometry.vertices.push(new THREE.Vector3(lastPoint[0], lastPoint[1], lastPoint[2]));
  61. } else { // 寻找与上一条线段相交的点
  62. let point = findPoint(face[j][key], face[j + 1][key], true);
  63. verticalLineAry.push(findverticalLine(point));
  64. point && geometry.vertices.push(new THREE.Vector3(point[0], point[1], point[2]));
  65. }
  66. }
  67. }
  68. let line = new THREE.Line(geometry, material);
  69. scene.add(line);
  70. findIntersectLine();
  71. }
  72. /**
  73. *
  74. * @param {*} point
  75. */
  76. function findverticalLine(point) {
  77. let lineAry = [];
  78. for(let i = 0; i < panorama_line_3d.length; i++) {
  79. for(let j = 0; j < panorama_line_3d[i].length; j++) {
  80. for(let k = 0; k < panorama_line_3d[i][j]['points'].length; k++) {
  81. let o_point = panorama_line_3d[i][j]['points'][k];
  82. if(arrayEquals(o_point, point)) {
  83. lineAry.push(panorama_line_3d[i][j]['id']);
  84. }
  85. }
  86. }
  87. }
  88. // console.log(lineAry);
  89. return lineAry;
  90. }
  91. /**
  92. * 初始化渲染器
  93. */
  94. function initRenderer() {
  95. renderer = new THREE.WebGLRenderer({
  96. antialias: true
  97. });
  98. renderer.setSize(window.innerWidth / 2, window.innerHeight);
  99. renderer.setClearColor("#ccc");
  100. renderer.shadowMap.enabled = true;
  101. let rightCav = document.getElementById('geo-cav');
  102. rightCav.appendChild(renderer.domElement);
  103. // canvas = renderer.domElement;
  104. }
  105. /**
  106. * 初始化场景
  107. */
  108. function initScene() {
  109. scene = new THREE.Scene();
  110. // initContents();
  111. }
  112. /**
  113. * 初始化相机
  114. */
  115. function initCamera() {
  116. camera = new THREE.PerspectiveCamera(
  117. 45,
  118. 0.5* window.innerWidth / window.innerHeight,
  119. 1,
  120. 100
  121. );
  122. // camera = new THREE.OrthographicCamera(3 / - 2, 3 / 2, 3 / 2, 3 / - 2, 1, 1000);
  123. camera.position.set(2.5, -2, 1);
  124. camera.up.x = 0;
  125. camera.up.y = 0;
  126. camera.up.z = 1;
  127. camera.lookAt(new THREE.Vector3(0, 0, 0));
  128. }
  129. /**
  130. * 初始化灯光
  131. */
  132. function initLight() {
  133. let ambientLight = new THREE.AmbientLight(0x333333);
  134. let directionalLight = new THREE.DirectionalLight("#FFFAFA", 1);
  135. directionalLight.position.set(100, -200, 200);
  136. directionalLight.castShadow = true;
  137. scene.add(ambientLight);
  138. scene.add(directionalLight);
  139. }
  140. /**
  141. * 初始化控制器
  142. */
  143. function initControls() {
  144. // TODO: 3D状态下启动控制器
  145. controls = new THREE.OrbitControls(camera, renderer.domElement);
  146. }
  147. /**
  148. * 初始化性能插件
  149. */
  150. function initStats() {
  151. let stats = new Stats();
  152. stats.setMode(0);
  153. stats.domElement.style.position = "absolute";
  154. stats.domElement.style.left = "0";
  155. stats.domElement.style.top = "0";
  156. document.body.appendChild(stats.domElement);
  157. return stats;
  158. }
  159. /* 循环调用 */
  160. function animate() {
  161. requestAnimationFrame(animate);
  162. renderer.render(scene, camera);
  163. update();
  164. }
  165. /* 更新数据 */
  166. function update() {
  167. stats.update();
  168. controls.update();
  169. }