/
renderSystem.py
173 lines (140 loc) · 5.78 KB
/
renderSystem.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
import sandbox
import math
from direct.actor.Actor import Actor
from panda3d.core import OrthographicLens, PerspectiveLens, Point3
import graphicsComponents
import shapeGenerator
import shipComponents
import solarSystem
import universals
from pandac.PandaModules import loadPrcFileData
loadPrcFileData("", "notify-level-ITF-RenderSystem debug")
from direct.directnotify.DirectNotify import DirectNotify
log = DirectNotify().newCategory("ITF-RenderSystem")
PERSPECTIVE = True
MIN_FILM_SIZE = 5
def report():
sandbox.base.render.analyze()
sandbox.base.cTrav.showCollisions(sandbox.base.render)
def orthographic():
global PERSPECTIVE
PERSPECTIVE = False
sandbox.base.disableMouse()
sandbox.base.camera.setPos(0, 0, 3000)
sandbox.base.camera.setHpr(0, -90, 0)
lens = OrthographicLens()
lens.setFilmSize(10)
sandbox.base.cam.node().setLens(lens)
def perspective():
global PERSPECTIVE
PERSPECTIVE = True
sandbox.base.disableMouse()
lens = PerspectiveLens()
sandbox.base.cam.node().setLens(lens)
def debugCamera():
global PERSPECTIVE
PERSPECTIVE = True
sandbox.base.enableMouse()
lens = PerspectiveLens()
sandbox.base.cam.node().setLens(lens)
sandbox.base.cam.setPos(0, 0, 0)
def wheel_up():
if not PERSPECTIVE:
if sandbox.base.cam.node().getLens().getFilmSize()[0] >= MIN_FILM_SIZE:
sandbox.base.cam.node().getLens().setFilmSize(sandbox.base.cam.node().getLens().getFilmSize() - 1)
def wheel_down():
if not PERSPECTIVE:
sandbox.base.cam.node().getLens().setFilmSize(sandbox.base.cam.node().getLens().getFilmSize() + 1)
def convertPos(point):
return Point3(point.getX(), point.getY(), point.getZ())
class RenderSystem(sandbox.EntitySystem):
def init(self):
#self.skybox = shapeGenerator.Sphere(-50000, 128, 'Skysphere')
self.skybox = shapeGenerator.Sphere(-40000, 64, 'Skysphere')
texture = sandbox.base.loader.loadTexture('galaxy.jpg')
self.skybox.setTexture(texture, 1)
self.skybox.reparentTo(sandbox.base.render)
self.accept('hideBG', self.hideBG)
self.accept('showBG', self.showBG)
self.accept('perspective', perspective)
self.accept('orthographic', orthographic)
self.accept('debugView', debugCamera)
self.accept('wheel_up', wheel_up)
self.accept('wheel_down', wheel_down)
self.accept('`', report)
self.accept('w', self.toggleWireframe)
def toggleWireframe(self):
sandbox.base.toggleWireframe()
def entityFromNode(self, nodepath):
for entity in self.entities:
renderComp = sandbox.entities[entity].getComponent(graphicsComponents.RenderComponent)
print renderComp.mesh
if isinstance(renderComp.mesh, Actor):
print renderComp.mesh.getPart('modelRoot')
if renderComp.mesh.getPart('modelRoot') is nodepath:
print "ZOMG YAY"
if nodepath is renderComp.mesh:
print "Yay!"
def hideBG(self):
self.skybox.hide()
log.debug('Hiding skybox')
def showBG(self):
self.skybox.show()
log.debug('Showing skybox')
def begin(self):
pass
def process(self, entity):
scaleFactor = 1
if entity.hasComponent(shipComponents.BulletPhysicsComponent):
phys = entity.getComponent(shipComponents.BulletPhysicsComponent)
pos = phys.getTruePos()
scaleFactor = universals.CONVERT # Convert ship meshes, in meters, to engine units
elif entity.hasComponent(solarSystem.CelestialComponent):
phys = entity.getComponent(solarSystem.CelestialComponent)
pos = phys.truePos
gfx = entity.getComponent(graphicsComponents.RenderComponent)
gfx.mesh.set_shader_input('time', universals.get_day_in_seconds())
if entity.id is not universals.shipid:
if universals.shipid is None:
return
playerShip = sandbox.entities[universals.shipid]
playerPhysics = playerShip.getComponent(shipComponents.BulletPhysicsComponent)
diff = playerPhysics.getTruePos() - pos
#scale = 1.0 / math.sqrt(diff.length())
scale = 1
if PERSPECTIVE:
#diff = diff * scale
gfx.mesh.set_pos(Point3(0, 0, 0) - convertPos(diff))
gfx.mesh.set_scale(scale)
if entity.hasComponent(solarSystem.CelestialComponent):
gfx.mesh.set_scale(scale)
else:
gfx.mesh.set_scale(scale / scaleFactor)
else:
gfx.mesh.set_pos(Point3(0, 0, 0) - convertPos(diff))
if entity.hasComponent(solarSystem.CelestialComponent):
gfx.mesh.set_scale(1)
else:
gfx.mesh.set_scale(1.0 / scaleFactor)
#near = sandbox.base.camLens.getFar() / 2.0
'''scale = 1.0 / math.sqrt(diff.length()) / scaleFactor
#if gfx.mesh.getName() == "Sol":
# print "Sol", gfx.mesh.getScale()
#TODO: LOD sphers to some other sort. Imposters maybe?
#if scale < 0.001:
# scale = 0.001
if not PERSPECTIVE:
diff = diff * scale
gfx.mesh.setPos(Point3(0, 0, 0) - convertPos(diff))
else:
diff = diff * scale
gfx.mesh.setPos(Point3(0, 0, 0) - convertPos(diff))'''
'''if diff.length() > near:
x = diff.length() - near
scale = 1.0 / math.sqrt(x)
else:
gfx.mesh.setPos(Point3(0, 0, 0) - convertPos(diff))'''
#print phys.nodePath.getHpr()
gfx.mesh.set_hpr(phys.nodePath.get_hpr())
def end(self):
pass