display.newMesh() Preview

Hello everyone.

As the next step in improving Corona’s graphics pipeline, I’m glad to introduce the preview of display.newMesh(). This new API creates a Shape Object with provided vertices. Please, give it a spin. I made demo project with different features of meshes, including a test implementation of the Spine runtime. Please, do not use it in real projects for now. I’ve sent a pull request to actual repo with Spine Runtimes. It still has to be properly tested.

Full documentation is available here: https://docs.coronalabs.com/daily/api/library/display/newMesh.html

Here is quick reference:

[lua]

local mesh = display.newMesh(

{

    – parent display group (optional)

    – parent = group

    – position relative to parent (optional)

    x = 100,

    y = 100,

    – mesh mode. One of “triangles”, “strip”, “fan”, or “indexed”

    mode = “triangles”,

    – vertices used to build a mesh

    vertices = {

        0,0, 50,0, 0,100,

        0,100, 50,0, 100,100,

        100,100, 50,0, 100,0

    },

    – optional texture coordinates; this must be same size as vertices

    uvs = {

        0,0, 0.5,0, 0,1,

        0,1, 0.5,0, 1,1,

        1,1, 0.5,0, 1,0

    },

    – if mode is “indexed” these triplets would be used to form triangles, ignored otherwise

    – indices = { 1,2,3,   2,3,4,  3,4,5 },

    – if set to true, indices would be treated ad 0 based array, false by default

    – zeroBasedIndices = true

})

– Accessing properties of Mesh path with mesh.path:

– All vertices would be offset so origin is in the center of mesh; to restore position

– use following code:

mesh:translate( mesh.path:getVertexOffset() )

– modifying & accessing mesh (you can not delete or create new vertices, only modify existing)

– positions:

local x, y = mesh.path:getVertex( 3 )

mesh.path:setVertex( 3, x, y )

– texture coordinates:

local u, v = mesh.path:getUV( 3 )

mesh.path:setUV( 3, u, v )

[/lua]

Please, note, that this feature is still in testing. I would suggest not to use in production, but feel free to play with it and tell me if something is wrong.

Download latest daily build to use it (note, there is a know rare crash when using meshes in 2016.2890, it would be fixed in next daily build).

Hi, this is pretty cool, I’m looking forward to this.

Can you give us a way to get the uvs from an ImageSheet? That would make this perfect.

Thanks,

Albert

I don’t really understand why do you need to extract UV information from image sheet. You can use image sheet paint with meshes.

[lua]

mesh.fill = { type=“image”, sheet=sheet, frame=2}

[/lua]

Just don’t specify UVs, or specify them as for full texture, not just sheet frame. Also, if you want to calculate UV yourself, it wouldn’t be too hard.

I’ve been using a preview of the preview.  :slight_smile:

Some examples I have ready:

Cloth (warning: could probably use some tuning, but some numbers can completely destabilize it  :D)

Mesh-based curve

Mesh-based curve, take 2

I have a couple smaller examples, but haven’t put up Gists of those yet.

I’ve also got something of a morphing plugin in the works, maybe around 50% done on the mesh side of things. (Perhaps I’ll just make this an independent component.)

Thanks [member=‘StarCrunch’], you was great help in putting together initial implementation of Spine runtime changes and testing.

Looks neat! How do you set the texture?

I would really like a mesh batching API:

https://forums.coronalabs.com/topic/55072-corona-is-the-only-runtime-not-supporting-meshes/?p=323341

IMHO, there should be an immediate mode API with the persistent / scene graph node API on top. If not, there is going to be a bunch of juggling scene graph objects.

Thanks for your input. You’re right that juggling scene object isn’t much fun, but incorporating manual batching exactly as you described may be problematic due to Corona’s internal batch system, as well as resource ownership model.

Currently, meshes would be batched if using same material (texture and shader/blend mode) and any other mode than “indexed”.

Implementation I attached to this post (and sent pull request for) is using single mesh nodes, which wasn’t a problem in my tests. However, it also include fixes to bugs in Spine Lua runtime.

Also, this is not final update on Corona’s graphics. I have some R&D planned, possibly to integrate other Spine runtimes. But I think this is best way we can do with current Spine Lua runtime implementation.

Update 1: setting texture is same as to any other Shape Object

[lua]mesh.fill = { type=“image”, filename=“cat.png” }[/lua]

