-
Notifications
You must be signed in to change notification settings - Fork 29
/
CP_SSSSS_Object.cs
104 lines (93 loc) · 3.58 KB
/
CP_SSSSS_Object.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
using UnityEngine;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif
[ExecuteInEditMode]
[RequireComponent(typeof(Renderer))]
public class CP_SSSSS_Object : MonoBehaviour {
[Tooltip("Material has no Scattering Color Parameter, thease are used instead of. If Material has Scattering Color Parameter and you want to use it, stay empty.")]
public TextureAndColor[] scatteringColorsPerSubmesh;
Renderer r;
// Use this for initialization
void Start () {
r = GetComponent<Renderer>();
parameters.Clear();
subMeshIndicies.Clear();
if(r) {
var sharedMaterials = r.sharedMaterials;
for(int i = 0; i < sharedMaterials.Length; ++i) {
var material = sharedMaterials[i];
if(i < scatteringColorsPerSubmesh.Length && scatteringColorsPerSubmesh[i].color != Color.black && material.renderQueue != (int)UnityEngine.Rendering.RenderQueue.Transparent) {
subMeshIndicies.Add(i);
parameters.Add(CP_SSSSS_Main.MakeSSSParameterFromTextureAndColor(scatteringColorsPerSubmesh[i]));
}
else if(CP_SSSSS_Main.HasMaterialSSSParameter(material) && material.renderQueue != (int)UnityEngine.Rendering.RenderQueue.Transparent) {
subMeshIndicies.Add(i);
parameters.Add(CP_SSSSS_Main.MakeSSSParameterFromMaterial(material));
}
}
}
}
void OnWillRenderObject()
{
if(r) {
var camera = Camera.current;
var sssssMain = camera.GetComponent<CP_SSSSS_Main>();
if(sssssMain && sssssMain.isActiveAndEnabled) {
if(Application.isPlaying) {
for(int i = 0; i < subMeshIndicies.Count; ++i) {
sssssMain.AddRenderer(r, subMeshIndicies[i], parameters[i]);
}
}
else {
var sharedMaterials = r.sharedMaterials;
for(int i = 0; i < sharedMaterials.Length; ++i) {
var material = sharedMaterials[i];
if(scatteringColorsPerSubmesh != null && i < scatteringColorsPerSubmesh.Length && scatteringColorsPerSubmesh[i].color != Color.black && material.renderQueue != (int)UnityEngine.Rendering.RenderQueue.Transparent) {
subMeshIndicies.Add(i);
sssssMain.AddRenderer(r, i, CP_SSSSS_Main.MakeSSSParameterFromTextureAndColor(scatteringColorsPerSubmesh[i]));
}
else if(CP_SSSSS_Main.HasMaterialSSSParameter(material) && material.renderQueue != (int)UnityEngine.Rendering.RenderQueue.Transparent) {
sssssMain.AddRenderer(r, i, CP_SSSSS_Main.MakeSSSParameterFromMaterial(material));
}
}
}
}
}
}
List<CP_SSSSS_Main.SSSParameter> parameters = new List<CP_SSSSS_Main.SSSParameter>();
List<int> subMeshIndicies = new List<int>();
void Reset()
{
scatteringColorsPerSubmesh = new TextureAndColor[] {
new TextureAndColor {
texture = null,
color = new Color(1.0f, 0.2f, 0.1f, 1.0f)
}
};
}
}
[System.Serializable]
public struct TextureAndColor
{
public Texture texture;
public Color color;
}
#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(TextureAndColor))]
public class TextureAndColorDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);
var textureRect = new Rect(position.x, position.y, 120, position.height);
var labelRect = new Rect(textureRect.x + textureRect.width, position.y, 70, position.height);
var colorRect = new Rect(labelRect.x + labelRect.width, position.y, 65, position.height);
EditorGUI.PropertyField(textureRect, property.FindPropertyRelative("texture"), GUIContent.none);
EditorGUI.LabelField(labelRect, "SSS Map");
EditorGUI.PropertyField(colorRect, property.FindPropertyRelative("color"), GUIContent.none);
EditorGUI.EndProperty();
}
}
#endif