Initial commit

This commit is contained in:
AbstractConcept 2022-09-13 00:36:34 -05:00
commit 3c7cc0c973
8391 changed files with 704313 additions and 0 deletions

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: cc4c383146400014d810176d3a637934
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,58 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.U2D;
#if UNITY_EDITOR
using UnityEditor;
#endif
// Demo Script Usage:
// When you want multiple SpriteShapes to share a common Spline,
// attach this script to the secondary objects you would like to
// copy the Spline and set the ParentObject to the original object
// you are copying from.
[ExecuteInEditMode]
public class ConformingSpline : MonoBehaviour
{
public GameObject m_ParentObject;
private int hashCode;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (m_ParentObject != null)
{
hashCode = CopySpline(m_ParentObject, gameObject, hashCode);
}
}
private static int CopySpline(GameObject src, GameObject dst, int hashCode)
{
#if UNITY_EDITOR
var parentSpriteShapeController = src.GetComponent<SpriteShapeController>();
var mirrorSpriteShapeController = dst.GetComponent<SpriteShapeController>();
if (parentSpriteShapeController != null && mirrorSpriteShapeController != null && parentSpriteShapeController.spline.GetHashCode() != hashCode)
{
SerializedObject srcController = new SerializedObject(parentSpriteShapeController);
SerializedObject dstController = new SerializedObject(mirrorSpriteShapeController);
SerializedProperty srcSpline = srcController.FindProperty("m_Spline");
dstController.CopyFromSerializedProperty(srcSpline);
dstController.ApplyModifiedProperties();
EditorUtility.SetDirty(mirrorSpriteShapeController);
return parentSpriteShapeController.spline.GetHashCode();
}
#endif
return hashCode;
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: db6f6067a1e6dd34ca4e2c4b7145e79b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,43 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.U2D;
// Please add this Component to Camera or some top level object on each loadable scene.
public class GenerateSpriteShapes : MonoBehaviour
{
// Once all SpriteShapes are rendered, remove this Component if On or remove it from elsewhere.
public bool destroyOnCompletion = true;
void OnGUI()
{
// Loop all invisible SpriteShapeRenderers and generate geometry.
SpriteShapeRenderer[] spriteShapeRenderers = (SpriteShapeRenderer[]) GameObject.FindObjectsOfType (typeof(SpriteShapeRenderer));
CommandBuffer rc = new CommandBuffer();
rc.GetTemporaryRT(0, 256, 256, 0);
rc.SetRenderTarget(0);
foreach (var spriteShapeRenderer in spriteShapeRenderers)
{
var spriteShapeController = spriteShapeRenderer.gameObject.GetComponent<SpriteShapeController>();
if (spriteShapeRenderer != null && spriteShapeController != null)
{
if (!spriteShapeRenderer.isVisible)
{
spriteShapeController.BakeMesh();
rc.DrawRenderer(spriteShapeRenderer, spriteShapeRenderer.sharedMaterial);
// Debug.Log("generating shape for " + spriteShapeRenderer.gameObject.name);
}
}
}
rc.ReleaseTemporaryRT(0);
Graphics.ExecuteCommandBuffer(rc);
// SpriteShape Renderers are generated. This component is no longer needed. Delete this [or] remove this Component from elsewhere.
if (destroyOnCompletion)
Destroy(this);
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: fccb8cf469e193a4ea6c24362f061c12
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,171 @@
using System.Collections;
using System.Collections.Generic;
using Unity.Collections;
using UnityEngine;
using UnityEngine.Experimental.U2D;
using UnityEngine.U2D;
#if UNITY_EDITOR
using UnityEditor;
#endif
[ExecuteAlways]
public class GeometryCollider : MonoBehaviour
{
[SerializeField]
bool m_UpdateCollider = false;
int m_HashCode = 0;
void Start()
{
}
// Update is called once per frame
void Update()
{
if (m_UpdateCollider)
Bake(gameObject, false);
}
static public void Bake(GameObject go, bool forced)
{
var spriteShapeController = go.GetComponent<SpriteShapeController>();
var spriteShapeRenderer = go.GetComponent<SpriteShapeRenderer>();
var polyCollider = go.GetComponent<PolygonCollider2D>();
var geometryCollider = go.GetComponent<GeometryCollider>();
if (spriteShapeController != null && polyCollider != null)
{
var spline = spriteShapeController.spline;
if (geometryCollider != null)
{
int splineHashCode = spline.GetHashCode();
if (splineHashCode == geometryCollider.m_HashCode && !forced)
return;
geometryCollider.m_HashCode = splineHashCode;
}
NativeArray<ushort> indexArray;
NativeSlice<Vector3> posArray;
NativeSlice<Vector2> uv0Array;
NativeArray<SpriteShapeSegment> geomArray;
spriteShapeRenderer.GetChannels(65536, out indexArray, out posArray, out uv0Array);
geomArray = spriteShapeRenderer.GetSegments(spline.GetPointCount() * 8);
NativeArray<ushort> indexArrayLocal = new NativeArray<ushort>(indexArray.Length, Allocator.Temp);
List<Vector2> points = new List<Vector2>();
int indexCount = 0, vertexCount = 0, counter = 0;
for (int u = 0; u < geomArray.Length; ++u)
{
if (geomArray[u].indexCount > 0)
{
for (int i = 0; i < geomArray[u].indexCount; ++i)
{
indexArrayLocal[counter] = (ushort)(indexArray[counter] + vertexCount);
counter++;
}
vertexCount += geomArray[u].vertexCount;
indexCount += geomArray[u].indexCount;
}
}
Debug.Log(go.name + " : " + counter);
OuterEdges(polyCollider, indexArrayLocal, posArray, indexCount);
}
}
// Generate the outer edges from the Renderer mesh. Based on code from www.h3xed.com
static void OuterEdges(PolygonCollider2D polygonCollider, NativeArray<ushort> triangles, NativeSlice<Vector3> vertices, int triangleCount)
{
// Get just the outer edges from the mesh's triangles (ignore or remove any shared edges)
Dictionary<string, KeyValuePair<int, int>> edges = new Dictionary<string, KeyValuePair<int, int>>();
for (int i = 0; i < triangleCount; i += 3)
{
for (int e = 0; e < 3; e++)
{
int vert1 = triangles[i + e];
int vert2 = triangles[i + e + 1 > i + 2 ? i : i + e + 1];
string edge = Mathf.Min(vert1, vert2) + ":" + Mathf.Max(vert1, vert2);
if (edges.ContainsKey(edge))
{
edges.Remove(edge);
}
else
{
edges.Add(edge, new KeyValuePair<int, int>(vert1, vert2));
}
}
}
// Create edge lookup (Key is first vertex, Value is second vertex, of each edge)
Dictionary<int, int> lookup = new Dictionary<int, int>();
foreach (KeyValuePair<int, int> edge in edges.Values)
{
if (lookup.ContainsKey(edge.Key) == false)
{
lookup.Add(edge.Key, edge.Value);
}
}
// Create empty polygon collider
polygonCollider.pathCount = 0;
// Loop through edge vertices in order
int startVert = 0;
int nextVert = startVert;
int highestVert = startVert;
List<Vector2> colliderPath = new List<Vector2>();
while (true)
{
// Add vertex to collider path
colliderPath.Add(vertices[nextVert]);
// Get next vertex
nextVert = lookup[nextVert];
// Store highest vertex (to know what shape to move to next)
if (nextVert > highestVert)
{
highestVert = nextVert;
}
// Shape complete
if (nextVert == startVert)
{
// Add path to polygon collider
polygonCollider.pathCount++;
polygonCollider.SetPath(polygonCollider.pathCount - 1, colliderPath.ToArray());
colliderPath.Clear();
// Go to next shape if one exists
if (lookup.ContainsKey(highestVert + 1))
{
// Set starting and next vertices
startVert = highestVert + 1;
nextVert = startVert;
// Continue to next loop
continue;
}
// No more verts
break;
}
}
}
#if UNITY_EDITOR
[MenuItem("SpriteShape/Generate Geometry Collider", false, 358)]
public static void BakeGeometryCollider()
{
if (Selection.activeGameObject != null)
GeometryCollider.Bake(Selection.activeGameObject, true);
}
#endif
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e0ecd6fe4dd55d640bd7ef3235e0e425
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,159 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.U2D;
using ExtrasClipperLib;
#if UNITY_EDITOR
using UnityEditor;
#endif
public enum ColliderCornerType
{
Square,
Round,
Sharp
}
[ExecuteAlways]
public class LegacyCollider : MonoBehaviour
{
[SerializeField]
ColliderCornerType m_ColliderCornerType = ColliderCornerType.Square;
[SerializeField]
float m_ColliderOffset = 1.0f;
[SerializeField]
bool m_UpdateCollider = false;
const float s_ClipperScale = 100000.0f;
int m_HashCode = 0;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (m_UpdateCollider)
Bake(gameObject, false);
}
static void SampleCurve(float colliderDetail, Vector3 startPoint, Vector3 startTangent, Vector3 endPoint, Vector3 endTangent, ref List<IntPoint> path)
{
if (startTangent.sqrMagnitude > 0f || endTangent.sqrMagnitude > 0f)
{
for (int j = 0; j <= colliderDetail; ++j)
{
float t = j / (float)colliderDetail;
Vector3 newPoint = BezierUtility.BezierPoint(startPoint, startTangent + startPoint, endTangent + endPoint, endPoint, t) * s_ClipperScale;
path.Add(new IntPoint((System.Int64)newPoint.x, (System.Int64)newPoint.y));
}
}
else
{
Vector3 newPoint = startPoint * s_ClipperScale;
path.Add(new IntPoint((System.Int64)newPoint.x, (System.Int64)newPoint.y));
newPoint = endPoint * s_ClipperScale;
path.Add(new IntPoint((System.Int64)newPoint.x, (System.Int64)newPoint.y));
}
}
public static void Bake(GameObject go, bool forced)
{
var sc = go.GetComponent<SpriteShapeController>();
var lc = go.GetComponent<LegacyCollider>();
if (sc != null)
{
List<IntPoint> path = new List<IntPoint>();
int splinePointCount = sc.spline.GetPointCount();
int pathPointCount = splinePointCount;
ColliderCornerType cct = ColliderCornerType.Square;
float co = 1.0f;
if (lc != null)
{
int hashCode = sc.spline.GetHashCode() + lc.m_ColliderCornerType.GetHashCode() + lc.m_ColliderOffset.GetHashCode();
if (lc.m_HashCode == hashCode && !forced)
return;
lc.m_HashCode = hashCode;
cct = lc.m_ColliderCornerType;
co = lc.m_ColliderOffset;
}
if (sc.spline.isOpenEnded)
pathPointCount--;
for (int i = 0; i < pathPointCount; ++i)
{
int nextIndex = SplineUtility.NextIndex(i, splinePointCount);
SampleCurve(sc.colliderDetail, sc.spline.GetPosition(i), sc.spline.GetRightTangent(i), sc.spline.GetPosition(nextIndex), sc.spline.GetLeftTangent(nextIndex), ref path);
}
if (co != 0f)
{
List<List<IntPoint>> solution = new List<List<IntPoint>>();
ClipperOffset clipOffset = new ClipperOffset();
EndType endType = EndType.etClosedPolygon;
if (sc.spline.isOpenEnded)
{
endType = EndType.etOpenSquare;
if (cct == ColliderCornerType.Round)
endType = EndType.etOpenRound;
}
clipOffset.ArcTolerance = 200f / sc.colliderDetail;
clipOffset.AddPath(path, (ExtrasClipperLib.JoinType)cct, endType);
clipOffset.Execute(ref solution, s_ClipperScale * co);
if (solution.Count > 0)
path = solution[0];
}
List<Vector2> pathPoints = new List<Vector2>(path.Count);
for (int i = 0; i < path.Count; ++i)
{
IntPoint ip = path[i];
pathPoints.Add(new Vector2(ip.X / s_ClipperScale, ip.Y / s_ClipperScale));
}
var pc = go.GetComponent<PolygonCollider2D>();
if (pc)
{
pc.pathCount = 0;
pc.SetPath(0, pathPoints.ToArray());
}
var ec = go.GetComponent<EdgeCollider2D>();
if (ec)
{
if (co > 0f || co < 0f && !sc.spline.isOpenEnded)
pathPoints.Add(pathPoints[0]);
ec.points = pathPoints.ToArray();
}
}
}
#if UNITY_EDITOR
[MenuItem("SpriteShape/Generate Legacy Collider", false, 358)]
public static void BakeLegacyCollider()
{
if (Selection.activeGameObject != null)
LegacyCollider.Bake(Selection.activeGameObject, true);
}
#endif
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6a8e65bafadf93f41a17da25163f2f51
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,59 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.U2D;
// Dynamic modification of spline to follow the path of mouse movement.
// This script is just a simplified demo to demonstrate the idea.
public class SimpleDraw : MonoBehaviour
{
public float minimumDistance = 1.0f;
private Vector3 lastPosition;
// Use this for initialization
void Start()
{
}
private void Smoothen(SpriteShapeController sc, int pointIndex)
{
Vector3 position = sc.spline.GetPosition(pointIndex);
Vector3 positionNext = sc.spline.GetPosition(SplineUtility.NextIndex(pointIndex, sc.spline.GetPointCount()));
Vector3 positionPrev = sc.spline.GetPosition(SplineUtility.PreviousIndex(pointIndex, sc.spline.GetPointCount()));
Vector3 forward = gameObject.transform.forward;
float scale = Mathf.Min((positionNext - position).magnitude, (positionPrev - position).magnitude) * 0.33f;
Vector3 leftTangent = (positionPrev - position).normalized * scale;
Vector3 rightTangent = (positionNext - position).normalized * scale;
sc.spline.SetTangentMode(pointIndex, ShapeTangentMode.Continuous);
SplineUtility.CalculateTangents(position, positionPrev, positionNext, forward, scale, out rightTangent, out leftTangent);
sc.spline.SetLeftTangent(pointIndex, leftTangent);
sc.spline.SetRightTangent(pointIndex, rightTangent);
}
// Update is called once per frame
void Update()
{
var mp = Input.mousePosition;
mp.z = 10.0f;
mp = Camera.main.ScreenToWorldPoint(mp);
var dt = Mathf.Abs((mp - lastPosition).magnitude);
var md = (minimumDistance > 1.0f) ? minimumDistance : 1.0f;
if (Input.GetMouseButton(0) && dt > md)
{
var spriteShapeController = gameObject.GetComponent<SpriteShapeController>();
var spline = spriteShapeController.spline;
spline.InsertPointAt(spline.GetPointCount(), mp);
var newPointIndex = spline.GetPointCount() - 1;
Smoothen(spriteShapeController, newPointIndex - 1);
spline.SetHeight(newPointIndex, UnityEngine.Random.Range(0.1f, 2.0f));
lastPosition = mp;
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 922bb1bbfeaeb9a4c9204b7d0bc3d8c8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,53 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.U2D;
public class Sprinkler : MonoBehaviour
{
public GameObject m_Prefab;
public float m_RandomFactor = 10.0f;
public bool m_UseNormals = false;
float Angle(Vector3 a, Vector3 b)
{
float dot = Vector3.Dot(a, b);
float det = (a.x * b.y) - (b.x * a.y);
return Mathf.Atan2(det, dot) * Mathf.Rad2Deg;
}
// Use this for initialization. Plant the Prefabs on Startup
void Start ()
{
SpriteShapeController ssc = GetComponent<SpriteShapeController>();
Spline spl = ssc.spline;
for (int i = 1; i < spl.GetPointCount() - 1; ++i)
{
if (Random.Range(0, 100) > (100 - m_RandomFactor) )
{
var go = GameObject.Instantiate(m_Prefab);
go.transform.position = spl.GetPosition(i);
if (m_UseNormals)
{
Vector3 lt = Vector3.Normalize(spl.GetPosition(i - 1) - spl.GetPosition(i));
Vector3 rt = Vector3.Normalize(spl.GetPosition(i + 1) - spl.GetPosition(i));
float a = Angle(Vector3.up, lt);
float b = Angle(lt, rt);
float c = a + (b * 0.5f);
if (b > 0)
c = (180 + c);
go.transform.rotation = Quaternion.Euler(0, 0, c);
}
}
}
}
// Update is called once per frame
void Update ()
{
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1c3eb1d33a6f9114bb5b51099948d2ce
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: