-
-
Save zeffii/398ab20ba4eeaa3c058a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# adapted from Robin Wilson's | |
# cgfromspace.blogspot.ca/2013/11/penrose-tiling-in-blender-building.html | |
def sv_main(iterations=6): | |
in_sockets = [ | |
['s', 'SubDiv', iterations], | |
] | |
import math | |
from mathutils import Vector | |
# place an upper bound on iterations. | |
subDivIterations = min(iterations, 8) | |
goldenRatio = (1 + math.sqrt(5)) / 2 | |
def subdivide(triangles): | |
result = [] | |
for color, A, B, C in triangles: | |
if color == 0: | |
# Subdivide red triangle | |
P = A + (B - A) / goldenRatio | |
result += [(0, C, P, B), (1, P, C, A)] | |
else: | |
# Subdivide blue triangle | |
Q = B + (A - B) / goldenRatio | |
R = B + (C - B) / goldenRatio | |
result += [(1, R, C, A), (1, Q, R, B), (0, R, Q, A)] | |
return result | |
# Create wheel of red triangles around the origin | |
def createWheel(): | |
triangles = [] | |
for i in range(10): | |
theta_i = i * 2.0 * math.pi / 10.0 | |
theta_ip1 = (i + 1) * 2.0 * math.pi / 10.0 | |
x_i = math.cos(theta_i) | |
x_ip1 = math.cos(theta_ip1) | |
y_i = math.sin(theta_i) | |
y_ip1 = math.sin(theta_ip1) | |
A = Vector((0.0, 0.0, 0.0)) | |
B = Vector((x_i, y_i, 0.0)) | |
C = Vector((x_ip1, y_ip1, 0.0)) | |
if i % 2 == 0: | |
# Make sure to mirror every second triangle | |
B, C = C, B | |
triangles.append((0, A, B, C)) | |
return triangles | |
# Generate map of tiling | |
listTriangles = createWheel() | |
for x in range(subDivIterations): | |
listTriangles = subdivide(listTriangles) | |
# Construct the lists necessary for generation of geometry | |
listVertices = [] | |
listFaces = [] | |
for triangle in listTriangles: | |
v_idx = len(listVertices) | |
v1, v2, v3 = triangle[1], triangle[2], triangle[3] | |
listVertices.extend([v1.to_tuple(), v2.to_tuple(), v3.to_tuple()]) | |
listFaces.append((v_idx, v_idx + 1, v_idx + 2)) | |
verts, faces = listVertices, listFaces | |
out_sockets = [ | |
['v', 'verts', verts], | |
['s', 'faces', faces] | |
] | |
return in_sockets, out_sockets |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment