forked from treeform/meshDrawer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meshDrawer.py
102 lines (82 loc) · 2.75 KB
/
meshDrawer.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
"""
This example show how to use MeshDrawer to draw
on the screen in any way shape or form you want
"""
import direct.directbase.DirectStart
from pandac.PandaModules import *
from random import *
from math import *
maxParticles = 500 # max number of particle (1000) triangles we will display
by = 16 # we have a 16x16 plate texture
generator = MeshDrawer()
generator.setBudget(maxParticles)
## this line is no longer required; in fact it causes an error :]
#generator.setPlateSize(by)
generatorNode = generator.getRoot()
generatorNode.reparentTo(render)
generatorNode.setDepthWrite(False)
generatorNode.setTransparency(True)
generatorNode.setTwoSided(True)
generatorNode.setTexture(loader.loadTexture("radarplate.png"))
generatorNode.setBin("fixed",0)
generatorNode.setLightOff(True)
# load some thing into our scene
base.setFrameRateMeter(True)
base.setBackgroundColor(.1,.1,.1,1)
t = loader.loadModel('teapot')
t.reparentTo(render)
t.setPos(0,0,-1)
# very usefull function
def randVec():
return Vec3(random()-.5,random()-.5,random()-.5)
def Frame(framenum):
"""
This function returns the Vec4 with the values for u, v, width and height as
expected by the lastest Meshdrawer implementation instead of setting up a
plate.
"""
w = h = 2048.0
frame_w = frame_h = 128.0
factor_x = factor_y = 1.0 / float(w / frame_w)
x_max = int(w / float(frame_w))
y_max = int(h / float(frame_h))
y = int((framenum - 1) / x_max )
x = int(framenum - (y * x_max) - 1)
u = float(x*factor_x)
v = float(1 - ((y+1) * factor_y))
ue = factor_x
ve = factor_y
return Vec4(u, v, ue, ve)
seed(1988) # random seed - remove if you always want different random results
# create 100 random particles
particles = []
for i in range(100):
frame = Frame(randint(181, 207))
p = [randVec()*1, randVec()*100, frame, 1, Vec4(random(),random(),random(),1)]
particles.append(p)
# create 100 random lines
lines = []
for i in range(100):
l = [randVec()*100,randVec()*100, Frame(194),.1,Vec4(random(),random(),random(),1)]
lines.append(l)
def drawtask(taks):
""" this is called every frame to regen the mesh """
t = globalClock.getFrameTime()
generator.begin(base.cam,render)
for v,pos,frame,size,color in particles:
generator.billboard(pos+v*t,
frame,
size*sin(t*2)+3,
color)
for start,stop,frame,size,color in lines:
generator.segment(start,
stop,
frame,
size*sin(t*2)+2,
color)
generator.end()
return taks.cont
# add the draw task to be drawn every frame
taskMgr.add(drawtask, "draw task")
# run the sample
run()