How to set position of mesh vertex to some go position

How I can set one corner of mesh to some game object(dot in image) position. So when I drag go also mesh shape dynamically change that I will work on later.

Is their any help with this I’m learning this mesh component thing all. And this whole shader is bit complex to understand for me. Transforming these mesh vertex coordinates respective to world coordinates.

I think you can do something like this:

  1. create a buffer resource
  2. create a gameobject and a add a mesh component to it
  3. assign the buffer resource to the mesh in the ‘vertices’ field

In a script, you can set the actual vertex data like so:

-- "MESH_COMPONENT_ID" should be the id of your mesh component
local res_path = go.get("MESH_COMPONENT_ID", "vertices")

local buf = resource.get_buffer(res_path)
local stream_positions = buffer.get_stream(self.buffer, "position")

local go_wp = go.get_world_position("SOME_GAMEOBJECT_ID")

-- now you can access the vertex position by indexing the stream_positions table
-- note that the positions here are structured like (p0_x, p0_y, p0_z, p1_x, p1_y, p1_z and so on
-- the vertex format is specified by the "count" element in the buffer file, so it depends on how you have specified your .buffer file
stream_positions[1] = go_wp.x 
stream_positions[2] = go_wp.y
stream_positions[3] = go_wp.z 

Have a look at these docs:

I haven’t tested this but I would start in this end at least

1 Like

So I was trying get world position of these four dots and setting it to the positions of mesh vertex.
But mesh is not showing.

local function fill_positions(self, v0, v1, v2, v3)
    -- first triangle
    self.positions[1] = v0.x
    self.positions[2] = v0.y
    self.positions[3] = v0.z

    self.positions[4] = v1.x
    self.positions[5] = v1.y
    self.positions[6] = v1.z

    self.positions[7] = v2.x
    self.positions[8] = v2.y
    self.positions[9] = v2.z

    -- second triangle
    self.positions[10] = v0.x
    self.positions[11] = v0.y
    self.positions[12] = v0.z

    self.positions[13] = v2.x
    self.positions[14] = v2.y
    self.positions[15] = v2.z

    self.positions[16] = v3.x
    self.positions[17] = v3.y
    self.positions[18] = v3.z
end

function init(self)
    
    self.res = go.get("#mesh", "vertices")
    print(self.res)
    
    self.buffer = resource.get_buffer(self.res)
    print(self.buffer)
    
    self.positions = buffer.get_stream(self.buffer, "position")
    print(self.positions)

    -- some game object
    local point = go.get_world_position('/point')
    local point1 = go.get_world_position('/point1')
    local point2 = go.get_world_position('/point2')
    local point3 = go.get_world_position('/point3')
    -- print(point)

    -- if the buffer has some data you could change these values here
    -- self.positions[1] = self.positions[1] + dt

    -- create a new buffer, since the one in the resource doesn't have enough size
    self.new_buffer = buffer.create(6, {
        { name = hash("position"),
         type=buffer.VALUE_TYPE_FLOAT32,
         count = 3 }
    })

    -- get the position stream
    self.positions = buffer.get_stream(self.new_buffer, "position")

    self.v0 = point
    self.v1 = point1
    self.v2 = point2
    self.v3 = point3
    
    -- self.v0 = vmath.vector3(-1, -1, 0)
    -- self.v1 = vmath.vector3( 1, -1, 0)
    -- self.v2 = vmath.vector3( 1,  1, 0)
    -- self.v3 = vmath.vector3(-1,  1, 0)

    fill_positions(self, self.v0, self.v1, self.v2, self.v3)

    resource.set_buffer(self.res, self.new_buffer)
end

function update(self, dt)
end

If I will put values between -1 and 1 then mesh will show as covering full view area.

    -- self.v0 = vmath.vector3(-1, -1, 0)
    -- self.v1 = vmath.vector3( 1, -1, 0)
    -- self.v2 = vmath.vector3( 1,  1, 0)
    -- self.v3 = vmath.vector3(-1,  1, 0)

Where I’m getting wrong. Is their I have to do some settings in materials or vertex shader script for these.

What do you do in your render script and vertex shader?

I’m using this example project and didn’t change render script and vertex shader.

1 Like

Ah I see the problem now… The example sets a projection matrix to the identity in the render script. It’s a bit hard to understand what exactly you are looking for with all this, but you need to change the ‘render.set_projection(vmath.matrix4())’ to something else if you dont want coordinates in [-1, 1]. Maybe a good start would be to simple set it with render.set_projection(get_projection(self))? That will give you a 2D orthogonal projection and maybe you can start with that.

-- render custom meshes
render.set_projection(vmath.matrix4())
render.set_view(vmath.matrix4())
render.draw(self.model_pred)

These this are kinda komplex to be honest, so maybe there are easier ways to solve what you want to do? Could you describe a bit more what you are doing and what you want to achieve?

Thanks @jhonny.goransson . It works. I will share what I’m working on soon. Basically trying to build game where flat 2d room of house can be desingend so room divider can drag to change room design layout and different wallpapers and floors textures can be set on these.

2 Likes

Cool! Yes please share some progress and how you use the mesh component :+1:

1 Like