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