Update 2: In your post you asked for different api, with direct batching. display.newMesh is quite similar to requested API from batching perspective, but not API-wise. In corona rendering happens not at same time as Lua code can be executed, so direct Lua access wouldn’t be possible. You can create batch in Lua, pass it to display.newMesh() and effectively get same result as in provided batching examples. But for how currently Spine Lua runtime and Spine Corona runtime are implemented, just using separate nodes seems most natural way to proceed.

Working as expected. Using them in spine.

Great to hear that. Spine already got some support of mesh animations. But there are some problems with it, test carefully before shipping a game with it. More info in Spine’s issue.

@Vlads, you are doing amazing work! I so appreciate the great new features you are adding to Corona SDK! We are about a month or so away from releasing a puzzle game that heavily utilizes the canvas texture feature you recently implemented and we are super impressed with the power of canvas textures. I have been monitoring the spine/mesh discussions for a while now. I am not sure yet how we would leverage spine specifically in an upcoming game but this new mesh feature shows HUGE potential, especially when combined with canvas textures! I am so excited. Thank you!

Thanks a lot for kind words. Everyone in Corona really glad to hear things like this. Looking forward to playing your game!

Okay, these were my “couple smaller examples”, which I just got around to putting up:

Degenerate vertices (map vertices with u-coordinate < 0 to (0, 0); doing this to a whole triangle will collapse it to a point, and it won’t be drawn)

Mesh “regions” (encode an ID into the integer part of the u-coordinate, then use it to make some decision in an effect; in this example, it encodes a low-res color)

These are proofs of concept, which I’ll probably be doing more experimentation on later. The second example might end up being an unnecessary duplication of Corona’s own batching; I’m hoping to play around with it and see where it differs. If nothing else, I guess it lets you keep things together in one mesh.

Cool stuff. I’ll have to check it out.

gracias, voy a probarlo.

Hi, this is pretty cool, I’m looking forward to this.

Can you give us a way to get the uvs from an ImageSheet? That would make this perfect.

Thanks,

Albert

I don’t really understand why do you need to extract UV information from image sheet. You can use image sheet paint with meshes.

[lua]

mesh.fill = { type=“image”, sheet=sheet, frame=2}

[/lua]

Just don’t specify UVs, or specify them as for full texture, not just sheet frame. Also, if you want to calculate UV yourself, it wouldn’t be too hard.

I’ve been using a preview of the preview.  :slight_smile:

Some examples I have ready:

Cloth (warning: could probably use some tuning, but some numbers can completely destabilize it  :D)

Mesh-based curve

Mesh-based curve, take 2

I have a couple smaller examples, but haven’t put up Gists of those yet.

I’ve also got something of a morphing plugin in the works, maybe around 50% done on the mesh side of things. (Perhaps I’ll just make this an independent component.)

Thanks [member=‘StarCrunch’], you was great help in putting together initial implementation of Spine runtime changes and testing.

Looks neat! How do you set the texture?

I would really like a mesh batching API:

https://forums.coronalabs.com/topic/55072-corona-is-the-only-runtime-not-supporting-meshes/?p=323341

IMHO, there should be an immediate mode API with the persistent / scene graph node API on top. If not, there is going to be a bunch of juggling scene graph objects.

Thanks for your input. You’re right that juggling scene object isn’t much fun, but incorporating manual batching exactly as you described may be problematic due to Corona’s internal batch system, as well as resource ownership model.

Currently, meshes would be batched if using same material (texture and shader/blend mode) and any other mode than “indexed”.

Implementation I attached to this post (and sent pull request for) is using single mesh nodes, which wasn’t a problem in my tests. However, it also include fixes to bugs in Spine Lua runtime.

Also, this is not final update on Corona’s graphics. I have some R&D planned, possibly to integrate other Spine runtimes. But I think this is best way we can do with current Spine Lua runtime implementation.

Update 1: setting texture is same as to any other Shape Object

[lua]mesh.fill = { type=“image”, filename=“cat.png” }[/lua]

Update 2: In your post you asked for different api, with direct batching. display.newMesh is quite similar to requested API from batching perspective, but not API-wise. In corona rendering happens not at same time as Lua code can be executed, so direct Lua access wouldn’t be possible. You can create batch in Lua, pass it to display.newMesh() and effectively get same result as in provided batching examples. But for how currently Spine Lua runtime and Spine Corona runtime are implemented, just using separate nodes seems most natural way to proceed.