three_old.html 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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/environments/RoomEnvironment.js"></script>
  48. <script src="/static/threejs135/js/renderers/CSS3DRenderer.js"></script>
  49. <script src="/static/threejs135/jsm/loaders/GLTFLoader.js"></script>
  50. <script src="/admin/js/common.js"></script>
  51. <script>
  52. window.onload=function(){
  53. var x = '{$x}';
  54. var y = '{$y}';
  55. var z = '{$z}';
  56. var gltf = null;
  57. /**
  58. * 创建场景对象Scene
  59. */
  60. var scene = new THREE.Scene();
  61. /**
  62. * 光源设置
  63. */
  64. //点光源
  65. var point = new THREE.PointLight(0xffffff);
  66. point.position.set(400, 200, 300); //点光源位置
  67. scene.add(point); //点光源添加到场景中
  68. //环境光
  69. var ambient = new THREE.AmbientLight(0x444444);
  70. scene.add(ambient);
  71. /**
  72. * 相机设置
  73. */
  74. var width = window.innerWidth; //窗口宽度
  75. var height = window.innerHeight; //窗口高度
  76. var k = width / height; //窗口宽高比
  77. var s = 150; //三维场景显示范围控制系数,系数越大,显示的范围越大
  78. //创建相机对象
  79. var camera = new THREE.OrthographicCamera(-s * k, s * k, s, -s, 0.01, 1000);
  80. camera.position.set(100, 100, 100); //设置相机位置
  81. camera.lookAt(scene.position); //设置相机方向(指向的场景对象)
  82. var renderer = new THREE.WebGLRenderer({
  83. antialias: true // 开启锯齿
  84. });
  85. renderer.setSize(width, height); //设置渲染区域尺寸
  86. // renderer.setClearColor(0xb9d3ff, 1); //设置背景颜色
  87. document.body.appendChild(renderer.domElement); //body元素中插入canvas对象
  88. renderer.domElement.style.position = 'absolute';
  89. renderer.domElement.style.top = '0px';
  90. renderer.domElement.style.zIndex = '-1'; // canvas全屏,不遮挡其它HTML元素
  91. // 执行渲染操作 指定场景、相机作为参数
  92. renderer.render(scene, camera);
  93. var pmremGenerator = new THREE.PMREMGenerator(renderer);
  94. pmremGenerator.compileEquirectangularShader();
  95. scene.environment = pmremGenerator.fromScene(new THREE.RoomEnvironment()).texture;
  96. // 创建一个CSS3渲染器CSS3DRenderer
  97. var CSS3Renderer = new THREE.CSS3DRenderer();
  98. CSS3Renderer.setSize(width, height);
  99. CSS3Renderer.domElement.style.position = 'absolute';
  100. // 相对标签原位置位置偏移大小
  101. CSS3Renderer.domElement.style.top = '0px';
  102. CSS3Renderer.domElement.style.left = '0px';
  103. // //设置.pointerEvents=none,以免模型标签HTML元素遮挡鼠标选择场景模型
  104. CSS3Renderer.domElement.style.pointerEvents = 'none';
  105. document.body.appendChild(CSS3Renderer.domElement);
  106. // 渲染函数
  107. function render() {
  108. CSS3Renderer.render(scene, camera); // 执行渲染操作
  109. renderer.render(scene, camera); // 执行渲染操作
  110. requestAnimationFrame(render); // 请求再次执行渲染函数render,渲染下一帧
  111. }
  112. render();
  113. const controls = new THREE.OrbitControls(camera, renderer.domElement);// 创建控件对象
  114. controls.enableZoom = false;
  115. console.log(controls);
  116. window.addEventListener('click', choose); // 监听窗口鼠标单击事件
  117. if(x){
  118. drawPointer(x,y,z);
  119. }
  120. function choose(event) {
  121. const Sx = event.clientX; // 鼠标单击位置横坐标
  122. const Sy = event.clientY; // 鼠标单击位置纵坐标
  123. // 屏幕坐标转WebGL标准设备坐标
  124. const x = (Sx / window.innerWidth) * 2 - 1; // WebGL标准设备横坐标
  125. const y = -(Sy / window.innerHeight) * 2 + 1; // WebGL标准设备纵坐标
  126. // 创建一个射线投射器`Raycaster`
  127. const raycaster = new THREE.Raycaster();
  128. // 通过鼠标单击位置标准设备坐标和相机参数计算射线投射器`Raycaster`的射线属性.ray
  129. raycaster.setFromCamera(new THREE.Vector2(x, y), camera);
  130. // 返回.intersectObjects()参数中射线选中的网格模型对象
  131. // 未选中对象返回空数组[],选中一个数组1个元素,选中两个数组两个元素
  132. const intersects = raycaster.intersectObjects(gltf.scene.children);
  133. console.log('射线器返回的对象', intersects);
  134. // console.log("射线投射器返回的对象 点point", intersects[0].point);
  135. // intersects.length大于0说明,说明选中了模型
  136. if (intersects.length > 0) {
  137. // 选中模型的第一个设置为半透明
  138. drawPointer(intersects[0].point.x,intersects[0].point.y,intersects[0].point.z);
  139. $('#x').val(intersects[0].point.x);
  140. $('#y').val(intersects[0].point.y);
  141. $('#z').val(intersects[0].point.z);
  142. }
  143. }
  144. function drawPointer(x,y,z) {
  145. let worldVector1 = new THREE.Vector3(x,y,z);
  146. const div = document.getElementById('label2');
  147. div.style.display = 'block';
  148. const obj = new THREE.CSS3DSprite(div);
  149. scene.add(obj);
  150. obj.position.copy(worldVector1);
  151. obj.scale.set(0.2, 0.2, 0.2); // 缩放CSS3DObject模型对象
  152. }
  153. // 显示坐标辅助线
  154. // const axes = new THREE.AxisHelper(200);
  155. // scene.add(axes);
  156. var loader = new THREE.GLTFLoader(); // 创建一个GLTF加载器
  157. loader.load('/lou.gltf', (gltfObj) => {
  158. console.log('控制台查看加载gltf文件返回的对象结构', gltfObj);
  159. gltf = gltfObj;
  160. scene.add(gltf.scene);
  161. scene.environment = pmremGenerator.fromScene(new THREE.RoomEnvironment(), 100).texture;
  162. gltf.scene.scale.set(0.13, 0.13, 0.13);// 整体缩放
  163. }, (xhr) => {
  164. // 控制台查看加载进度xhr
  165. // 通过加载进度xhr可以控制前端进度条进度 Math.floor:小数加载进度取整
  166. console.log(`加载进度${Math.floor(xhr.loaded / xhr.total * 100)}%`);
  167. });
  168. };
  169. </script>
  170. </body>
  171. </html>