Skip to content
Snippets Groups Projects
PrismGenerator.cs 1.6 KiB
Newer Older
  • Learn to ignore specific revisions
  • using System.Linq;
    using UnityEngine;
    
    public class PrismGenerator : PlaneGenerator
    {
        #region InspectorVariables
        [Header("Plane values")]
        [SerializeField]
        private Vector3 _offset;
        public Vector3 Offset
        {
            get => _offset;
            set
            {
                _offset = value;
                GenerateShapeForAll();
            }
        }
    
        #endregion InspectorVariables
    
        #region Implementation
        protected override (Vector3[] vertices, int[] triangles) GenerateTopology()
        {
            (Vector3[] vertices, int[] triangles) plane = CreatePlane(_vertices);
    
            Vector3[] normals = CreateMesh(plane).normals;
    
                    ( // Topology for top
                        plane.vertices
                            .Zip(normals, (v, n) =>
                                v + Quaternion.FromToRotation(Vector3.up, n) *
                                    (ExpandBothDirections ? (Offset * 0.5f) : Offset))
                            .ToArray(),
                        plane.triangles
                    ),
                    ( // Topology for bottom
                        ExpandBothDirections
                            ? plane.vertices
                                .Zip(normals, (v, n) =>
                                    v + Quaternion.FromToRotation(Vector3.up, n) *
                                        -(Offset * 0.5f))
                                .ToArray()
                            : plane.vertices,
                        plane.triangles.Reverse().ToArray()
                    ),
    
                    null, null
                );
        }
        #endregion Implementation
    }