当前位置:   article > 正文

three.js CSS2DRenderer、CSS2DObject渲染HTML标签_css2dobject threejs

css2dobject threejs

 有空的老铁关注一下我的抖音:

效果

  1. <template>
  2. <div>
  3. <el-container>
  4. <el-main>
  5. <div class="box-card-left">
  6. <div id="threejs" style="border: 1px solid red;position: relative;"></div>
  7. </div>
  8. </el-main>
  9. </el-container>
  10. </div>
  11. </template>
  12. <script>
  13. // 引入轨道控制器扩展库OrbitControls.js
  14. import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
  15. import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";
  16. // 效果制作器
  17. import { EffectComposer } from "three/examples/jsm/postprocessing/EffectComposer.js";
  18. // 渲染通道
  19. import { RenderPass } from "three/examples/jsm/postprocessing/RenderPass.js";
  20. // 发光描边OutlinePass
  21. import { OutlinePass } from "three/examples/jsm/postprocessing/OutlinePass.js";
  22. import { CSS2DObject, CSS2DRenderer } from "three/examples/jsm/renderers/CSS2DRenderer.js";
  23. export default {
  24. data() {
  25. return {
  26. name: "",
  27. scene: null,
  28. camera: null,
  29. renderer: null,
  30. css2DRenderer: null,
  31. effectComposer: null,
  32. mesh: null,
  33. geometry: null,
  34. group: null,
  35. material: null,
  36. texture: null,
  37. position: null,
  38. outlinePass: null,
  39. canvasWidth: 1000,
  40. canvasHeight: 800,
  41. color: [],
  42. meshArr: [],
  43. };
  44. },
  45. created() {},
  46. mounted() {
  47. this.name = this.$route.query.name;
  48. this.init();
  49. },
  50. methods: {
  51. goBack() {
  52. this.$router.go(-1);
  53. },
  54. init() {
  55. // 创建场景对象
  56. this.scene = new this.$three.Scene();
  57. // 调用方法创建盒模型 1
  58. this.careateBox1();
  59. // 调用方法创建盒模型 2
  60. this.careateBox2();
  61. this.createCone();
  62. // 创建辅助坐标轴对象
  63. const axesHelper = new this.$three.AxesHelper(200);
  64. this.scene.add(axesHelper);
  65. // 创建透视投影相机对象
  66. this.camera = new this.$three.PerspectiveCamera(60,1,0.01, 2000);
  67. // 设置相机位置
  68. this.camera.position.set(200,200,200);
  69. // 设置相机指向
  70. this.camera.lookAt(0,0,0);
  71. // 创建WebGL渲染器对象
  72. this.renderer = new this.$three.WebGLRenderer();
  73. // 设置渲染器渲染的尺寸
  74. this.renderer.setSize(1000,800);
  75. // 调用渲染器的render方法开始渲染,参数是 场景,相机 对象
  76. this.renderer.render(this.scene, this.camera);
  77. // 获取id 是 threejs 的元素并添加domElement
  78. window.document.getElementById("threejs").appendChild(this.renderer.domElement);
  79. // 创建 CSS2D 渲染器对象(主要:)
  80. this.css2DRenderer = new CSS2DRenderer();
  81. this.css2DRenderer.setSize(1000,800);
  82. this.css2DRenderer.render(this.scene, this.camera);
  83. this.css2DRenderer.domElement.style.position = "absolute";
  84. this.css2DRenderer.domElement.style.top = 0;
  85. this.css2DRenderer.domElement.style.pointerEvents = 'none'; // 取消标签的点击事件
  86. window.document.getElementById("threejs").appendChild(this.css2DRenderer.domElement);
  87. this.renderFun();
  88. // 创建空间轨道控制器对象
  89. const controls = new OrbitControls(this.camera, this.renderer.domElement);
  90. controls.addEventListener("change", () => {
  91. this.renderer.render(this.scene, this.camera);
  92. })
  93. },
  94. // 创建一个盒模型,父组件是 scene 场景;盒模型设置了位置
  95. careateBox1() {
  96. // 创建立方缓存几何体对象
  97. this.geometry = new this.$three.BoxGeometry(50,50,60);
  98. // 创建网格基础材质对象
  99. this.material = new this.$three.MeshBasicMaterial({
  100. color: 0xfafa,
  101. });
  102. // 创建网格对象
  103. this.mesh = new this.$three.Mesh(this.geometry, this.material);
  104. // 设置网格对象位置
  105. this.mesh.position.set(-50,60,60);
  106. // 调用方法创建html元素
  107. let dom = this.createDiv("父对象是Scene的模型的标签");
  108. // 创建 CSS2DObject对象,参数是html元素,作用:将html元素转为threejs的css2模型对象
  109. const tag = new CSS2DObject(dom);
  110. // 创建Vector3对象
  111. const wp = new this.$three.Vector3();
  112. // 获得this.mesh的世界坐标
  113. this.mesh.getWorldPosition(wp);
  114. tag.position.set(wp.x, wp.y, wp.z);
  115. // this.mesh.add(tag);
  116. this.scene.add(this.mesh);
  117. this.scene.add(tag);
  118. },
  119. // 创建一个盒模型,父组件是 组对象,盒模型与组对象都设置了位置
  120. careateBox2() {
  121. const group = new this.$three.Group();
  122. group.position.set(30,0,0);
  123. const geometry = new this.$three.BoxGeometry(50,50,60);
  124. const material = new this.$three.MeshBasicMaterial({
  125. color: 0xfaf33a,
  126. });
  127. const mesh = new this.$three.Mesh(geometry, material);
  128. // 网格模型设置了位置,且父对象是组对象,这样css2模型对象添加到mesh中后,会直接继承mesh的世界坐标
  129. mesh.position.set(50,60,-60);
  130. let dom = this.createDiv("父对象是Group的模型标签");
  131. const obj = new CSS2DObject(dom);
  132. mesh.add(obj);
  133. group.add(mesh);
  134. this.scene.add(group);
  135. },
  136. // 标注模型几何体的某个顶点
  137. createCone() {
  138. // 创建圆锥缓冲几何体对象
  139. const coneGeometry = new this.$three.ConeGeometry(20,100,32);
  140. // 创建材质对象
  141. const material = new this.$three.MeshBasicMaterial({
  142. color: 0xffaadd
  143. });
  144. // 创建网格模型对象
  145. const cone_mesh = new this.$three.Mesh(coneGeometry, material);
  146. const pos = coneGeometry.attributes.position;
  147. const dom = this.createDiv("我是圆锥的标注标签");
  148. const tag = new CSS2DObject(dom);
  149. cone_mesh.add(tag);
  150. cone_mesh.translateY(50); // 圆锥几何体Y轴平移50
  151. tag.position.y += 50; // css2模型对象
  152. this.scene.add(cone_mesh);
  153. },
  154. createDiv(innerText='盒子模型标签') {
  155. let dom = window.document.createElement("div");
  156. dom.innerHTML = innerText;
  157. dom.style.border = '1px solid blue';
  158. dom.style.height = '20px';
  159. dom.style.color = '#0A58F6';
  160. dom.style.padding = '5px 10px';
  161. dom.style.borderRadius = "5px";
  162. dom.style.top = '-20px';
  163. dom.style.background = "#F7F2EE";
  164. return dom;
  165. },
  166. renderFun() {
  167. this.renderer.render(this.scene, this.camera);
  168. this.css2DRenderer.render(this.scene, this.camera);
  169. window.requestAnimationFrame(this.renderFun);
  170. },
  171. },
  172. };
  173. </script>
  174. //
  175. <style lang="less" scoped>
  176. .box-card-left {
  177. display: flex;
  178. align-items: flex-start;
  179. flex-direction: row;
  180. position: relative;
  181. width: 100%;
  182. .box-right {
  183. img{
  184. width:500px;
  185. user-select: none;
  186. }
  187. }
  188. }
  189. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/135208
推荐阅读
相关标签
  

闽ICP备14008679号