# Creating mesh (vertex buffer) at runtime

**URL:** <https://forum.defold.com/t/creating-mesh-vertex-buffer-at-runtime/2525>\
**Category:** Questions\
**Created:** [August 1, 2016, 1:00pm UTC](https://forum.defold.com/t/creating-mesh-vertex-buffer-at-runtime/2525 "2016-08-01T13:00:58Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ivan\_Lytkin](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/ivan_lytkin/32/2654_2.png) [@Ivan\_Lytkin](https://forum.defold.com/u/Ivan_Lytkin)\
**Post date:** [August 1, 2016, 1:00pm UTC](https://forum.defold.com/t/creating-mesh-vertex-buffer-at-runtime/2525/1 "2016-08-01T13:00:58Z")

</div>

Hi!  
Is it possible to define vertices via script and render them with particular material (shaders) ?  
-thanks!

---

<div class="post-metadata">

**Author:** ![sicher](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/sicher/32/8_2.png) [@sicher](https://forum.defold.com/u/sicher)\
**Post date:** [August 1, 2016, 2:56pm UTC](https://forum.defold.com/t/creating-mesh-vertex-buffer-at-runtime/2525/2 "2016-08-01T14:56:40Z")

</div>

Not at the moment.

---

<div class="post-metadata">

**Author:** ![Ivan\_Lytkin](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/ivan_lytkin/32/2654_2.png) [@Ivan\_Lytkin](https://forum.defold.com/u/Ivan_Lytkin)\
**Post date:** [August 1, 2016, 3:54pm UTC](https://forum.defold.com/t/creating-mesh-vertex-buffer-at-runtime/2525/3 "2016-08-01T15:54:33Z")

</div>

So what is the best way to implement something like this?

[http://gamedevelopment.tutsplus.com/tutorials/make-a-splash-with-dynamic-2d-water-effects--gamedev-236](http://gamedevelopment.tutsplus.com/tutorials/make-a-splash-with-dynamic-2d-water-effects--gamedev-236)

using predefined collada model with a custom shader?

---

<div class="post-metadata">

**Author:** ![Ragnar\_Svensson](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/r/b5ac83/32.png) [@Ragnar\_Svensson](https://forum.defold.com/u/Ragnar_Svensson)\
**Post date:** [August 1, 2016, 4:22pm UTC](https://forum.defold.com/t/creating-mesh-vertex-buffer-at-runtime/2525/4 "2016-08-01T16:22:50Z")

</div>

I would say use a collada model for the springy waves and the builtin particle system for the droplets (could also be done with models of course). I was really inspired by [this](https://directtovideo.wordpress.com/2009/10/06/a-thoroughly-modern-particle-system/) earlier this summer and wanted to try it out in Defold when I was on vacation. I ended up using a massive collada mesh (generated it in python) to get the quads and then did all of the sim in shaders. Because of GLES I had to do some work of squashing floats into the 8-bit color channels, but it was ok. It didn’t look anything like the link above, but it was cool to see 150k particles spraying at 60 fps in the browser. 🙂

---

<div class="post-metadata">

**Author:** ![Ivan\_Lytkin](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/ivan_lytkin/32/2654_2.png) [@Ivan\_Lytkin](https://forum.defold.com/u/Ivan_Lytkin)\
**Post date:** [August 1, 2016, 4:31pm UTC](https://forum.defold.com/t/creating-mesh-vertex-buffer-at-runtime/2525/5 "2016-08-01T16:31:03Z")

</div>

Cool.  
Could you share the script to generate collada model please?

---

<div class="post-metadata">

**Author:** ![Ragnar\_Svensson](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/r/b5ac83/32.png) [@Ragnar\_Svensson](https://forum.defold.com/u/Ragnar_Svensson)\
**Post date:** [August 1, 2016, 4:58pm UTC](https://forum.defold.com/t/creating-mesh-vertex-buffer-at-runtime/2525/6 "2016-08-01T16:58:03Z")

</div>

Sure! Can’t upload the python file for some reason, but this is the code. It’s a little hard coded with paths, you might want to change that.

```python
import sys

template = """<?xml version="1.0" encoding="utf-8"?>
<COLLADA xmlns="http://www.collada.org/2005/11/COLLADASchema" version="1.4.1">
  <asset><contributor><author></author><authoring_tool>FBX COLLADA exporter</authoring_tool><comments></comments></contributor><created>2012-10-10T12:34:45Z</created><keywords></keywords><modified>2012-10-10T12:34:45Z</modified><revision></revision><subject></subject><title></title><unit meter="1.000000" name="centimeter"></unit><up_axis>Y_UP</up_axis></asset>
  <library_geometries>
    <geometry id="pPlane1-lib" name="pPlane1Mesh">
      <mesh>
        <source id="pPlane1-POSITION">
          <float_array id="pPlane1-POSITION-array" count="{vertex_elem_count}">
            {positions}
          </float_array>
          <technique_common>
            <accessor source="#pPlane1-POSITION-array" count="{vertex_count}" stride="3">
              <param name="X" type="float"/>
              <param name="Y" type="float"/>
              <param name="Z" type="float"/>
            </accessor>
          </technique_common>
        </source>
        <source id="pPlane1-Normal0">
          <float_array id="pPlane1-Normal0-array" count="{vertex_elem_count}">
            {normals}
          </float_array>
          <technique_common>
            <accessor source="#pPlane1-Normal0-array" count="{vertex_count}" stride="3">
              <param name="X" type="float"/>
              <param name="Y" type="float"/>
              <param name="Z" type="float"/>
            </accessor>
          </technique_common>
        </source>
        <source id="pPlane1-UV0">
          <float_array id="pPlane1-UV0-array" count="{uv_elem_count}">
            {uvs}
          </float_array>
          <technique_common>
            <accessor source="#pPlane1-UV0-array" count="{uv_count}" stride="2">
              <param name="S" type="float"/>
              <param name="T" type="float"/>
            </accessor>
          </technique_common>
        </source>
        <vertices id="pPlane1-VERTEX">
          <input semantic="POSITION" source="#pPlane1-POSITION"/>
          <input semantic="NORMAL" source="#pPlane1-Normal0"/>
        </vertices>
        <triangles count="{tri_count}" material="lambert1">
          <input semantic="VERTEX" offset="0" source="#pPlane1-VERTEX"/>
          <input semantic="TEXCOORD" offset="1" set="0" source="#pPlane1-UV0"/>
          <p> {indices}</p></triangles>
      </mesh>
    </geometry>
  </library_geometries>
  <scene>
    <instance_visual_scene url="#"></instance_visual_scene>
  </scene>
</COLLADA>
"""

def flatten(x):
    return [v for sub in x for v in sub]

def gen_model(width, height, filename):
    quads = [[x, y] for x in range(width) for y in range(height)]
    positions = ([[x, y, 0, (x+1), y, 0, x, (y+1), 0, (x+1), (y+1), 0] for x,y in quads])
    positions = flatten(positions)
    vertex_elem_count = len(positions)
    vertex_count = vertex_elem_count / 3
    tri_count = len(quads)*2
    normals = ([[(x+0.5)/width, (y+0.5)/height, (x+(y*width))/float(width*height)] for x,y in quads for i in range(4)])
    normals = flatten(normals)
    uvs = [0.000000, 0.000000,
           1.000000, 0.000000,
           0.000000, 1.000000,
           1.000000, 1.000000]
    indices = [3, 2, 0, 3, 0, 1]
    indices = [[(i + q*4), i] for q in range(len(quads)) for i in indices]
    indices = flatten(indices)
    with open(filename, "w") as f:
        f.write(template.format(positions = " ".join(map(str, positions)),
            normals = " ".join(map(str, normals)),
            uvs = " ".join(map(str, uvs)),
            vertex_elem_count = vertex_elem_count,
            vertex_count = vertex_count,
            uv_elem_count = str(8),
            uv_count = str(4),
            indices = " ".join(map(str, indices)),
            tri_count = tri_count))

def main():
    width = int(sys.argv[1])
    height = int(sys.argv[2])
    filename = "models/grid_{}_{}.dae".format(width, height)
    gen_model(width, height, filename)

if __name__ == " __main__":
    main()

```

---

<div class="post-metadata">

**Author:** ![Ivan\_Lytkin](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/ivan_lytkin/32/2654_2.png) [@Ivan\_Lytkin](https://forum.defold.com/u/Ivan_Lytkin)\
**Post date:** [August 1, 2016, 5:32pm UTC](https://forum.defold.com/t/creating-mesh-vertex-buffer-at-runtime/2525/7 "2016-08-01T17:32:13Z")

</div>

Awesome, thanks!

---

<div class="post-metadata">

**Author:** ![sicher](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/sicher/32/8_2.png) [@sicher](https://forum.defold.com/u/sicher)\
**Post date:** [August 1, 2016, 7:12pm UTC](https://forum.defold.com/t/creating-mesh-vertex-buffer-at-runtime/2525/8 "2016-08-01T19:12:52Z")

</div>

I enabled file uploads of any file type into the forum.

EDIT: Enabled for python files, and some other types. It did not like \* it seems.

---

<div class="post-metadata">

**Author:** ![Ivan\_Lytkin](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/ivan_lytkin/32/2654_2.png) [@Ivan\_Lytkin](https://forum.defold.com/u/Ivan_Lytkin)\
**Post date:** [August 6, 2016, 3:31pm UTC](https://forum.defold.com/t/creating-mesh-vertex-buffer-at-runtime/2525/9 "2016-08-06T15:31:33Z")

</div>

@Ragnar_Svensson Don’t you think it could be a useful feature - the simplest collada grid generator in editor? E.g. New → Mesh → … and let define size and uv dimensions. It can be used to make full-screen quads also.

---

<div class="post-metadata">

**Author:** ![Ragnar\_Svensson](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/r/b5ac83/32.png) [@Ragnar\_Svensson](https://forum.defold.com/u/Ragnar_Svensson)\
**Post date:** [August 6, 2016, 3:48pm UTC](https://forum.defold.com/t/creating-mesh-vertex-buffer-at-runtime/2525/10 "2016-08-06T15:48:46Z")

</div>

Absolutely! Another idea could be to have a mode in the mesh where you don’t define a file, but rather the generational properties. So there is never a source file on disk, but there will be a compiled model for the game runtime. What do you think about that? It’s a small difference in UX, but conceptually it’s quite big and powerful.

---

<div class="post-metadata">

**Author:** ![Ivan\_Lytkin](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/ivan_lytkin/32/2654_2.png) [@Ivan\_Lytkin](https://forum.defold.com/u/Ivan_Lytkin)\
**Post date:** [August 6, 2016, 8:19pm UTC](https://forum.defold.com/t/creating-mesh-vertex-buffer-at-runtime/2525/11 "2016-08-06T20:19:44Z")

</div>

Yes, this approach is even better!

---

<div class="post-metadata">

**Author:** ![gregor.gullwi](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/g/0ea827/32.png) [@gregor.gullwi](https://forum.defold.com/u/gregor.gullwi)\
**Post date:** [August 9, 2017, 7:51pm UTC](https://forum.defold.com/t/creating-mesh-vertex-buffer-at-runtime/2525/12 "2017-08-09T19:51:46Z")

</div>

@Ragnar_Svensson, was this ever added as a feature request? Ping @Bjorn\_Ritzl as well 🙂

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [August 10, 2017, 6:49am UTC](https://forum.defold.com/t/creating-mesh-vertex-buffer-at-runtime/2525/13 "2017-08-10T06:49:07Z")

</div>

We have tasks in the backlog that covers this, but it’s hadn’t been given top priority I’m afraid.

---

<div class="post-metadata">

**Author:** ![morgerion](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/morgerion/32/6641_2.png) [@morgerion](https://forum.defold.com/u/morgerion)\
**Post date:** [February 9, 2022, 12:36am UTC](https://forum.defold.com/t/creating-mesh-vertex-buffer-at-runtime/2525/14 "2022-02-09T00:36:23Z")

</div>

What about **vertex texture fetch**? Also known as texture fetch in the vertex shader.  
As far as I understand, this option should be supported by default in GLES.
