• threeJS嵌入可交互的普通页面


    • 效果图:

            1.这是我将一个地图当作地面,外面再包一个天空盒就更好看了

             2.上面的例子可能不够直观,下面这个例子是嵌入的bilibili官网,嵌入的网页内容可以正常交互

    • 关键
      关键是用到了CSS3DRenderer渲染器。CSS3DRenderer仅仅关注普通的DOM元素,这些元素被包含到了特殊的对象中(CSS3DObject或者CSS3DSprite),然后被加入到场景图中。
    • 代码(是第一个地图的例子哦)
      1. <template>
      2. <div class="webgl-container">
      3. <div id="webglDom"
      4. ref="webglDom">div>
      5. div>
      6. template>
      7. <script>
      8. import * as THREE from 'three'
      9. // import { TrackballControls } from 'three/examples/jsm/controls/TrackballControls.js'
      10. import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'
      11. import { CSS3DRenderer, CSS3DObject } from 'three/examples/jsm/renderers/CSS3DRenderer.js'
      12. export default {
      13. name: 'threeJSMap',
      14. data () {
      15. return {
      16. controls: null,
      17. camera: null,
      18. glScene: null,
      19. cssScene: null,
      20. cssRenderer: null
      21. }
      22. },
      23. mounted () {
      24. this.initialize()
      25. },
      26. methods: {
      27. createCssRenderer () {
      28. const cssRenderer = new CSS3DRenderer()
      29. cssRenderer.setSize(window.innerWidth, window.innerHeight)
      30. cssRenderer.domElement.style.position = 'absolute'
      31. cssRenderer.domElement.style.top = 0
      32. return cssRenderer
      33. },
      34. createCssObject (w, h, position, rotation, url) {
      35. const iframe = document.createElement('iframe')
      36. iframe.src = 'http://localhost:8081/demo-web/#/map' // 写你自己要嵌入的网页地址即可
      37. iframe.style.width = w + 'px'
      38. iframe.style.height = h + 'px'
      39. iframe.style.border = '0px'
      40. const cssObject = new CSS3DObject(iframe)
      41. cssObject.position.x = position.x
      42. cssObject.position.y = position.y
      43. cssObject.position.z = position.z
      44. cssObject.rotation.x = rotation.x
      45. cssObject.rotation.y = rotation.y
      46. cssObject.rotation.z = rotation.z
      47. return cssObject
      48. },
      49. create3dPage (w, h, position, rotation, url) {
      50. const cssObject = this.createCssObject(
      51. w, h,
      52. position,
      53. rotation,
      54. url)
      55. this.cssScene.add(cssObject)
      56. },
      57. initialize () {
      58. this.camera = new THREE.PerspectiveCamera(
      59. 45,
      60. window.innerWidth / window.innerHeight,
      61. 1,
      62. 10000)
      63. this.camera.position.set(0, 30, 100)
      64. this.cssRenderer = this.createCssRenderer()
      65. document.getElementById('webglDom').appendChild(this.cssRenderer.domElement)
      66. this.controls = new OrbitControls(this.camera, this.cssRenderer.domElement)
      67. // 定义当平移的时候摄像机的位置将如何移动。如果为true,摄像机将在屏幕空间内平移。 否则,摄像机将在与摄像机向上方向垂直的平面中平移。
      68. this.controls.screenSpacePanning = true
      69. this.controls.minDistance = 10
      70. this.controls.maxDistance = 500
      71. this.controls.maxPolarAngle = Math.PI / 2
      72. this.glScene = new THREE.Scene()
      73. this.cssScene = new THREE.Scene()
      74. const ambientLight = new THREE.AmbientLight(0x555555)
      75. this.glScene.add(ambientLight)
      76. const directionalLight = new THREE.DirectionalLight(0xffffff)
      77. directionalLight.position.set(-0.5, 0.5, -1.5).normalize()
      78. this.glScene.add(directionalLight)
      79. this.create3dPage(
      80. 1000, 1000,
      81. new THREE.Vector3(0, 0, 0),
      82. new THREE.Vector3(-90 * Math.PI / 180, 0, 0),
      83. 'http://localhost:8081/demo-web/#/map')
      84. this.update()
      85. },
      86. update () {
      87. this.controls.update()
      88. this.cssRenderer.render(this.cssScene, this.camera)
      89. requestAnimationFrame(this.update)
      90. }
      91. }
      92. }
      93. script>
      94. <style scoped>
      95. #webglDom,
      96. .webgl-container {
      97. width: 100%;
      98. height: 100%;
      99. overflow: hidden;
      100. }
      101. style>

  • 相关阅读:
    Oracle数据库连接之TNS-03505_无法解析服务名异常
    gradle-7.6.3-all 快速下载百度网盘下载
    java基于ssm的在线IT项目任务管理系统
    【Linux】yum 报错ModuleNotFoundError: No module named ‘dnf‘
    【Java复健指南09】项目练习全解--房屋出租系统
    C++封装着色器
    MySql 查询优化
    大数据Flink(八十九):Temporal Join(快照 Join)
    设计行业中如何保证图纸设计稿在数据传输中不会泄密
    RabbitMq,jf,企业微信 或 钉钉
  • 原文地址:https://blog.csdn.net/weixin_42077503/article/details/127922758