Loading.vue 2.1 KB
Newer Older
yma16's avatar
yma16 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
<template>
  <div id="loading">
    <canvas id="fbxload" style="width: 100%; height: 100%"> </canvas>
  </div>
</template>

<script>
import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
// import { FBXLoader } from 'three/examples/jsm/loaders/FBXLoader'
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'
// threejs引入
export default {
  name: 'Loading',
  data () {
    return {
      msg: 'fbxloading'
    }
  },
  methods: {
    loadingFbx: function () {
      const scene = new THREE.Scene()
      const width = window.innerWidth
      const height = window.innerHeight
      const camera = new THREE.PerspectiveCamera(45, width / height, 1, 100)
      const canvas = document.querySelector('#fbxload') // 根据canvas的id获取dom节点
      console.log(canvas)
      const renderer = new THREE.WebGLRenderer({ canvas, antialias: true }) // 渲染场景
      const controls = new OrbitControls(camera, renderer.domElement)
      controls.enableDamping = true
      controls.update()
      camera.position.z = 5
      //   const geometry = new THREE.BoxGeometry();
      //   const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
      //   const cube = new THREE.Mesh(geometry, material);
      const light = new THREE.AmbientLight(0x0000ff) // 蓝色环境环境光
      scene.add(light)
      //   scene.add(cube);
      const gltfLoader = new GLTFLoader()
      gltfLoader.load(
        '/static/utils/fx_background/model/glb/snow.glb',
        function (gltf) {
          console.log(gltf)
          scene.add(gltf.scene)
        },
        undefined,
        function (error) {
          console.error(error)
        }
      )

      function animate () {
        // 渲染
        renderer.render(scene, camera)
        requestAnimationFrame(animate)
      }
      animate()
    }
  },
  created () {
    window.onload = this.loadingFbx // dom加载完再执行
  }
}
</script>

<style scoped>
#loading {
  width: 100%;
  height: 100%;
  line-height: 1em;
}
#fbxload {
  width: 100%;
  height: 100%;
}
</style>