-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpool_table.py
More file actions
228 lines (203 loc) · 11 KB
/
pool_table.py
File metadata and controls
228 lines (203 loc) · 11 KB
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
"""
three.js/Cannon.js pool table definition
"""
from copy import deepcopy
import numpy as np
from three import *
INCH2METER = 0.0254
FT2METER = INCH2METER / 12
def pool_table(L_table=2.3368, W_table=None, H_table=0.77,
L_playable=None, W_playable=None,
ball_diameter=2.25*INCH2METER,
W_cushion=2*INCH2METER, H_cushion=None, W_rail=None,
**kwargs):
"""
Creates parameterized three.js pool table
:param L_table: length of the pool table (longer than the playable surface); default is 8ft.
:param W_table: width the of the pool table; usually half the length.
:param H_table: height of the playable surface; if no transformations are applied to the pool table
`Object3D`, the base/bottom of the pool table is at `y=0` and the playable surface is at `y=H_table`.
:param L_playable: length of the playable area, I still don't understand exactly what it refers to
:param W_cushion: width of the cushions
:param H_cushion: height of the nose of the cushions; default is 63.5% of ball diameter
"""
if W_table is None:
W_table = 0.5*L_table
if L_playable is None:
L_playable = L_table - 2*W_cushion
if W_playable is None:
W_playable = W_table - 2*W_cushion
if H_cushion is None:
H_cushion = 0.635 * ball_diameter
if W_rail is None:
W_rail = 1.5*W_cushion
H_rail = 1.25 * H_cushion
poolTable = Object3D(name="poolTable")
headSpotMaterial = MeshPhongMaterial(name="headSpotMaterial", color=0x777777, shading=FlatShading)
surfaceMaterial = MeshPhongMaterial(name="surfaceMaterial", color=0x00aa00, shininess=5, shading=FlatShading)
cushionMaterial = MeshPhongMaterial(name="cushionMaterial", color=0x028844, shininess=5, shading=FlatShading)
railMaterial = MeshPhongMaterial(name="railMaterial", color=0xdda400, shininess=10, shading=FlatShading)
thickness = INCH2METER
playableSurfaceGeom = BoxBufferGeometry(W_playable, thickness, L_playable)
playableSurfaceMesh = Mesh(name='playableSurfaceMesh',
geometry=playableSurfaceGeom,
material=surfaceMaterial,
position=[0, H_table - 0.5*thickness, 0],
receiveShadow=True,
userData={'cannonData': {'mass': 0,
'shapes': ['Box']}})
poolTable.add(playableSurfaceMesh)
ball_radius = ball_diameter / 2
spotGeom = CircleBufferGeometry(name='spotGeom', radius=0.7*ball_radius, segments=5)
headSpotMesh = Mesh(geometry=spotGeom,
material=headSpotMaterial,
position=[0, H_table + 0.0002, 0.25*L_table],
rotation=[-np.pi/2, 0, np.pi/10],
receiveShadow=True)
poolTable.add(headSpotMesh)
H_nose = 0.5 * H_cushion
W_nose = 0.05 * W_cushion
sqrt2 = np.sqrt(2)
# headCushionGeom = HexaBufferGeometry(vertices=np.array([
# # bottom quad:
# [[-0.5*W_playable + 0.4*W_cushion, 0, 0.5*W_cushion],
# [ 0.5*W_playable - 0.4*W_cushion, 0, 0.5*W_cushion],
# [ 0.5*W_playable - 1.2*sqrt2*W_cushion, H_cushion - H_nose, -0.5*W_cushion + W_nose],
# [-0.5*W_playable + 1.2*sqrt2*W_cushion, H_cushion - H_nose, -0.5*W_cushion + W_nose]],
# # top quad:
# [[-0.5*W_playable + 0.4*W_cushion, H_rail, 0.5*W_cushion],
# [ 0.5*W_playable - 0.4*W_cushion, H_rail, 0.5*W_cushion],
# [ 0.5*W_playable - 1.2*sqrt2*W_cushion, H_cushion, -0.5*W_cushion],
# [-0.5*W_playable + 1.2*sqrt2*W_cushion, H_cushion, -0.5*W_cushion]]]))
headCushionGeom = HexaBufferGeometry(name="headCushionGeom",
vertices=np.array([
# bottom quad:
[[-0.5*W_playable + 0.4*W_cushion, 0.0, 0.5*W_cushion],
[ 0.5*W_playable - 0.4*W_cushion, 0.0, 0.5*W_cushion],
[ 0.5*W_playable - 1.2*sqrt2*W_cushion, 0.0, -0.5*W_cushion + W_nose],
[-0.5*W_playable + 1.2*sqrt2*W_cushion, 0.0, -0.5*W_cushion + W_nose]],
# top quad:
[[-0.5*W_playable + 0.4*W_cushion, H_rail, 0.5*W_cushion],
[ 0.5*W_playable - 0.4*W_cushion, H_rail, 0.5*W_cushion],
[ 0.5*W_playable - 1.2*sqrt2*W_cushion, H_cushion, -0.5*W_cushion],
[-0.5*W_playable + 1.2*sqrt2*W_cushion, H_cushion, -0.5*W_cushion]]]))
rightHeadCushionGeom = HexaBufferGeometry(name="rightHeadCushionGeom",
vertices=headCushionGeom.vertices.copy())
rightHeadCushionGeom.vertices[0, 2, 0] = 0.5*W_playable - 0.6*sqrt2*W_cushion
rightHeadCushionGeom.vertices[1, 2, 0] = rightHeadCushionGeom.vertices[0, 2, 0]
leftHeadCushionGeom = HexaBufferGeometry(name="leftHeadCushionGeom",
vertices=headCushionGeom.vertices.copy())
leftHeadCushionGeom.vertices[0, 3, 0] = -0.5*W_playable + 0.6*sqrt2*W_cushion
leftHeadCushionGeom.vertices[1, 3, 0] = leftHeadCushionGeom.vertices[0, 3, 0]
cushionData = {'cannonData': {'mass': 0, 'shapes': ['ConvexPolyhedron'], 'faces': HexaBufferGeometry.faces}}
headCushionMesh = Mesh(name='headCushionMesh',
geometry=headCushionGeom,
material=cushionMaterial,
position=[0, H_table, 0.5*L_table - 0.5*W_cushion],
receiveShadow=True,
userData=cushionData)
poolTable.add(headCushionMesh)
footCushionMesh = Mesh(name='footCushionMesh',
geometry=headCushionGeom,
material=cushionMaterial,
position=[0, H_table, -0.5*L_table + 0.5*W_cushion],
rotation=[0, np.pi, 0],
receiveShadow=True,
userData=cushionData)
poolTable.add(footCushionMesh)
leftHeadCushionMesh = Mesh(name='leftHeadCushionMesh',
geometry=leftHeadCushionGeom,
material=cushionMaterial,
position=[-0.5*W_table + 0.5*W_cushion, H_table, 0.25*L_table],
rotation=[0, -np.pi/2, 0],
receiveShadow=True,
userData=cushionData)
poolTable.add(leftHeadCushionMesh)
leftFootCushionMesh = Mesh(name='leftFootCushionMesh',
geometry=rightHeadCushionGeom,
material=cushionMaterial,
position=[-0.5*W_table + 0.5*W_cushion, H_table, -0.25*L_table],
rotation=[0, -np.pi/2, 0],
receiveShadow=True,
userData=cushionData)
poolTable.add(leftFootCushionMesh)
rightHeadCushionMesh = Mesh(name='rightHeadCushionMesh',
geometry=rightHeadCushionGeom,
material=cushionMaterial,
position=[0.5*W_table - 0.5*W_cushion, H_table, 0.25*L_table],
rotation=[0, np.pi/2, 0],
receiveShadow=True,
userData=cushionData)
poolTable.add(rightHeadCushionMesh)
rightFootCushionMesh = Mesh(name='rightFootCushionMesh',
geometry=leftHeadCushionGeom,
material=cushionMaterial,
position=[0.5*W_table - 0.5*W_cushion, H_table, -0.25*L_table],
rotation=[0, np.pi/2, 0],
receiveShadow=True,
userData=cushionData)
poolTable.add(rightFootCushionMesh)
headRailGeom = BoxBufferGeometry(W_playable - 2*0.4*W_cushion, H_rail, W_rail)
railData = {'cannonData': {'mass': 0, 'shapes': ['Box']}}
headRailMesh = Mesh(name='headRailMesh',
geometry=headRailGeom,
material=railMaterial,
position=[0, H_table + 0.5*H_rail, 0.5*L_table + 0.5*W_rail],
receiveShadow=True,
userData=railData)
poolTable.add(headRailMesh)
footRailMesh = Mesh(name='footRailMesh',
geometry=headRailGeom,
material=railMaterial,
position=[0, H_table + 0.5*H_rail, -(0.5*L_table + 0.5*W_rail)],
rotation=[0, np.pi, 0],
receiveShadow=True,
userData=railData)
poolTable.add(footRailMesh)
leftHeadRailMesh = Mesh(name='leftHeadRailMesh',
geometry=headRailGeom,
material=railMaterial,
position=[-(0.5*W_table + 0.5*W_rail), H_table + 0.5*H_rail, 0.25*L_table],
rotation=[0, np.pi/2, 0],
receiveShadow=True,
userData=railData)
poolTable.add(leftHeadRailMesh)
rightHeadRailMesh = Mesh(name='rightHeadRailMesh',
geometry=headRailGeom,
material=railMaterial,
position=[0.5*W_table + 0.5*W_rail, H_table + 0.5*H_rail, 0.25*L_table],
rotation=[0, np.pi/2, 0],
receiveShadow=True,
userData=railData)
poolTable.add(rightHeadRailMesh)
leftFootRailMesh = Mesh(name='leftFootRailMesh',
geometry=headRailGeom,
material=railMaterial,
position=[-(0.5*W_table + 0.5*W_rail), H_table + 0.5*H_rail, -0.25*L_table],
rotation=[0, np.pi/2, 0],
receiveShadow=True,
userData=railData)
poolTable.add(leftFootRailMesh)
rightFootRailMesh = Mesh(name='rightFootRailMesh',
geometry=headRailGeom,
material=railMaterial,
position=[0.5*W_table + 0.5*W_rail, H_table + 0.5*H_rail, -0.25*L_table],
rotation=[0, np.pi/2, 0],
receiveShadow=True,
userData=railData)
poolTable.add(rightFootRailMesh)
return poolTable
def pool_hall(L_table=2.3368,
H_table=0.74295,
ball_diameter=2.25*INCH2METER,
L_room=6,
W_room=6,
url_prefix="",
**kwargs):
"""
Defines a three.js scene containing a pool table.
"""
scene = Scene()
poolTable = pool_table(L_table=L_table, H_table=H_table, ball_diameter=ball_diameter, **kwargs)
scene.add(poolTable)
return scene