Skip to content
Snippets Groups Projects
Commit a194612c authored by Tobias Schöner's avatar Tobias Schöner
Browse files

feat: add generator for holed plane

parent 867ef8d0
Branches
No related tags found
No related merge requests found
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
fileFormatVersion: 2
guid: 9965b0da5ebc17848beccc46e84bd114
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment