Dynamically generate a mesh

Visualising unstructured or analytical data can require generating 3D meshes.

This snippet creates a cube model with positions and normals, and adds an instance of it to the scene.

-- create the required library nodes
local modelNode = vrCreateNode("Model", "Cube", vrLibrariesNode())
local geoGroup = vrCreateNode("GeoGroup", "GeoGroup", modelNode)
local mesh = vrCreateNode("Mesh", "Mesh", geoGroup)
local pos = vrCreateNode("VertexAttrib", "pos", mesh)<span class="fr-marker" data-id="0" data-type="false" style="display: none; line-height: 0;"></span><span class="fr-marker" data-id="0" data-type="true" style="display: none; line-height: 0;"></span>
local nrm = vrCreateNode("VertexAttrib", "nrm", mesh)
local trilist = vrCreateNode("IndexPrim", "trilist", mesh)
 
-- setup vertex/index attributes
pos.Size = 3
nrm.Size = 3
trilist.Prim = "trilist"
 
-- create positions
vrNodeArraySetSize(pos, "Content", 72)
local vertvec = pos.Content
-- left
vertvec[ 1], vertvec[ 2], vertvec[ 3] = -1, -1, -1
vertvec[ 4], vertvec[ 5], vertvec[ 6] = -1, -1,  1
vertvec[ 7], vertvec[ 8], vertvec[ 9] = -1,  1,  1
vertvec[10], vertvec[11], vertvec[12] = -1,  1, -1
 
-- right
vertvec[13], vertvec[14], vertvec[15] =  1, -1, -1
vertvec[16], vertvec[17], vertvec[18] =  1, -1, -1
vertvec[19], vertvec[20], vertvec[21] =  1, -1, -1
vertvec[22], vertvec[23], vertvec[24] =  1, -1, -1
 
-- front
vertvec[25], vertvec[26], vertvec[27] = -1, -1, -1
vertvec[28], vertvec[29], vertvec[30] = -1,  1, -1
vertvec[31], vertvec[32], vertvec[33] =  1,  1, -1
vertvec[34], vertvec[35], vertvec[36] =  1, -1, -1
 
-- back
vertvec[37], vertvec[38], vertvec[39] = -1, -1,  1
vertvec[40], vertvec[41], vertvec[42] = -1,  1,  1
vertvec[43], vertvec[44], vertvec[45] =  1,  1,  1
vertvec[46], vertvec[47], vertvec[48] =  1, -1,  1
 
-- bottom
vertvec[49], vertvec[50], vertvec[51] = -1, -1, -1
vertvec[52], vertvec[53], vertvec[54] = -1, -1,  1
vertvec[55], vertvec[56], vertvec[57] =  1, -1,  1
vertvec[58], vertvec[59], vertvec[60] =  1, -1, -1
 
-- top
vertvec[61], vertvec[62], vertvec[63] = -1, 1, -1
vertvec[64], vertvec[65], vertvec[66] = -1, 1,  1
vertvec[67], vertvec[68], vertvec[69] =  1, 1,  1
vertvec[70], vertvec[71], vertvec[72] =  1, 1, -1
 
 
vrNodeArraySetSize(nrm, "Content", 72)
local nrmvec = nrm.Content
for i=0,3 do
  nrmvec[3*i + 1], nrmvec[3*i + 2], nrmvec[3*i + 3] = -1, 0, 0
end
for i=4,7 do
  nrmvec[3*i + 1], nrmvec[3*i + 2], nrmvec[3*i + 3] = 1, 0, 0
end
for i=8,11 do
  nrmvec[3*i + 1], nrmvec[3*i + 2], nrmvec[3*i + 3] = 0, 0, -1
end
for i=12,15 do
  nrmvec[3*i + 1], nrmvec[3*i + 2], nrmvec[3*i + 3] = 0, 0, 1
end
for i=16,19 do
  nrmvec[3*i + 1], nrmvec[3*i + 2], nrmvec[3*i + 3] = 0, -1, 0
end
for i=20,23 do
  nrmvec[3*i + 1], nrmvec[3*i + 2], nrmvec[3*i + 3] = 0, -1, 0
end
 
-- create index buffer
vrNodeArraySetSize(trilist, "Content", 36)
local idxvec = trilist.Content
idxvec[ 1], idxvec[ 2], idxvec[ 3] = 0, 1, 2
idxvec[ 4], idxvec[ 5], idxvec[ 6] = 2, 3, 0
idxvec[ 7], idxvec[ 8], idxvec[ 9] = 4, 5, 6
idxvec[10], idxvec[11], idxvec[12] = 6, 7, 4
idxvec[13], idxvec[14], idxvec[15] = 8, 9, 10
idxvec[16], idxvec[17], idxvec[18] = 10, 11, 8
idxvec[19], idxvec[20], idxvec[21] = 12, 13, 14
idxvec[22], idxvec[23], idxvec[24] = 14, 15, 12
idxvec[25], idxvec[26], idxvec[27] = 16, 17, 18
idxvec[28], idxvec[29], idxvec[30] = 18, 19, 16
idxvec[31], idxvec[32], idxvec[33] = 20, 21, 22
idxvec[34], idxvec[35], idxvec[36] = 22, 23, 20
 
-- create a visual in the scene
local assembly = vrCreateNode("Assembly", "Cube", vrScenesNode())
local visual = vrCreateNode("Visual", "Cube", assembly)
visual.Model = modelNode


E
Ericka is the author of this solution article.

Did you find it helpful? Yes No

Send feedback
Sorry we couldn't be helpful. Help us improve this article with your feedback.