コード例 #1
0
ファイル: renderSystem.py プロジェクト: croxis/apollo
    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())
コード例 #2
0
ファイル: renderSystem.py プロジェクト: croxis/apollo
    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())
コード例 #3
0
ファイル: solarSystem.py プロジェクト: croxis/apollo
    def generateNode(self, name, DB, parentNode):
        log.debug("Setting up " + name)
        bodyEntity = sandbox.createEntity()
        component = CelestialComponent()
        if DB["type"] == "solid":
            body = Body(name)
        elif DB["type"] == "moon":
            body = Body(name)
            body.kind = "moon"
        elif DB["type"] == "star":
            body = Star(name)
            body.absoluteM = DB["absolute magnitude"]
            body.spectral = DB["spectral"]
        elif DB["type"] == "barycenter":
            body = BaryCenter(name)

        component.kind = TYPES[DB["type"]]

        if DB["type"] != "barycenter":
            component.mass = DB["mass"]
            body.radius = DB["radius"]
            body.rotation = DB["rotation"]

        if "orbit" in DB:
            component.orbit = DB["orbit"]
            body.period = DB["period"]
            # body.setPos(self.get2DBodyPosition(component, universals.day))
            component.truePos = self.get2DBodyPosition(component, universals.day)
            if name == "Earth":
                # universals.spawn = component.truePos + LPoint3d(0, 6671, 0)
                universals.spawn = component.truePos + LPoint3d(6671, 0, 0)

        if parentNode == universals.solarSystemRoot:
            universals.defaultSOIid = bodyEntity.id
            component.soi = 0
        elif DB["type"] != "star" or DB["type"] != "barycenter":
            component.soi = self.getSOI(component.mass, self.bodies[0].mass, component.orbit["a"])

        body.type = DB["type"]
        body.reparentTo(parentNode)
        component.nodePath = body
        self.bodies.append(component)
        bodyEntity.addComponent(component)

        if universals.runClient and DB["type"] == "star":
            component = graphicsComponents.RenderComponent()
            # component.mesh = NodePath(name)
            # self.sphere.copyTo(component.mesh)
            # component.mesh = shapeGenerator.Sphere(body.radius, 128, name)
            component.mesh = shapeGenerator.Sphere(body.radius, 64, name)
            # component.mesh.setScale(body.radius)
            component.mesh.reparentTo(sandbox.base.render)
            sandbox.send("makePickable", [component.mesh])
            # texture = sandbox.base.loader.loadTexture('planets/' + DB['texture'])
            # texture.setMinfilter(Texture.FTLinearMipmapLinear)
            # ts1 = TextureStage('textures1')
            # ts1.setMode(TextureStage.MGlow)
            # component.mesh.setTexture(ts1, texture)
            # component.mesh.setTexture(texture, 1)

            component.light = component.mesh.attachNewNode(PointLight("sunPointLight"))
            component.light.node().setColor(Vec4(1, 1, 1, 1))
            sandbox.base.render.setLight(component.light)
            bodyEntity.addComponent(component)

            # Shader test
            componentStar = graphicsComponents.StarRender()
            componentStar.noise_texture = Texture("noise")
            componentStar.noise_texture.setup2dTexture()
            img = PNMImage(1024, 1024)
            for y in range(1024):
                for x in range(1024):
                    img.setXel(x, y, componentStar.noise.noise(x, y))
                    # print componentStar.noise.noise(x, y)
            componentStar.noise_texture.load(img)
            # componentStar.noise_texture.write('test.png')
            # component.mesh.setTexture(componentStar.noise_texture, 1)

            texture = sandbox.base.loader.loadTexture("planets/" + DB["texture"])
            ts1 = TextureStage("textures1")
            ts1.setMode(TextureStage.MGlow)
            component.mesh.setTexture(ts1, componentStar.noise_texture)
            # component.mesh.setTexture(ts1, texture)

            component.mesh.setShaderInput("time", universals.get_day_in_seconds())
            shaders = Shader.load(Shader.SLGLSL, "vortexVertex.glsl", "starFrag.glsl")
            component.mesh.setShader(shaders)
            sandbox.send("makePickable", [component.mesh])

        if universals.runClient and (DB["type"] == "solid" or DB["type"] == "moon"):
            component = graphicsComponents.RenderComponent()
            # component.mesh = shapeGenerator.Sphere(body.radius, 128, name)
            # component.mesh = shapeGenerator.Sphere(body.radius, 64, name)
            component.mesh = surface_mesh.make_planet(name=name, scale=body.radius)
            # sandbox.send('makePickable', [component.mesh])
            sandbox.send("makePickable", [component.mesh.node_path])
            # component.mesh.setScale(body.radius)
            component.mesh.reparent_to(sandbox.base.render)
            # Doing world text
            text = TextNode("node name")
            text.setText(name)
            # textNodePath = component.mesh.attachNewNode(text)
            textNodePath = component.mesh.node_path.attachNewNode(text)
            textNodePath.setScale(0.07)

            component.mesh.set_textures(DB["texture"], night_path=DB["night"], gloss_path=DB["spec"])

            component.mesh.set_ambient(1, 1, 1, 1)
            component.mesh.set_diffuse(1, 1, 1, 1)
            component.mesh.set_specular(1, 1, 1, 1)
            component.mesh.set_shininess(100)

            """if '#' in DB['texture']:
                component.mesh.setTexGen(TextureStage.getDefault(), TexGenAttrib.MWorldPosition)
                component.mesh.setTexProjector(TextureStage.getDefault(), sandbox.base.render, component.mesh)
                component.mesh.setTexScale(TextureStage.getDefault(), 1,  1, -1)
                component.mesh.setTexHpr(TextureStage.getDefault(), 90, -18, 90)
                #self.mesh.setHpr(0, 90, 0)
                texture = loader.loadCubeMap('planets/' + DB['texture'])
            else:
                texture = sandbox.base.loader.loadTexture('planets/' + DB['texture'])
            #texture.setMinfilter(Texture.FTLinearMipmapLinear)
            component.mesh.setTexture(texture, 1)"""
            """if "atmosphere" in DB:
                component.atmosphere = shapeGenerator.Sphere(-1, 128)
                component.atmosphere.reparentTo(render)
                component.atmosphere.setScale(body.radius * 1.025)
                outerRadius = component.atmosphere.getScale().getX()
                scale = 1 / (outerRadius - component.body.getScale().getX())
                component.atmosphere.setShaderInput("fOuterRadius", outerRadius)
                component.atmosphere.setShaderInput("fInnerRadius", component.mesh.getScale().getX())
                component.atmosphere.setShaderInput("fOuterRadius2", outerRadius * outerRadius)
                component.atmosphere.setShaderInput("fInnerRadius2",
                    component.mesh.getScale().getX()
                    * component.mesh.getScale().getX())

                component.atmosphere.setShaderInput("fKr4PI",
                    0.000055 * 4 * 3.14159)
                component.atmosphere.setShaderInput("fKm4PI",
                    0.000015 * 4 * 3.14159)

                component.atmosphere.setShaderInput("fScale", scale)
                component.atmosphere.setShaderInput("fScaleDepth", 0.25)
                component.atmosphere.setShaderInput("fScaleOverScaleDepth", scale / 0.25)

                # Currently hard coded in shader
                component.atmosphere.setShaderInput("fSamples", 10.0)
                component.atmosphere.setShaderInput("nSamples", 10)
                # These do sunsets and sky colors
                # Brightness of sun
                ESun = 15
                # Reyleight Scattering (Main sky colors)
                component.atmosphere.setShaderInput("fKrESun", 0.000055 * ESun)
                # Mie Scattering -- Haze and sun halos
                component.atmosphere.setShaderInput("fKmESun", 0.000015 * ESun)
                # Color of sun
                component.atmosphere.setShaderInput("v3InvWavelength", 1.0 / math.pow(0.650, 4),
                                                  1.0 / math.pow(0.570, 4),
                                                  1.0 / math.pow(0.465, 4))
                #component.atmosphere.setShader(Shader.load("atmo.cg"))"""
            bodyEntity.addComponent(component)
        log.info(name + " set Up")
        if "bodies" in DB:
            for bodyName, bodyDB in DB["bodies"].items():
                self.generateNode(bodyName, bodyDB, body)
