Created
January 27, 2013 16:42
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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