Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add triangle mesh support #53

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 54 additions & 20 deletions Assets/Pcx/Editor/PlyImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class PlyImporter : ScriptedImporter
{
#region ScriptedImporter implementation

public enum ContainerType { Mesh, ComputeBuffer, Texture }
public enum ContainerType { Mesh, ComputeBuffer, Texture }

[SerializeField] ContainerType _containerType = ContainerType.Mesh;

Expand Down Expand Up @@ -90,7 +90,8 @@ static Material GetDefaultMaterial()

#region Internal data structure

enum DataProperty {
enum DataProperty
{
Invalid,
R8, G8, B8, A8,
R16, G16, B16, A16,
Expand Down Expand Up @@ -128,18 +129,21 @@ static int GetPropertySize(DataProperty p)
class DataHeader
{
public List<DataProperty> properties = new List<DataProperty>();
public int vertexCount = -1;
public int vertexCount = 0;
public int triangleCount = 0;
}

class DataBody
{
public List<Vector3> vertices;
public List<Color32> colors;
public List<int> triangles;

public DataBody(int vertexCount)
public DataBody(int vertexCount, int triangleCount)
{
vertices = new List<Vector3>(vertexCount);
colors = new List<Color32>(vertexCount);
triangles = new List<int>(triangleCount);
}

public void AddPoint(
Expand All @@ -150,6 +154,13 @@ public void AddPoint(
vertices.Add(new Vector3(x, y, z));
colors.Add(new Color32(r, g, b, a));
}

public void AddTriangle(int v1, int v2, int v3)
{
triangles.Add(v1);
triangles.Add(v2);
triangles.Add(v3);
}
}

#endregion
Expand All @@ -172,11 +183,17 @@ Mesh ImportAsMesh(string path)

mesh.SetVertices(body.vertices);
mesh.SetColors(body.colors);

mesh.SetIndices(
Enumerable.Range(0, header.vertexCount).ToArray(),
MeshTopology.Points, 0
);
if (body.triangles.Count > 0)
{
mesh.SetTriangles(body.triangles, 0);
}
else
{
mesh.SetIndices(
Enumerable.Range(0, header.vertexCount).ToArray(),
MeshTopology.Points, 0
);
}

mesh.UploadMeshData(true);
return mesh;
Expand Down Expand Up @@ -246,7 +263,7 @@ DataHeader ReadDataHeader(StreamReader reader)
"Should be binary/little endian.");

// Read header contents.
for (var skip = false;;)
for (var skip = false; ;)
{
// Read a line and split it with white space.
line = reader.ReadLine();
Expand All @@ -262,9 +279,14 @@ DataHeader ReadDataHeader(StreamReader reader)
data.vertexCount = Convert.ToInt32(col[2]);
skip = false;
}
else if (col[1] == "face")
{
data.triangleCount = Convert.ToInt32(col[2]);
skip = true;
}
else
{
// Don't read elements other than vertices.
// Don't read elements other than vertices or triangles.
skip = true;
}
}
Expand All @@ -279,13 +301,13 @@ DataHeader ReadDataHeader(StreamReader reader)
// Parse the property name entry.
switch (col[2])
{
case "red" : prop = DataProperty.R8; break;
case "red": prop = DataProperty.R8; break;
case "green": prop = DataProperty.G8; break;
case "blue" : prop = DataProperty.B8; break;
case "blue": prop = DataProperty.B8; break;
case "alpha": prop = DataProperty.A8; break;
case "x" : prop = DataProperty.SingleX; break;
case "y" : prop = DataProperty.SingleY; break;
case "z" : prop = DataProperty.SingleZ; break;
case "x": prop = DataProperty.SingleX; break;
case "y": prop = DataProperty.SingleY; break;
case "z": prop = DataProperty.SingleZ; break;
}

// Check the property type.
Expand All @@ -311,15 +333,15 @@ DataHeader ReadDataHeader(StreamReader reader)
if (GetPropertySize(prop) != 2)
throw new ArgumentException("Invalid property type ('" + line + "').");
}
else if (col[1] == "int" || col[1] == "uint" || col[1] == "float" ||
else if (col[1] == "int" || col[1] == "uint" || col[1] == "float" ||
col[1] == "int32" || col[1] == "uint32" || col[1] == "float32")
{
if (prop == DataProperty.Invalid)
prop = DataProperty.Data32;
else if (GetPropertySize(prop) != 4)
throw new ArgumentException("Invalid property type ('" + line + "').");
}
else if (col[1] == "int64" || col[1] == "uint64" ||
else if (col[1] == "int64" || col[1] == "uint64" ||
col[1] == "double" || col[1] == "float64")
{
switch (prop)
Expand Down Expand Up @@ -349,11 +371,12 @@ DataHeader ReadDataHeader(StreamReader reader)

DataBody ReadDataBody(DataHeader header, BinaryReader reader)
{
var data = new DataBody(header.vertexCount);
var data = new DataBody(header.vertexCount, header.triangleCount);

float x = 0, y = 0, z = 0;
Byte r = 255, g = 255, b = 255, a = 255;

// read vertices xyzrgba
for (var i = 0; i < header.vertexCount; i++)
{
foreach (var prop in header.properties)
Expand Down Expand Up @@ -384,10 +407,21 @@ DataBody ReadDataBody(DataHeader header, BinaryReader reader)
case DataProperty.Data64: reader.BaseStream.Position += 8; break;
}
}

data.AddPoint(x, y, z, r, g, b, a);
}

int v1, v2, v3;

// read triangles
for (var i = 0; i < header.triangleCount; i++)
{
reader.ReadByte(); // number of vertices of the triangle, always 3
v1 = reader.ReadInt32();
v2 = reader.ReadInt32();
v3 = reader.ReadInt32();
data.AddTriangle(v1, v2, v3);
}

return data;
}
}
Expand Down
1 change: 1 addition & 0 deletions Assets/Pcx/Runtime/Shaders/Point.shader
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Shader "Point Cloud/Point"
SubShader
{
Tags { "RenderType"="Opaque" }
Cull Off
Pass
{
CGPROGRAM
Expand Down