def createLeaves(tree, probability=0.5, size=0.5, randomsize=0.1, randomrot=0.1, maxconnections=2, bunchiness=1.0, connectoffset=-0.1):
p = bpy.context.scene.cursor_location
verts = []
faces = []
c1 = Vector((connectoffset, -size / 2, 0))
c2 = Vector((size+connectoffset, -size / 2, 0))
c3 = Vector((size+connectoffset, size / 2, 0))
c4 = Vector((connectoffset, size / 2, 0))
t = gauss(1.0 / probability, 0.1)
bpswithleaves = 0
for bp in tree.branchpoints:
if bp.connections < maxconnections:
dv = tree.branchpoints[bp.parent].v - bp.v if bp.parent else Vector((0, 0, 0))
dvp = Vector((0, 0, 0))
bpswithleaves += 1
nleavesonbp = 0
while t < bpswithleaves:
nleavesonbp += 1
rx = (random() - 0.5) * randomrot * 6.283 # TODO vertical tilt in direction of tropism
ry = (random() - 0.5) * randomrot * 6.283
rot = Euler((rx, ry, random() * 6.283), 'ZXY')
scale = 1 + (random() - 0.5) * randomsize
v = c1.copy()
v.rotate(rot)
verts.append(v * scale + bp.v + dvp)
v = c2.copy()
v.rotate(rot)
verts.append(v * scale + bp.v + dvp)
v = c3.copy()
v.rotate(rot)
verts.append(v * scale + bp.v + dvp)
v = c4.copy()
v.rotate(rot)
verts.append(v * scale + bp.v + dvp)
n = len(verts)
faces.append((n - 1, n - 4, n - 3, n - 2))
t += gauss(1.0 / probability, 0.1) # this is not the best choice of distribution because we might get negative values especially if sigma is large
dvp = nleavesonbp * (dv / (probability ** bunchiness)) # TODO add some randomness to the offset
mesh = bpy.data.meshes.new('Leaves')
mesh.from_pydata(verts, [], faces)
mesh.update(calc_edges=True)
mesh.uv_textures.new()
return mesh
评论列表
文章目录