|
1 | 1 | <script lang="ts"> |
2 | 2 | import { T } from '@threlte/core'; |
3 | 3 | import { OrbitControls } from '@threlte/extras'; |
4 | | - import { onMount } from 'svelte'; |
5 | | - import type { BufferGeometry, NormalOrGLBufferAttributes } from 'three'; |
| 4 | + import { BufferAttribute, type BufferGeometry, type NormalOrGLBufferAttributes } from 'three'; |
6 | 5 |
|
7 | | - export let vertices: Float32Array; |
8 | | - export let volume: number; |
9 | | - export let wireframe: boolean; |
10 | | - export let color: string = '#118811'; |
11 | | - export let positionOffset: number = 0; |
12 | | - export let opacity: number = 1; |
13 | | - export let flipZ: boolean = false; |
| 6 | + interface Properties { |
| 7 | + vertices: Float32Array; |
| 8 | + volume: number; |
| 9 | + wireframe: boolean; |
| 10 | + color?: string; |
| 11 | + positionOffset?: number; |
| 12 | + opacity?: number; |
| 13 | + flipZ?: boolean; |
| 14 | + } |
| 15 | +
|
| 16 | + const { |
| 17 | + vertices, |
| 18 | + volume, |
| 19 | + wireframe, |
| 20 | + color = '#118811', |
| 21 | + positionOffset = 0, |
| 22 | + opacity = 1, |
| 23 | + flipZ = false |
| 24 | + }: Properties = $props(); |
14 | 25 |
|
15 | 26 | const CAMERA_FAR = 2; |
16 | 27 |
|
17 | | - let bufferGeometry: BufferGeometry<NormalOrGLBufferAttributes>; |
18 | | - onMount(() => { |
19 | | - if (bufferGeometry) bufferGeometry.computeVertexNormals(); |
20 | | - }); |
| 28 | + const setupGeometry = (geo: BufferGeometry<NormalOrGLBufferAttributes>) => { |
| 29 | + geo.setAttribute('position', new BufferAttribute(vertices, 3)); |
| 30 | + geo.computeVertexNormals(); |
| 31 | + }; |
21 | 32 | </script> |
22 | 33 |
|
23 | 34 | <T.PerspectiveCamera |
|
31 | 42 | <T.AmbientLight color="white" intensity={1} /> |
32 | 43 |
|
33 | 44 | <T.Mesh position.y={positionOffset} rotation.x={-Math.PI / 2} scale.z={flipZ ? -1 : 1}> |
34 | | - <T.BufferGeometry bind:ref={bufferGeometry}> |
35 | | - <T.BufferAttribute args={[vertices, 3]} attach="attributes.position" /> |
36 | | - </T.BufferGeometry> |
| 45 | + <T.BufferGeometry oncreate={setupGeometry} /> |
37 | 46 | <T.MeshPhongMaterial {color} {opacity} transparent={opacity < 1} {wireframe} /> |
38 | 47 | </T.Mesh> |
0 commit comments