From 5cb7d19f3f9f0f93669a38773e43310fb8ffdae8 Mon Sep 17 00:00:00 2001 From: Kseninia Mikhaylova Date: Thu, 26 Sep 2024 15:13:21 +0300 Subject: [PATCH] uv --- utils/material.ts | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/utils/material.ts b/utils/material.ts index b36cba9..a537978 100644 --- a/utils/material.ts +++ b/utils/material.ts @@ -15,27 +15,26 @@ const set_metaril_func = (scene: any, material: any) => { const uvAttribute = el.geometry.getAttribute('uv'); const uvs = []; - for (let i = 0; i < uvAttribute.count; i++) { - uvs.push(uvAttribute.getX(i), uvAttribute.getY(i)); + if (uvAttribute) { + for (let i = 0; i < uvAttribute.count; i++) { + uvs.push(uvAttribute.getX(i), uvAttribute.getY(i)); + } + + // Вычисление минимальных и максимальных UV + const minU = Math.min(...uvs.filter((_, index) => index % 2 === 0)); // X координаты + const maxU = Math.max(...uvs.filter((_, index) => index % 2 === 0)); + const minV = Math.min(...uvs.filter((_, index) => index % 2 === 1)); // Y координаты + const maxV = Math.max(...uvs.filter((_, index) => index % 2 === 1)); + + // Определите размеры текстуры на поверхности + const surfaceWidth = maxU - minU; + const surfaceHeight = (maxV - minV) * el.scale.y; + + material.normalMap.wrapS = RepeatWrapping; + material.normalMap.wrapT = RepeatWrapping; + material.normalMap.repeat.set(surfaceWidth, surfaceHeight); + material.normalMap.needsUpdate = true } - - // Вычисление минимальных и максимальных UV - const minU = Math.min(...uvs.filter((_, index) => index % 2 === 0)); // X координаты - const maxU = Math.max(...uvs.filter((_, index) => index % 2 === 0)); - const minV = Math.min(...uvs.filter((_, index) => index % 2 === 1)); // Y координаты - const maxV = Math.max(...uvs.filter((_, index) => index % 2 === 1)); - - // Определите размеры текстуры на поверхности - const surfaceWidth = maxU - minU; - const surfaceHeight = (maxV - minV) * el.scale.y; - - material.normalMap.wrapS = RepeatWrapping; - material.normalMap.wrapT = RepeatWrapping; - material.normalMap.repeat.set(surfaceWidth, surfaceHeight); - material.normalMap.needsUpdate = true - - console.log(`Ширина текстуры на поверхности: ${surfaceWidth * 128}`); - console.log(`Высота текстуры на поверхности: ${surfaceHeight * 128}`); } if (el.material) el.material = material