import React from 'react'; import { BufferAttribute, BufferGeometry, Camera, LineBasicMaterial, Mesh, PerspectiveCamera, Scene, WebGLRenderer } from "three"; interface Props {} interface State {} class Index extends React.Component { render() { return (
) } componentDidMount(): void { this.drawRectangle(); } drawRectangle() { const geometry = new BufferGeometry(); // 顶点位置 const vertices = new Float32Array([ 0, 0, 0, //顶点1坐标 80, 0, 0, //顶点2坐标 80, 80, 0, //顶点3坐标 0, 80, 0, //顶点4坐标 ]); geometry.attributes.position = new BufferAttribute(vertices, 3); // 指定三个值构成一个点 // 顶点法向量 const normals = new Float32Array([ 0, 0, 1, //顶点1法向量 0, 0, 1, //顶点2法向量 0, 0, 1, //顶点3法向量 0, 0, 1, //顶点4法向量 ]); geometry.attributes.normal = new BufferAttribute(normals, 3); // 顶点索引 const indexes = new Uint16Array([ 0, 1, 2, // 注意顶点的顺序,必需逆时针。 0, 2, 3, ]); geometry.index = new BufferAttribute(indexes, 1); const material = new LineBasicMaterial({ // vertexColors: VertexColors, color: 0xff00ff, }); const mesh = new Mesh(geometry, material); const scene = new Scene(); scene.add(mesh); const camera: Camera = new PerspectiveCamera(30, 1, 0.1, 2000); camera.position.x = 40; camera.position.z = 400; camera.position.y = 0; // 将canvas元素添加到dom中 const wrapper: HTMLElement = document.querySelector('.canvas-wrapper'); const renderer: WebGLRenderer = new WebGLRenderer(); renderer.setSize(wrapper.offsetWidth, wrapper.offsetHeight); renderer.render(scene, camera); wrapper.appendChild(renderer.domElement); } } export default Index;