Screw it, I added light

This commit is contained in:
RochesterX
2025-04-20 22:23:34 -04:00
parent 14970499b8
commit be63f2e19d
19 changed files with 9834 additions and 29 deletions

View File

@@ -0,0 +1,27 @@
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEngine.Rendering.Universal;
namespace TilemapShadowCaster.Runtime
{
public class PathShadow : ShadowCaster2D
{
static FieldInfo shapeFieldInfo = typeof(ShadowCaster2D).GetField("m_ShapePath",
BindingFlags.NonPublic | BindingFlags.Instance);
static FieldInfo shapeHashFieldInfo = typeof(ShadowCaster2D).GetField("m_ShapePathHash",
BindingFlags.NonPublic | BindingFlags.Instance);
static FieldInfo sortingLayersFieldInfo = typeof(ShadowCaster2D).GetField("m_ApplyToSortingLayers",
BindingFlags.NonPublic | BindingFlags.Instance);
internal void SetShape(List<Vector2> points, int[] sortingLayers)
{
Vector3[] shapev3 = points.ConvertAll((point) => new Vector3(point.x, point.y)).ToArray();
shapeFieldInfo.SetValue(this, shapev3);
sortingLayersFieldInfo.SetValue(this, sortingLayers);
shapeHashFieldInfo.SetValue(this, (int) UnityEngine.Random.Range(0f, 10000000f));
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 2c916149db92e4f9ea5091a170cf88f2

View File

@@ -0,0 +1,88 @@
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
namespace TilemapShadowCaster.Runtime
{
[ExecuteInEditMode]
[DisallowMultipleComponent]
[AddComponentMenu("Rendering/2D/Tilemap Shadow Caster")]
public class TilemapShadowCaster2D : MonoBehaviour
{
[SerializeField] private uint colliderHash;
[SerializeField] private bool m_SelfShadows = false;
[SerializeField] private int m_ApplyToSortingLayers = -1;
private void Update()
{
CompositeCollider2D collider = GetComponent<CompositeCollider2D>();
uint shapeHash = collider.GetShapeHash();
if (shapeHash == colliderHash) return;
colliderHash = shapeHash;
ReinitializeShapes(collider);
}
private int[] getLayers(){
int[] values = SortingLayer.layers.Select(l => l.id).ToArray();
List<int> sortingLayers = new List<int>();
int propCount = 0;
for (int i = 0; i < values.Length; i++)
{
int layer = 1 << i;
if ((m_ApplyToSortingLayers & layer) != 0)
{
sortingLayers.Add(values[propCount]);
propCount ++;
}
}
int[] layerArray = sortingLayers.ToArray();
return layerArray;
}
private void ReinitializeShapes(CompositeCollider2D collider)
{
RemoveCurrentShadows();
for (int i = 0; i < collider.pathCount; i++)
{
List<Vector2> points = new List<Vector2>();
collider.GetPath(i, points);
GameObject go = new GameObject("AutogeneratedShadowPath", typeof(MeshRenderer));
go.transform.parent = transform;
PathShadow path = go.AddComponent<PathShadow>();
path.useRendererSilhouette = false;
path.selfShadows = m_SelfShadows;
path.SetShape(points, getLayers());
}
}
private void RemoveCurrentShadows()
{
new List<PathShadow>(GetComponentsInChildren<PathShadow>())
.ConvertAll(comp => comp.transform.gameObject)
.ForEach(gameObject =>
{
if (Application.isEditor)
{
DestroyImmediate(gameObject);
}
else
{
Destroy(gameObject);
}
});
}
public void ReinitializeShapes()
{
ReinitializeShapes(GetComponent<CompositeCollider2D>());
}
public void OnDestroy()
{
RemoveCurrentShadows();
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: b44ee1dccb1a340e299b7e9ccd5d003b