Three.js中文网
<template>
<div id="webgl"></div>
</template>
<script setup>
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
const scene = new THREE.Scene();
const geometry = new THREE.BoxGeometry(100, 60, 20);
const material = new THREE.MeshLambertMaterial({
color: 0xff0000,
transparent: true,
opacity: 0.8,
});
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(80, 0, 0);
scene.add(mesh);
const axesHelper = new THREE.AxesHelper(250);
scene.add(axesHelper);
const pointLight = new THREE.PointLight(0xffffff, 1.0);
pointLight.position.set(80, 50, 50);
pointLight.intensity = 10000.0;
scene.add(pointLight);
const pointLightHelper = new THREE.PointLightHelper(pointLight, 10);
scene.add(pointLightHelper);
const camera = new THREE.PerspectiveCamera();
camera.position.set(200, 200, 200);
camera.lookAt(0, 0, 0);
const renderer = new THREE.WebGLRenderer();
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setSize(width, height);
renderer.render(scene, camera);
document.body.appendChild(renderer.domElement);
const clock = new THREE.Clock();
function render() {
const spt = clock.getDelta() * 1000;
console.log('两帧渲染时间间隔(毫秒)', spt);
console.log('帧率FPS', 1000 / spt);
renderer.render(scene, camera);
mesh.rotateY(0.01);
requestAnimationFrame(render);
}
render();
const controls = new OrbitControls(camera, renderer.domElement);
controls.addEventListener('change', function () {
renderer.render(scene, camera);
console.log('camera.position', camera.position);
});
window.onresize = function () {
renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
};
</script>