Exemple #1
0
    def modifyGeometryUVs(self):
        # Modifies the geometry vertex UVs in-place
        twriter = GeomVertexWriter(self.vdata, InternalName.getTexcoord())
        tanwriter = GeomVertexWriter(self.vdata, InternalName.getTangent())
        bwriter = GeomVertexWriter(self.vdata, InternalName.getBinormal())

        for i in range(len(self.vertices)):
            twriter.setData2f(self.vertices[i].uv)
            tanwriter.setData3f(self.material.tangent)
            bwriter.setData3f(self.material.binormal)
 def generate(self):
     '''(Re)generate the entire terrain erasing any current changes'''
     factor = self.blockSize*self.chunkSize
     #print "Factor:", factor
     for terrain in self.terrains:
         terrain.getRoot().removeNode()
     self.terrains = []
     # Breaking master heightmap into subimages
     heightmaps = []
     self.xchunks = (self.heightfield.getXSize()-1)/factor
     self.ychunks = (self.heightfield.getYSize()-1)/factor
     #print "X,Y chunks:", self.xchunks, self.ychunks
     n = 0
     for y in range(0, self.ychunks):
         for x in range(0, self.xchunks):
             heightmap = PNMImage(factor+1, factor+1)
             heightmap.copySubImage(self.heightfield, 0, 0, xfrom = x*factor, yfrom = y*factor)
             heightmaps.append(heightmap)
             n += 1
     
     # Generate GeoMipTerrains
     n = 0
     y = self.ychunks-1
     x = 0
     for heightmap in heightmaps:
         terrain = GeoMipTerrain(str(n))
         terrain.setHeightfield(heightmap)
         terrain.setBruteforce(self.bruteForce)
         terrain.setBlockSize(self.blockSize)
         terrain.generate()
         self.terrains.append(terrain)
         root = terrain.getRoot()
         root.reparentTo(self.root)
         root.setPos(n%self.xchunks*factor, (y)*factor, 0)
         
         # In order to texture span properly we need to reiterate through every vertex
         # and redefine the uv coordinates based on our size, not the subGeoMipTerrain's
         root = terrain.getRoot()
         children = root.getChildren()
         for child in children:
             geomNode = child.node()
             for i in range(geomNode.getNumGeoms()):
                 geom = geomNode.modifyGeom(i)
                 vdata = geom.modifyVertexData()
                 texcoord = GeomVertexWriter(vdata, 'texcoord')
                 vertex = GeomVertexReader(vdata, 'vertex')
                 while not vertex.isAtEnd():
                     v = vertex.getData3f()
                     t = texcoord.setData2f((v[0]+ self.blockSize/2 + self.blockSize*x)/(self.xsize - 1),
                                                     (v[1] + self.blockSize/2 + self.blockSize*y)/(self.ysize - 1))
         x += 1
         if x >= self.xchunks:
             x = 0
             y -= 1
         n += 1
Exemple #3
0
class Mesh:
    def __init__(self):
        self.views = []
        self.vertexBuffer = GeomVertexData('mesh-vdata',
                                           GeomVertexFormat.getV3n3t2(),
                                           GeomEnums.UHDynamic)
        self.np = NodePath("mesh")

        self.vwriter = None
        self.twriter = None
        self.nwriter = None

    def addView(self, primitiveType, drawMask, state=None):
        self.views.append(
            PolygonView(self, primitiveType, drawMask, renderState=state))

    def begin(self, numVerts):
        self.vertexBuffer.uncleanSetNumRows(numVerts)
        for view in self.views:
            view.clear()
        self.vwriter = GeomVertexWriter(self.vertexBuffer,
                                        InternalName.getVertex())
        self.twriter = GeomVertexWriter(self.vertexBuffer,
                                        InternalName.getTexcoord())
        self.nwriter = GeomVertexWriter(self.vertexBuffer,
                                        InternalName.getNormal())

    def end(self):
        self.vwriter = None
        self.nwriter = None
        self.twriter = None

    def addFace(self, meshVertices, state=RenderState.makeEmpty()):
        row = self.vwriter.getWriteRow()
        for vert in meshVertices:
            self.vwriter.setData3f(vert.pos)
            self.nwriter.setData3f(vert.normal)
            self.twriter.setData2f(vert.texcoord)
        for view in self.views:
            view.generateIndices()