コード例 #4
0
    def generateNode(self, name, DB, parentNode):
        log.debug("Setting up " + name)
        bodyEntity = sandbox.createEntity()
        component = CelestialComponent()
        if DB['type'] == 'solid':
            body = Body(name)
        elif DB['type'] == 'moon':
            body = Body(name)
            body.kind = "moon"
        elif DB['type'] == 'star':
            body = Star(name)
            body.absoluteM = DB['absolute magnitude']
            body.spectral = DB['spectral']
        elif DB['type'] == 'barycenter':
            body = BaryCenter(name)

        component.kind = TYPES[DB['type']]

        if DB['type'] != "barycenter":
            component.mass = DB['mass']
            body.radius = DB['radius']
            body.rotation = DB['rotation']

        if 'orbit' in DB:
            component.orbit = DB['orbit']
            body.period = DB['period']
            #body.setPos(self.get2DBodyPosition(component, universals.day))
            component.truePos = self.get2DBodyPosition(component, universals.day)
            if name == "Earth":
                #universals.spawn = component.truePos + LPoint3d(0, 6671, 0)
                universals.spawn = component.truePos + LPoint3d(6671, 0, 0)

        if parentNode == universals.solarSystemRoot:
            universals.defaultSOIid = bodyEntity.id
            component.soi = 0
        elif DB['type'] != 'star' or DB['type'] != 'barycenter':
            component.soi = self.getSOI(component.mass, self.bodies[0].mass, component.orbit['a'])

        body.type = DB['type']
        body.reparentTo(parentNode)
        component.nodePath = body
        self.bodies.append(component)
        bodyEntity.addComponent(component)

        if universals.runClient and DB['type'] == 'star':
            component = graphicsComponents.RenderComponent()
            #component.mesh = NodePath(name)
            #self.sphere.copyTo(component.mesh)
            #component.mesh = shapeGenerator.Sphere(body.radius, 128, name)
            component.mesh = shapeGenerator.Sphere(body.radius, 64, name)
            #component.mesh.setScale(body.radius)
            component.mesh.reparentTo(sandbox.base.render)
            sandbox.send('makePickable', [component.mesh])
            #texture = sandbox.base.loader.loadTexture('planets/' + DB['texture'])
            #texture.setMinfilter(Texture.FTLinearMipmapLinear)
            #ts1 = TextureStage('textures1')
            #ts1.setMode(TextureStage.MGlow)
            #component.mesh.setTexture(ts1, texture)
            #component.mesh.setTexture(texture, 1)

            component.light = component.mesh.attachNewNode(PointLight("sunPointLight"))
            component.light.node().setColor(Vec4(1, 1, 1, 1))
            sandbox.base.render.setLight(component.light)
            bodyEntity.addComponent(component)

            #Shader test
            componentStar = graphicsComponents.StarRender()
            componentStar.noise_texture = Texture('noise')
            componentStar.noise_texture.setup2dTexture()
            img = PNMImage(1024, 1024)
            for y in range(1024):
                for x in range(1024):
                    img.setXel(x, y, componentStar.noise.noise(x, y))
                    #print componentStar.noise.noise(x, y)
            componentStar.noise_texture.load(img)
            #componentStar.noise_texture.write('test.png')
            #component.mesh.setTexture(componentStar.noise_texture, 1)

            texture = sandbox.base.loader.loadTexture('planets/' + DB['texture'])
            ts1 = TextureStage('textures1')
            ts1.setMode(TextureStage.MGlow)
            component.mesh.setTexture(ts1, componentStar.noise_texture)
            #component.mesh.setTexture(ts1, texture)

            component.mesh.setShaderInput('time', universals.get_day_in_seconds())
            shaders = Shader.load(Shader.SLGLSL, 'vortexVertex.glsl', 'starFrag.glsl')
            component.mesh.setShader(shaders)
            sandbox.send('makePickable', [component.mesh])


        if universals.runClient and (DB['type'] == 'solid' or DB['type'] == 'moon'):
            component = graphicsComponents.RenderComponent()
            #component.mesh = shapeGenerator.Sphere(body.radius, 128, name)
            #component.mesh = shapeGenerator.Sphere(body.radius, 64, name)
            component.mesh = surface_mesh.make_planet(name=name, scale=body.radius)
            #sandbox.send('makePickable', [component.mesh])
            sandbox.send('makePickable', [component.mesh.node_path])
            #component.mesh.setScale(body.radius)
            component.mesh.reparent_to(sandbox.base.render)
            # Doing world text
            text = TextNode('node name')
            text.setText(name)
            #textNodePath = component.mesh.attachNewNode(text)
            textNodePath = component.mesh.node_path.attachNewNode(text)
            textNodePath.setScale(0.07)

            component.mesh.set_textures(DB['texture'],
                night_path=DB['night'],
                gloss_path=DB['spec'])


            component.mesh.set_ambient(1, 1, 1, 1)
            component.mesh.set_diffuse(1, 1, 1, 1)
            component.mesh.set_specular(1, 1, 1, 1)
            component.mesh.set_shininess(100)

            '''if '#' in DB['texture']:
                component.mesh.setTexGen(TextureStage.getDefault(), TexGenAttrib.MWorldPosition)
                component.mesh.setTexProjector(TextureStage.getDefault(), sandbox.base.render, component.mesh)
                component.mesh.setTexScale(TextureStage.getDefault(), 1,  1, -1)
                component.mesh.setTexHpr(TextureStage.getDefault(), 90, -18, 90)
                #self.mesh.setHpr(0, 90, 0)
                texture = loader.loadCubeMap('planets/' + DB['texture'])
            else:
                texture = sandbox.base.loader.loadTexture('planets/' + DB['texture'])
            #texture.setMinfilter(Texture.FTLinearMipmapLinear)
            component.mesh.setTexture(texture, 1)'''
            '''if "atmosphere" in DB:
                component.atmosphere = shapeGenerator.Sphere(-1, 128)
                component.atmosphere.reparentTo(render)
                component.atmosphere.setScale(body.radius * 1.025)
                outerRadius = component.atmosphere.getScale().getX()
                scale = 1 / (outerRadius - component.body.getScale().getX())
                component.atmosphere.setShaderInput("fOuterRadius", outerRadius)
                component.atmosphere.setShaderInput("fInnerRadius", component.mesh.getScale().getX())
                component.atmosphere.setShaderInput("fOuterRadius2", outerRadius * outerRadius)
                component.atmosphere.setShaderInput("fInnerRadius2",
                    component.mesh.getScale().getX()
                    * component.mesh.getScale().getX())

                component.atmosphere.setShaderInput("fKr4PI",
                    0.000055 * 4 * 3.14159)
                component.atmosphere.setShaderInput("fKm4PI",
                    0.000015 * 4 * 3.14159)

                component.atmosphere.setShaderInput("fScale", scale)
                component.atmosphere.setShaderInput("fScaleDepth", 0.25)
                component.atmosphere.setShaderInput("fScaleOverScaleDepth", scale / 0.25)

                # Currently hard coded in shader
                component.atmosphere.setShaderInput("fSamples", 10.0)
                component.atmosphere.setShaderInput("nSamples", 10)
                # These do sunsets and sky colors
                # Brightness of sun
                ESun = 15
                # Reyleight Scattering (Main sky colors)
                component.atmosphere.setShaderInput("fKrESun", 0.000055 * ESun)
                # Mie Scattering -- Haze and sun halos
                component.atmosphere.setShaderInput("fKmESun", 0.000015 * ESun)
                # Color of sun
                component.atmosphere.setShaderInput("v3InvWavelength", 1.0 / math.pow(0.650, 4),
                                                  1.0 / math.pow(0.570, 4),
                                                  1.0 / math.pow(0.465, 4))
                #component.atmosphere.setShader(Shader.load("atmo.cg"))'''
            bodyEntity.addComponent(component)
        log.info(name + " set Up")
        if 'bodies' in DB:
            for bodyName, bodyDB in DB['bodies'].items():
                self.generateNode(bodyName, bodyDB, body)