// 引入Three.js库
import * as THREE from 'three';
// 创建场景
const scene = new THREE.Scene();
// 创建相机
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 5;
// 创建渲染器
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// 定义线的起点和终点
const points = [];
points.push(new THREE.Vector3(-1, 0, 0));
points.push(new THREE.Vector3(0, 1, 0));
points.push(new THREE.Vector3(1, 0, 0));
// 创建几何体
const geometry = new THREE.BufferGeometry().setFromPoints(points);
// 创建材质
const material = new THREE.LineBasicMaterial({ color: 0x0000ff });
// 创建线条对象
const line = new THREE.Line(geometry, material);
// 将线条添加到场景中
scene.add(line);
// 渲染循环
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
animate();
import * as THREE from 'three';
引入Three.js库。THREE.Scene()
创建一个场景对象。THREE.PerspectiveCamera()
创建一个透视相机,并设置其位置。THREE.WebGLRenderer()
创建一个WebGL渲染器,并将其大小设置为窗口大小,然后将渲染器的DOM元素添加到页面中。points
,并添加三个点作为线的顶点。THREE.BufferGeometry().setFromPoints(points)
创建几何体。THREE.LineBasicMaterial()
创建线的材质,并设置颜色。THREE.Line()
创建线条对象,并传入几何体和材质。scene.add(line)
将线条添加到场景中。animate
函数,使用requestAnimationFrame
实现动画效果,并调用renderer.render(scene, camera)
进行渲染。上一篇:js 设置style
下一篇:js 唯一id
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站