Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
},
"pnpm": {
"onlyBuiltDependencies": [
"better-sqlite3"
"better-sqlite3",
"nx"
],
"overrides": {
"ipx": "3.0.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { useTres } from '@tresjs/core'
import { ref, watch, watchEffect } from 'vue'
import type { InstancedMesh, Mesh } from 'three'
import { useSurfaceSampler } from '.'
import type { useSurfaceSamplerProps } from '.'
import type { SurfaceSamplerOptions } from '.'

const props = defineProps<useSurfaceSamplerProps>()
const props = defineProps<SurfaceSamplerOptions>()

const samplerRef = ref()
const instancedRef = ref()
Expand Down
36 changes: 16 additions & 20 deletions packages/cientos/src/core/abstractions/useSurfaceSampler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import {
Vector3,
} from 'three'
import { MeshSurfaceSampler } from 'three-stdlib'
import { ref } from 'vue'
import { isReactive, reactive, ref, toRefs } from 'vue'
import type { InstancedMesh, Mesh, Object3DEventMap } from 'three'
import type { TresObject } from '@tresjs/core'

export interface useSurfaceSamplerProps {
export interface SurfaceSamplerOptions {
/*
* A function that can be .
*
Expand Down Expand Up @@ -79,23 +79,19 @@ type TransformPayload = SamplePayload & {

export type TransformFn = (payload: TransformPayload, i: number) => void

export const useSurfaceSampler = (
mesh: Mesh,
count: number = 16,
instanceMesh?: InstancedMesh | null,
weight?: string,
transform?: TransformFn,
) => {
const arr = new Float32Array(count * 16)
export function useSurfaceSampler(options: SurfaceSamplerOptions) {
const { mesh, count, instanceMesh, weight, transform } = isReactive(options) ? toRefs(options) : toRefs(reactive(options))
const counter = ref(count?.value || 16)
const arr = new Float32Array(counter.value * 16)
const buffer = ref(new InterleavedBuffer(arr, 16))

const updateBuffer = () => {
if (!mesh) { return }
if (!mesh?.value) { return }

const sampler = new MeshSurfaceSampler(mesh)
const sampler = new MeshSurfaceSampler(mesh.value)

if (weight) {
sampler.setWeightAttribute(weight)
if (weight?.value) {
sampler.setWeightAttribute(weight.value)
}
sampler.build()

Expand All @@ -104,9 +100,9 @@ export const useSurfaceSampler = (
const color = new Color()
const dummy = new Object3D<Object3DEventMap>() as TresObject

mesh.updateMatrixWorld(true)
mesh.value.updateMatrixWorld(true)

for (let i = 0; i < count; i++) {
for (let i = 0; i < counter.value; i++) {
sampler.sample(position, normal, color)

if (typeof transform === 'function') {
Expand All @@ -126,13 +122,13 @@ export const useSurfaceSampler = (
}
dummy.updateMatrix()

if (instanceMesh) {
instanceMesh.setMatrixAt(i, dummy.matrix)
if (instanceMesh?.value) {
instanceMesh.value.setMatrixAt(i, dummy.matrix)
}
dummy.matrix.toArray(buffer.value.array, i * 16)
}
if (instanceMesh) {
instanceMesh.instanceMatrix.needsUpdate = true
if (instanceMesh?.value) {
instanceMesh.value.instanceMatrix.needsUpdate = true
}

buffer.value.needsUpdate = true
Expand Down
Loading