Runtime mesh from scratch
Look at the sample “MeshWave”: the onInit shows how to create a mesh from scratch, and on the onEnterFrame shows how to modify positions/normals at runtime.
--------------------------------------------------------------------------------
function aiLevelEditor.generateMeshObject ( x, y, z )
--------------------------------------------------------------------------------
local hObject = this.createObject ( "mdlBlank" )
if hObject then
log.message ( "LE: Blank object created" )
-- The first reference to hMesh doesn't seem to point to the right thing...
-- Override mesh of object with a new blank one
local hMesh = shape.createRuntimeMesh ( hObject )
-- But this reference will work
hMesh = shape.getMesh ( hObject )
mesh.addSubset ( hMesh )
-- Index buffer requires 3 indexes per triangle, hence 6 for a quad
mesh.createSubsetIndexBuffer ( hMesh, 0, 0, 6 )
-- 4 vertices per quad
mesh.createSubsetVertexBuffer ( hMesh, 0, 4 )
-- Locking sounds like a good idea
mesh.lockSubsetVertexBuffer ( hMesh, 0, mesh.kLockModeWrite )
mesh.lockSubsetIndexBuffer ( hMesh, 0, 0, mesh.kLockModeWrite )
-- Quad is a total of 20 x 20 large, created with it's center at x, y, z
mesh.setSubsetVertexPosition ( hMesh, 0, 0, 10, 0, 10)
mesh.setSubsetVertexPosition ( hMesh, 0, 1, -10, 0, 10)
mesh.setSubsetVertexPosition ( hMesh, 0, 2, -10, 0, -10)
mesh.setSubsetVertexPosition ( hMesh, 0, 3, 10, 0, -10)
-- Set texture coordinates per vertex, note the coordinate system is OpenGL
-- So the 'v' coordinate is sort of opposite than you'd expect
mesh.setSubsetVertexTexCoord ( hMesh, 0, 0, 0, 0, 1)
mesh.setSubsetVertexTexCoord ( hMesh, 0, 1, 0, 1, 1)
mesh.setSubsetVertexTexCoord ( hMesh, 0, 2, 0, 1, 0)
mesh.setSubsetVertexTexCoord ( hMesh, 0, 3, 0, 0, 0)
-- Indice buffer tells the order to connect each point to make triangles
-- These need to be drawn counter-clockwise or the faces will be flipped
-- Note some vertices are connected more than once, you can connect these any way you
-- want if it works
mesh.setSubsetIndexValue ( hMesh, 0, 0, 0, 0 )
mesh.setSubsetIndexValue ( hMesh, 0, 0, 1, 3 )
mesh.setSubsetIndexValue ( hMesh, 0, 0, 2, 1 )
mesh.setSubsetIndexValue ( hMesh, 0, 0, 3, 3 )
mesh.setSubsetIndexValue ( hMesh, 0, 0, 4, 2 )
mesh.setSubsetIndexValue ( hMesh, 0, 0, 5, 1 )
mesh.unlockSubsetVertexBuffer ( hMesh, 0 )
mesh.unlockSubsetIndexBuffer ( hMesh, 0, 0 )
-- These will update stuff...
-- updateBoundingVolumes is really necessary, otherwise your geometry will spaz out
-- and be picky about showing itself
mesh.computeSubsetVertexNormals ( hMesh, 0, 0 )
mesh.computeSubsetVertexTangents ( hMesh, 0 )
mesh.updateBoundingVolumes ( hMesh )
log.message ( "Subset Count: " .. mesh.getSubsetCount ( hMesh ) )
log.message ( "Mesh name: " .. shape.getMeshName ( hObject ) )
log.message ( "Vertex Count: " .. mesh.getSubsetVertexCount ( hMesh, 0 ) )
log.message ( "Index count: " .. mesh.getSubsetIndexCount ( hMesh, 0, 0 ) )
-- Set the material (grass in this case)
shape.setMeshMaterial ( hObject, "matGrass" )
-- Center the quad at the passed coordinates
object.setTranslation ( hObject, x, y, z, object.kGlobalSpace )
end
--------------------------------------------------------------------------------
end
--------------------------------------------------------------------------------