forked from pi3d/pi3d_demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClothWalk.py
185 lines (156 loc) · 5.7 KB
/
ClothWalk.py
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
#!/usr/bin/python
from __future__ import absolute_import, division, print_function, unicode_literals
import math, random
import demo
import pi3d
import numpy as np
########################################################################
# cloth generation section
########################################################################
def gcd(a, b):
"""Return greatest common divisor using Euclid's Algorithm."""
while b:
a, b = b, a % b
return a
def lcm(a, b):
"""Return lowest common multiple."""
return a * b // gcd(a, b)
EPM = 3200 # ends per gpu unit (aka warp threads)
PPM = 3200 # picks per gpu unit (aka weft threads)
yarn_colours = np.array([ # RGB values 0-255
[50, 10, 20],
[40, 55, 20],
[120, 150, 40],
[80, 200, 120],
[30, 5, 110],
[100, 10, 140],
[255, 20, 220]])
# yarn plan numbers refer to yarn_colours index (starting at 0 obviously)
warp_plan = np.array(([3]*8 + [3,0,2]*2 + [4,4])*3 + [0]*7 + [5])
weft_plan = np.array(([2]*8 + [1,5,4]*2 + [6,5])*3 + [0,1]*3 + [0,5])
# draft aka heald, heddle, shaft, frame: the set of wires attached to a liftable frame
# position in the array corresponds to warp thread number is the 'column' of the peg_plan
draft_plan = np.array([0,1,2,3, 0,1,2,3, 1,0,3,2, 1,0,3,2]) # simple herringbone
# peg or lifting plan, rows are weft insertions, column are shafts, 0 is down 1 is up
peg_plan = np.array([
[0,1,1,0],
[1,1,0,0],
[1,0,0,1],
[0,0,1,1]])
pick_rpt = peg_plan.shape[0] # length in 1st dimension of array
draft_rpt = draft_plan.size # length of array
weft_rpt = weft_plan.size
warp_rpt = warp_plan.size
# find the size of the sample
wv_width = lcm(warp_rpt, draft_rpt) # lowest common multiple of weave and yarn patterns
wv_height = lcm(weft_rpt, pick_rpt)
# extend each of the plans to the size of the sample
warp_plan = np.tile(warp_plan, (wv_width // warp_rpt))
weft_plan = weft_plan.reshape((weft_rpt, 1)) # make into vertical array
weft_plan = np.tile(weft_plan, (wv_height // weft_rpt, 1))
draft_plan = np.tile(draft_plan, (wv_width // draft_rpt))
peg_plan = np.tile(peg_plan, (wv_height // pick_rpt, 1))
lifts = peg_plan[:,draft_plan] # generate an up/down map for the plan
# then fill it with the yarn for warp if it's up or weft if it's down
yarn_plan = lifts * warp_plan + (lifts * -1 + 1) * weft_plan
wv = yarn_colours[yarn_plan].astype(np.uint8) # wv takes the [R,G,B] from yarn colours
clothimg = pi3d.Texture(wv)
########################################################################
# Setup display and initialise pi3d
DISPLAY = pi3d.Display.create(x=100, y=100)
DISPLAY.set_background(0.4, 0.8, 0.8, 1.0) # r,g,b,alpha
# yellowish directional light blueish ambient light
pi3d.Light(lightpos=(1, -1, -3), lightcol =(1.0, 1.0, 0.8), lightamb=(0.45, 0.5, 0.6))
#========================================
# load shader
shader = pi3d.Shader("uv_bump")
shinesh = pi3d.Shader("uv_reflect")
flatsh = pi3d.Shader("uv_flat")
bumpimg = pi3d.Texture("textures/weave.png")
reflimg = pi3d.Texture("textures/stars.jpg")
rockimg = pi3d.Texture("textures/rock1.jpg")
FOG = ((0.3, 0.3, 0.4, 0.8), 650.0)
TFOG = ((0.2, 0.24, 0.22, 1.0), 150.0)
#myecube = pi3d.EnvironmentCube(900.0,"HALFCROSS")
ectex=pi3d.loadECfiles("textures/ecubes","skybox_hall")
myecube = pi3d.EnvironmentCube(size=900.0, maptype="FACES", name="cube")
myecube.set_draw_details(flatsh, ectex)
# Create elevation map
mapwidth = 1000.0
mapdepth = 1000.0
mapheight = 60.0
mountimg1 = pi3d.Texture("textures/mountains3_512.jpg")
mymap = pi3d.ElevationMap("textures/mountainsHgt.jpg", name="map",
width=mapwidth, depth=mapdepth, height=mapheight,
ntiles=128, divx=32, divy=32) #testislands.jpg
mymap.set_draw_details(shader, [clothimg, bumpimg, reflimg], 128.0 * wv_width / 8.0, 0.0)
mymap.set_fog(*FOG)
#Create monument
monument = pi3d.Model(file_string="models/pi3d.obj", name="monument")
monument.set_shader(shinesh)
monument.set_draw_details(shinesh, [clothimg, bumpimg, reflimg], ntiles=PPM/8,
shiny=0.03, umult=EPM/wv_width, vmult=PPM/wv_height)
monument.set_fog(*FOG)
monument.translate(100.0, -mymap.calcHeight(100.0, 235) + 12.0, 235.0)
monument.scale(20.0, 20.0, 20.0)
monument.rotateToY(-25)
#screenshot number
scshots = 1
#avatar camera
rot = 0.0
tilt = 0.0
avhgt = 3.5
xm = 100.0
zm = 220.0
ym = mymap.calcHeight(xm, zm) + avhgt
# Fetch key presses
mykeys = pi3d.Keyboard()
mymouse = pi3d.Mouse(restrict = False)
mymouse.start()
omx, omy = mymouse.position()
CAMERA = pi3d.Camera.instance()
# Display scene and rotate cuboid
while DISPLAY.loop_running():
CAMERA.reset()
CAMERA.rotate(tilt, rot, 0)
CAMERA.position((xm, ym, zm))
# for opaque objects it is more efficient to draw from near to far as the
# shader will not calculate pixels already concealed by something nearer
monument.draw()
mymap.draw()
myecube.draw()
mx, my = mymouse.position()
#if mx>display.left and mx<display.right and my>display.top and my<display.bottom:
rot -= (mx-omx)*0.2
tilt += (my-omy)*0.2
omx=mx
omy=my
#Press ESCAPE to terminate
k = mykeys.read()
if k >-1:
if k==119: #key W
xm -= math.sin(math.radians(rot))
zm += math.cos(math.radians(rot))
ym = mymap.calcHeight(xm, zm) + avhgt
elif k==115: #kry S
xm += math.sin(math.radians(rot))
zm -= math.cos(math.radians(rot))
ym = mymap.calcHeight(xm, zm) + avhgt
elif k==39: #key '
tilt -= 2.0
elif k==47: #key /
tilt += 2.0
elif k==97: #key A
rot -= 2
elif k==100: #key D
rot += 2
elif k==112: #key P
pi3d.screenshot("forestWalk"+str(scshots)+".jpg")
scshots += 1
elif k==10: #key RETURN
mc = 0
elif k==27: #Escape key
mykeys.close()
mymouse.stop()
DISPLAY.stop()
break