Skip to content

Instantly share code, notes, and snippets.

@rhulha
Created January 27, 2013 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rhulha/4649183 to your computer and use it in GitHub Desktop.
Save rhulha/4649183 to your computer and use it in GitHub Desktop.
Converts a PLY 3D Model to JavaScript notation that can be used with WebGL
public class ConvertPly2JS {
public static void main(String[] args) throws Exception {
BufferedReader fr = new BufferedReader(new FileReader("C:\\Raytemp\\asteroid.ply"));
String line;
ArrayList<String> vertices = new ArrayList<String>();
ArrayList<String> textureCoords = new ArrayList<String>();
ArrayList<String> normals = new ArrayList<String>();
ArrayList<String> vertexIndices = new ArrayList<String>();
// read header
while ((line = fr.readLine()) != null) {
if (line.equals("end_header")) {
System.out.println(line);
break;
}
}
int vertex_count = 681;
while ((line = fr.readLine()) != null) {
String[] parts = line.split(" ");
String x = parts[0];
String y = parts[1];
String z = parts[2];
String nx = parts[3];
String ny = parts[4];
String nz = parts[5];
String s = parts[6];
String t = parts[7];
vertices.add(x);
vertices.add(y);
vertices.add(z);
normals.add(nx);
normals.add(ny);
normals.add(nz);
textureCoords.add(s);
textureCoords.add(t);
if (--vertex_count <= 0)
break;
}
while ((line = fr.readLine()) != null) {
String[] parts = line.split(" ");
vertexIndices.add(parts[1]);
vertexIndices.add(parts[2]);
vertexIndices.add(parts[3]);
}
printJSArray("vertices", vertices);
printJSArray("normals", normals);
printJSArray("textureCoords", textureCoords);
printJSArray("vertexIndices", vertexIndices);
}
private static void printJSArray(String name, ArrayList<String> array) {
System.out.print("hull."+name+" = [");
System.out.print(array.get(0));
for (int i = 1; i < array.size(); i++) {
System.out.print(","+array.get(i));
}
System.out.println("];");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment