/
RaskolnickOFF
/
3D
Обзор
Документация
Войти
/
RaskolnickOFF
/
3D
Код
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
js/source/scene/Tree.js
60 строк
2 KB
RaskolnickOFF
Structure update
06 июн 2026, 03:18
06 июн 2026, 03:18
9bc1c25
Код
Авторство
О чём код?
// js/source/scene/Tree.js import * as THREE from 'three'; // Кэш материалов: ключ "цвет|roughness" → материал const _materialCache = new Map(); function getMaterial(color, roughness) { const key = `${color}|${roughness}`; if (!_materialCache.has(key)) { _materialCache.set(key, new THREE.MeshStandardMaterial({ color, roughness })); } return _materialCache.get(key); } /** * @param {string} type — ключ в treesConfig ("pine", "oak", ...) * @param {Object} treesConfig — весь объект settings.trees * @returns {THREE.Group | null} */ export function createTreeMesh(type, treesConfig) { const cfg = treesConfig?.[type]; if (!cfg) { console.warn(`createTreeMesh: тип дерева "${type}" не найден в treesConfig`); return null; } const trunkMaterial = getMaterial(cfg.trunkColor, cfg.trunkRoughness); const leavesMaterial = getMaterial(cfg.leavesColor, cfg.leavesRoughness); const group = new THREE.Group(); group.name = `tree_${type}`; // Ствол const trunkGeo = new THREE.CylinderGeometry( cfg.trunkRadius * 0.8, cfg.trunkRadius, cfg.trunkHeight, cfg.radialSegments ); const trunkMesh = new THREE.Mesh(trunkGeo, trunkMaterial); trunkMesh.name = 'trunk'; trunkMesh.position.y = cfg.trunkHeight / 2; trunkMesh.castShadow = true; trunkMesh.receiveShadow = true; group.add(trunkMesh); // Ярусы for (const tier of cfg.tiers) { const coneGeo = new THREE.CylinderGeometry(0, tier.radius, tier.height, cfg.radialSegments); const coneMesh = new THREE.Mesh(coneGeo, leavesMaterial); coneMesh.name = 'leaves'; coneMesh.position.y = tier.y; coneMesh.castShadow = true; coneMesh.receiveShadow = true; group.add(coneMesh); } return group; }