-
Notifications
You must be signed in to change notification settings - Fork 561
/
material.py
114 lines (88 loc) · 2.98 KB
/
material.py
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
"""
material.py
-------------
Store visual materials as objects.
"""
import numpy as np
from . import color
class Material(object):
def __init__(self, *args, **kwargs):
raise NotImplementedError('material must be subclassed!')
@property
def main_color(self):
raise NotImplementedError('material must be subclassed!')
class SimpleMaterial(Material):
"""
Hold a single image texture.
"""
def __init__(self,
image=None,
diffuse=None,
ambient=None,
specular=None,
**kwargs):
# save image
self.image = image
# save material colors as RGBA
self.ambient = color.to_rgba(ambient)
self.diffuse = color.to_rgba(diffuse)
self.specular = color.to_rgba(specular)
# save other keyword arguments
self.kwargs = kwargs
def to_color(self, uv):
return color.uv_to_color(uv, self.image)
@property
def main_color(self):
return self.diffuse
class PBRMaterial(Material):
"""
Create a material for physically based rendering as
specified by GLTF 2.0:
https://git.io/fhkPZ
Parameters with `Texture` in them must be PIL.Image objects
"""
def __init__(self,
name=None,
emissiveFactor=None,
emissiveTexture=None,
normalTexture=None,
occlusionTexture=None,
baseColorTexture=None,
baseColorFactor=None,
metallicFactor=None,
roughnessFactor=None,
metallicRoughnessTexture=None,
doubleSided=False,
alphaMode='OPAQUE',
alphaCutoff=0.5):
# To to-float conversions
if baseColorFactor is not None:
baseColorFactor = np.array(baseColorFactor, dtype=np.float)
if emissiveFactor is not None:
emissiveFactor = np.array(emissiveFactor, dtype=np.float)
# (4,) float
self.baseColorFactor = color.to_rgba(baseColorFactor)
# (3,) float
self.emissiveFactor = color.to_rgba(emissiveFactor)
# float
self.metallicFactor = metallicFactor
self.roughnessFactor = roughnessFactor
self.alphaCutoff = alphaCutoff
# PIL image
self.normalTexture = normalTexture
self.emissiveTexture = emissiveTexture
self.occlusionTexture = occlusionTexture
self.baseColorTexture = baseColorTexture
self.metallicRoughnessTexture = metallicRoughnessTexture
# bool
self.doubleSided = doubleSided
# str
alphaMode = alphaMode
def to_color(self, uv):
colors = color.uv_to_color(uv=uv, image=self.baseColorTexture)
if colors is None and self.baseColorFactor is not None:
colors = self.baseColorFactor.copy()
return colors
@property
def main_color(self):
return self.baseColorFactor