-
Notifications
You must be signed in to change notification settings - Fork 0
/
DecalPainter.cs
208 lines (170 loc) · 5.87 KB
/
DecalPainter.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Author: Damien Mayance (http://dmayance.com)
// 2013 - Pixelnest Studio (http://pixelnest.io)
//
// This script paints decals on surfaces it hits from a point.
// See http://dmayance.com/2013-10-09-unity-paint-part-2/ for further explanations.
//
// Usage:
// - Attach it to an unique object that won't be deleted of your choice. There should be only one instance of this script.
// - Then fill the "PaintDecalPrefabs" list with your decals prefabs.
// - Finally, just call DecalPainter.Instance.Paint from another script to paint!
//
// It included a debug mode where you can click in the scene to paint. Everything between UNITY_EDITOR is just debug stuff that can be removed.
//
using UnityEngine;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// Generate paint decals
/// </summary>
public class DecalPainter : MonoBehaviour
{
public static DecalPainter Instance;
/// <summary>
/// Paint decals to reproduce on textures
/// </summary>
public List<Transform> PaintDecalPrefabs;
/// <summary>
/// Parent to affect for scene management
/// </summary>
public Transform DecalsParent;
/// <summary>
/// Minimal scale of a prefab
/// </summary>
public float MinScale = 0.75f;
/// <summary>
/// Maximal scale of a prefab
/// </summary>
public float MaxScale = 3f;
/// <summary>
/// Range of the splash raycast
/// </summary>
public float SplashRange = 1.5f;
/// <summary>
/// Number of decals
/// </summary>
public int PoolSize = 300;
private Transform[] paintDecals;
private int currentPoolIndex;
private List<Material> materials;
#if UNITY_EDITOR
private bool mDrawDebug;
private Vector3 mHitPoint;
private List<Ray> mRaysDebug = new List<Ray>();
#endif
void Awake()
{
materials = new List<Material>();
if (Instance != null) Debug.LogError("More than one Painter has been instanciated in this scene!");
Instance = this;
if (PaintDecalPrefabs.Count == 0) Debug.LogError("Missing Paint decals prefabs!");
paintDecals = new Transform[PoolSize];
currentPoolIndex = 0;
}
void Update()
{
#if UNITY_EDITOR
// Check for a click
if (Input.GetMouseButtonDown(0))
{
// Raycast
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Mathf.Infinity))
{
// Paint!
Color color = Color.red;
Paint(hit.point + hit.normal * 1f, color, 10); // Step back a little
}
}
#endif
}
public void Paint(Vector3 location, Color color, int drops, float scaleBonus = 1f)
{
#if UNITY_EDITOR
mHitPoint = location;
mRaysDebug.Clear();
mDrawDebug = true;
#endif
RaycastHit hit;
// Generate multiple decals in once
int n = 0;
while(n < drops)
{
var dir = transform.TransformDirection(Random.onUnitSphere * SplashRange);
// Avoid raycast backward as we're in a 2D space
if (dir.z < 0) dir.z = Random.Range(0f, 1f);
// Raycast around the position to splash everwhere we can
if (Physics.Raycast(location, dir, out hit, SplashRange))
{
PaintDecal(hit, color, scaleBonus);
#if UNITY_EDITOR
mRaysDebug.Add(new Ray(location, dir));
#endif
n++;
}
}
}
private void PaintDecal(RaycastHit hit, Color color, float scaleBonus)
{
// Create a splash if we found a surface
int randomIndex = Random.Range(0, PaintDecalPrefabs.Count);
Transform paintDecal = PaintDecalPrefabs[randomIndex];
var paintSplatter = GameObject.Instantiate(paintDecal,
hit.point,
// Rotation from the original sprite to the normal
// Prefab are currently oriented to z+ so we use the opposite
Quaternion.FromToRotation(Vector3.back, hit.normal)
) as Transform;
// Find an existing material to enable batching
var sharedMat = materials.Where(m => m.name.Equals(paintSplatter.renderer.material.name)
&& m.color.Equals(color)
).FirstOrDefault();
// New material
if (sharedMat == null)
{
Material mat = paintSplatter.renderer.material;
mat.color = color;
materials.Add(mat);
}
// Old one
else
{
paintSplatter.renderer.sharedMaterial = sharedMat;
}
// Random scale
var scaler = Random.Range(MinScale, MaxScale) * scaleBonus;
paintSplatter.localScale = new Vector3(
paintSplatter.localScale.x * scaler,
paintSplatter.localScale.y * scaler,
paintSplatter.localScale.z
);
// Random rotation effect
var rater = Random.Range(0, 359);
paintSplatter.transform.RotateAround(hit.point, hit.normal, rater);
paintSplatter.parent = DecalsParent;
// Pool
if (paintDecals[currentPoolIndex] != null)
{
Destroy(paintDecals[currentPoolIndex].gameObject);
paintDecals[currentPoolIndex] = null;
}
paintDecals[currentPoolIndex] = paintSplatter;
currentPoolIndex++;
if (currentPoolIndex >= PoolSize) currentPoolIndex = 0;
}
void OnDrawGizmos()
{
#if UNITY_EDITOR
if (mDrawDebug)
{
Gizmos.DrawSphere(mHitPoint, 0.2f);
foreach (var r in mRaysDebug)
{
Gizmos.DrawRay(r);
}
}
#endif
}
}