-
Notifications
You must be signed in to change notification settings - Fork 2
/
ray.py
177 lines (117 loc) · 4.1 KB
/
ray.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
from pandac.PandaModules import *
winSize=600
loadPrcFileData("", "win-size "+str(winSize)+" "+str(winSize))
loadPrcFileData("", "sync-video 0")
loadPrcFileData("", "basic-shaders-only 0")
import direct.directbase.DirectStart
from direct.showbase.DirectObject import DirectObject
from direct.task.Task import Task
from simplex import ShaderSimplexNoise
base.setFrameRateMeter(True)
base.disableMouse()
c=CardMaker("CardMaker")
c.setFrameFullscreenQuad()
n=render2d.attachNewNode(c.generate())
def setShaderMat(mat,node,name):
v=mat.getCol(0)
node.setShaderInput(name+"1", v.getX(),v.getY(),v.getZ(),v.getW())
v=mat.getCol(1)
node.setShaderInput(name+"2", v.getX(),v.getY(),v.getZ(),v.getW())
v=mat.getCol(2)
node.setShaderInput(name+"3", v.getX(),v.getY(),v.getZ(),v.getW())
v=mat.getCol(3)
node.setShaderInput(name+"4", v.getX(),v.getY(),v.getZ(),v.getW())
noiseD=3
s=ShaderSimplexNoise(0,noiseD)
n.setTexture(s.makeVecTex())
n.setShader(loader.loadShader("Ray.sha"))
base.camera.setPos(0.077093, -0.210524, 0.0781958)
base.setBackgroundColor(0,0,0,0)
print base.win.supportsPixelZoom()
base.win.setPixelZoom(8)
class keyTracker(DirectObject):
def __init__(self):
self.keyMap = {}
def addKey(self,key,name):
self.accept(key.lower(), self.setKey, [name,True])
self.accept(key.lower()+"-up", self.setKey, [name,False])
self.accept(key.upper(), self.setKey, [name,True])
self.accept(key.upper()+"-up", self.setKey, [name,False])
self.keyMap[name]=False
def setKey(self, key, value):
self.keyMap[key] = value
k=keyTracker()
k.addKey("w","forward")
k.addKey("a","left")
k.addKey("s","backward")
k.addKey("d","right")
k.addKey("q","rollLeft")
k.addKey("e","rollRight")
k.addKey("arrow_left","turnLeft")
k.addKey("arrow_right","turnRight")
k.addKey("arrow_down","turnDown")
k.addKey("arrow_up","turnUp")
class MouseTracker(DirectObject):
def __init__(self,sort=0):
self.dt=1
self.mouseX=0
self.mouseY=0
props = WindowProperties()
props.setCursorHidden(True)
base.win.requestProperties(props)
self.trackMouse()
taskMgr.add(self.trackMouse,"trackMouse",sort=sort)
self.dx=0
self.dy=0
def trackMouse(self,task=None):
self.dt=globalClock.getDt()
# figure out how much the mouse has moved (in pixels)
md = base.win.getPointer(0)
x=self.mouseX
y=self.mouseY
self.mouseX = md.getX()
self.mouseY = md.getY()
self.dx=self.mouseX - x
self.dy=self.mouseY - y
base.win.movePointer(0,winSize/2,winSize/2)
self.mouseX=winSize/2
self.mouseY=winSize/2
if task != None: return Task.cont
mouseTracker=MouseTracker()
from time import time
from math import sin
def doFrame(task=None):
elapsed=globalClock.getDt()
forwardMove=0.0
rightMove=0.0
rollRight=0.0
turnRight=0.0
turnUp=0.0
m=k.keyMap
if m["forward"]: forwardMove+=1.0
if m["backward"]: forwardMove-=.5
if m["left"]: rightMove-=1.0
if m["right"]: rightMove+=1.0
if m["rollLeft"]: rollRight-=1.0
if m["rollRight"]: rollRight+=1.0
if m["turnLeft"]: turnRight-=1.0
if m["turnRight"]: turnRight+=1.0
if m["turnDown"]: turnUp-=1.0
if m["turnUp"]: turnUp+=1.0
rightMove*=.05
forwardMove*=.1
rollRight*=100
upMove=0.0
turnUp*=100
turnRight*=100
turnRight+=mouseTracker.dx/mouseTracker.dt*.2
turnUp+=mouseTracker.dy/mouseTracker.dt*-.2
base.camera.setPos(base.camera,Vec3(rightMove,forwardMove,upMove)*elapsed)
base.camera.setHpr(base.camera,-turnRight*elapsed,turnUp*elapsed,rollRight*elapsed)
n.setShaderInput("cam", base.camera.getX(),base.camera.getY(),base.camera.getZ())
setShaderMat(base.camera.getMat(),n,"camMat")
n.setShaderInput("threshold", sin(time()/2)/16-.05)
if task != None: return Task.cont
doFrame()
taskMgr.add(doFrame, "doFrame",sort = 49)
run()