diff --git a/Assets/Scripts/MeshGenerator/HoledPlaneGenerator.cs b/Assets/Scripts/MeshGenerator/HoledPlaneGenerator.cs new file mode 100644 index 0000000000000000000000000000000000000000..7fc04f48fc6ceda52ee994b95c80254f6ff456f1 --- /dev/null +++ b/Assets/Scripts/MeshGenerator/HoledPlaneGenerator.cs @@ -0,0 +1,64 @@ +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using UnityEngine; + +public class HoledPlaneGenerator : ShapeGenerator +{ + #region InspectorVariables + [Header("Plane values")] + public Vector2 planeSize = new(100, 100); + public Vector2 holeLocation = new(50, 50); + public float holeRadius = 1f; + + [Header("Technical")] + [Range(4, 1000, order = 4)] public int holeSideCount = 500; + + [Header("Parts")] + public MeshFilter planeMesh; + #endregion InspectorVariables + + #region Implementation + protected override void GenerateShape() + { + AssignMesh(planeMesh, CreateHoledPlane(planeSize, holeLocation, holeRadius, holeSideCount)); + } + + private static (Vector3[] vertices, int[] triangles) CreateHoledPlane(Vector2 planeSize, Vector2 holeLocation, float holeRadius, int holeSideCount) + { + var corners = new Vector3[4] { + new Vector3( planeSize.x/2, 0, planeSize.y/2), + new Vector3( planeSize.x/2, 0, -planeSize.y/2), + new Vector3(-planeSize.x/2, 0, -planeSize.y/2), + new Vector3(-planeSize.x/2, 0, planeSize.y/2) }; + var circlePoints = GetCirclePoints(holeRadius, holeSideCount, new Vector3(holeLocation.x, 0, holeLocation.y)); + Vector3[] vertices = circlePoints.Union(corners).ToArray(); + + int[] triangles = new int[vertices.Length * 3]; + int cpCount = circlePoints.Length; + int quarter = Mathf.FloorToInt((float)cpCount / 4); + int rest = cpCount % 4; + + int start = 0; + for (int corner = 0; corner < 4; corner++) + { + int cornerIdx = cpCount + corner; + int end = start + (rest-- > 0 ? quarter + 1 : quarter); + for (int i = start; i < end; i++) + { + triangles[i * 3 + 0] = (i + 1) % cpCount; + triangles[i * 3 + 1] = i; + triangles[i * 3 + 2] = cornerIdx; + } + triangles[triangles.Length - ((corner+1)*3) + 0] = end % cpCount; + triangles[triangles.Length - ((corner+1)*3) + 1] = cornerIdx; + triangles[triangles.Length - ((corner+1)*3) + 2] = cpCount + (corner + 1) % 4; + + start = end; + } + + + return (vertices, triangles); + } + #endregion Implementation +} \ No newline at end of file diff --git a/Assets/Scripts/MeshGenerator/HoledPlaneGenerator.cs.meta b/Assets/Scripts/MeshGenerator/HoledPlaneGenerator.cs.meta new file mode 100644 index 0000000000000000000000000000000000000000..07e5b068550e07a1eec50bf528fe0b8e793f00d3 --- /dev/null +++ b/Assets/Scripts/MeshGenerator/HoledPlaneGenerator.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9965b0da5ebc17848beccc46e84bd114 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: