-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpmtest.py
More file actions
127 lines (117 loc) · 4.46 KB
/
pmtest.py
File metadata and controls
127 lines (117 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import os
import sys
import pyprogmesh
import pyffi.formats.nif
from pyffi.formats.nif import NifFormat
def PMBlock(block):
print "========================NEW BLOCK========================="
verts = list()
faces = list()
PMSettings = pyprogmesh.ProgMeshSettings()
if block.num_uv_sets > 0:
PMSettings.ProtectTexture = True
if block.has_vertex_colors:
PMSettings.ProtectColor = True
## if block.num_vertices > block.num_triangles:
### raw_input("Border condition decteted. Enabling option. Press ENTER to continue...")
## PMSettings.KeepBorder = True
PMSettings.RemoveDuplicate = True
PMSettings.KeepBorder = False
for i in range(0, len(block.vertices)):
_v = block.vertices[i]
# print "vertex: (%f, %f, %f)" % (_v.x, _v.y, _v.z)
v = [_v.x, _v.y, _v.z]
if block.num_uv_sets > 0:
_uv = [block.uv_sets[0][i].u, block.uv_sets[0][i].v]
else:
_uv = None
if block.has_normals:
_normal = [block.normals[i].x, block.normals[i].y, block.normals[i].z]
else:
_normal = None
if block.has_vertex_colors:
_vc = [block.vertex_colors[i].r, block.vertex_colors[i].g, block.vertex_colors[i].b, block.vertex_colors[i].a]
else:
_vc = None
verts.append(pyprogmesh.RawVertex(Position=v, UV=_uv, Normal=_normal, RGBA=_vc))
for i in range(0, len(block.triangles)):
_t = block.triangles[i]
# print "triangle: [%d, %d, %d]" % (_t.v_1, _t.v_2, _t.v_3)
f = [_t.v_1, _t.v_2, _t.v_3]
faces.append(f)
print "PREP: old verts = %d, old faces = %d" % (len(verts), len(faces))
pm = pyprogmesh.ProgMesh(len(verts), len(faces), verts, faces, PMSettings)
# raw_input("Press ENTER to compute progressive mesh.")
pm.ComputeProgressiveMesh()
# raw_input("Press ENTER to perform decimation.")
result = pm.DoProgressiveMesh(0.25)
if result == 0:
return
else:
numVerts, verts, numFaces, faces = result[0], result[1], result[2], result[3]
print "RESULTS: new verts = %d, new faces = %d" % (numVerts, numFaces)
block.num_vertices = numVerts
block.vertices.update_size()
if block.num_uv_sets > 0:
block.uv_sets.update_size()
if block.has_normals:
block.normals.update_size()
if block.has_vertex_colors:
block.vertex_colors.update_size()
for i in range(0, numVerts):
rawVert = verts[i]
v = block.vertices[i]
v.x = rawVert.Position[0]
v.y = rawVert.Position[1]
v.z = rawVert.Position[2]
if block.num_uv_sets > 0:
uv = block.uv_sets[0][i]
uv.u = rawVert.UV[0]
uv.v = rawVert.UV[1]
if block.has_normals:
normals = block.normals[i]
normals.x = rawVert.Normal[0]
normals.y = rawVert.Normal[1]
normals.z = rawVert.Normal[2]
if block.has_vertex_colors:
vc = block.vertex_colors[i]
vc.r = rawVert.RGBA[0]
vc.g = rawVert.RGBA[1]
vc.b = rawVert.RGBA[2]
vc.a = rawVert.RGBA[3]
block.num_triangles = numFaces
block.triangles.update_size()
for i in range(0, numFaces):
triangle = faces[i]
t = block.triangles[i]
t.v_1 = triangle[0]
t.v_2 = triangle[1]
t.v_3 = triangle[2]
## i = 0
## for r in result[1]:
## print "New Triangle[%d]: (%f, %f, %f)" % (i, r.Position[0], r.Position[1], r.Position[2])
## i = i + 1
## if i > 5:
## break
## i = 0
## for f in result[3]:
## print "New Face[%d]: [%d, %d, %d]" % (i, f[0], f[1], f[2])
## i = i + 1
## if i > 5:
## break
input_filename = 'meshes/floraUbcUtreeU01.nif'
#input_filename = 'meshes/cessna.nif'
fstream = open(input_filename, 'rb')
x = NifFormat.Data()
x.read(fstream)
fstream.close()
for root in x.roots:
for block in root.tree():
if isinstance(block, NifFormat.NiTriShapeData):
if block.has_vertices and block.has_triangles:
if block.num_vertices < 10000:
PMBlock(block)
output_filename = input_filename.lower().replace(".nif", "_reduced.nif")
ostream = open(output_filename, 'wb')
x.write(ostream)
ostream.close()