当前位置:   article > 正文

three.js CSS3DRenderer、CSS3DObject渲染HTML标签_threejs css3dobject

threejs css3dobject

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

79e6b7730af347fa95f6f02198c7fd82.jpeg

效果

28595f4d6bd64da484cf3c336cd563e1.gif

  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. import { CSS3DObject, CSS3DSprite, CSS3DRenderer } from "three/examples/jsm/renderers/CSS3DRenderer.js";
  24. export default {
  25. data() {
  26. return {
  27. name: "",
  28. scene: null,
  29. camera: null,
  30. renderer: null,
  31. css3DRenderer: null,
  32. effectComposer: null,
  33. mesh: null,
  34. geometry: null,
  35. group: null,
  36. material: null,
  37. texture: null,
  38. position: null,
  39. outlinePass: null,
  40. canvasWidth: 1000,
  41. canvasHeight: 800,
  42. color: [],
  43. meshArr: [],
  44. };
  45. },
  46. created() {},
  47. mounted() {
  48. this.name = this.$route.query.name;
  49. this.init();
  50. },
  51. methods: {
  52. goBack() {
  53. this.$router.go(-1);
  54. },
  55. init() {
  56. // 创建场景对象
  57. this.scene = new this.$three.Scene();
  58. // 创建环境光对象
  59. const light = new this.$three.AmbientLight(0xffffff,20);
  60. this.scene.add(light);
  61. // 创建辅助坐标轴对象
  62. const axesHelper = new this.$three.AxesHelper(200);
  63. this.scene.add(axesHelper);
  64. // 创建gltf对象,加载模型文件
  65. const gltfLoader = new GLTFLoader();
  66. gltfLoader.load("/models/gltf/phone/iphone 12 mini/Model_formats/iphone12mini-3.glb", e => {
  67. e.scene.children.forEach(item => {
  68. if(item.type === "Object3D") {
  69. let div = this.createDom();
  70. div.innerHTML = item.name;
  71. const wp = new this.$three.Vector3();
  72. item.getWorldPosition(wp);
  73. let css3DObject = new CSS3DObject(div);
  74. css3DObject.scale.set(0.03,0.03,0.03);
  75. item.add(css3DObject);
  76. }
  77. })
  78. console.log( e.scene.children);
  79. this.scene.add(e.scene);
  80. this.renderFun();
  81. })
  82. // 创建相机对象
  83. this.camera = new this.$three.PerspectiveCamera(60,1,0.01,2000);
  84. this.camera.position.set(3,2,2);
  85. this.camera.lookAt(0,0,0);
  86. // 创建WebGL渲染器对象
  87. this.renderer = new this.$three.WebGLRenderer();
  88. // 设置渲染器尺寸
  89. this.renderer.setSize(1500,1000);
  90. this.renderer.render(this.scene, this.camera);
  91. window.document.getElementById("threejs").appendChild(this.renderer.domElement);
  92. this.css3DRenderer = new CSS3DRenderer();
  93. this.css3DRenderer.setSize(1500,1000);
  94. this.css3DRenderer.render(this.scene, this.camera);
  95. this.css3DRenderer.domElement.style.position = 'absolute';
  96. this.css3DRenderer.domElement.style.top = 0;
  97. this.css3DRenderer.domElement.style.pointerEvents = 'none';
  98. window.document.getElementById("threejs").appendChild(this.css3DRenderer.domElement);
  99. const controls = new OrbitControls(this.camera, this.renderer.domElement);
  100. controls.addEventListener("change", () => {
  101. this.renderer.render(this.scene, this.camera);
  102. });
  103. },
  104. createDom() {
  105. let dom = document.createElement("div");
  106. dom.style.padding = "5px 10px";
  107. dom.style.border = "1px solid skyblue";
  108. dom.style.borderRadius = '5px';
  109. dom.style.height = '20px';
  110. dom.style.pointerEvents = 'none';
  111. return dom;
  112. },
  113. renderFun() {
  114. this.renderer.render(this.scene, this.camera);
  115. this.css3DRenderer.render(this.scene, this.camera);
  116. requestAnimationFrame(this.renderFun); // 一定要设置这一句,否则,不渲染
  117. },
  118. },
  119. };
  120. </script>
  121. //
  122. <style lang="less" scoped>
  123. .box-card-left {
  124. display: flex;
  125. align-items: flex-start;
  126. flex-direction: row;
  127. position: relative;
  128. width: 100%;
  129. .box-right {
  130. img{
  131. width:500px;
  132. user-select: none;
  133. }
  134. }
  135. }
  136. </style>

 

 

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

闽ICP备14008679号