-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObjectStateMaterialUtils.cs
135 lines (119 loc) · 5.79 KB
/
ObjectStateMaterialUtils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class ObjectStateMaterialUtils{
public enum TMaterialState { Visible, Transparent, Hide }; // Use to indicate the new material state
public enum TBlendMode { Opaque, Cutout, Fade, Transparent }; // Use to change Material Standart Shader Render Mode
/// <summary>Change the Material Standart Shader Render mode in runtime</summary>
/// <param name="material">Material to be changed</param>
/// <param name="blendMode">New render mode of the standar shader of this material</param>
public static void SetMaterialRenderingMode(Material material, TBlendMode blendMode)
{
switch (blendMode)
{
case TBlendMode.Opaque:
if (material.GetFloat("_Mode") != 0)
{
material.SetFloat("_Mode", 0);
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
material.SetInt("_ZWrite", 1);
material.DisableKeyword("_ALPHATEST_ON");
material.DisableKeyword("_ALPHABLEND_ON");
material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
material.renderQueue = -1;
}
break;
case TBlendMode.Cutout:
if (material.GetFloat("_Mode") != 1)
{
material.SetFloat("_Mode", 1);
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
material.SetInt("_ZWrite", 1);
material.EnableKeyword("_ALPHATEST_ON");
material.DisableKeyword("_ALPHABLEND_ON");
material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
material.renderQueue = 2450;
}
break;
case TBlendMode.Fade:
if (material.GetFloat("_Mode") != 2)
{
material.SetFloat("_Mode", 2);
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
material.SetInt("_ZWrite", 0);
material.DisableKeyword("_ALPHATEST_ON");
material.EnableKeyword("_ALPHABLEND_ON");
material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
material.renderQueue = 3000;
}
break;
case TBlendMode.Transparent:
if (material.GetFloat("_Mode") != 3)
{
material.SetFloat("_Mode", 3);
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
material.SetInt("_ZWrite", 0);
material.DisableKeyword("_ALPHATEST_ON");
material.DisableKeyword("_ALPHABLEND_ON");
material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
material.renderQueue = 3000;
}
break;
}
}
/// <summary>Change alpha colour component of object material</summary>
/// <param name="alpha">New alpha component to be set on the material colour</param>
public static void SetAlphaColorToMaterial(Material material, float alpha)
{
Color currentColor = material.GetColor("_Color");
currentColor.a = alpha;
material.SetColor("_Color", currentColor);
}
/// <summary>Set a new colour to this object material</summary>
/// <param name="newColor">New colour to set into this object material</param>
public static void SetColourToMaterial(Material material, Color32 newColor)
{
material.color = newColor;
}
/// <summary>Get object material colour</summary>
/// <returns>Material color</returns>
public static Color32 GetColourOfMaterial(Material material)
{
return material.color;
}
// Note that Color32 and Color implictly convert to each other. You may pass a Color object to this method without first casting it.
public static string ColorToHex(Color32 color)
{
string hex = color.r.ToString("X2") + color.g.ToString("X2") + color.b.ToString("X2");
return hex;
}
public static Color HexToColor(string hex)
{
hex = hex.Replace("0x", "");//in case the string is formatted 0xFFFFFF
hex = hex.Replace("#", "");//in case the string is formatted #FFFFFF
byte a = 255;//assume fully visible unless specified in hex
byte r = byte.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
byte g = byte.Parse(hex.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
byte b = byte.Parse(hex.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);
//Only use alpha if the string has enough characters
if (hex.Length == 8)
{
a = byte.Parse(hex.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);
}
return new Color32(r, g, b, a);
}//hexToColor
public static Color32 IndicatedColourCalculate(Color32 _confirmedColour)
{
Color32 _indicatedColor;
float h, s, v;
Color.RGBToHSV(_confirmedColour, out h, out s, out v);
s -= s * 0.75f;
_indicatedColor = Color.HSVToRGB(h, s, v);
return _indicatedColor;
}
}