/
bake.py
196 lines (157 loc) · 5.25 KB
/
bake.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#######################################################################
# ShadowGroup
#######################################################################
class ShadowGroup:
def __init__(self):
self.obj = None
self.texture = None
self.materialGroups = []
#######################################################################
# MaterialGroup
#######################################################################
class MaterialGroup:
def __init__(self):
self.texture = None
self.mat = None
self.faces = []
#######################################################################
# SimpleFace
#######################################################################
class SimpleFace:
def __init__(self):
self.quad = False
#######################################################################
# bake
#######################################################################
def bake(state):
print("**")
print("** Baking")
print("**")
state.shadowGroups = []
for obj in bpy.data.objects:
if obj.type != 'MESH':
continue
generateLightmap(state, obj)
for obj in bpy.data.objects:
if obj.type != 'MESH':
continue
bakeObj(obj)
#######################################################################
# bakeObj
#######################################################################
def bakeObj(obj):
if obj.data.uv_layers.active == None:
print("Ignoring Mesh: " + obj.name)
return
selectObj(obj)
#bpy.context.scene.cycles.samples = 24
#bpy.context.scene.cycles.use_square_samples = True
#bpy.ops.object.bake(type='SHADOW')
#bpy.ops.object.bake(type='COMBINED')
#bpy.context.scene.cycles.use_pass_indirect = False
#bpy.ops.object.bake(type='DIFFUSE', pass_filter={'DIRECT'})
bpy.ops.object.bake(type='COMBINED', pass_filter={'DIRECT', 'DIFFUSE'})
selectObj(None)
#######################################################################
# generateLightmap
#
# Generate a new shadow group for the specified object. Create the
# required lightmap in Blender and the required UV layer.
#######################################################################
def generateLightmap(state, obj):
if len(obj.data.uv_layers) < 1:
print("Ignoring Mesh (No UV layers): " + obj.name)
return
if obj.data.uv_layers.active == None:
print("Ignoring Mesh (UV Layer in invalid state): " + obj.name)
return
tcuv = obj.data.uv_layers.active
lmuv = obj.data.uv_textures.new(name="LightMap")
obj.data.uv_textures.active = lmuv
lmuv = obj.data.uv_layers[len(obj.data.uv_layers) - 1]
selectObj(obj)
bpy.ops.uv.lightmap_pack(PREF_CONTEXT='ALL_FACES', PREF_PACK_IN_ONE=False)
selectObj(None)
lightMap = bpy.data.images.new("LightMap_" + obj.name,
width=state.lightmapSize, height=state.lightmapSize)
sg = ShadowGroup()
sg.texture = lightMap
sg.obj = obj
state.shadowGroups.append(sg)
for m in obj.material_slots:
prepareMaterial(state, m.material, sg)
for face in obj.data.polygons:
prepareFace(state, face, sg, tcuv.data, lmuv.data)
#######################################################################
# prepareFace
#
# Obtain the material group from within the shadow group that matches
# the face material. Copy the face data into it.
#######################################################################
def prepareFace(state, face, sg, uvdat, lmdat):
mg = None
mat = sg.obj.material_slots[face.material_index].material
for m in sg.materialGroups:
if m.mat == mat:
mg = m
if mg == None:
print("Ignoring face in " + sg.obj.name + " (No material found)")
return
shape = len(face.vertices)
a = sg.obj.data.vertices[face.vertices[0]]
b = sg.obj.data.vertices[face.vertices[1]]
c = sg.obj.data.vertices[face.vertices[2]]
sf = SimpleFace()
sf.normal = face.normal
sf.pa = a.co
sf.pb = b.co
sf.pc = c.co
sf.na = a.normal
sf.nb = b.normal
sf.nc = c.normal
#
# For hard edges
#
#sf.na = face.normal
#sf.nb = face.normal
#sf.nc = face.normal
sf.ta = uvdat[face.loop_start + 0].uv
sf.tb = uvdat[face.loop_start + 1].uv
sf.tc = uvdat[face.loop_start + 2].uv
sf.la = lmdat[face.loop_start + 0].uv
sf.lb = lmdat[face.loop_start + 1].uv
sf.lc = lmdat[face.loop_start + 2].uv
if shape == 4:
d = sg.obj.data.vertices[face.vertices[3]]
sf.quad = True
sf.pd = d.co
sf.nd = d.normal
sf.td = uvdat[face.loop_start + 3].uv
sf.ld = lmdat[face.loop_start + 3].uv
mg.faces.append(sf)
#######################################################################
# prepareMaterial
#######################################################################
def prepareMaterial(state, mat, sg):
print("Preparing Material [" + mat.name + "] in " + sg.obj.name)
if mat.use_nodes == False:
mat.use_nodes = True
mg = MaterialGroup()
mg.mat = mat
nodes = mat.node_tree.nodes
#
# Replace any texture with a white one. It provides a better shadow map
#
for node in nodes:
if node.type == 'TEX_IMAGE':
mg.texture = node.image
node.image = state.whiteTexture
break
if mg.texture != None:
sg.materialGroups.append(mg)
#
# Set the render target to the groups lightmap image.
#
tiNode = nodes.new('ShaderNodeTexImage')
tiNode.image = sg.texture
mat.node_tree.nodes.active = tiNode