-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
60,700 additions
and
325 deletions.
There are no files selected for viewing
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
/** | ||
* Inspired from https://stackoverflow.com/a/36046727 | ||
* | ||
* Modified to work directly with Float32Arrays and to match | ||
* the Python API. | ||
* | ||
*/ | ||
var base64 = {}; | ||
|
||
base64.b64encode = function (array) { | ||
|
||
array = new Uint8Array(array.buffer); | ||
|
||
return btoa(String.fromCharCode.apply(null, array)); | ||
|
||
}; | ||
|
||
base64.b64decode = function (str) { | ||
|
||
array = atob(str).split('').map(function (c) { return c.charCodeAt(0); }); | ||
|
||
return new Float32Array(new Uint8Array(array).buffer); | ||
|
||
}; | ||
/** | ||
* Inspired from https://stackoverflow.com/a/36046727 | ||
* | ||
* Modified to work directly with Float32Arrays and to match | ||
* the Python API. | ||
* | ||
*/ | ||
var base64 = {}; | ||
|
||
base64.b64encode = function (array) { | ||
|
||
array = new Uint8Array(array.buffer); | ||
|
||
return btoa(String.fromCharCode.apply(null, array)); | ||
|
||
}; | ||
|
||
base64.b64decode = function (str) { | ||
|
||
array = atob(str).split('').map(function (c) { return c.charCodeAt(0); }); | ||
|
||
return new Float32Array(new Uint8Array(array).buffer); | ||
|
||
}; |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,106 +1,163 @@ | ||
import numpy as np | ||
import base64 | ||
|
||
VERTICES = np.array([0.,0.,0., 0.,1.,0., 1.,0.,0.], dtype=np.float32) | ||
INDICES = np.array([0, 1, 2], dtype=np.ushort) | ||
|
||
HOWMANY = 3 | ||
MAX_X = 1 | ||
MAX_Y = 1 | ||
MAX_Z = 0 | ||
MIN_X = 0 | ||
MIN_Y = 0 | ||
MIN_Z = 0 | ||
MAX = 2 | ||
MIN = 0 | ||
|
||
HOWMANYBYTES_V = VERTICES.nbytes | ||
HOWMANYBYTES_I = INDICES.nbytes | ||
|
||
B64_VERTICES = base64.b64encode(VERTICES) | ||
B64_INDICES = base64.b64encode(INDICES) | ||
|
||
gltf = { | ||
"asset": { | ||
"version": "2.0", | ||
"generator": "CS460 Magic Fingers" | ||
}, | ||
|
||
"accessors": [ | ||
{ | ||
"bufferView": 0, | ||
"byteOffset": 0, | ||
"componentType": 5126, | ||
"count": HOWMANY, | ||
"type": "VEC3", | ||
"max": [MAX_X, MAX_Y, MAX_Z], | ||
"min": [MIN_X, MIN_Y, MIN_Z] | ||
}, | ||
{ | ||
"bufferView": 1, | ||
"byteOffset": 0, | ||
"componentType": 5123, | ||
"count": HOWMANY, | ||
"type": "SCALAR", | ||
"max": [MAX], | ||
"min": [MIN] | ||
} | ||
], | ||
|
||
"bufferViews": [ | ||
{ | ||
"buffer": 0, | ||
"byteOffset": 0, | ||
"byteLength": HOWMANYBYTES_V, | ||
"target": 34962 | ||
}, | ||
{ | ||
"buffer": 1, | ||
"byteOffset": 0, | ||
"byteLength": HOWMANYBYTES_I, | ||
"target": 34963 | ||
} | ||
], | ||
|
||
"buffers": [ | ||
{ | ||
"uri": "data:application/octet-stream;base64,"+str(B64_VERTICES, 'utf-8'), | ||
"byteLength": HOWMANYBYTES_V | ||
}, | ||
{ | ||
"uri": "data:application/octet-stream;base64,"+str(B64_INDICES, 'utf-8'), | ||
"byteLength": HOWMANYBYTES_I | ||
} | ||
], | ||
|
||
"meshes": [ | ||
{ | ||
"primitives": [{ | ||
"mode": 4, | ||
"attributes": { | ||
"POSITION": 0 | ||
}, | ||
"indices": 1 | ||
}] | ||
} | ||
], | ||
|
||
"nodes": [ | ||
{ | ||
"mesh": 0 | ||
} | ||
], | ||
|
||
"scenes": [ | ||
{ | ||
"nodes": [ | ||
0 | ||
] | ||
} | ||
], | ||
|
||
"scene": 0 | ||
} | ||
|
||
print ( str(gltf).replace("'", '"') ) # we need double quotes instead of single quotes | ||
|
||
import numpy as np | ||
import base64 | ||
|
||
with open("Dragon 2.5_ply.ply", 'r') as f: | ||
lines = f.readlines() | ||
|
||
counter = 0 | ||
vertices_coming = False | ||
faces_coming = False | ||
|
||
## get the infomation of vertices and faces | ||
for i,l in enumerate(lines): | ||
|
||
if l.startswith('element vertex'): | ||
vertexcounter = int(l.split(' ')[-1].strip()) | ||
elif l.startswith('element face'): | ||
facecounter = int(l.split(' ')[-1].strip()) | ||
elif l.startswith('end_header'): | ||
break | ||
|
||
vertex_data = lines[i+1:i+vertexcounter+1] | ||
face_data = lines[i+vertexcounter+1:] | ||
|
||
## create the VERTICES array | ||
VERTICES = [] | ||
MAX_X = -np.inf | ||
MAX_Y = -np.inf | ||
MAX_Z = -np.inf | ||
MIN_X = np.inf | ||
MIN_Y = np.inf | ||
MIN_Z = np.inf | ||
|
||
## parse all vertices and find the required fields | ||
for v in vertex_data: | ||
v = [float(v.strip()) for v in v.strip().split(' ')] | ||
|
||
MAX_X = max(MAX_X, v[0]) | ||
MAX_Y = max(MAX_Y, v[1]) | ||
MAX_Z = max(MAX_Z, v[2]) | ||
MIN_X = min(MIN_X, v[0]) | ||
MIN_Y = min(MIN_Y, v[1]) | ||
MIN_Z = min(MIN_Z, v[2]) | ||
|
||
VERTICES += v | ||
|
||
## parse all indices and find the required fields | ||
INDICES = [] | ||
MAX = -np.inf | ||
MIN = np.inf | ||
for i in face_data: | ||
i = [int(i) for i in i.strip().split(' ')[1:]] | ||
INDICES += i | ||
|
||
MAX = max(MAX, i[0]) | ||
MAX = max(MAX, i[1]) | ||
MAX = max(MAX, i[2]) | ||
MIN = min(MIN, i[0]) | ||
MIN = min(MIN, i[1]) | ||
MIN = min(MIN, i[2]) | ||
|
||
VERTICES = np.array(VERTICES, dtype=np.float32) | ||
INDICES = np.array(INDICES, dtype=np.ushort) | ||
|
||
|
||
HOWMANY_V = len(VERTICES) / 3 # because its the number of VEC3 groups | ||
HOWMANY_I = len(INDICES) | ||
|
||
HOWMANYBYTES_V = VERTICES.nbytes | ||
HOWMANYBYTES_I = INDICES.nbytes | ||
|
||
## base 64 code | ||
B64_VERTICES = base64.b64encode(VERTICES) | ||
B64_INDICES = base64.b64encode(INDICES) | ||
|
||
## generate the glTF JSON code | ||
gltf = { | ||
"asset": { | ||
"version": "2.0", | ||
"generator": "CS460 Magic Fingers" | ||
}, | ||
|
||
"accessors": [ | ||
{ | ||
"bufferView": 0, | ||
"byteOffset": 0, | ||
"componentType": 5126, | ||
"count": HOWMANY_V, | ||
"type": "VEC3", | ||
"max": [MAX_X, MAX_Y, MAX_Z], | ||
"min": [MIN_X, MIN_Y, MIN_Z] | ||
}, | ||
{ | ||
"bufferView": 1, | ||
"byteOffset": 0, | ||
"componentType": 5123, | ||
"count": HOWMANY_I, | ||
"type": "SCALAR", | ||
"max": [MAX], | ||
"min": [MIN] | ||
} | ||
], | ||
|
||
"bufferViews": [ | ||
{ | ||
"buffer": 0, | ||
"byteOffset": 0, | ||
"byteLength": HOWMANYBYTES_V, | ||
"target": 34962 | ||
}, | ||
{ | ||
"buffer": 1, | ||
"byteOffset": 0, | ||
"byteLength": HOWMANYBYTES_I, | ||
"target": 34963 | ||
} | ||
], | ||
|
||
"buffers": [ | ||
{ | ||
"uri": "data:application/octet-stream;base64,"+str(B64_VERTICES), | ||
"byteLength": HOWMANYBYTES_V | ||
}, | ||
{ | ||
"uri": "data:application/octet-stream;base64,"+str(B64_INDICES), | ||
"byteLength": HOWMANYBYTES_I | ||
} | ||
], | ||
|
||
"meshes": [ | ||
{ | ||
"primitives": [{ | ||
"mode": 4, | ||
"attributes": { | ||
"POSITION": 0 | ||
}, | ||
"indices": 1 | ||
}] | ||
} | ||
], | ||
|
||
"nodes": [ | ||
{ | ||
"mesh": 0 | ||
} | ||
], | ||
|
||
"scenes": [ | ||
{ | ||
"nodes": [ | ||
0 | ||
] | ||
} | ||
], | ||
|
||
"scene": 0 | ||
} | ||
|
||
|
||
print ( str(gltf).replace("'", '"') ) # we need double quotes instead of single quotes | ||
|
||
text_file = open("dragon.gltf", "w") | ||
n = text_file.write(str(gltf).replace("'", '"')) | ||
text_file.close() |
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.