-
Notifications
You must be signed in to change notification settings - Fork 0
/
amcs.py
423 lines (358 loc) · 11.5 KB
/
amcs.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
bl_info = {
"name": "Automatic MCS",
"blender": (2, 90, 0),
"category": "Mesh",
}
import bpy
import bmesh
import math
from bpy.props import *
from bpy.types import Panel, Operator
from mathutils import Vector, Matrix
#
# 812
# 7-3 0b12345678
# 654
#
noCornerWangs = {
"dims": (4, 4),
0b00000000: (0, 3),
0b00000100: (1, 3),
0b01000100: (2, 3),
0b01000000: (3, 3),
#b12345678
0b00010000: (0, 2),
0b00011100: (1, 2),
0b01111100: (2, 2),
0b01110000: (3, 2),
#b12345678
0b00010001: (0, 1),
0b00011111: (1, 1),
0b11111111: (2, 1),
0b11110001: (3, 1),
#b12345678
0b00000001: (0, 0),
0b00000111: (1, 0),
0b11000111: (2, 0),
0b11000001: (3, 0),
# Edge Cases
0b11100111: (2, 0),
0b11001111: (2, 0),
0b11111100: (2, 2),
0b01111110: (2, 2)
}
#
# 812
# 7-3 0b12345678
# 654
#
lemmmyWangs = {
"dims": (5, 4),
#b12345678
0b00000000: (0, 3),
0b00000100: (1, 3),
0b01000100: (2, 3),
0b01000000: (3, 3),
0b00010111: (4, 3),
#b12345678
0b00010000: (0, 2),
0b00011100: (1, 2),
0b01111100: (2, 2),
0b01110000: (3, 2),
0b11010001: (4, 2),
#b12345678
0b00010001: (0, 1),
0b00011111: (1, 1),
0b11111111: (2, 1),
0b11110001: (3, 1),
#b12345678
0b00000001: (0, 0),
0b00000111: (1, 0),
0b11000111: (2, 0),
0b11000001: (3, 0),
# Edge Cases
0b11100111: (2, 0),
0b11001111: (2, 0),
0b11111100: (2, 2),
0b01111110: (2, 2)
}
#
# 812
# 7-3 0b12345678
# 654
#
lemmmysGlowingWang = {
"dims": (5, 4),
#b12345678
0b00000000: (0, 3),
0b00000100: (1, 3),
0b01000100: (2, 3),
0b01000000: (3, 3),
0b00010100: (4, 3),
#b12345678
0b00010000: (0, 2),
0b00011100: (1, 2),
0b01111100: (2, 2),
0b01110000: (3, 2),
0b00000101: (4, 2),
#b12345678
0b00010001: (0, 1),
0b00011111: (1, 1),
0b11111111: (2, 1),
0b11110001: (3, 1),
0b01010000: (4, 1),
#b12345678
0b00000001: (0, 0),
0b00000111: (1, 0),
0b11000111: (2, 0),
0b11000001: (3, 0),
0b01000001: (4, 0),
# Edge Cases
0b11100111: (2, 0),
0b11001111: (2, 0),
0b11111100: (2, 2),
0b01111110: (2, 2)
}
noWangsForYou = {
"dims": (1, 1),
0: (0, 0)
}
#
# 812
# 7-3 0b12345678
# 654
#
fullWangs = {
"dims": (12, 4),
0b00000000: (0, 3),
0b00000100: (1, 3),
0b01000100: (2, 3),
0b01000000: (3, 3),
0b00010100: (4, 3),
0b01010000: (5, 3),
0b00010101: (6, 3),
0b01010100: (7, 3),
0b01010111: (8, 3),
0b01011101: (9, 3),
0b10110101: (10, 3),
0b11010111: (11, 3),
0b00010000: (0, 2),
0b00011100: (1, 2),
0b01111100: (2, 2),
0b01110000: (3, 2),
0b00000101: (4, 2),
0b01000001: (5, 2),
0b01000101: (6, 2),
0b01010001: (7, 2),
0b11010101: (8, 2),
0b01110101: (9, 2),
0b01111101: (10, 2),
0b01011111: (11, 2),
0b00010001: (0, 1),
0b00011111: (1, 1),
0b11111111: (2, 1),
0b11110001: (3, 1),
0b00011101: (4, 1),
0b01110100: (5, 1),
0b00010111: (6, 1),
0b01011100: (7, 1),
0b11110111: (8, 1),
0b11011111: (9, 1),
0b01110111: (10, 1),
0b11011101: (11, 1),
0b00000001: (0, 0),
0b00000111: (1, 0),
0b11000111: (2, 0),
0b11000001: (3, 0),
0b01000111: (4, 0),
0b11010001: (5, 0),
0b11000101: (6, 0),
0b01110001: (7, 0),
0b11111101: (8, 0),
0b01111111: (9, 0),
0b01010101: (10, 0),
}
wangList = [
{ "name": "Normal Wangs", "value": fullWangs },
{ "name": "Reset Blocks", "value": noWangsForYou },
{ "name": "Marble", "value": lemmmyWangs },
{ "name": "Glowstone", "value": lemmmysGlowingWang },
{ "name": "Monitor", "value": noCornerWangs },
]
def getWang():
scn = bpy.context.scene
return wangList[int(scn.tmp_mcs_wangset.source)]["value"]
class WangSourceProperty(bpy.types.PropertyGroup):
mode_options = [
(str(i), ws["name"], "")
for i, ws in enumerate(wangList)
]
source: bpy.props.EnumProperty(
items=mode_options,
description="Wang Tileset",
default="0",
update=lambda x, y: print(x, y)
)
def sign(x):
"""Returns the sign of x as 0 or 1"""
return 1 if x > 0 else 0
def main(op, context):
wangSource = getWang()
# Grab a BMesh for editing
obj = bpy.context.object
me = obj.data
bm = bmesh.new()
bm.from_mesh(me)
# Acting on selected faces only
selectedQuads = [face for face in bm.faces if face.select]
# Active view
r3d = bpy.context.space_data.region_3d
# View directions
outVector = r3d.view_rotation @ Vector((0.0, 0.0, 1.0))
upVector = r3d.view_rotation @ Vector((0.0, 1.0, 0.0))
rightVector = r3d.view_rotation @ Vector((1.0, 0.0, 0.0))
# All BMesh coordinates are in local space, but our view
# is in global space, so we need to normalize the coordinate space.
def localToWorld(vec):
t = vec.to_4d()
t.w = 0
return (obj.matrix_world @ t).to_3d()
# Misaligned quads almost definitely weren't meant
# to be selected and will be set incorrectly.
# So, we check and alert if we find any.
def checkAlignment(quad):
norm = localToWorld(quad.normal)
adj = norm @ outVector
if adj < 0.5:
return False # Misaligned
return True
oldLen = len(selectedQuads)
selectedQuads = [q for q in selectedQuads if checkAlignment(q)]
if len(selectedQuads) != oldLen:
op.report({"ERROR"}, "The view is misaligned with some face(s), they will be skipped.")
origin = localToWorld(selectedQuads[0].calc_center_median())
planarTransform = Matrix.Translation(-origin)
localizedCoords = [planarTransform @ localToWorld(f.calc_center_median()) for f in selectedQuads]
# Snap the View Vectors to the closest plane
# TODO: Snap to face normals so that diagonal/rotated geometry is possible
outVector = Vector((round(outVector.x ), round(outVector.y ), round(outVector.z )))
upVector = Vector((round(upVector.x ), round(upVector.y ), round(upVector.z )))
rightVector = Vector((round(rightVector.x), round(rightVector.y), round(rightVector.z)))
# Normalized coordinates that take rotation into account
inlineCoords = [
(round(upVector @ v), round(rightVector @ v), round(outVector @ v))
for v in localizedCoords
]
# Build the surface map
mapping = {}
for i in range(len(inlineCoords)):
x, y, z = inlineCoords[i]
mapping[str(x)+";"+str(y)+";"+str(z)] = i
stepX, stepY = wangSource["dims"]
stepX, stepY = 1 / stepX, 1 / stepY
# Grab the UV layer so we can start editing
uv_layer = bm.loops.layers.uv.active
# Compute the wang tiles
for i in range(len(inlineCoords)):
x, y, z = inlineCoords[i]
def check(dx, dy):
return (str(x+dx)+";"+str(y+dy)+";"+str(z)) in mapping
bitmask = (
check( 1, -1) << 7 |
check( 1, 0) << 0 |
check( 1, 1) << 1 |
check( 0, -1) << 6 |
check( 0, 1) << 2 |
check(-1, -1) << 5 |
check(-1, 0) << 4 |
check(-1, 1) << 3 )
if bitmask in wangSource:
mappedWang = wangSource[bitmask]
else:
# Strip corners on hard edges
bareBitmask = bitmask
if ~bitmask & 0b00000001: bareBitmask &= 0b01111101
if ~bitmask & 0b00000100: bareBitmask &= 0b11110101
if ~bitmask & 0b00010000: bareBitmask &= 0b11010111
if ~bitmask & 0b01000000: bareBitmask &= 0b01011111
if bareBitmask in wangSource:
mappedWang = wangSource[bareBitmask]
else:
op.report({"WARNING"}, "Encountered an unmapped wang: " + bin(bitmask))
# Substitute base case
mappedWang = wangSource[0]
# Reposition the mapping for this specific face
singleOrigin = localToWorld(selectedQuads[i].calc_center_median())
faceTransform = Matrix.Translation(-singleOrigin)
for loop in selectedQuads[i].loops:
wangX, wangY = mappedWang
xp = rightVector @ (faceTransform @ localToWorld(loop.vert.co))
yp = upVector @ (faceTransform @ localToWorld(loop.vert.co))
u = round(stepX * (sign(xp) + wangX), 3)
v = round(stepY * (sign(yp) + wangY), 3)
uvDest = loop[uv_layer].uv
uvDest.x, uvDest.y = u, v
bm.to_mesh(bpy.context.object.data)
class UvMCSOperator(Operator):
"""UV Onperator description"""
bl_idname = "uv.connect_mc_textures"
bl_label = "Connect MC Textures"
bl_options = {'REGISTER', 'UNDO'} # Enable undo for the operator.
@classmethod
def poll(cls, context):
obj = context.active_object
return obj and obj.type == 'MESH' and obj.mode == 'EDIT'
def execute(self, context):
returnMode = None
if bpy.context.active_object.mode != "OBJECT":
returnMode = bpy.context.active_object.mode
bpy.ops.object.mode_set(mode="OBJECT")
main(self, context)
if returnMode != None:
bpy.ops.object.mode_set(mode=returnMode)
return {'FINISHED'}
class MCSPanel(Panel):
""""""
bl_idname = "VIEW3D_PT_mcs"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_label = "Connected Tiles"
bl_category = "TMPIM"
wang_enum = bpy.props.EnumProperty(
items = [
("wang-" + str(i), ws["name"], "")
for i, ws in enumerate(wangList)
]
)
def draw(self, context):
layout = self.layout
if not bpy.context.active_object or bpy.context.active_object.mode != "EDIT":
row = layout.row()
row.label(text="Please enter edit mode to use this tool.")
else:
obj = bpy.context.edit_object
me = obj.data
bm = bmesh.from_edit_mesh(me)
faceCnt = len([face for face in bm.faces if face.select])
col = layout.column()
col.label(text="Faces selected for joining: " + str(faceCnt))
row = col.row(align=True)
row.label(text="WangSet")
row.prop(bpy.context.scene.tmp_mcs_wangset, "source", text="")
col.separator()
col = col.column()
col.scale_y = 1.5
col.operator("uv.connect_mc_textures", text="Join Faces", icon="OUTLINER_DATA_LATTICE")
def register():
bpy.utils.register_class(WangSourceProperty)
bpy.types.Scene.tmp_mcs_wangset = PointerProperty(type=WangSourceProperty)
bpy.utils.register_class(UvMCSOperator)
bpy.utils.register_class(MCSPanel)
def unregister():
del bpy.types.Scene.tmp_mcs_wangset
bpy.utils.unregister_class(WangSourceProperty)
bpy.utils.unregister_class(UvMCSOperator)
bpy.utils.unregister_class(MCSPanel)
if __name__ == "__main__":
self = bpy.data.texts["AMCS"].as_module()
self.register()