lamelle as instance mesh

This commit is contained in:
Kseninia Mikhaylova 2024-06-28 11:30:00 +03:00
parent dcdd269368
commit a115e6e31b
2 changed files with 29 additions and 31 deletions

View File

@ -1,5 +1,5 @@
<script setup lang="ts">
import { BufferGeometry, InstancedBufferGeometry, Matrix4, MeshNormalMaterial } from 'three';
import { BufferGeometry, Matrix4, Vector3 } from 'three';
const props = defineProps(['index', 'models'])
@ -37,31 +37,33 @@ if (extra.value) {
pillar_two_pos.value = (extra.value as number) * 0.5 + pillar_size + bSize - 0.01
}
const instancedMesh = shallowRef();
watch(instancedMesh, (mesh) => {
console.log('rr')
for (let i = 0; i < 4; i++) {
instancedMesh.value.setMatrixAt(
i,
new Matrix4().makeTranslation(i, 1, 1)
);
}
// mesh.instanceMatrix.setUsage(DynamicDrawUsage);
});
onMounted(() => {
for (let i = 0; i < 4; i++) {
const elapsed = 1
const x = Math.sin(elapsed + i * 0.3) * 3.5;
const y = Math.cos(elapsed + i * 0.5) * 4;
const z = Math.cos(elapsed + i * 0.3) * 3.5;
if (instancedMesh.value) {
instancedMesh.value.setMatrixAt(
i,
new Matrix4().makeTranslation(x, y, z)
);
const instanced_lamelle = shallowRef();
const instanced_lamelle_geometry = Object.assign(new BufferGeometry, props.models.lamelle.children[0].geometry)
const instanced_lamelle_material = props.models.lamelle.children[0].material
const instanced_lamelle_count = 24
const instanced_v = [instanced_lamelle_geometry, instanced_lamelle_material, instanced_lamelle_count]
watch([instanced_lamelle, fence_section, extra_section, lamelles_count], (mesh) => {
for (let i = 0; i < instanced_lamelle_count; i++) {
if (instanced_lamelle.value) {
const scale_x = (((extra.value as number) || fence_section.value) * 10)
const pos_x = pillar_size * 0.5
const pos_y = (lSize * i)
const pos_z = -0.02
const newmatrix = new Matrix4().fromArray([
scale_x, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
pos_x, pos_y, pos_z, 1
])
instanced_lamelle.value.setMatrixAt(i, newmatrix);
if (i >= lamelles_count.value) {
instanced_lamelle.value.setMatrixAt(i, new Matrix4().makeTranslation(new Vector3(0,0,-2)));
}
}
instanced_lamelle.value.instanceMatrix.needsUpdate = true
}
})
});
watch([section_count, fence_section, extra_section], () => {
extra.value = (extra_section.value && props.index == (section_count.value + 1)) ? extra_section.value * 0.001 : false
if (extra_section && props.index == (section_count.value + 1)) {
@ -109,18 +111,14 @@ watch([section_count, fence_section, extra_section], () => {
</TresGroup>
<TresGroup name="lamelles">
<template v-for="(n, i) in lamelles_count">
<template v-for="(n, i) in lamelles_count" v-if=false>
<TresGroup :position="[pillar_size * 0.5, (lSize * i), 0.02]"
:scale-x="((extra as number) || fence_section) * 10">
<TresObject3D v-bind="props.models.lamelle.children[0]" />
</TresGroup>
</template>
<TresInstancedMesh ref="instancedMesh" :args="[props.models.lamelle.children[0].geometry, null!, 4]"
:scale-x="((extra as number) || fence_section) * 10" v-if=false>
<TresBufferGeometry v-bind="props.models.lamelle.children[0].geometry" />
<TresMeshBasicMaterial color="red" />
</TresInstancedMesh>
<TresInstancedMesh ref="instanced_lamelle" :args="instanced_v" cast-shadow />
</TresGroup>
<TresGroup name="lam_fastening_one">
<template v-for="(n, i) in lamelles_count">

View File

@ -2,7 +2,7 @@ import { Color, MeshStandardMaterial } from "three"
const set_metaril_func = (scene: any, material: any) => {
scene.children.forEach((el: any) => {
if (el.isMesh) {
if (el.isMesh && !el.isInstancedMesh) {
el.castShadow = true
el.receiveShadow = true
}