Skip to content

Commit 667fa7f

Browse files
committed
Various changes. Fixes #13
- Responsibility of the programmer to supply Renderers, for my example I wrote a "DecalableCollider" script that is required to be attached to each collider. This isn't required though, any way you can get PaintDecal the renderer should be sufficient. - Responsibility of programmer to request the correct kind of texture they want. You can now supply TextureFormat and RenderTextureRead type. - Added an error message that only triggers in-editor that informs user that you cannot render decals on inside-out models. (Determinant of the matrix needs to be > 0).
1 parent 4aed4d3 commit 667fa7f

File tree

5 files changed

+2866
-53
lines changed

5 files changed

+2866
-53
lines changed

Scripts/DecalableInfo.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,17 @@ public void Release() {
4242
}
4343
}
4444

45-
public TextureTarget(string textureName, int textureScale, Material[] materials, bool dilationEnabled) {
45+
public TextureTarget(string textureName, int textureScale, Material[] materials, bool dilationEnabled,RenderTextureFormat renderTextureFormat, RenderTextureReadWrite renderTextureReadWrite) {
4646
this.dilationEnabled = dilationEnabled;
4747
drawIndices = new List<int>();
48-
baseTexture = new RenderTexture(textureScale, textureScale, 0) {
48+
baseTexture = new RenderTexture(textureScale, textureScale, 0, renderTextureFormat, renderTextureReadWrite) {
4949
antiAliasing = 1,
5050
useMipMap = !this.dilationEnabled,
5151
autoGenerateMips = false,
5252
};
5353
ClearRenderTexture(baseTexture);
5454
if (this.dilationEnabled) {
55-
outputTexture = new RenderTexture(textureScale, textureScale, 0) {
55+
outputTexture = new RenderTexture(textureScale, textureScale, 0, renderTextureFormat, renderTextureReadWrite) {
5656
antiAliasing = 1,
5757
useMipMap = true,
5858
autoGenerateMips = false,
@@ -101,7 +101,7 @@ public void Initialize() {
101101
dilationEnabled = PaintDecal.IsDilateEnabled();
102102
PaintDecal.AddDecalableInfo(this);
103103
}
104-
public void Render(CommandBuffer buffer, Material projector, string textureName) {
104+
public void Render(CommandBuffer buffer, Material projector, string textureName, RenderTextureFormat renderTextureFormat, RenderTextureReadWrite renderTextureReadWrite) {
105105
if (textureTargets == null) {
106106
Destroy(this);
107107
return;
@@ -123,7 +123,7 @@ public void Render(CommandBuffer buffer, Material projector, string textureName)
123123
return;
124124
}
125125

126-
TextureTarget texTarget = new TextureTarget(textureName, textureScale, renderer.materials, dilationEnabled);
126+
TextureTarget texTarget = new TextureTarget(textureName, textureScale, renderer.materials, dilationEnabled, renderTextureFormat, renderTextureReadWrite);
127127
textureTargets.Add(textureName, texTarget);
128128
renderer.GetPropertyBlock(propertyBlock);
129129
propertyBlock.SetTexture(textureName, dilationEnabled ? texTarget.GetOutputTexture() : texTarget.GetBaseTexture());

Scripts/PaintDecal.cs

Lines changed: 9 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,6 @@ public static void SetTexelsPerMeter(float newTexelsPerMeter) {
5858
private const string defaultTextureName = "_DecalColorMap";
5959
public static Material GetDilationMaterial() => instance.dilationMaterial;
6060
public static float GetTexelsPerMeter() => instance.texelsPerMeter;
61-
private static void GetComponentsInChildrenNoAlloc<T>(Transform t, List<T> temp, List<T> result) {
62-
t.GetComponents<T>(temp);
63-
result.AddRange(temp);
64-
for(int i=0;i<t.childCount;i++) {
65-
GetComponentsInChildrenNoAlloc<T>(t.GetChild(i), temp, result);
66-
}
67-
}
6861

6962
private int InternalMemoryInUse() {
7063
int memoryInUse = 0;
@@ -139,49 +132,18 @@ public static void RemoveDecalableInfo(MonoBehaviourHider.DecalableInfo info) {
139132
}
140133
}
141134

142-
public static void RenderDecalForCollider(Collider c, Material projector, Vector3 position, Quaternion rotation, Vector2 size, float depth = 0.5f, string textureName = defaultTextureName) {
143-
LODGroup group = c.GetComponentInParent<LODGroup>();
144-
if (group != null) {
145-
instance.staticRenderers.Clear();
146-
GetComponentsInChildrenNoAlloc<Renderer>(group.transform, instance.staticTempRenderers, instance.staticRenderers);
147-
foreach(Renderer renderer in instance.staticRenderers) {
148-
RenderDecal(renderer, projector, position, rotation, size, depth, textureName);
149-
}
150-
return;
151-
}
152-
Renderer parentRenderer = c.GetComponentInParent<Renderer>();
153-
if (parentRenderer != null) {
154-
RenderDecal(parentRenderer, projector, position, rotation, size, depth, textureName);
155-
}
156-
157-
instance.staticRenderers.Clear();
158-
GetComponentsInChildrenNoAlloc<Renderer>(c.transform, instance.staticTempRenderers, instance.staticRenderers);
159-
foreach(Renderer renderer in instance.staticRenderers) {
160-
RenderDecal(renderer, projector, position, rotation, size, depth, textureName);
161-
}
162-
}
163-
public static void RenderDecalInSphere(Vector3 position, float radius, Material projector, Quaternion rotation, LayerMask hitMask, string textureName = defaultTextureName) {
164-
int hits = Physics.OverlapSphereNonAlloc(position, radius, instance.colliders, hitMask, QueryTriggerInteraction.UseGlobal);
165-
for(int i=0;i<hits;i++) {
166-
Collider c = instance.colliders[i];
167-
RenderDecalForCollider(c, projector, position-rotation*Vector3.forward*radius, rotation, Vector2.one*radius, radius*2f, textureName);
168-
}
169-
}
170-
public static void RenderDecalInBox(Vector3 boxHalfExtents, Vector3 position, Material projector, Quaternion boxOrientation, LayerMask hitMask, string textureName = defaultTextureName) {
171-
int hits = Physics.OverlapBoxNonAlloc(position, boxHalfExtents, instance.colliders, boxOrientation, hitMask, QueryTriggerInteraction.UseGlobal);
172-
for(int i=0;i<hits;i++) {
173-
Collider c = instance.colliders[i];
174-
RenderDecalForCollider(c, projector, position-boxOrientation*Vector3.forward*boxHalfExtents.z, boxOrientation, new Vector2(boxHalfExtents.x, boxHalfExtents.y)*2f, boxHalfExtents.z*2f, textureName);
175-
}
176-
}
177-
public static void RenderDecalForCollision(Collider c, Material projector, Vector3 position, Vector3 normal, float rotationAboutNormal, Vector2 size, float halfDepth = 0.5f, string textureName = defaultTextureName) {
178-
RenderDecalForCollider(c, projector, position+normal*halfDepth, Quaternion.AngleAxis(rotationAboutNormal, normal)*Quaternion.FromToRotation(Vector3.forward, -normal), size, halfDepth*2f, textureName);
179-
}
180-
public static void RenderDecal(Renderer r, Material projector, Vector3 position, Quaternion rotation, Vector2 size, float depth = 0.5f, string textureName = defaultTextureName) {
135+
public static void RenderDecal(Renderer r, Material projector, Vector3 position, Quaternion rotation, Vector2 size, float depth = 0.5f, string textureName = defaultTextureName, RenderTextureFormat renderTextureFormat = RenderTextureFormat.Default, RenderTextureReadWrite renderTextureReadWrite = RenderTextureReadWrite.Default) {
181136
// Only can draw on meshes.
182137
if (!(r is SkinnedMeshRenderer) && !(r is MeshRenderer)) {
183138
return;
184139
}
140+
141+
#if UNITY_EDITOR
142+
if (r.localToWorldMatrix.determinant < 0f) {
143+
Debug.LogError(
144+
"Tried to render a decal on an inside-out object, this isn't supported! Make sure scales aren't negative.", r.gameObject);
145+
}
146+
#endif
185147

186148
if (!r.TryGetComponent(out MonoBehaviourHider.DecalableInfo info)) {
187149
info = r.gameObject.AddComponent<MonoBehaviourHider.DecalableInfo>();
@@ -194,7 +156,7 @@ public static void RenderDecal(Renderer r, Material projector, Vector3 position,
194156
// We just queue up the commands, paintdecal will send them all together when its ready.
195157
instance.commandBuffer.Clear();
196158
instance.commandBuffer.SetViewProjectionMatrices(view, projection);
197-
info.Render(instance.commandBuffer, projector, textureName);
159+
info.Render(instance.commandBuffer, projector, textureName, renderTextureFormat, renderTextureReadWrite);
198160
Graphics.ExecuteCommandBuffer(instance.commandBuffer);
199161
}
200162

0 commit comments

Comments
 (0)