diff options
author | chai <215380520@qq.com> | 2024-03-13 11:00:58 +0800 |
---|---|---|
committer | chai <215380520@qq.com> | 2024-03-13 11:00:58 +0800 |
commit | 6ce8b9e22fc13be34b442c7b6af48b42cd44275a (patch) | |
tree | b38119d2acf0a982cb67e381f146924b9bfc3b3f /ch.sycoforge.Decal.Demo |
+init
Diffstat (limited to 'ch.sycoforge.Decal.Demo')
-rw-r--r-- | ch.sycoforge.Decal.Demo/AdvancedBulletHoles.cs | 55 | ||||
-rw-r--r-- | ch.sycoforge.Decal.Demo/BasicBulletHoles.cs | 37 | ||||
-rw-r--r-- | ch.sycoforge.Decal.Demo/BezierUtil.cs | 77 | ||||
-rw-r--r-- | ch.sycoforge.Decal.Demo/LineUtil.cs | 50 | ||||
-rw-r--r-- | ch.sycoforge.Decal.Demo/PathAgent.cs | 106 | ||||
-rw-r--r-- | ch.sycoforge.Decal.Demo/ProxyRegister.cs | 13 | ||||
-rw-r--r-- | ch.sycoforge.Decal.Demo/Sinoid.cs | 29 |
7 files changed, 367 insertions, 0 deletions
diff --git a/ch.sycoforge.Decal.Demo/AdvancedBulletHoles.cs b/ch.sycoforge.Decal.Demo/AdvancedBulletHoles.cs new file mode 100644 index 0000000..28e5311 --- /dev/null +++ b/ch.sycoforge.Decal.Demo/AdvancedBulletHoles.cs @@ -0,0 +1,55 @@ +using UnityEngine; + +namespace ch.sycoforge.Decal.Demo; + +public class AdvancedBulletHoles : MonoBehaviour +{ + public EasyDecal DecalPrefab; + + public GameObject ImpactParticles; + + public float CastRadius = 0.25f; + + private void Start() + { + if (DecalPrefab == null) + { + Debug.LogError("The AdvancedBulletHoles script has no decal prefab attached."); + } + EasyDecal.HideMesh = false; + } + + private void Update() + { + if (!Input.GetMouseButtonUp(0)) + { + return; + } + Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); + if (!Physics.Raycast(ray, out var hitInfo, 200f)) + { + return; + } + GameObject receiver = hitInfo.collider.gameObject; + Vector3 point = hitInfo.point; + RaycastHit[] array = Physics.SphereCastAll(ray, CastRadius, Vector3.Distance(Camera.main.transform.position, point) + 2f); + Vector3 normal = hitInfo.normal; + if (array.Length > 0) + { + RaycastHit[] array2 = array; + for (int i = 0; i < array2.Length; i++) + { + RaycastHit raycastHit = array2[i]; + Debug.DrawLine(ray.origin, raycastHit.point, Color.red); + normal += raycastHit.normal; + } + } + normal /= (float)(array.Length + 1); + EasyDecal.ProjectAt(DecalPrefab.gameObject, receiver, point, normal); + if (ImpactParticles != null) + { + Quaternion rotation = Quaternion.FromToRotation(Vector3.up, normal); + Object.Instantiate(ImpactParticles, point, rotation); + } + } +} diff --git a/ch.sycoforge.Decal.Demo/BasicBulletHoles.cs b/ch.sycoforge.Decal.Demo/BasicBulletHoles.cs new file mode 100644 index 0000000..0d6eef1 --- /dev/null +++ b/ch.sycoforge.Decal.Demo/BasicBulletHoles.cs @@ -0,0 +1,37 @@ +using UnityEngine; + +namespace ch.sycoforge.Decal.Demo; + +public class BasicBulletHoles : MonoBehaviour +{ + public EasyDecal DecalPrefab; + + private bool t; + + public void Start() + { + if (DecalPrefab == null) + { + Debug.LogError("The DynamicDemo script has no decal prefab attached."); + } + } + + public void Update() + { + if (!Input.GetMouseButtonUp(0)) + { + return; + } + Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); + if (Physics.Raycast(ray, out var hitInfo, 200f)) + { + Debug.DrawLine(ray.origin, hitInfo.point, Color.red); + EasyDecal easyDecal = EasyDecal.ProjectAt(DecalPrefab.gameObject, hitInfo.collider.gameObject, hitInfo.point, hitInfo.normal); + t = !t; + if (t) + { + easyDecal.CancelFade(); + } + } + } +} diff --git a/ch.sycoforge.Decal.Demo/BezierUtil.cs b/ch.sycoforge.Decal.Demo/BezierUtil.cs new file mode 100644 index 0000000..4784ce7 --- /dev/null +++ b/ch.sycoforge.Decal.Demo/BezierUtil.cs @@ -0,0 +1,77 @@ +using System.Collections.Generic; +using UnityEngine; + +namespace ch.sycoforge.Decal.Demo; + +public static class BezierUtil +{ + public static List<Vector3> InterpolatePath(List<Vector3> path, int segments, float radius, float angleThreshold) + { + if (path.Count >= 3) + { + List<Vector3> list = new List<Vector3>(); + int num = path.Count - 1; + list.Add(path[0]); + int num2 = 0; + for (int i = 2; i < path.Count; i++) + { + Vector3 vector = path[i - 2]; + Vector3 vector2 = path[i - 1]; + Vector3 vector3 = path[i]; + Vector3 vector4 = vector2 - vector; + Vector3 vector5 = vector3 - vector2; + float num3 = Mathf.Abs(Vector3.Angle(vector4, vector5)); + if (!(num3 <= angleThreshold)) + { + float magnitude = vector4.magnitude; + float magnitude2 = vector5.magnitude; + vector4.Normalize(); + vector5.Normalize(); + magnitude = Mathf.Min(magnitude * 0.5f, radius); + magnitude2 = Mathf.Min(magnitude2 * 0.5f, radius); + Vector3 vector6 = vector2 - vector4 * magnitude; + Vector3 vector7 = vector2; + Vector3 vector8 = vector2 + vector5 * magnitude2; + for (int j = 0; j < segments; j++) + { + float num4 = (float)j / ((float)segments - 1f); + float num5 = 1f - num4; + Vector3 item = num5 * num5 * vector6 + 2f * num5 * num4 * vector7 + num4 * num4 * vector8; + list.Add(item); + } + num2 = i; + } + } + if (num2 <= num) + { + list.Add(path[num]); + } + return list; + } + return path; + } + + public static Vector3[] GetBezierApproximation(Vector3[] controlPoints, int outputSegmentCount) + { + Vector3[] array = new Vector3[outputSegmentCount + 1]; + for (int i = 0; i < outputSegmentCount; i++) + { + float t = (float)i / (float)outputSegmentCount; + ref Vector3 reference = ref array[i]; + reference = GetBezierPoint(t, controlPoints, 0, controlPoints.Length); + } + return array; + } + + public static Vector3 GetBezierPoint(float t, Vector3[] controlPoints, int index, int count) + { + if (count == 1) + { + return controlPoints[index]; + } + Vector3 bezierPoint = GetBezierPoint(t, controlPoints, index - 1, count - 1); + Vector3 bezierPoint2 = GetBezierPoint(t, controlPoints, index, count - 1); + Vector3 bezierPoint3 = GetBezierPoint(t, controlPoints, index + 1, count - 1); + return (1f - t) * (1f - t) * bezierPoint + 2f * (1f - t) * t * bezierPoint2 + t * t * bezierPoint3; + } +} diff --git a/ch.sycoforge.Decal.Demo/LineUtil.cs b/ch.sycoforge.Decal.Demo/LineUtil.cs new file mode 100644 index 0000000..864ee19 --- /dev/null +++ b/ch.sycoforge.Decal.Demo/LineUtil.cs @@ -0,0 +1,50 @@ +using System.Collections.Generic; +using UnityEngine; + +namespace ch.sycoforge.Decal.Demo; + +public static class LineUtil +{ + public static void DrawPath(float thickness, Material material, List<Vector3> path) + { + if (path != null && (path == null || path.Count >= 2)) + { + if (thickness <= Mathf.Epsilon) + { + GL.Begin(1); + } + else + { + GL.Begin(7); + } + material.SetPass(0); + GL.Color(Color.blue); + Vector3 start = path[0]; + for (int i = 1; i < path.Count; i++) + { + Vector3 vector = path[i]; + DrawLine(thickness, start, vector); + start = vector; + } + GL.End(); + } + } + + private static void DrawLine(float thickness, Vector3 start, Vector3 end) + { + if (thickness <= Mathf.Epsilon) + { + GL.Vertex(start); + GL.Vertex(end); + return; + } + Camera main = Camera.main; + Vector3 normalized = (end - start).normalized; + Vector3 normalized2 = (start - main.transform.position).normalized; + Vector3 vector = Vector3.Cross(normalized2, normalized) * (thickness / 2f); + GL.Vertex(start - vector); + GL.Vertex(start + vector); + GL.Vertex(end + vector); + GL.Vertex(end - vector); + } +} diff --git a/ch.sycoforge.Decal.Demo/PathAgent.cs b/ch.sycoforge.Decal.Demo/PathAgent.cs new file mode 100644 index 0000000..27908d7 --- /dev/null +++ b/ch.sycoforge.Decal.Demo/PathAgent.cs @@ -0,0 +1,106 @@ +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.AI; + +namespace ch.sycoforge.Decal.Demo; + +[RequireComponent(typeof(NavMeshAgent))] +[RequireComponent(typeof(LineRenderer))] +public class PathAgent : MonoBehaviour +{ + public float PathThickness = 1f; + + [Tooltip("Distance from the ground.")] + public float NormalPathOffset; + + [Tooltip("Max radius between segments.")] + [Range(0.001f, 0.5f)] + public float Radius = 0.25f; + + [Tooltip("Discard segments when their angle is smaller than this value.")] + public float AngleThreshold = 5f; + + public bool DrawGizmos; + + public EasyDecal TargetAimDecal; + + public GameObject TargetPointDecalPrefab; + + private List<Vector3> path = new List<Vector3>(); + + private NavMeshAgent agent; + + private LineRenderer lineRenderer; + + private Vector3 decalOffset = Vector3.up * 0.5f; + + private const int MAXDISTANCE = 50; + + private void Start() + { + TargetAimDecal.gameObject.SetActive(value: false); + agent = GetComponent<NavMeshAgent>(); + lineRenderer = GetComponent<LineRenderer>(); + } + + private void Update() + { + Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); + CreatePath(ray); + SetTarget(ray); + } + + private void SetTarget(Ray mouseRay) + { + if (Input.GetMouseButtonUp(0) && Physics.Raycast(mouseRay, out var hitInfo, 50f)) + { + agent.SetDestination(hitInfo.point); + EasyDecal.ProjectAt(TargetPointDecalPrefab, hitInfo.collider.gameObject, hitInfo.point + decalOffset, Quaternion.identity); + } + } + + private void CreatePath(Ray mouseRay) + { + if (Physics.Raycast(mouseRay, out var hitInfo, 50f)) + { + Vector3 position = base.transform.position; + Vector3 point = hitInfo.point; + path.Clear(); + NavMeshPath navMeshPath = new NavMeshPath(); + if (NavMesh.CalculatePath(position, point, -1, navMeshPath) && navMeshPath.status == NavMeshPathStatus.PathComplete) + { + int num = navMeshPath.corners.Length; + Vector3 vector = base.transform.up; + for (int i = 0; i < num; i++) + { + if (i > 0 && NormalPathOffset > 0f && Physics.Raycast(navMeshPath.corners[i], Vector3.down, out var _, NormalPathOffset * 10f)) + { + vector = hitInfo.normal; + } + Vector3 item = navMeshPath.corners[i] + vector * NormalPathOffset; + path.Add(item); + } + Vector3[] array = BezierUtil.InterpolatePath(path, 10, Radius, AngleThreshold).ToArray(); + lineRenderer.SetVertexCount(array.Length); + lineRenderer.SetPositions(array); + TargetAimDecal.gameObject.SetActive(value: true); + TargetAimDecal.gameObject.transform.position = navMeshPath.corners[num - 1] + decalOffset; + return; + } + } + TargetAimDecal.gameObject.SetActive(value: false); + } + + private void OnDrawGizmos() + { + if (!DrawGizmos) + { + return; + } + Gizmos.color = Color.red; + foreach (Vector3 item in path) + { + Gizmos.DrawSphere(item, 0.05f); + } + } +} diff --git a/ch.sycoforge.Decal.Demo/ProxyRegister.cs b/ch.sycoforge.Decal.Demo/ProxyRegister.cs new file mode 100644 index 0000000..1a171bf --- /dev/null +++ b/ch.sycoforge.Decal.Demo/ProxyRegister.cs @@ -0,0 +1,13 @@ +using UnityEngine; + +namespace ch.sycoforge.Decal.Demo; + +public class ProxyRegister : MonoBehaviour +{ + public StaticProxyCollection ProxyCollection; + + private void Start() + { + EasyDecal.SetStaticProxyCollection(ProxyCollection); + } +} diff --git a/ch.sycoforge.Decal.Demo/Sinoid.cs b/ch.sycoforge.Decal.Demo/Sinoid.cs new file mode 100644 index 0000000..38ee213 --- /dev/null +++ b/ch.sycoforge.Decal.Demo/Sinoid.cs @@ -0,0 +1,29 @@ +using System; +using UnityEngine; + +namespace ch.sycoforge.Decal.Demo; + +public class Sinoid : MonoBehaviour +{ + public float AngularVelocity = 2f; + + public float SineFreq = 0.2f; + + public float Amplitude = 0.25f; + + private float accuTime; + + private Vector3 startPos; + + private void Start() + { + startPos = base.transform.position; + } + + private void Update() + { + accuTime += Time.deltaTime; + base.transform.position = startPos + Vector3.up * Amplitude * Mathf.Sin(accuTime * 2f * (float)Math.PI * SineFreq); + base.transform.Rotate((Vector3.up + Vector3.forward) * AngularVelocity * Time.deltaTime); + } +} |