I’ve been working through the Panda3D manual to implement “at-runtime” model generation, but am having issues mapping a basic texture to the GoemNode I created. Here’s the code I’m using:
from direct.showbase.ShowBase import ShowBase
from panda3d.core import Geom, GeomNode, GeomVertexArrayFormat, \
GeomVertexFormat, GeomVertexData, \
GeomVertexWriter, GeomTristrips, Texture, \
TextureStage, TexGenAttrib
from math import pi, sin, cos
from direct.task import Task
class Game(ShowBase):
def __init__(self):
ShowBase.__init__(self)
# Disable default mouse controls
self.disableMouse()
# Initialize a single Vertex Array Format (VAF) with two columns
vaf = GeomVertexArrayFormat()
vaf.addColumn("vertex", 3, Geom.NT_uint8, Geom.C_point)
vaf.addColumn("texcoord", 2, Geom.NT_uint8, Geom.C_texcoord)
# Add VAF to new Vertex Format (VF) object
vf = GeomVertexFormat()
vf.addArray(vaf)
# Register Vertex Format and overwrite back into "vf"
vf = GeomVertexFormat.registerFormat(vf)
# Associate Vertex Format with a new Vertex Data (VD)
vd = GeomVertexData('cubeface', vf, Geom.UH_static)
# Set expected row count (number of vertices) in newVertex Data object
vd.setNumRows(4)
# Initialize writers for each column specified in the VAF
vertex_writer = GeomVertexWriter(vd, "vertex")
texture_writer = GeomVertexWriter(vd, "texcoord")
# Simple vertex dictionary used to populate Vertex Data object
verts = {
0: {
"vertex": (0,0,1),
"uv": (0,1)
},
1: {
"vertex": (0,0,0),
"uv": (0,0)
},
2: {
"vertex": (1,0,1),
"uv": (1,0)
},
3: {
"vertex": (1,0,0),
"uv": (0,1)
}
}
# Iterate through vertices and add to Vertex Data object
for i in range(4):
v = verts[i]
vertex_writer.addData3(*v.get("vertex"))
texture_writer.addData2(*v.get("uv"))
# Initialize Tristrips primative and add vertices sequentially
prim = GeomTristrips(Geom.UH_static)
prim.add_consecutive_vertices(0, 4)
prim.close_primitive()
# Add primative to new Geom object
geom = Geom(vd)
geom.addPrimitive(prim)
# Add Geom to new GeomNode
gnode = GeomNode('gnode')
gnode.addGeom(geom)
# Attach GeomNode to scene graph at render Node Path
cubeface_np = self.render.attachNewNode(gnode)
# Load 16x16 pixel 2D texture to map onto cubeface
tex = loader.loadTexture("./assets/models/maps/grassblock/side.png")
cubeface_np.setTexture(tex)
# Position camera to look at cube face (positioned at 0,0,0)
self.camera.setPos(2,-5,0)
self.camera.setH(20)
app = Game()
app.run()
Here’s a screenshot of the expected 16x16 pixel texture image: expected.png
And here’s the scene I get when I run the game: actual.png
I’d appreciate any input on what I’m doing wrong here. Thanks!