Для работы примера требуется положить рядом три файла 1.png, 2.png, 3.png которые будут служить текстурой
Код
from pandac.PandaModules import *
loadPrcFileData("editor-startup", "show-frame-rate-meter #t")
import direct.directbase.DirectStart
from random import randint
node = GeomNode('gnode')
geoms = []
textures_count = 5
# Creating a separate geom for the each textures
# and prepearing the vertex data format for this geom
for i in xrange(textures_count):
gvd = GeomVertexData('name', GeomVertexFormat.getV3t2(), Geom.UHStatic)
geom = Geom(gvd)
prim = GeomTriangles(Geom.UHStatic)
vertex = GeomVertexWriter(gvd, 'vertex')
texcoord = GeomVertexWriter(gvd, 'texcoord')
tex = loader.loadTexture('%i.png' % (i+1))
tex.setMagfilter(Texture.FTLinearMipmapLinear)
tex.setMinfilter(Texture.FTLinearMipmapLinear)
geoms.append({'geom':geom,
'prim':prim,
'vertex':vertex,
'texcoord':texcoord,
'index':0,
'gvd':gvd,
'texture':tex})
# Making and filling the mesh data
for x in xrange(0,100):
for z in xrange(0,100):
t_img = randint(0,textures_count - 1) # assign with random texture
i = geoms[t_img]['index']
geoms[t_img]['vertex'].addData3f(x, 0, z)
geoms[t_img]['texcoord'].addData2f(0, 0)
geoms[t_img]['vertex'].addData3f(x, 0, z+1)
geoms[t_img]['texcoord'].addData2f(0, 1)
geoms[t_img]['vertex'].addData3f(x+1, 0, z+1)
geoms[t_img]['texcoord'].addData2f(1, 1)
geoms[t_img]['vertex'].addData3f(x+1, 0, z)
geoms[t_img]['texcoord'].addData2f(1, 0)
# d: index displace, becouse we use one vertex pool for the all geoms
d = i * 4
geoms[t_img]['prim'].addVertices(d, d + 2, d + 1)
geoms[t_img]['prim'].addVertices(d, d + 3, d + 2)
geoms[t_img]['index'] += 1
# Close a primitive, add them to the geom and add the texture attribute
for i in xrange(textures_count):
geoms[i]['prim'].closePrimitive()
geoms[i]['geom'].addPrimitive(geoms[i]['prim'])
node.addGeom(geoms[i]['geom'])
node.setGeomState(i, node.getGeomState(i).addAttrib(TextureAttrib.make(geoms[i]['texture'])))
terrain = render.attachNewNode(node)
#terrain.setRenderModeWireframe()
terrain.analyze()
base.cam.setPos(0,-50,15)
run()