123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507 |
-
- using System.Collections.Generic;
- using UnityEngine;
- using System.Threading;
- using System.Runtime.InteropServices;
- #if ZED_URP || ZED_HDRP
- using UnityEngine.Rendering;
- #endif
- public class ZEDPlaneGameObject : MonoBehaviour
- {
-
-
-
-
- public enum PLANE_TYPE
- {
-
-
-
- FLOOR,
-
-
-
- HIT_HORIZONTAL,
-
-
-
- HIT_VERTICAL,
-
-
-
- HIT_UNKNOWN
- };
-
-
-
-
- [StructLayout(LayoutKind.Sequential)]
- public struct PlaneData
- {
-
-
-
- public sl.ERROR_CODE ErrorCode;
-
-
-
- public ZEDPlaneGameObject.PLANE_TYPE Type;
-
-
-
- public Vector3 PlaneNormal;
-
-
-
- public Vector3 PlaneCenter;
-
-
-
- public Vector3 PlaneTransformPosition;
-
-
-
- public Quaternion PlaneTransformOrientation;
-
-
-
- public Vector4 PlaneEquation;
-
-
-
- public Vector2 Extents;
-
-
-
- public int BoundsSize;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
-
- public Vector3[] Bounds;
- }
-
-
-
-
- public ZEDPlaneGameObject.PlaneData planeData;
-
-
-
- private bool isCreated = false;
-
-
-
- public bool IsCreated
- {
- get { return isCreated; }
- }
-
-
-
- public Vector3 worldNormal { get; private set; }
-
-
-
- public Vector3 worldCenter
- {
- get
- {
- return gameObject.transform.position;
- }
- }
-
-
-
- MeshFilter mfilter;
-
-
-
- MeshRenderer rend;
-
-
-
-
- private bool lastRenderState = true;
-
-
-
-
-
-
-
- private void SetComponents(PlaneData plane, Vector3[] vertices, int[] triangles, Material rendermaterial)
- {
-
- mfilter = gameObject.GetComponent<MeshFilter>();
- if (mfilter == null)
- mfilter = gameObject.AddComponent<MeshFilter>();
-
- int highestvertindex = 0;
- for (int i = 0; i < triangles.Length; i++)
- {
- if (triangles[i] > highestvertindex) highestvertindex = triangles[i];
- }
- System.Array.Resize(ref vertices, highestvertindex + 1);
-
- Vector2[] uvs = new Vector2[vertices.Length];
- Quaternion rotatetobackward = Quaternion.FromToRotation(worldNormal, Vector3.back);
- for (int i = 0; i < vertices.Length; i++)
- {
- Vector3 upwardcoords = rotatetobackward * (vertices[i] + worldCenter);
- uvs[i] = new Vector2(upwardcoords.x, upwardcoords.y);
- }
-
- mfilter.mesh.Clear();
- mfilter.mesh.vertices = vertices;
- mfilter.mesh.triangles = triangles;
- mfilter.mesh.uv = uvs;
- mfilter.mesh.RecalculateNormals();
- mfilter.mesh.RecalculateBounds();
-
- rend = gameObject.GetComponent<MeshRenderer>();
- if (rend == null)
- rend = gameObject.AddComponent<MeshRenderer>();
- rend.material = rendermaterial;
-
- rend.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
- rend.receiveShadows = false;
- rend.enabled = true;
- rend.lightProbeUsage = UnityEngine.Rendering.LightProbeUsage.Off;
- rend.reflectionProbeUsage = UnityEngine.Rendering.ReflectionProbeUsage.Off;
-
- MeshCollider mc = gameObject.GetComponent<MeshCollider>();
- if (mc == null)
- mc = gameObject.AddComponent<MeshCollider>();
-
- mc.sharedMesh = mfilter.mesh;
- lastRenderState = true;
- }
-
-
-
-
-
-
-
-
- public void Create(Camera cam, PlaneData plane, Vector3[] vertices, int[] triangles, int opt_count)
- {
- Create(cam, plane, vertices, triangles, opt_count, GetDefaultMaterial(plane.Type));
- }
-
-
-
-
-
-
-
-
-
- public void Create(Camera cam, PlaneData plane, Vector3[] vertices, int[] triangles, int opt_count, Material rendermaterial)
- {
-
- planeData.ErrorCode = plane.ErrorCode;
- planeData.Type = plane.Type;
- planeData.PlaneNormal = plane.PlaneNormal;
- planeData.PlaneCenter = plane.PlaneCenter;
- planeData.PlaneTransformPosition = plane.PlaneTransformPosition;
- planeData.PlaneTransformOrientation = plane.PlaneTransformOrientation;
- planeData.PlaneEquation = plane.PlaneEquation;
- planeData.Extents = plane.Extents;
- planeData.BoundsSize = plane.BoundsSize;
- planeData.Bounds = new Vector3[plane.BoundsSize];
- System.Array.Copy(plane.Bounds, planeData.Bounds, plane.BoundsSize);
-
- Camera leftCamera = cam;
- worldNormal = cam.transform.TransformDirection(planeData.PlaneNormal);
-
- gameObject.AddComponent<MeshCollider>().sharedMesh = null;
- if (plane.Type != PLANE_TYPE.FLOOR)
- gameObject.name = "Hit Plane " + opt_count;
- else
- gameObject.name = "Floor Plane";
- gameObject.layer = 12;
- SetComponents(plane, vertices, triangles, rendermaterial);
- isCreated = true;
-
- #if !ZED_URP && !ZED_HDRP
- Camera.onPreCull += PreCull;
- Camera.onPostRender += PostRender;
- #else
- RenderPipelineManager.beginFrameRendering += SRPFrameBegin;
- #endif
- }
-
-
-
-
-
-
-
-
- public bool UpdateFloorPlane(bool force, ZEDPlaneGameObject.PlaneData plane, Vector3[] vertices, int[] triangles, Material rendermaterial = null)
- {
- bool need_update = false;
- if (!force)
- {
- if (!gameObject.GetComponent<MeshRenderer>().isVisible)
- need_update = true;
- else
- {
- Mesh tmp = new Mesh();
- tmp.vertices = vertices;
- tmp.SetTriangles(triangles, 0, false);
- tmp.RecalculateBounds();
- Bounds tmpNBnds = tmp.bounds;
- MeshFilter mf = gameObject.GetComponent<MeshFilter>();
- if ((tmpNBnds.Contains(mf.mesh.bounds.min) && tmpNBnds.Contains(mf.mesh.bounds.max)) || (tmpNBnds.size.x * tmpNBnds.size.y > 1.1 * mf.mesh.bounds.size.x * mf.mesh.bounds.size.y))
- need_update = true;
- tmp.Clear();
- }
- }
- else
- need_update = true;
- if (need_update)
- SetComponents(plane, vertices, triangles, gameObject.GetComponent<MeshRenderer>().material);
- MeshRenderer mr = gameObject.GetComponent<MeshRenderer>();
- if (mr != null)
- {
- if (rendermaterial != null)
- {
- mr.material = rendermaterial;
- }
- else
- {
- mr.material = GetDefaultMaterial(plane.Type);
- }
- }
- return need_update;
- }
-
-
-
-
- public void SetPhysics(bool c)
- {
- MeshCollider mc = gameObject.GetComponent<MeshCollider>();
- if (mc != null)
- mc.enabled = c;
- }
-
-
-
-
- public void SetVisible(bool c)
- {
- MeshRenderer mr = gameObject.GetComponent<MeshRenderer>();
- if (mr != null)
- mr.enabled = c;
- }
-
-
-
-
- public Vector2 GetScale()
- {
- return planeData.Extents;
- }
-
-
-
-
- public Bounds GetBounds()
- {
- MeshFilter mf = gameObject.GetComponent<MeshFilter>();
- if (mf != null)
- return mf.mesh.bounds;
- else
- return new Bounds(gameObject.transform.position, Vector3.zero);
- }
-
-
-
-
-
- public float getMinimumDistanceToBoundaries(Camera cam, Vector3 worldPosition, out Vector3 minimumBoundsPosition)
- {
- Camera leftCamera = cam;
- float minimal_distance = ZEDSupportFunctions.DistancePointLine(worldPosition, leftCamera.transform.TransformPoint(planeData.Bounds[0]), leftCamera.transform.TransformPoint(planeData.Bounds[1]));
- Vector3 BestFoundPoint = new Vector3(0.0f, 0.0f, 0.0f);
- if (planeData.BoundsSize > 2)
- {
- for (int i = 1; i < planeData.BoundsSize - 1; i++)
- {
- float currentDistance = ZEDSupportFunctions.DistancePointLine(worldPosition, leftCamera.transform.TransformPoint(planeData.Bounds[i]), leftCamera.transform.TransformPoint(planeData.Bounds[i + 1]));
- if (currentDistance < minimal_distance)
- {
- minimal_distance = currentDistance;
- BestFoundPoint = ZEDSupportFunctions.ProjectPointLine(worldPosition, leftCamera.transform.TransformPoint(planeData.Bounds[i]), leftCamera.transform.TransformPoint(planeData.Bounds[i + 1]));
- }
- }
- }
- minimumBoundsPosition = BestFoundPoint;
- return minimal_distance;
- }
-
-
-
-
- public bool IsFloorPlaneVisible()
- {
- return gameObject.GetComponent<MeshRenderer>().isVisible;
- }
-
-
-
-
-
-
- private Material GetDefaultMaterial(PLANE_TYPE type)
- {
-
- Material defaultmaterial = new Material(Resources.Load("Materials/PlaneDetection/Mat_ZED_Geometry_WirePlane") as Material);
- switch (type)
- {
- case PLANE_TYPE.FLOOR:
-
- defaultmaterial.SetColor("_WireColor", new Color(44.0f / 255.0f, 157.0f / 255.0f, 222.0f / 255.0f, 174.0f / 255.0f));
- break;
- case PLANE_TYPE.HIT_HORIZONTAL:
- case PLANE_TYPE.HIT_VERTICAL:
- case PLANE_TYPE.HIT_UNKNOWN:
-
- defaultmaterial.SetColor("_WireColor", new Color(221.0f / 255.0f, 20.0f / 255.0f, 149.0f / 255.0f, 174.0f / 255.0f));
- break;
- default:
-
- defaultmaterial.SetColor("_WireColor", new Color(1, 1, 1, 174.0f / 255.0f));
- break;
- }
- return defaultmaterial;
- }
- #if! ZED_URP && !ZED_HDRP
-
-
-
-
- private void PreCull(Camera currentcam)
- {
- lastRenderState = rend.enabled;
- if (!rend.enabled) return;
- if (currentcam.name.ToLower().Contains("scenecamera"))
- {
- rend.enabled = ZEDPlaneDetectionManager.isSceneDisplay;
- }
- else
- {
- rend.enabled = ZEDPlaneDetectionManager.isGameDisplay;
- }
- }
-
-
-
-
- private void PostRender(Camera currentcam)
- {
- rend.enabled = lastRenderState;
- }
- #else
- private void SRPFrameBegin(ScriptableRenderContext context, Camera[] rendercams)
- {
- rend.enabled = false;
- foreach (Camera rendcam in rendercams)
- {
- if (rendcam.name.ToLower().Contains("scenecamera"))
- {
- if (ZEDPlaneDetectionManager.isSceneDisplay) DrawPlane(rendcam);
- }
- else if (ZEDPlaneDetectionManager.isGameDisplay) DrawPlane(rendcam);
- }
- }
- private void DrawPlane(Camera drawcam)
- {
- Matrix4x4 canvastrs = Matrix4x4.TRS(transform.position, transform.rotation, transform.localScale);
- Graphics.DrawMesh(mfilter.mesh, canvastrs, rend.material, 0, drawcam);
- }
- #endif
- private void OnDestroy()
- {
- #if !ZED_URP && !ZED_HDRP
- Camera.onPreCull -= PreCull;
- Camera.onPostRender -= PostRender;
- #else
- RenderPipelineManager.beginFrameRendering -= SRPFrameBegin;
- #endif
- }
- }
|