| 1 | import soy |
|---|
| 2 | from random import random |
|---|
| 3 | |
|---|
| 4 | class BlockMesh(soy.models.Mesh) : |
|---|
| 5 | def __init__(self, mats) : |
|---|
| 6 | Vert = VertexGroup(self) |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | for a in (1.0, -1.0) : |
|---|
| 10 | |
|---|
| 11 | for b in (0, 1, 2) : |
|---|
| 12 | |
|---|
| 13 | for c in (1.0, -1.0) : |
|---|
| 14 | for d in (1.0, -1.0) : |
|---|
| 15 | material = mats[a*c*d > 0] |
|---|
| 16 | points = ( |
|---|
| 17 | Vert(b, |
|---|
| 18 | ( a*.500, 0, 0 ), |
|---|
| 19 | ( a, 0, 0 ), |
|---|
| 20 | ( 0, a, 0 ), |
|---|
| 21 | ( 0, 0, 0 )), |
|---|
| 22 | Vert(b, |
|---|
| 23 | ( a*.500, 0, d*.450 ), |
|---|
| 24 | ( a, 0, 0 ), |
|---|
| 25 | ( 0, a, 0 ), |
|---|
| 26 | ( 0, d*.450, 0 )), |
|---|
| 27 | Vert(b, |
|---|
| 28 | ( a*.500, c*.450, d*.450 ), |
|---|
| 29 | ( a, 0, 0 ), |
|---|
| 30 | ( 0, a, 0 ), |
|---|
| 31 | ( c*.450, d*.450, 0 )), |
|---|
| 32 | Vert(b, |
|---|
| 33 | ( a*.500, c*.450, 0 ), |
|---|
| 34 | ( a, 0, 0 ), |
|---|
| 35 | ( 0, a, 0 ), |
|---|
| 36 | ( c*.450, 0, 0 )), |
|---|
| 37 | Vert(b, |
|---|
| 38 | ( a*.485, c*.485, 0 ), |
|---|
| 39 | ( a*.7071070, c*.7071070, 0 ), |
|---|
| 40 | ( 0, c*1, 0 ), |
|---|
| 41 | (c*.485, 0, 0, )), |
|---|
| 42 | Vert(b, |
|---|
| 43 | ( a*.485, c*.485, d*.450 ), |
|---|
| 44 | ( a*.7071070, c*.7071070, 0 ), |
|---|
| 45 | ( 0, c*1, 0 ), |
|---|
| 46 | (c*.485, d*.450, 0 )), |
|---|
| 47 | Vert(b, |
|---|
| 48 | ( a*.475, c*.475, d*.475 ), |
|---|
| 49 | ( a*.5773503, c*.5773503, d*.5773503), |
|---|
| 50 | ( 0, c*1, 0 ), |
|---|
| 51 | (c*.475, d*.475, 0 )), |
|---|
| 52 | Vert(b, |
|---|
| 53 | ( a*.485, c*.450, d*.485 ), |
|---|
| 54 | ( a*.7071070, 0, d*.7071070), |
|---|
| 55 | ( 0, c*1, 0), |
|---|
| 56 | (c*.450, d*.485, 0 )), |
|---|
| 57 | Vert(b, |
|---|
| 58 | ( a*.485, 0, d*.485 ), |
|---|
| 59 | ( a*.7071070, 0, d*.7071070), |
|---|
| 60 | ( 0, c*1, 0 ), |
|---|
| 61 | (0, d*.485, 0 ))) |
|---|
| 62 | faces = ([points[0], points[1], points[2]], |
|---|
| 63 | [points[0], points[2], points[3]], |
|---|
| 64 | [points[2], points[4], points[3]], |
|---|
| 65 | [points[2], points[5], points[4]], |
|---|
| 66 | [points[2], points[6], points[5]], |
|---|
| 67 | [points[2], points[7], points[6]], |
|---|
| 68 | [points[2], points[8], points[7]], |
|---|
| 69 | [points[2], points[1], points[8]]) |
|---|
| 70 | for face in faces : |
|---|
| 71 | if a*c*d==1 : |
|---|
| 72 | face.reverse() |
|---|
| 73 | f = soy.atoms.Face(self, verts=face, material=material) |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | class VertexGroup : |
|---|
| 77 | def __init__(self, mesh) : |
|---|
| 78 | self.verts = {} |
|---|
| 79 | self.mesh = mesh |
|---|
| 80 | |
|---|
| 81 | def __call__(self, shift, coord, normal, tangent, texcoord) : |
|---|
| 82 | if shift == 1 : |
|---|
| 83 | coord = (coord[1], coord[2], coord[0]) |
|---|
| 84 | normal = (normal[1], normal[2], normal[0]) |
|---|
| 85 | tangent = (tangent[1], tangent[2], tangent[0]) |
|---|
| 86 | elif shift == 2 : |
|---|
| 87 | coord = (coord[2], coord[0], coord[1]) |
|---|
| 88 | normal = (normal[2], normal[0], normal[1]) |
|---|
| 89 | tangent = (tangent[2], tangent[0], tangent[1]) |
|---|
| 90 | |
|---|
| 91 | if not self.verts.has_key(coord) : |
|---|
| 92 | self.verts[coord] = soy.atoms.Vertex(self.mesh, position=coord, |
|---|
| 93 | normal=normal, tangent=tangent, |
|---|
| 94 | texcoord=texcoord) |
|---|
| 95 | return self.verts[coord] |
|---|
| 96 | |
|---|
| 97 | def srand() : |
|---|
| 98 | return random()-.5 |
|---|
| 99 | |
|---|
| 100 | def blocks(sce, colors): |
|---|
| 101 | blocks = {} |
|---|
| 102 | for color in colors : |
|---|
| 103 | blocks[color] = soy.bodies.Body(sce, |
|---|
| 104 | model=BlockMesh(colors[color][:2]), |
|---|
| 105 | position=colors[color][2]) |
|---|
| 106 | blocks[color].rotation = (srand(), srand(), srand()) |
|---|
| 107 | blocks[color].shape = soy.shapes.Box(1,1,1) |
|---|
| 108 | t = -0.5 |
|---|
| 109 | blocks[color].velocity = (blocks[color].position[0] * t, blocks[color].position[1] * t, blocks[color].position[2] * t) |
|---|
| 110 | return blocks |
|---|