|
| 1 | +import { Vector3 } from '@babylonjs/core/Maths/math.vector' |
| 2 | +import { VertexData } from '@babylonjs/core/Meshes/mesh.vertexData' |
| 3 | +// this import is just so it works with debug builds. you should import from '@babylonjs/*` directly as above |
| 4 | +import { Mesh } from 'react-babylonjs/node_modules/@babylonjs/core/Meshes/mesh' |
| 5 | + |
| 6 | +import React, { FC, useState } from 'react' |
| 7 | +import { Engine, Scene, useScene } from 'react-babylonjs' |
| 8 | + |
| 9 | +type CustomMeshType = { |
| 10 | + name: string |
| 11 | + position: Vector3 |
| 12 | + useWireframe: boolean |
| 13 | +} |
| 14 | + |
| 15 | +const CustomMesh: FC<CustomMeshType> = (props) => { |
| 16 | + const scene = useScene() |
| 17 | + |
| 18 | + const [customMesh] = useState(() => { |
| 19 | + const meshInstance = new Mesh(props.name, scene!) |
| 20 | + |
| 21 | + //Set arrays for positions and indices |
| 22 | + var positions = [-5, 2, -3, -7, -2, -3, -3, -2, -3, 5, 2, 3, 7, -2, 3, 3, -2, 3] |
| 23 | + var indices = [0, 1, 2, 3, 4, 5] |
| 24 | + var colors = [1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1] |
| 25 | + |
| 26 | + //Empty array to contain calculated values |
| 27 | + var normals: number[] = [] |
| 28 | + |
| 29 | + var vertexData = new VertexData() |
| 30 | + VertexData.ComputeNormals(positions, indices, normals) |
| 31 | + |
| 32 | + //Assign positions, indices and normals to vertexData |
| 33 | + vertexData.positions = positions |
| 34 | + vertexData.indices = indices |
| 35 | + vertexData.normals = normals |
| 36 | + vertexData.colors = colors |
| 37 | + |
| 38 | + //Apply vertexData to custom mesh |
| 39 | + vertexData.applyToMesh(meshInstance) |
| 40 | + |
| 41 | + return meshInstance |
| 42 | + }) |
| 43 | + |
| 44 | + return ( |
| 45 | + <mesh name="mesh" fromInstance={customMesh} disposeInstanceOnUnmount position={props.position}> |
| 46 | + <standardMaterial name={`${props.name}-mat`} wireframe={props.useWireframe} /> |
| 47 | + </mesh> |
| 48 | + ) |
| 49 | +} |
| 50 | + |
| 51 | +const CustomMeshes = () => { |
| 52 | + const [displayLast, setDisplayLast] = useState(true) |
| 53 | + const toggleDisplay = () => { |
| 54 | + setDisplayLast((cur) => !cur) |
| 55 | + } |
| 56 | + return ( |
| 57 | + <> |
| 58 | + <div> |
| 59 | + <button className="btn btn-primary m-2" onClick={toggleDisplay}> |
| 60 | + Toggle Top Triangle Visibility |
| 61 | + </button> |
| 62 | + </div> |
| 63 | + <div style={{ flex: 1, display: 'flex' }}> |
| 64 | + <Engine antialias adaptToDeviceRatio canvasId="babylon-js"> |
| 65 | + <Scene> |
| 66 | + <arcRotateCamera |
| 67 | + name="camera1" |
| 68 | + target={Vector3.Zero()} |
| 69 | + alpha={Math.PI / 2} |
| 70 | + beta={Math.PI / 2} |
| 71 | + radius={20} |
| 72 | + /> |
| 73 | + <hemisphericLight name="light1" intensity={0.7} direction={Vector3.Up()} /> |
| 74 | + <CustomMesh name="custom-0" position={new Vector3(0, 0, 0)} useWireframe={false} /> |
| 75 | + <CustomMesh name="custom-2" position={new Vector3(0, 2, 0)} useWireframe={true} /> |
| 76 | + {displayLast && ( |
| 77 | + <CustomMesh name="custom-4" position={new Vector3(0, 4, 0)} useWireframe={false} /> |
| 78 | + )} |
| 79 | + </Scene> |
| 80 | + </Engine> |
| 81 | + </div> |
| 82 | + </> |
| 83 | + ) |
| 84 | +} |
| 85 | + |
| 86 | +export default CustomMeshes |
0 commit comments