| 9 | |
| 10 | |
| 11 | class MeshSampler(object): |
| 12 | def __init__(self, path): |
| 13 | self.path = path |
| 14 | self._load(path) |
| 15 | |
| 16 | def _load(self, path): |
| 17 | # load mesh obj and mtl |
| 18 | scene = trimesh.load(path, force="scene", process=False) |
| 19 | vs = [] |
| 20 | fs = [] |
| 21 | vns = [] |
| 22 | uvs = [] |
| 23 | tex_ids = [] |
| 24 | images = [] |
| 25 | Kas = [] |
| 26 | Kds = [] |
| 27 | Kss = [] |
| 28 | Nss = [] |
| 29 | |
| 30 | _i = 0 |
| 31 | for name, geo in scene.geometry.items(): |
| 32 | print("load material: ", name) |
| 33 | v = geo.vertices |
| 34 | f = geo.faces |
| 35 | vn = geo.vertex_normals |
| 36 | f += sum([v.shape[0] for v in vs]) |
| 37 | vs.append(v) |
| 38 | fs.append(f) |
| 39 | vns.append(vn) |
| 40 | |
| 41 | uv = geo.visual.uv |
| 42 | image = geo.visual.material.image |
| 43 | |
| 44 | if uv is None: |
| 45 | uv = np.zeros((v.shape[0], 2)) # FIXME: this is a hack |
| 46 | |
| 47 | tex_id = np.zeros((uv.shape[0], 1), dtype=np.int32) |
| 48 | tex_id[:] = _i |
| 49 | |
| 50 | Ka = np.array(geo.visual.material.ambient)[:3] / 255.0 |
| 51 | Kd = np.array(geo.visual.material.diffuse)[:3] / 255.0 |
| 52 | Ks = np.array(geo.visual.material.specular)[:3] / 255.0 |
| 53 | Ns = np.array(geo.visual.material.glossiness) |
| 54 | |
| 55 | uvs.append(uv) |
| 56 | images.append(image) |
| 57 | tex_ids.append(tex_id) |
| 58 | Kas.append(Ka) |
| 59 | Kds.append(Kd) |
| 60 | Kss.append(Ks) |
| 61 | Nss.append(Ns) |
| 62 | _i += 1 |
| 63 | |
| 64 | self.vs = np.concatenate(vs, axis=0) |
| 65 | self.fs = np.concatenate(fs, axis=0) |
| 66 | self.vns = np.concatenate(vns, axis=0) |
| 67 | self.uvs = np.concatenate(uvs, axis=0) |
| 68 | self.tex_ids = np.concatenate(tex_ids, axis=0) |