First Commit
This commit is contained in:
commit
eed6b79efc
2836 files changed
+948481
No files matched your search
@@ -0,0 +1,229 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// Справочник данных деталей, канистр и материалов (Resources/Perekup/PartCatalog).
|
||||
/// Создается автонастройкой «Arcade Simulator → Auto-Setup». Если ассета нет — используются
|
||||
/// безопасные процедурные значения по умолчанию, чтобы игра никогда не падала с NullReference.
|
||||
/// </summary>
|
||||
[CreateAssetMenu(fileName = "PartCatalog", menuName = "Car System/Part Catalog")]
|
||||
public class PartCatalog : ScriptableObject
|
||||
{
|
||||
[Header("Подкапотные агрегаты")]
|
||||
public CarPartData battery;
|
||||
public CarPartData sparkPlugs;
|
||||
public CarPartData airFilter;
|
||||
public CarPartData oilFilter;
|
||||
public CarPartData radiator;
|
||||
public CarPartData alternator;
|
||||
public CarPartData fuelFilter;
|
||||
|
||||
[Header("Кузов и ходовая")]
|
||||
public CarPartData wheel;
|
||||
public CarPartData hood;
|
||||
public CarPartData door;
|
||||
public CarPartData trunk;
|
||||
public CarPartData fender;
|
||||
public CarPartData bumperFront;
|
||||
public CarPartData bumperBack;
|
||||
|
||||
[Header("Канистры технических жидкостей")]
|
||||
public GameObject canisterEngineOil;
|
||||
public GameObject canisterCoolant;
|
||||
public GameObject canisterBrakeFluid;
|
||||
public GameObject canisterWasherFluid;
|
||||
|
||||
[Header("Материалы")]
|
||||
public Material ghost;
|
||||
public Material carPaint;
|
||||
public List<NamedMaterial> materials = new List<NamedMaterial>();
|
||||
|
||||
[Serializable]
|
||||
public class NamedMaterial
|
||||
{
|
||||
public string key;
|
||||
public Material material;
|
||||
}
|
||||
|
||||
private static PartCatalog cached;
|
||||
private static readonly Dictionary<string, Material> runtimeMaterials = new Dictionary<string, Material>();
|
||||
|
||||
public static PartCatalog Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (cached != null) return cached;
|
||||
cached = Resources.Load<PartCatalog>("Perekup/PartCatalog");
|
||||
if (cached == null)
|
||||
{
|
||||
cached = CreateInstance<PartCatalog>();
|
||||
cached.hideFlags = HideFlags.DontSave;
|
||||
}
|
||||
return cached;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Сброс кэша после создания/изменения ассета каталога автонастройкой.</summary>
|
||||
public static void ResetCache()
|
||||
{
|
||||
cached = null;
|
||||
particleMaterial = null;
|
||||
dirtOverlay = null;
|
||||
runtimeMaterials.Clear();
|
||||
}
|
||||
|
||||
public CarPartData GetEngineData(EnginePartType type)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case EnginePartType.Battery: return battery;
|
||||
case EnginePartType.SparkPlugs: return sparkPlugs;
|
||||
case EnginePartType.AirFilter: return airFilter;
|
||||
case EnginePartType.OilFilter: return oilFilter;
|
||||
case EnginePartType.Radiator: return radiator;
|
||||
case EnginePartType.Alternator: return alternator;
|
||||
case EnginePartType.FuelFilter: return fuelFilter;
|
||||
default: return null;
|
||||
}
|
||||
}
|
||||
|
||||
public GameObject GetCanister(FluidType type)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case FluidType.EngineOil: return canisterEngineOil;
|
||||
case FluidType.Coolant: return canisterCoolant;
|
||||
case FluidType.BrakeFluid: return canisterBrakeFluid;
|
||||
case FluidType.WasherFluid: return canisterWasherFluid;
|
||||
default: return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Материал по ключу (metal, black, battery, red, blue, yellow, white, rubber, copper, steam...).</summary>
|
||||
public Material GetMaterial(string key, Color fallbackColor)
|
||||
{
|
||||
foreach (NamedMaterial nm in materials)
|
||||
if (nm != null && nm.key == key && nm.material != null)
|
||||
return nm.material;
|
||||
|
||||
if (runtimeMaterials.TryGetValue(key, out Material m) && m != null) return m;
|
||||
m = CreateLitMaterial(fallbackColor);
|
||||
m.name = "Runtime_" + key;
|
||||
runtimeMaterials[key] = m;
|
||||
return m;
|
||||
}
|
||||
|
||||
private static Material particleMaterial;
|
||||
private static Material dirtOverlay;
|
||||
|
||||
/// <summary>Материал слоя грязи (прозрачная текстура подтеков/пыли), из каталога или процедурный.</summary>
|
||||
public static Material DirtOverlayMaterial
|
||||
{
|
||||
get
|
||||
{
|
||||
if (dirtOverlay != null) return dirtOverlay;
|
||||
foreach (NamedMaterial nm in Instance.materials)
|
||||
if (nm != null && nm.key == "dirt_overlay" && nm.material != null) return dirtOverlay = nm.material;
|
||||
|
||||
dirtOverlay = CreateLitMaterial(Color.white, 0.05f);
|
||||
dirtOverlay.name = "Runtime_DirtOverlay";
|
||||
Texture2D tex = GenerateDirtTexture(256);
|
||||
if (dirtOverlay.HasProperty("_BaseMap")) dirtOverlay.SetTexture("_BaseMap", tex);
|
||||
dirtOverlay.mainTexture = tex;
|
||||
MakeTransparent(dirtOverlay);
|
||||
return dirtOverlay;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Процедурная текстура грязи: землистые разводы и брызги снизу с альфа-каналом.</summary>
|
||||
public static Texture2D GenerateDirtTexture(int size)
|
||||
{
|
||||
Texture2D t = new Texture2D(size, size, TextureFormat.RGBA32, true) { name = "Tex_Dirt", wrapMode = TextureWrapMode.Repeat };
|
||||
Color mud = new Color(0.33f, 0.27f, 0.19f);
|
||||
Color dust = new Color(0.55f, 0.5f, 0.42f);
|
||||
for (int y = 0; y < size; y++)
|
||||
{
|
||||
for (int x = 0; x < size; x++)
|
||||
{
|
||||
float u = x / (float)size, v = y / (float)size;
|
||||
float n1 = Mathf.PerlinNoise(u * 6f + 3.1f, v * 6f + 7.7f);
|
||||
float n2 = Mathf.PerlinNoise(u * 22f + 11.3f, v * 22f + 1.9f);
|
||||
float n3 = Mathf.PerlinNoise(u * 60f, v * 60f);
|
||||
float a = Mathf.Clamp01(0.35f + n1 * 0.55f + (n2 - 0.5f) * 0.5f + (n3 - 0.5f) * 0.25f);
|
||||
Color c = Color.Lerp(mud, dust, n2);
|
||||
c.a = a;
|
||||
t.SetPixel(x, y, c);
|
||||
}
|
||||
}
|
||||
t.Apply();
|
||||
return t;
|
||||
}
|
||||
|
||||
/// <summary>Общий материал частиц (вода, пар, масло, краска) с поддержкой цвета вершин.</summary>
|
||||
public static Material ParticleMaterial
|
||||
{
|
||||
get
|
||||
{
|
||||
if (particleMaterial != null) return particleMaterial;
|
||||
Material fromCatalog = null;
|
||||
foreach (NamedMaterial nm in Instance.materials)
|
||||
if (nm != null && nm.key == "fx_particle" && nm.material != null) fromCatalog = nm.material;
|
||||
if (fromCatalog != null) return particleMaterial = fromCatalog;
|
||||
|
||||
Shader sh = Shader.Find("Universal Render Pipeline/Particles/Unlit") ?? Shader.Find("Particles/Standard Unlit") ?? Shader.Find("Sprites/Default");
|
||||
particleMaterial = new Material(sh) { name = "Runtime_Particles" };
|
||||
MakeTransparent(particleMaterial);
|
||||
return particleMaterial;
|
||||
}
|
||||
}
|
||||
|
||||
public static Material CreateLitMaterial(Color color, float smoothness = 0.35f, float metallic = 0f)
|
||||
{
|
||||
Shader sh = Shader.Find("Universal Render Pipeline/Lit") ?? Shader.Find("Standard");
|
||||
Material m = new Material(sh);
|
||||
if (m.HasProperty("_BaseColor")) m.SetColor("_BaseColor", color);
|
||||
m.color = color;
|
||||
if (m.HasProperty("_Smoothness")) m.SetFloat("_Smoothness", smoothness);
|
||||
if (m.HasProperty("_Metallic")) m.SetFloat("_Metallic", metallic);
|
||||
return m;
|
||||
}
|
||||
|
||||
public static Material CreateUnlitMaterial(Color color, bool transparent = false)
|
||||
{
|
||||
Shader sh = Shader.Find("Universal Render Pipeline/Unlit") ?? Shader.Find("Unlit/Color");
|
||||
Material m = new Material(sh);
|
||||
if (m.HasProperty("_BaseColor")) m.SetColor("_BaseColor", color);
|
||||
m.color = color;
|
||||
if (transparent) MakeTransparent(m);
|
||||
return m;
|
||||
}
|
||||
|
||||
public static void MakeTransparent(Material m)
|
||||
{
|
||||
if (m == null) return;
|
||||
if (m.HasProperty("_Surface"))
|
||||
{
|
||||
m.SetFloat("_Surface", 1f);
|
||||
m.SetFloat("_Blend", 0f);
|
||||
m.SetOverrideTag("RenderType", "Transparent");
|
||||
m.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
|
||||
m.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
|
||||
m.SetInt("_ZWrite", 0);
|
||||
m.EnableKeyword("_SURFACE_TYPE_TRANSPARENT");
|
||||
m.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
|
||||
}
|
||||
}
|
||||
|
||||
public static Material CreateEmissiveMaterial(Color baseColor, Color emission)
|
||||
{
|
||||
Material m = CreateLitMaterial(baseColor, 0.6f);
|
||||
if (m.HasProperty("_EmissionColor"))
|
||||
{
|
||||
m.EnableKeyword("_EMISSION");
|
||||
m.SetColor("_EmissionColor", emission);
|
||||
m.globalIlluminationFlags = MaterialGlobalIlluminationFlags.RealtimeEmissive;
|
||||
}
|
||||
return m;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user