three.html 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport"
  6. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  7. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  8. <title>模型标注</title>
  9. <link rel="stylesheet" href="/static/hplus/css/bootstrap.min.css">
  10. <link rel="stylesheet" href="/admin/css/common.css">
  11. <style>
  12. html,body {
  13. padding: 0;
  14. margin: 0;
  15. overflow: hidden;
  16. }
  17. .red-label{
  18. position: fixed;
  19. z-index: 100;
  20. width: 10px;
  21. height: 10px;
  22. border-radius: 50%;
  23. background-color: red;
  24. }
  25. .pbtn{
  26. padding: 10px;
  27. }
  28. </style>
  29. </head>
  30. <body>
  31. <div id="label2" class="red-label"></div>
  32. <div class="pbtn">
  33. <form method="post" action="{:url('three')}" class="form-horizontal">
  34. <input type="hidden" name="id" value="{$id}">
  35. <input type="hidden" name="x" id="x" value="{$x}">
  36. <input type="hidden" name="y" id="y" value="{$y}">
  37. <input type="hidden" name="z" id="z" value="{$z}">
  38. <button class="btn btn-primary ajax-post" data-layer="1" target-form="form-horizontal" type="submit">保 存</button>
  39. </form>
  40. </div>
  41. <script src="/static/hplus/js/jquery.min.js?v=2.1.4"></script>
  42. <script src="/static/layer3.2.0/layer.js"></script>
  43. <script src="/static/threejs135/three.min.js"></script>
  44. <script src="/static/threejs135/js/controls/OrbitControls.js"></script>
  45. <!-- 引入gltf模型加载库GLTFLoader.js -->
  46. <script src="/static/threejs135/js/loaders/GLTFLoader.js"></script>
  47. <script src="/static/threejs135/js/loaders/DRACOLoader.js"></script>
  48. <script src="/static/threejs135/js/environments/RoomEnvironment.js"></script>
  49. <script src="/static/threejs135/js/renderers/CSS3DRenderer.js"></script>
  50. <!--<script src="/static/threejs135/jsm/loaders/DRACOLoader.js"></script>-->
  51. <!--<script src="/static/threejs135/jsm/loaders/GLTFLoader.js"></script>-->
  52. <script src="/admin/js/common.js"></script>
  53. <script>
  54. window.onload=function(){
  55. var x = '{$x}';
  56. var y = '{$y}';
  57. var z = '{$z}';
  58. var gltf = null;
  59. /**
  60. * 创建场景对象Scene
  61. */
  62. var scene = new THREE.Scene();
  63. /**
  64. * 光源设置
  65. */
  66. //点光源
  67. // var point = new THREE.PointLight(0x444444);
  68. // point.position.set(200, 200, 300); //点光源位置
  69. // // scene.add(point); //点光源添加到场景中
  70. // //环境光
  71. // var ambient = new THREE.AmbientLight(0x444444);
  72. // scene.add(ambient);
  73. /**
  74. * 相机设置
  75. */
  76. var width = window.innerWidth; //窗口宽度
  77. var height = window.innerHeight; //窗口高度
  78. var k = width / height; //窗口宽高比
  79. var s = 15; //三维场景显示范围控制系数,系数越大,显示的范围越大
  80. //创建相机对象
  81. // var camera = new THREE.OrthographicCamera(-s * k, s * k, s, -s, 0.01, 1000);
  82. // var camera = new THREE.OrthographicCamera(30, width * height, s, -s, 0.01, 1000);
  83. var camera = new THREE.PerspectiveCamera(60, width / height, 0.01, 3000);
  84. // camera.position.set(200, 300, 200);
  85. // camera.position.set(-30, 18, 0); //设置相机位置
  86. // camera.position.set(30, 20, 20);
  87. camera.position.set(0, 11, 39);
  88. camera.lookAt(scene.position); //设置相机方向(指向的场景对象)
  89. var renderer = new THREE.WebGLRenderer({
  90. antialias: true // 开启锯齿
  91. });
  92. renderer.setSize(width, height); //设置渲染区域尺寸
  93. // renderer.setClearColor(0xb9d3ff, 1); //设置背景颜色
  94. document.body.appendChild(renderer.domElement); //body元素中插入canvas对象
  95. renderer.domElement.style.position = 'absolute';
  96. renderer.domElement.style.top = '0px';
  97. renderer.domElement.style.zIndex = '-1'; // canvas全屏,不遮挡其它HTML元素
  98. // 执行渲染操作 指定场景、相机作为参数
  99. renderer.render(scene, camera);
  100. var pmremGenerator = new THREE.PMREMGenerator(renderer);
  101. pmremGenerator.compileEquirectangularShader();
  102. scene.environment = pmremGenerator.fromScene(new THREE.RoomEnvironment()).texture;
  103. // 创建一个CSS3渲染器CSS3DRenderer
  104. var CSS3Renderer = new THREE.CSS3DRenderer();
  105. CSS3Renderer.setSize(width, height);
  106. CSS3Renderer.domElement.style.position = 'absolute';
  107. // 相对标签原位置位置偏移大小
  108. CSS3Renderer.domElement.style.top = '0px';
  109. CSS3Renderer.domElement.style.left = '0px';
  110. // //设置.pointerEvents=none,以免模型标签HTML元素遮挡鼠标选择场景模型
  111. CSS3Renderer.domElement.style.pointerEvents = 'none';
  112. document.body.appendChild(CSS3Renderer.domElement);
  113. // 渲染函数
  114. function render() {
  115. CSS3Renderer.render(scene, camera); // 执行渲染操作
  116. renderer.render(scene, camera); // 执行渲染操作
  117. requestAnimationFrame(render); // 请求再次执行渲染函数render,渲染下一帧
  118. }
  119. render();
  120. const controls = new THREE.OrbitControls(camera, renderer.domElement);// 创建控件对象
  121. controls.enableZoom = true;
  122. console.log(controls);
  123. window.addEventListener('click', choose); // 监听窗口鼠标单击事件
  124. if(x){
  125. drawPointer(x,y,z);
  126. }
  127. function choose(event) {
  128. console.log(camera,camera.position);
  129. const Sx = event.clientX; // 鼠标单击位置横坐标
  130. const Sy = event.clientY; // 鼠标单击位置纵坐标
  131. // 屏幕坐标转WebGL标准设备坐标
  132. const x = (Sx / window.innerWidth) * 2 - 1; // WebGL标准设备横坐标
  133. const y = -(Sy / window.innerHeight) * 2 + 1; // WebGL标准设备纵坐标
  134. // 创建一个射线投射器`Raycaster`
  135. const raycaster = new THREE.Raycaster();
  136. // 通过鼠标单击位置标准设备坐标和相机参数计算射线投射器`Raycaster`的射线属性.ray
  137. raycaster.setFromCamera(new THREE.Vector2(x, y), camera);
  138. // 返回.intersectObjects()参数中射线选中的网格模型对象
  139. // 未选中对象返回空数组[],选中一个数组1个元素,选中两个数组两个元素
  140. const intersects = raycaster.intersectObjects(gltf.scene.children);
  141. console.log('射线器返回的对象', intersects);
  142. // console.log("射线投射器返回的对象 点point", intersects[0].point);
  143. // intersects.length大于0说明,说明选中了模型
  144. if (intersects.length > 0) {
  145. // 选中模型的第一个设置为半透明
  146. drawPointer(intersects[0].point.x,intersects[0].point.y,intersects[0].point.z);
  147. $('#x').val(intersects[0].point.x);
  148. $('#y').val(intersects[0].point.y);
  149. $('#z').val(intersects[0].point.z);
  150. }
  151. }
  152. function drawPointer(x,y,z) {
  153. let worldVector1 = new THREE.Vector3(x,y,z);
  154. const div = document.getElementById('label2');
  155. div.style.display = 'block';
  156. const obj = new THREE.CSS3DSprite(div);
  157. scene.add(obj);
  158. obj.position.copy(worldVector1);
  159. obj.scale.set(0.05, 0.05, 0.05); // 缩放CSS3DObject模型对象
  160. }
  161. // 显示坐标辅助线
  162. // const axes = new THREE.AxisHelper(200);
  163. // scene.add(axes);
  164. var loader = new THREE.GLTFLoader(); // 创建一个GLTF加载器
  165. var dracoLoader = new THREE.DRACOLoader();
  166. dracoLoader.setDecoderPath('/static/threejs135/js/libs/draco/gltf/');
  167. loader.setDRACOLoader(dracoLoader);
  168. loader.load('/hbzyy.gltf', (gltfObj) => {
  169. console.log('控制台查看加载gltf文件返回的对象结构', gltfObj);
  170. gltf = gltfObj;
  171. scene.add(gltf.scene);
  172. scene.environment = pmremGenerator.fromScene(new THREE.RoomEnvironment(), 100).texture;
  173. gltf.scene.scale.set(0.02, 0.02, 0.02);// 整体缩放
  174. }, (xhr) => {
  175. // 控制台查看加载进度xhr
  176. // 通过加载进度xhr可以控制前端进度条进度 Math.floor:小数加载进度取整
  177. console.log(`加载进度${Math.floor(xhr.loaded / xhr.total * 100)}%`);
  178. });
  179. };
  180. </script>
  181. </body>
  182. </html>