123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Folding : MonoBehaviour
- {
- [Header(" Gizmos Settings ")]
- public bool showGizmos;
- [Header(" Settings ")]
- [SerializeField] private RotationAxis rotationAxis;
- [SerializeField] private float foldingAngle;
- [SerializeField] private Folding[] requiredFoldings;
- private bool folded;
- [Header(" Mesh Settings ")]
- List<int> backFoldedVerticesIndices = new List<int>();
- List<int> frontFoldedVerticesIndices = new List<int>();
- public RotationAxis GetRotationAxis()
- {
- return rotationAxis;
- }
- public void SetRotationAxis(RotationAxis rt)
- {
- rotationAxis = rt;
- }
- public float GetRotationAngle()
- {
- return foldingAngle;
- }
- public Folding[] GetRequiredFoldings()
- {
- return requiredFoldings;
- }
- public void SetFoldedState(bool folded)
- {
- if (folded)
- {
- Debug.Log($"Folding {gameObject.name} folded");
- }
- for(int i = 0; i < requiredFoldings.Length; i++)
- {
- requiredFoldings[i].gameObject.SetActive(folded);
- }
- this.folded = folded;
- }
- public bool IsFolded()
- {
- return folded;
- }
- public Plane GetFoldingPlane()
- {
- return rotationAxis.AsPlane();
- }
- public Vector3 GetFoldingPosition()
- {
- return rotationAxis.position;
- }
- public void SetBackFoldedVerticesIndices(int[] foldedVerticesIndices)
- {
- backFoldedVerticesIndices.AddRange(foldedVerticesIndices);
- }
- public void SetFrontFoldedVerticesIndices(int[] foldedVerticesIndices)
- {
- frontFoldedVerticesIndices.AddRange(foldedVerticesIndices);
- }
- public int[] GetBackFoldedVerticesIndices()
- {
- return backFoldedVerticesIndices.ToArray();
- }
- public int[] GetFrontFoldedVerticesIndices()
- {
- return frontFoldedVerticesIndices.ToArray();
- }
- public void ClearFoldedVertices()
- {
- backFoldedVerticesIndices.Clear();
- frontFoldedVerticesIndices.Clear();
- }
- }
- [System.Serializable]
- public struct RotationAxis
- {
- public Vector3 position;
- public float angle;
- public Vector3 AsVector()
- {
- Vector3[] fourPlanePoints = PlaneUtils.GetFourPlanePoints(position, angle);
- return fourPlanePoints[2] - fourPlanePoints[1];
- }
- public Plane AsPlane()
- {
- Vector3[] fourPlanePoints = PlaneUtils.GetFourPlanePoints(position, angle);
- return new Plane(fourPlanePoints[0], fourPlanePoints[2], fourPlanePoints[1]);
- }
- public Vector3[] AsLine()
- {
- Vector3[] fourPlanePoints = PlaneUtils.GetFourPlanePoints(position, angle);
- return new Vector3[] { fourPlanePoints[2], fourPlanePoints[1] };
- }
- }
|