Exemple #4
0
    def regenerateGeometry(self):
        #
        # Generate vertex data
        #

        numVerts = len(self.vertices)

        vdata = GeomVertexData("SolidFace", getFaceFormat(), GeomEnums.UHStatic)
        vdata.uncleanSetNumRows(len(self.vertices))

        vwriter = GeomVertexWriter(vdata, InternalName.getVertex())
        twriter = GeomVertexWriter(vdata, InternalName.getTexcoord())
        nwriter = GeomVertexWriter(vdata, InternalName.getNormal())
        tanwriter = GeomVertexWriter(vdata, InternalName.getTangent())
        bwriter = GeomVertexWriter(vdata, InternalName.getBinormal())

        for i in range(len(self.vertices)):
            vert = self.vertices[i]
            vwriter.setData3f(vert.pos)
            twriter.setData2f(vert.uv)
            nwriter.setData3f(self.plane.getNormal())
            tanwriter.setData3f(self.material.tangent)
            bwriter.setData3f(self.material.binormal)

        #
        # Generate indices
        #

        # Triangles in 3D view
        prim3D = GeomTriangles(GeomEnums.UHStatic)
        prim3D.reserveNumVertices((numVerts - 2) * 3)
        for i in range(1, numVerts - 1):
            prim3D.addVertices(i + 1, i, 0)
            prim3D.closePrimitive()

        # Line loop in 2D view.. using line strips
        prim2D = GeomLinestrips(GeomEnums.UHStatic)
        prim2D.reserveNumVertices(numVerts + 1)
        for i in range(numVerts):
            prim2D.addVertex(i)
        # Close off the line strip with the first vertex.. creating a line loop
        prim2D.addVertex(0)
        prim2D.closePrimitive()

        #
        # Generate mesh objects
        #

        geom3D = SolidFaceGeom(vdata)
        geom3D.setDrawMask(VIEWPORT_3D_MASK)
        geom3D.setPlaneCulled(True)
        geom3D.setPlane(self.plane)
        geom3D.addPrimitive(prim3D)
        self.index3D = self.solid.addFaceGeom(geom3D, self.state3D)

        geom3DLines = SolidFaceGeom(vdata)
        geom3DLines.addPrimitive(prim2D)
        geom3DLines.setDrawMask(VIEWPORT_3D_MASK)
        geom3DLines.setDraw(False)
        self.index3DLines = self.solid.addFaceGeom(geom3DLines, self.state3DLines)

        geom2D = SolidFaceGeom(vdata)
        geom2D.addPrimitive(prim2D)
        geom2D.setDrawMask(VIEWPORT_2D_MASK)
        self.index2D = self.solid.addFaceGeom(geom2D, self.state2D)

        self.geom3D = geom3D
        self.geom3DLines = geom3DLines
        self.geom2D = geom2D

        self.vdata = vdata

        self.hasGeometry = True
    def generate(self):
        '''(Re)generate the entire terrain erasing any current changes'''
        factor = self.blockSize * self.chunkSize
        #print "Factor:", factor
        for terrain in self.terrains:
            terrain.getRoot().removeNode()
        self.terrains = []
        # Breaking master heightmap into subimages
        heightmaps = []
        self.xchunks = (self.heightfield.getXSize() - 1) / factor
        self.ychunks = (self.heightfield.getYSize() - 1) / factor
        #print "X,Y chunks:", self.xchunks, self.ychunks
        n = 0
        for y in range(0, self.ychunks):
            for x in range(0, self.xchunks):
                heightmap = PNMImage(factor + 1, factor + 1)
                heightmap.copySubImage(self.heightfield,
                                       0,
                                       0,
                                       xfrom=x * factor,
                                       yfrom=y * factor)
                heightmaps.append(heightmap)
                n += 1

        # Generate GeoMipTerrains
        n = 0
        y = self.ychunks - 1
        x = 0
        for heightmap in heightmaps:
            terrain = GeoMipTerrain(str(n))
            terrain.setHeightfield(heightmap)
            terrain.setBruteforce(self.bruteForce)
            terrain.setBlockSize(self.blockSize)
            terrain.generate()
            self.terrains.append(terrain)
            root = terrain.getRoot()
            root.reparentTo(self.root)
            root.setPos(n % self.xchunks * factor, (y) * factor, 0)

            # In order to texture span properly we need to reiterate through every vertex
            # and redefine the uv coordinates based on our size, not the subGeoMipTerrain's
            root = terrain.getRoot()
            children = root.getChildren()
            for child in children:
                geomNode = child.node()
                for i in range(geomNode.getNumGeoms()):
                    geom = geomNode.modifyGeom(i)
                    vdata = geom.modifyVertexData()
                    texcoord = GeomVertexWriter(vdata, 'texcoord')
                    vertex = GeomVertexReader(vdata, 'vertex')
                    while not vertex.isAtEnd():
                        v = vertex.getData3f()
                        t = texcoord.setData2f(
                            (v[0] + self.blockSize / 2 + self.blockSize * x) /
                            (self.xsize - 1),
                            (v[1] + self.blockSize / 2 + self.blockSize * y) /
                            (self.ysize - 1))
            x += 1
            if x >= self.xchunks:
                x = 0
                y -= 1
            n += 1
Exemple #6
0
 def __init__(self, parent):
     DirectObject.DirectObject.__init__(self)
     self.debugMode = config.GetInt('loading-screen') == 2
     self.parent = parent
     self.state = False
     self.currScreenshot = None
     self.snapshot = None
     self.snapshotFrame = None
     self.snapshotFrameBasic = None
     self.currentTime = 0
     self.analyzeMode = False
     self.loadScale = 1.0
     self.unmappedTicks = []
     self.stepInfo = { }
     self.accept(base.win.getWindowEvent(), self.adjustSize)
     self.accept('tick', self.tick)
     self.currStage = 'unmapped'
     self.stagePercent = 0
     self.numObjects = 0
     self.currPercent = 0.0
     self.line = LineSegs()
     self.line.setColor((0, 0, 0, 1))
     self.line.setThickness(1)
     self.stageLabel = None
     self.currNum = 0
     self.overallPercent = 0
     self.lastPercent = 0
     self.topLock = aspect2dp.attachNewNode('topShift')
     self.root = self.topLock.attachNewNode('loadingScreenRoot')
     self.root.setZ(-1)
     self.root.stash()
     self.model = loader.loadModel('models/gui/pir_m_gui_gen_loadScreen.bam')
     self.model.setP(90)
     self.model.reparentTo(self.root)
     cm = CardMaker('backdrop')
     cm.setFrame(-10, 10, -10, 10)
     if self.debugMode:
         self.backdrop = self.root.attachNewNode(cm.generate())
         self.backdrop.setX(-1.5)
         self.backdrop.setZ(-1)
         self.backdrop.setScale(4)
         self.backdrop.setColor(0.5, 0.5, 0.5, 1)
         cm = CardMaker('loadingBarBase')
         cm.setFrame(-0.90000000000000002, 0.90000000000000002, 0.10000000000000001, 0.5)
         self.loadingBarBacking = self.root.attachNewNode(cm.generate())
         self.loadingBarRoot = self.root.attachNewNode('loadingBarRoot')
         cm.setName('analysisBarBase')
         cm.setFrame(-0.90000000000000002, 0.90000000000000002, -0.5, -0.10000000000000001)
         self.analysisBar = self.root.attachNewNode(cm.generate())
         self.analysisBarRoot = self.root.attachNewNode('analysisBarRoot')
         self.analysisBar.hide()
         self.analysisButtons = []
         self.enterToContinue = DirectLabel(parent = self.root, text = 'Press Shift To Continue', relief = None, text_scale = 0.10000000000000001, pos = (0, 0, -0.90000000000000002), text_align = TextNode.ACenter)
         self.enterToContinue.hide()
         self.stageLabel = DirectLabel(parent = self.root, text = '', relief = None, text_scale = 0.10000000000000001, pos = (-1.25, 0, 0.75), text_align = TextNode.ALeft, textMayChange = 1)
         self.tickLabel = DirectLabel(parent = self.root, text = '', relief = None, text_scale = 0.10000000000000001, pos = (0.75, 0, 0.75), textMayChange = 1)
         self.overallLabel = DirectLabel(parent = self.root, text = '', relief = None, text_scale = 0.10000000000000001, pos = (0, 0, -0.75), textMayChange = 1)
     else:
         self.backdrop = loader.loadModel('models/gui/pir_m_gui_gen_loadScreen')
         self.backdrop.reparentTo(self.root)
         bg = self.backdrop.find('**/expandable_bg')
         bg.setScale(1000, 1, 1000)
         bg.flattenStrong()
         self.backdrop.find('**/loadbar_grey').setColorScale(0.14999999999999999, 0.14999999999999999, 0.14999999999999999, 0.10000000000000001)
         self.loadingBar = self.backdrop.find('**/loadbar')
         self.loadingBar.setColorScale(0.20000000000000001, 0.59999999999999998, 0.5, 1)
         self.loadingPlank = NodePathCollection()
         self.loadingPlank.addPath(self.backdrop.find('**/plank_loading_bar'))
         self.loadingPlank.addPath(self.backdrop.find('**/loadbar'))
         self.loadingPlank.addPath(self.backdrop.find('**/loadbar_frame'))
         self.loadingPlank.addPath(self.backdrop.find('**/loadbar_grey'))
         self.titlePlank = self.backdrop.find('**/plank_title')
         self.percentLabel = DirectLabel(text = '0%', parent = self.root, relief = None, text_font = PiratesGlobals.getPirateFont(), text_fg = PiratesGuiGlobals.TextFG2, text_shadow = PiratesGuiGlobals.TextShadow, text_scale = 0.031, pos = (0, 0, -0.44450000000000001), textMayChange = 1)
         self.loadingPlank.addPath(self.percentLabel)
         self.screenshot = self.backdrop.find('**/screenshot')
         copyGeom = self.loadingBar.find('**/+GeomNode').node().getGeom(0)
         format = copyGeom.getVertexData().getFormat()
         primitive = copyGeom.getPrimitive(0)
         data = GeomVertexData(self.screenshot.node().getGeom(0).getVertexData())
         data.setFormat(format)
         writer = GeomVertexWriter(data, 'texcoord')
         writer.setData2f(0, 0)
         writer.setData2f(1, 0)
         writer.setData2f(1, 1)
         writer.setData2f(0, 1)
         geom = Geom(data)
         geom.addPrimitive(primitive)
         self.screenshot.node().removeGeom(0)
         self.screenshot.node().addGeom(geom)
         self.titlePlankMiddle = self.backdrop.find('**/plank_title_middle_box')
         self.titlePlankLeft = self.backdrop.find('**/plank_title_left')
         self.titlePlankRight = self.backdrop.find('**/plank_title_right')
     self.loadingBarColors = [ (((i % 10) / 10.0 + 0.5) / 2.0, ((i % 100) / 10 / 10.0 + 0.5) / 2.0, (i / 100 / 10.0 + 0.5) / 2.0, 1) for i in range(1000) ]
     random.shuffle(self.loadingBarColors)
     self.lastUpdateTime = globalClock.getRealTime()
     self.locationLabel = DirectLabel(parent = self.root, relief = None, text = '', text_font = PiratesGlobals.getPirateOutlineFont(), text_fg = PiratesGuiGlobals.TextFG1, text_shadow = PiratesGuiGlobals.TextShadow, text_scale = PiratesGuiGlobals.TextScaleTitleJumbo * 0.69999999999999996, text_align = TextNode.ACenter, pos = (0.0, 0.0, 0.51500000000000001), textMayChange = 1)
     self.locationText = None
     self.hintLabel = DirectLabel(parent = self.root, relief = None, text = '', text_font = PiratesGlobals.getPirateOutlineFont(), text_fg = PiratesGuiGlobals.TextFG1, text_shadow = PiratesGuiGlobals.TextShadow, text_scale = PiratesGuiGlobals.TextScaleTitleJumbo * 0.5, text_align = TextNode.ACenter, pos = (0.0, 0.0, -0.62), text_wordwrap = 30, textMayChange = 1)
     self.hintText = None
     self.adImage = None
     self.allowLiveFlatten = ConfigVariableBool('allow-live-flatten')
     self.title_art = []
     self.tempVolume = []
     self.adjustSize(base.win)
     gsg = base.win.getGsg()
     if gsg:
         self.root.prepareScene(gsg)