Skip to content
This repository has been archived by the owner on Feb 11, 2024. It is now read-only.

Commit

Permalink
Merge branch 'master' of https://github.com/sabresaurus/SabreCSG into…
Browse files Browse the repository at this point in the history
… 2DSEConcave
  • Loading branch information
Henry00IS committed May 12, 2018
2 parents fc0fae4 + b2e072b commit 84d3c5b
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 8 deletions.
9 changes: 4 additions & 5 deletions Scripts/Brushes/PrimitiveBrush.cs
Original file line number Diff line number Diff line change
Expand Up @@ -674,17 +674,16 @@ public override void Invalidate(bool polygonsChanged)

if (polygons != null)
{
List<int> polygonIndices;
BrushFactory.GenerateMeshFromPolygons(polygons, ref renderMesh, out polygonIndices);
// generate a mesh preview for the transparent brushes.
// we also displace the triangles along the normals slightly so we can overlay built geometry
// with semi-transparent geometry and avoid depth fighting.
BrushFactory.GenerateMeshFromPolygonsFast(polygons, ref renderMesh, mode == CSGMode.Add ? 0.001f : -0.001f);
}

if (mode == CSGMode.Subtract)
{
MeshHelper.Invert(ref renderMesh);
}
// Displace the triangles for display along the normals very slightly (this is so we can overlay built
// geometry with semi-transparent geometry and avoid depth fighting)
MeshHelper.Displace(ref renderMesh, 0.001f);

meshFilter.sharedMesh = renderMesh;

Expand Down
3 changes: 1 addition & 2 deletions Scripts/CSGModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,7 @@ public void OnSceneGUI(SceneView sceneView)
SabreCSGResources.GetExcludedMaterial().SetPass(0);

Mesh mesh = new Mesh();
List<int> indices = new List<int>();
BrushFactory.GenerateMeshFromPolygons(excluded.ToArray(), ref mesh, out indices);
BrushFactory.GenerateMeshFromPolygonsFast(excluded.ToArray(), ref mesh, 0.0f);

Graphics.DrawMeshNow(mesh, Vector3.zero, Quaternion.identity);

Expand Down
67 changes: 66 additions & 1 deletion Scripts/Geometry/BrushFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,71 @@ public static void GenerateMeshFromPolygons(Polygon[] polygons, ref Mesh mesh, o
mesh.uv = uvs.ToArray();
mesh.triangles = triangles.ToArray();
}
}

/// <summary>
/// Generates a mesh from the supplied polygons. particularly useful for visualising a
/// brush's polygons on a MeshFilter. This method can optionally displace the vertices along
/// the normal slightly. This is a faster method than <see
/// cref="GenerateMeshFromPolygons(Polygon[], ref Mesh, out List{int})"/> as it doesn't
/// generate optimized indices.
/// </summary>
/// <param name="polygons">The polygons to be triangulated.</param>
/// <param name="mesh">
/// Mesh to be written to. If the existing mesh is null it will be set to a new one,
/// otherwise the existing mesh is cleared.
/// </param>
/// <param name="displacement">The amount of vertex displacement.</param>
public static void GenerateMeshFromPolygonsFast(Polygon[] polygons, ref Mesh mesh, float displacement)
{
if (mesh == null)
{
mesh = new Mesh();
}
mesh.Clear();

int vertexIndex = 0;
int vertexCount = 0;
for (int i = 0; i < polygons.Length; i++)
vertexCount += polygons[i].Vertices.Length * 3;

Vector3[] vertices = new Vector3[vertexCount];
Vector3[] normals = new Vector3[vertexCount];
Vector2[] uvs = new Vector2[vertexCount];
int[] triangles = new int[vertexCount];
int tri = 0;

// Iterate through every polygon and triangulate
int offset = 0;
for (int i = 0; i < polygons.Length; i++)
{
Polygon polygon = polygons[i];
offset += polygon.Vertices.Length;

for (int j = 0; j < polygon.Vertices.Length; j++)
{
Vertex v = polygon.Vertices[j];
vertices[vertexIndex] = v.Position + v.Normal * displacement;
normals[vertexIndex] = v.Normal;
uvs[vertexIndex] = v.UV;
vertexIndex++;
}

// Triangulate the n-sided polygon and allow vertex reuse by using indexed geometry
for (int j = 2; j < polygon.Vertices.Length; j++)
{
int start = offset - polygon.Vertices.Length;
triangles[tri] = start; tri++;
triangles[tri] = start + j - 1; tri++;
triangles[tri] = start + j; tri++;
}
}

// Set the mesh buffers
mesh.vertices = vertices;
mesh.normals = normals;
mesh.uv = uvs;
mesh.triangles = triangles;
}
}
}
#endif

0 comments on commit 84d3c5b

Please sign in to comment.