| 1 | import soy |
|---|
| 2 | from random import random |
|---|
| 3 | |
|---|
| 4 | mrbl = soy.transports.File('media/marble.soy')['gimp'] |
|---|
| 5 | dot3 = soy.transports.File('media/fieldstone-dot3.soy')['gimp'] |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | class BlockMesh(soy.models.Mesh) : |
|---|
| 9 | def __init__(self, mycol) : |
|---|
| 10 | black = soy.materials.Material() |
|---|
| 11 | black.ambient = soy.colors.black |
|---|
| 12 | black.diffuse = soy.colors.Color('#222') |
|---|
| 13 | black.specular= soy.colors.Color('#222') |
|---|
| 14 | black.shininess = 5.0 |
|---|
| 15 | black.normal = dot3 |
|---|
| 16 | Vert = VertexGroup(self) |
|---|
| 17 | |
|---|
| 18 | # Create Faces |
|---|
| 19 | # 2 Halves |
|---|
| 20 | for a in (1.0, -1.0) : |
|---|
| 21 | # 3 Sides in each half |
|---|
| 22 | for b in (0, 1, 2) : |
|---|
| 23 | # 4 Quarters in each side |
|---|
| 24 | for c in (1.0, -1.0) : |
|---|
| 25 | for d in (1.0, -1.0) : |
|---|
| 26 | if a*c*d > 0 : |
|---|
| 27 | material = mycol |
|---|
| 28 | else : |
|---|
| 29 | material = black |
|---|
| 30 | points = (Vert(b, (.500*a, .000*c, .000*d), (a, 0, 0)), # 0 |
|---|
| 31 | Vert(b, (.500*a, .000*c, .450*d), (a, 0, 0)), # 1 |
|---|
| 32 | Vert(b, (.500*a, .450*c, .450*d), (a, 0, 0)), # 2 |
|---|
| 33 | Vert(b, (.500*a, .450*c, .000*d), (a, 0, 0)), # 3 |
|---|
| 34 | Vert(b, (.485*a, .485*c, .000*d), (.7071070*a, # 4 |
|---|
| 35 | .7071070*c, 0)), |
|---|
| 36 | Vert(b, (.485*a, .485*c, .450*d), (.7071070*a, # 5 |
|---|
| 37 | .7071070*c, 0)), |
|---|
| 38 | Vert(b, (.475*a, .475*c, .475*d), (.5773503*a, # 6 |
|---|
| 39 | .5773503*c, |
|---|
| 40 | .5773503*d)), |
|---|
| 41 | Vert(b, (.485*a, .450*c, .485*d), (.7071070*a, # 7 |
|---|
| 42 | 0, .7071070*d)), |
|---|
| 43 | Vert(b, (.485*a, .000*c, .485*d), (.7071070*a, # 8 |
|---|
| 44 | 0, .7071070*d))) |
|---|
| 45 | faces = ([points[0], points[1], points[2]], |
|---|
| 46 | [points[0], points[2], points[3]], |
|---|
| 47 | [points[2], points[4], points[3]], |
|---|
| 48 | [points[2], points[5], points[4]], |
|---|
| 49 | [points[2], points[6], points[5]], |
|---|
| 50 | [points[2], points[7], points[6]], |
|---|
| 51 | [points[2], points[8], points[7]], |
|---|
| 52 | [points[2], points[1], points[8]]) |
|---|
| 53 | for face in faces : |
|---|
| 54 | if a*c*d==1 : |
|---|
| 55 | face.reverse() |
|---|
| 56 | f = soy.atoms.Face(self, verts=face, material=material) |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | class VertexGroup : |
|---|
| 60 | def __init__(self, mesh) : |
|---|
| 61 | self.verts = {} |
|---|
| 62 | self.mesh = mesh |
|---|
| 63 | |
|---|
| 64 | def __call__(self, shift, coord, normal) : |
|---|
| 65 | if shift == 1 : |
|---|
| 66 | coord = (coord[1], coord[2], coord[0]) |
|---|
| 67 | normal = (normal[1], normal[2], normal[0]) |
|---|
| 68 | elif shift == 2 : |
|---|
| 69 | coord = (coord[2], coord[0], coord[1]) |
|---|
| 70 | normal = (normal[2], normal[0], normal[1]) |
|---|
| 71 | if not self.verts.has_key(coord) : |
|---|
| 72 | self.verts[coord] = soy.atoms.Vertex(self.mesh, |
|---|
| 73 | position=coord, texcoord=coord, |
|---|
| 74 | normal=normal, tangent=(0,1,0)) |
|---|
| 75 | return self.verts[coord] |
|---|
| 76 | |
|---|
| 77 | def srand() : |
|---|
| 78 | return random()-.5 |
|---|
| 79 | |
|---|
| 80 | def blocks(sce): |
|---|
| 81 | colors = { |
|---|
| 82 | 'Marble' : (soy.materials.Material(color=mrbl, normal=dot3), (0,0,0)), |
|---|
| 83 | } |
|---|
| 84 | blocks = {} |
|---|
| 85 | for color in colors : |
|---|
| 86 | blocks[color] = soy.bodies.Body(sce, |
|---|
| 87 | model=BlockMesh(colors[color][0]), |
|---|
| 88 | position=colors[color][1]) |
|---|
| 89 | #blocks[color].rotation = (srand(), srand(), srand()) |
|---|
| 90 | blocks[color].rotation = (0,0,1) |
|---|
| 91 | blocks[color].shape = soy.shapes.Box(1,1,1) |
|---|
| 92 | t = -0.5 |
|---|
| 93 | blocks[color].velocity = (blocks[color].position[0] * t, blocks[color].position[1] * t, blocks[color].position[2] * t) |
|---|
| 94 | return blocks |
|---|