Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- #
- # Generate a mesh
- #
- #
- from panda3d.core import Texture, GeomNode
- from panda3d.core import GeomVertexFormat, GeomVertexData
- from panda3d.core import Geom, GeomTriangles, GeomVertexWriter
- from panda3d.core import Vec3, Vec4, Point3
- import time
- def myNormalize(myVec):
- myVec.normalize()
- return myVec
- class MeshGenerator():
- def __init__(self, name = 'Mesh'):
- self.name = name
- self.finished = False
- self.format = GeomVertexFormat.getV3c4t2()
- #print self.format
- self.vertexData = GeomVertexData(name, self.format, Geom.UHStatic)
- self.vertexData.setNumRows(26136)
- self.mesh = Geom(self.vertexData)
- self.triangles = GeomTriangles(Geom.UHStatic)
- self.triangleData = self.triangles.modifyVertices()
- self.triangleData.setNumRows(26136)
- self.vertex = GeomVertexWriter(self.vertexData, 'vertex')
- self.normal = GeomVertexWriter(self.vertexData, 'normal')
- self.color = GeomVertexWriter(self.vertexData, 'color')
- self.texcoord = GeomVertexWriter(self.vertexData, 'texcoord')
- self.faceCount = 0
- def makeFace(self, x1, y1, z1, x2, y2, z2, id, color):
- if x1 != x2:
- self.vertex.addData3f(x1, y1, z1)
- self.vertex.addData3f(x2, y1, z1)
- self.vertex.addData3f(x2, y2, z2)
- self.vertex.addData3f(x1, y2, z2)
- else:
- self.vertex.addData3f(x1, y1, z1)
- self.vertex.addData3f(x2, y2, z1)
- self.vertex.addData3f(x2, y2, z2)
- self.vertex.addData3f(x1, y1, z2)
- self.color.addData4f(color, color, color, 1.0)
- self.color.addData4f(color, color, color, 1.0)
- self.color.addData4f(color, color, color, 1.0)
- self.color.addData4f(color, color, color, 1.0)
- if id == 1:
- self.texcoord.addData2f(0.0, 1.0)
- self.texcoord.addData2f(0.0, 0.0)
- self.texcoord.addData2f(0.5, 0.0)
- self.texcoord.addData2f(0.5, 1.0)
- elif id == 2:
- self.texcoord.addData2f(0.5, 1.0)
- self.texcoord.addData2f(0.5, 0.0)
- self.texcoord.addData2f(1.0, 0.0)
- self.texcoord.addData2f(1.0, 1.0)
- vertexId = self.faceCount * 4
- self.triangles.addVertices(vertexId, vertexId + 1, vertexId + 3)
- self.triangles.addVertices(vertexId + 1, vertexId + 2, vertexId + 3)
- self.faceCount += 1
- def makeFrontFace(self, x, y, z, id):
- self.makeFace(x + 1, y + 1, z - 1, x, y + 1, z, id, 0.6)
- def makeBackFace(self, x, y, z, id):
- self.makeFace(x, y, z - 1, x + 1, y, z, id, 0.85)
- def makeRightFace(self, x, y, z, id):
- self.makeFace(x + 1, y, z - 1, x + 1, y + 1, z, id, 1.0)
- def makeLeftFace(self, x, y, z, id):
- self.makeFace(x, y + 1, z - 1, x, y, z, id, 0.9)
- def makeTopFace(self, x, y, z, id):
- self.makeFace(x + 1, y + 1, z, x, y, z, id, 1.0)
- def makeBottomFace(self, x, y, z, id):
- self.makeFace(x, y + 1, z - 1, x + 1, y, z - 1, id, 0.70)
- def getMesh(self):
- return self.mesh
- def getGeomNode(self):
- if self.finished == False:
- self.triangles.closePrimitive()
- self.mesh.addPrimitive(self.triangles)
- self.finished = True
- geomNode = GeomNode(self.name)
- geomNode.addGeom(self.mesh)
- return geomNode
- '''ft = time.time()
- mesh = MeshGenerator('Meshymesh')
- for i in range(0, 100000):
- mesh.makeRightFace(0,1,1, 1)
- print time.time() - ft'''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement