154 lines
4.8 KiB
C#
154 lines
4.8 KiB
C#
using UnityEngine;
|
|
using UnityEngine.Rendering;
|
|
|
|
namespace Vanguard.Board3D
|
|
{
|
|
/// <summary>Общие материалы стола. Текстуры лиц задаются property block.</summary>
|
|
internal static class BoardMaterials
|
|
{
|
|
private static Material _edge;
|
|
private static Material _back;
|
|
private static Material _highlight;
|
|
private static Material _white;
|
|
|
|
public static Material Edge
|
|
{
|
|
get
|
|
{
|
|
if (_edge == null)
|
|
_edge = CreateLit(new Color(0.10f, 0.11f, 0.14f));
|
|
return _edge;
|
|
}
|
|
}
|
|
|
|
public static Material Back
|
|
{
|
|
get
|
|
{
|
|
if (_back == null)
|
|
_back = CreateUnlit(BoardTextures.Back, Color.white);
|
|
return _back;
|
|
}
|
|
}
|
|
|
|
public static Material Highlight
|
|
{
|
|
get
|
|
{
|
|
if (_highlight == null)
|
|
_highlight = CreateUnlit(null, new Color(1f, 0.78f, 0.24f));
|
|
return _highlight;
|
|
}
|
|
}
|
|
|
|
public static Material White
|
|
{
|
|
get
|
|
{
|
|
if (_white == null)
|
|
_white = CreateUnlit(null, Color.white);
|
|
return _white;
|
|
}
|
|
}
|
|
|
|
public static Material CreateLit(Color color, float emission = 0f)
|
|
{
|
|
var shader = Shader.Find("Universal Render Pipeline/Lit");
|
|
if (shader == null)
|
|
shader = Shader.Find("Standard");
|
|
if (shader == null)
|
|
return CreateUnlit(null, color);
|
|
|
|
var material = new Material(shader)
|
|
{
|
|
name = "Board Lit",
|
|
enableInstancing = true,
|
|
hideFlags = HideFlags.DontSave,
|
|
};
|
|
SetColor(material, color);
|
|
if (emission > 0f && material.HasProperty("_EmissionColor"))
|
|
{
|
|
material.EnableKeyword("_EMISSION");
|
|
material.SetColor("_EmissionColor", color * emission);
|
|
}
|
|
|
|
return material;
|
|
}
|
|
|
|
public static Material CreateUnlit(Texture texture, Color tint)
|
|
{
|
|
var shader = Shader.Find("Universal Render Pipeline/Unlit");
|
|
if (shader == null)
|
|
shader = Shader.Find("Unlit/Texture");
|
|
if (shader == null)
|
|
shader = Shader.Find("Sprites/Default");
|
|
if (shader == null)
|
|
shader = Shader.Find("Standard");
|
|
if (shader == null)
|
|
return null;
|
|
|
|
var material = new Material(shader)
|
|
{
|
|
name = "Board Unlit",
|
|
enableInstancing = true,
|
|
hideFlags = HideFlags.DontSave,
|
|
};
|
|
|
|
if (texture != null)
|
|
{
|
|
if (material.HasProperty("_BaseMap"))
|
|
material.SetTexture("_BaseMap", texture);
|
|
if (material.HasProperty("_MainTex"))
|
|
material.SetTexture("_MainTex", texture);
|
|
}
|
|
|
|
SetColor(material, tint);
|
|
if (material.HasProperty("_Surface"))
|
|
material.SetFloat("_Surface", 0f);
|
|
if (material.HasProperty("_Blend"))
|
|
material.SetFloat("_Blend", 0f);
|
|
return material;
|
|
}
|
|
|
|
public static void SetColor(Material material, Color color)
|
|
{
|
|
if (material == null)
|
|
return;
|
|
if (material.HasProperty("_BaseColor"))
|
|
material.SetColor("_BaseColor", color);
|
|
if (material.HasProperty("_Color"))
|
|
material.SetColor("_Color", color);
|
|
}
|
|
|
|
public static void SetColor(MaterialPropertyBlock block, Material material, Color color)
|
|
{
|
|
if (block == null)
|
|
return;
|
|
if (material != null && material.HasProperty("_BaseColor"))
|
|
block.SetColor("_BaseColor", color);
|
|
else
|
|
block.SetColor("_Color", color);
|
|
}
|
|
|
|
public static void SetTexture(MaterialPropertyBlock block, Material material, Texture texture)
|
|
{
|
|
if (block == null)
|
|
return;
|
|
if (material != null && material.HasProperty("_BaseMap"))
|
|
block.SetTexture("_BaseMap", texture);
|
|
else
|
|
block.SetTexture("_MainTex", texture);
|
|
}
|
|
|
|
public static void SetShadows(Renderer renderer, bool cast)
|
|
{
|
|
if (renderer == null)
|
|
return;
|
|
renderer.shadowCastingMode = cast
|
|
? ShadowCastingMode.On
|
|
: ShadowCastingMode.Off;
|
|
renderer.receiveShadows = cast;
|
|
}
|
|
}
|
|
}
|