Skip to content

Instantly share code, notes, and snippets.

@KelSolaar
Created July 19, 2018 09:58
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 KelSolaar/fe54fb6ef0f8584880da3f1fa08e8239 to your computer and use it in GitHub Desktop.
Save KelSolaar/fe54fb6ef0f8584880da3f1fa08e8239 to your computer and use it in GitHub Desktop.
Three.js - Helpers
def face_mask(
quad=False,
material=False,
face_uvs=False,
face_vertex_uvs=False,
face_normals=False,
face_vertex_normals=False,
face_colours=False,
face_vertex_colours=False,
):
def update_mask(value, position, attribute):
"""
https://github.com/timoxley/threejs/blob/master/utils/exporters/obj/convert_obj_three.py
"""
if attribute:
mask = 1 << position
return value | mask
else:
mask = ~(1 << position)
return value & mask
mask = 0
for i, attribute in enumerate([
quad, material, face_uvs, face_vertex_uvs, face_normals,
face_vertex_normals, face_colours, face_vertex_colours
]):
mask = update_mask(mask, i, attribute)
return mask
def geometry(name='Geometry',
vertices=None,
normals=None,
colours=None,
uvs=None,
faces=None,
materials=None):
if vertices is not None:
vertices = np.asarray(vertices).reshape(-1, 3)
vertices_count = vertices.shape[0]
vertices = np.ravel(vertices).tolist()
else:
vertices = []
vertices_count = 0
if normals is not None:
normals = np.asarray(normals).reshape(-1, 3)
normals_count = normals.shape[0]
normals = np.ravel(normals).tolist()
else:
normals = []
normals_count = 0
if colours is not None:
colours = np.asarray(colours).reshape(-1, 1)
colours_count = colours.shape[0]
colours = np.ravel(colours).tolist()
else:
colours = []
colours_count = 0
if uvs is not None:
uvs = np.asarray(uvs).reshape(-1, 2)
uvs_count = uvs.shape[0]
uvs = np.ravel(uvs).tolist()
else:
uvs = []
uvs_count = 0
if faces is not None:
faces_count = len(faces)
faces = np.ravel(faces).tolist()
else:
faces = []
faces_count = 0
if materials is not None:
materials_count = len(materials)
else:
materials = []
materials_count = 0
data = {
'name': name,
'metadata': {
'formatVersion': 3.1,
'sourceFile': 'Undefined',
'generatedBy': 'colour-three',
'vertices': vertices_count,
'normals': normals_count,
'colors': colours_count,
'uvs': uvs_count,
'faces': faces_count,
'materials': materials_count
},
'scale': 1,
'vertices': vertices,
'normals': normals,
'colors': colours,
'uvs': uvs,
'faces': faces,
'materials': materials,
'morphTargets': [],
'morphColors': [],
}
return json.dumps(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment