代码展示
import * as THREE from 'three';
import { onMounted } from "vue"
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
onMounted(() => {
const width = document.documentElement.clientWidth
const height = document.documentElement.clientHeight
const scene = new THREE.Scene()
const pointLight = new THREE.PointLight("#ffffff", 200, 200)
pointLight.position.set(10, 10, 10)
scene.add(pointLight)
const camera = new THREE.PerspectiveCamera(45, width / height, 1, 1000)
camera.position.z = 20
camera.position.y = 10
camera.position.x = 10
const loader = new GLTFLoader()
loader.load("/test2.glb", (gltf) => {
scene.add(gltf.scene)
})
const renderer = new THREE.WebGLRenderer({ antialias: true })
renderer.setSize(width, height)
renderer.setAnimationLoop(animate)
document.querySelector('#canvas').appendChild(renderer.domElement)
const controls = new OrbitControls(camera, renderer.domElement);
function animate(time) {
controls.update()
renderer.render(scene, camera);
}
})