Добавил более гибкую настройку света, добавил префаб источника света. Готовый ассет темы конматы для лабы
This commit is contained in:
1 parent
7559740a28
commit
7de81f0147
52 files changed
+2464
-568
No files matched your search
@@ -1,4 +1,5 @@
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace _Project.Scripts.LevelGenerating
|
||||
{
|
||||
@@ -13,7 +14,7 @@ namespace _Project.Scripts.LevelGenerating
|
||||
void Generate(RoomView view, FloorData floor, RoomTheme theme);
|
||||
}
|
||||
|
||||
// Универсальный генератор: материалы из темы и сетка ламп под потолком.
|
||||
// Универсальный генератор: материалы из темы и сетка светильников под потолком.
|
||||
// Позже сюда добавится расстановка предметов по правилам темы.
|
||||
public class ThemedRoomGenerator : IRoomGenerator
|
||||
{
|
||||
@@ -31,43 +32,72 @@ namespace _Project.Scripts.LevelGenerating
|
||||
ApplyMaterial(view.WallRenderers, theme.wallMaterial);
|
||||
ApplyMaterial(view.CeilingRenderers, theme.ceilingMaterial);
|
||||
|
||||
if (theme.createLights) CreateLights(view, theme);
|
||||
if (theme.lightConfig != null)
|
||||
{
|
||||
CreateLights(view, theme.lightConfig);
|
||||
}
|
||||
}
|
||||
|
||||
private static void ApplyMaterial(System.Collections.Generic.List<Renderer> renderers, Material mat)
|
||||
private static void ApplyMaterial(List<Renderer> renderers, Material mat)
|
||||
{
|
||||
if (mat == null) return; // остаётся материал префаба
|
||||
foreach (var r in renderers) r.sharedMaterial = mat;
|
||||
}
|
||||
|
||||
// Сетка ламп: чем больше комната, тем больше ламп (шаг задаёт lightSpacing)
|
||||
private static void CreateLights(RoomView view, RoomTheme theme)
|
||||
// Сетка светильников: спавнит префаб из RoomLightConfig и применяет параметры
|
||||
private static void CreateLights(RoomView view, RoomLightConfig config)
|
||||
{
|
||||
Vector3 size = view.SizeMeters;
|
||||
Vector3 origin = view.OriginMeters;
|
||||
|
||||
int nx = Mathf.Max(1, Mathf.CeilToInt(size.x / theme.lightSpacing));
|
||||
int nz = Mathf.Max(1, Mathf.CeilToInt(size.z / theme.lightSpacing));
|
||||
float y = view.Height - theme.lightDrop;
|
||||
float spacing = Mathf.Max(0.5f, config.spacing);
|
||||
int nx = Mathf.Max(1, Mathf.CeilToInt(size.x / spacing));
|
||||
int nz = Mathf.Max(1, Mathf.CeilToInt(size.z / spacing));
|
||||
float y = view.Height - config.ceilingDrop;
|
||||
|
||||
var lightsRoot = new GameObject("Lights").transform;
|
||||
lightsRoot.SetParent(view.transform, false);
|
||||
|
||||
Quaternion rot = Quaternion.Euler(config.rotationOffset);
|
||||
|
||||
for (int ix = 0; ix < nx; ix++)
|
||||
{
|
||||
for (int iz = 0; iz < nz; iz++)
|
||||
{
|
||||
var go = new GameObject($"Light_{ix}_{iz}");
|
||||
go.transform.SetParent(lightsRoot, false);
|
||||
go.transform.position = origin + new Vector3(
|
||||
(ix + 0.5f) / nx * size.x, y, (iz + 0.5f) / nz * size.z);
|
||||
Vector3 pos = origin + new Vector3(
|
||||
(ix + 0.5f) / nx * size.x,
|
||||
y,
|
||||
(iz + 0.5f) / nz * size.z);
|
||||
|
||||
var light = go.AddComponent<Light>();
|
||||
light.type = LightType.Point;
|
||||
light.color = theme.lightColor;
|
||||
light.intensity = theme.lightIntensity;
|
||||
light.range = theme.lightRange;
|
||||
light.shadows = theme.lightShadows;
|
||||
GameObject lightGo;
|
||||
|
||||
if (config.lightPrefab != null)
|
||||
{
|
||||
lightGo = Object.Instantiate(config.lightPrefab, pos, rot, lightsRoot);
|
||||
lightGo.name = $"{config.lightPrefab.name}_{ix}_{iz}";
|
||||
}
|
||||
else
|
||||
{
|
||||
// Запасной вариант, если префаб забыли назначить в SO
|
||||
lightGo = new GameObject($"FallbackLight_{ix}_{iz}");
|
||||
lightGo.transform.SetParent(lightsRoot, false);
|
||||
lightGo.transform.SetPositionAndRotation(pos, rot);
|
||||
var fallbackLight = lightGo.AddComponent<Light>();
|
||||
fallbackLight.type = LightType.Point;
|
||||
}
|
||||
|
||||
// Если включена перезапись параметров (или сработал запасной вариант без префаба)
|
||||
if (config.overrideLightParameters || config.lightPrefab == null)
|
||||
{
|
||||
var lights = lightGo.GetComponentsInChildren<Light>(true);
|
||||
foreach (var l in lights)
|
||||
{
|
||||
l.color = config.color;
|
||||
l.intensity = config.intensity;
|
||||
l.range = config.range;
|
||||
l.shadows = config.shadows;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user