49 lines
1.7 KiB
TypeScript
49 lines
1.7 KiB
TypeScript
import { Color, DoubleSide, MeshBasicMaterial, MeshStandardMaterial, RepeatWrapping, TextureLoader, Vector2 } from "three"
|
|
import { useLoader, type TresLoader } from '@tresjs/core'
|
|
import { getFilename, type patternTypes } from "~/components/pattern"
|
|
|
|
const set_metaril_func = (scene: any, material: any) => {
|
|
scene.children.forEach((el: any) => {
|
|
if (el.isMesh && !el.isInstancedMesh) {
|
|
el.castShadow = true
|
|
el.receiveShadow = true
|
|
}
|
|
if (el.material) el.material = material
|
|
set_metaril_func(el, material)
|
|
})
|
|
}
|
|
// const texture = await useLoader(TextureLoader, '/fence_one/svg_tile1.svg');
|
|
// const get_texture = async () => {
|
|
// const texture = await useLoader(TextureLoader, '/fence_one/tile1.png');
|
|
// return texture
|
|
// }
|
|
// const texture = await get_texture()
|
|
// texture.wrapT = RepeatWrapping;
|
|
|
|
export const set_material = (scene: any, color: any, pattern: { pattern: patternTypes, count: number } | undefined = undefined) => {
|
|
let c = color
|
|
|
|
const material = new MeshStandardMaterial({
|
|
color: new Color(c || '#9c9c00'),
|
|
// alphaMap: pattern ? texture : null,
|
|
transparent: true,
|
|
opacity: 1,
|
|
roughness: 0.2,
|
|
metalness: 0,
|
|
side: DoubleSide,
|
|
})
|
|
|
|
if (pattern && pattern.pattern !== undefined) {
|
|
const filename = getFilename(pattern.pattern)
|
|
const texture = useLoader(TextureLoader, filename)
|
|
texture.then(res => {
|
|
res.wrapT = RepeatWrapping;
|
|
res.repeat.set(1, pattern.count);
|
|
res.needsUpdate = true
|
|
// return res
|
|
material.alphaMap = res
|
|
})
|
|
}
|
|
|
|
set_metaril_func(scene, material)
|
|
} |