1
|
|
|
from ed2d import material |
2
|
|
|
|
3
|
|
|
class MTL(object): |
4
|
|
|
def __init__(self, filename): |
5
|
|
|
''' Wavefront .mtl file parser. ''' |
6
|
|
|
self.data = {} |
7
|
|
|
self.material = material.Material() |
8
|
|
|
|
9
|
|
|
# The file name is passed in with the full path via OBJ parser. |
10
|
|
|
self.__load(filename) |
11
|
|
|
|
12
|
|
|
def __load(self, filename): |
13
|
|
|
materialName = None |
14
|
|
|
value = None |
15
|
|
|
valueType = None |
16
|
|
|
|
17
|
|
|
for line in open(filename, "r"): |
18
|
|
|
# Avoid lines starting with "#" |
19
|
|
|
if line.startswith('#'): |
20
|
|
|
continue |
21
|
|
|
else: |
22
|
|
|
value = line.split() |
23
|
|
|
# Make sure the line is no empty |
24
|
|
|
if not value: |
25
|
|
|
continue |
26
|
|
|
else: |
27
|
|
|
valueType = value[0] |
28
|
|
|
|
29
|
|
|
# Check for material |
30
|
|
|
if valueType == 'newmtl': |
31
|
|
|
# Store current material and make it active |
32
|
|
|
self.material = self.data[value[1]] = material.Material() |
33
|
|
|
materialName = value[1] |
34
|
|
|
continue |
35
|
|
|
elif self.material is None: |
36
|
|
|
print("Error, material member is None.") |
37
|
|
|
break |
38
|
|
|
|
39
|
|
|
# Generate a list of floats using the numbers |
40
|
|
|
value = list(map(float, value[1:])) |
41
|
|
|
|
42
|
|
|
if valueType == 'Ns': |
43
|
|
|
# Material Specular Exponent which is multipled by texture value |
44
|
|
|
self.data[materialName].ispecular = value[0] |
45
|
|
|
elif valueType == 'Ka': |
46
|
|
|
# Ambient color |
47
|
|
|
self.data[materialName].ambient = [value[0], value[1], value[2]] |
48
|
|
|
elif valueType == 'Kd': |
49
|
|
|
# Diffuse color |
50
|
|
|
self.data[materialName].diffuse = [value[0], value[1], value[2]] |
51
|
|
|
elif valueType == 'Ks': |
52
|
|
|
# Specular color |
53
|
|
|
self.data[materialName].specular = [value[0], value[1], value[2]] |
54
|
|
|
elif valueType == 'Ke': |
55
|
|
|
# Emission color |
56
|
|
|
self.data[materialName].emission = [value[0], value[1], value[2]] |
57
|
|
|
elif valueType == 'Ni': |
58
|
|
|
# Optical Denisty or Index of Refraction |
59
|
|
|
self.data[materialName].IOR = value[0] |
60
|
|
|
elif valueType == 'map_Kd': |
61
|
|
|
# Diffuse texture is multiplied by the Kd |
62
|
|
|
self.data[materialName].albedoLayers['test'] = value[0] |
63
|
|
|
# Do texture stuff here |
64
|
|
|
elif valueType == 'map_Ks': |
65
|
|
|
# Specular texture is multiplied by the Ks |
66
|
|
|
self.data[materialName].specularMapLayers['test'] = value[0] |
67
|
|
|
elif valueType == 'map_Ns': |
68
|
|
|
# Texture linked to the specular exponent and is multiplied by Ns |
69
|
|
|
pass |
70
|
|
|
|
71
|
|
|
# NOTE: |
72
|
|
|
''' |
73
|
|
|
During rendering, the Ka, Kd, and Ks values and the map_Ka, map_Kd, and map_Ks values are blended according to the following formula: |
74
|
|
|
|
75
|
|
|
result_color=tex_color(tv)*decal(tv)+mtl_color*(1.0-decal(tv)) |
76
|
|
|
|
77
|
|
|
where tv is the texture vertex. |
78
|
|
|
|
79
|
|
|
"result_color" is the blended Ka, Kd, and Ks values. |
80
|
|
|
''' |
81
|
|
|
|