Jump to content

How to change vertices of a mesh?


timetocode
 Share

Recommended Posts

I'm trying to create a mesh that consists of a few dozen triangles that are going to change every frame (hopefully performs okay....)

How do I actually do that though?

Create mesh:

const shader = Shader.from(vertexSrc, fragSrc)

const geometry = new Geometry()
    .addAttribute('aVertexPosition', [initialVerts])
    .addAttribute('aVertexColor', [initialColors])

const mesh = new Mesh(geometry, shader, null, DRAW_MODES.TRIANGLES)

Attempt at changing aVertexPosition and aVertexColor each frame:

mesh.geometry.addAttribute('aVertexPosition', newVertices)
mesh.geometry.addAttribute('aVertexColor',  newColors)

Error:  Cannot read property 'updateID' of undefined;  originating from GeometrySystem.updateBuffers.

Link to comment
Share on other sites

Thanks @bubamara and @ivan.popelyshev I've been able to mutate my mesh a bit now. 

I'm stuck trying to change the indices, any idea what I'm doing wrong?

const vertBuffer = geometry.getBuffer('aVertexPosition')
vertBuffer.update(new Float32Array(vertices))  // works
const colorBuffer = geometry.getBuffer('aVertexColor')
colorBuffer.update(new Float32Array(colors))   // works
const indexBuffer = geometry.getIndex()
indexBuffer.update(new Float32Array(indices))  // makes my mesh disappear

All of this except changing the indices seems to work. If I change the indices my object vanishes.

The `vertices`, `colors` and `indices` aren't actually changing -- i'm just creating the same circle over and over again and trying to update the buffers.. so maybe I have a more basic issues that is making my shape disappear.

Here is the shape that I'm drawing (black lines and green points added for debugging, the mesh is the whole lighting overlay of a radial gradient within the circle and shadows in the other triangles).

image.png.7df831ace16f6f643b0d2d2f45f909e9.png

Edited by timetocode
Link to comment
Share on other sites

not all devices support int32, use uint16

btw you can pass Float32Array there once and then modify the array itself , just calling "buffer.update()" each time you do that. No need of creating new instances of array each time. It doesnt matter in small cases (~1000 vertices) but does matter in big ones 10k-100k

Edited by ivan.popelyshev
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...