import React from 'react'; import { BufferAttribute, BufferGeometry, Camera, Line, LineBasicMaterial, Mesh, PerspectiveCamera, Points, PointsMaterial, Scene, WebGLRenderer } from "three"; interface Props {} interface State {} class Infos extends React.Component { render() { return (
) } componentDidMount(): void { this.draw(); } private draw() { const scene = new Scene(); const geometry = new BufferGeometry(); // 几何体顶点位置 const vertices = new Float32Array([ 0, 0, 0, //顶点1坐标 50, 0, 0, //顶点2坐标 0, 100, 0, //顶点3坐标 0, 0, 10, //顶点4坐标 0, 0, 100, //顶点5坐标 50, 0, 10, //顶点6坐标 ]); // 指定三个值构成一个点 geometry.attributes.position = new BufferAttribute(vertices, 3); // 几何体顶点颜色 const colors = new Float32Array([ 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, ]); geometry.attributes.color = new BufferAttribute(colors, 3); // 几何体顶点法向量 const normal = new Float32Array([ 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0 ]); geometry.attributes.normal = new BufferAttribute(normal, 3); const mesh = this.createMesh(geometry); scene.add(mesh); const camera: Camera = new PerspectiveCamera(30, 1, 0.1, 2000); camera.position.x = 100; camera.position.z = 500; camera.position.y = 100; // 将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); } private createMesh(geometry) { const material = new LineBasicMaterial({ // vertexColors: VertexColors, color: 0x0000ff, }); return new Mesh(geometry, material); } } export default Infos;