Rendering a simple mesh in 2D (solved)

Hi everyone, complete newbie here. I’m trying to render a simple mesh (triangle) on the screen with default renderer but only get a black screen. I’m able to successfully render a debug message (using msg.post) though. I can see the triangle properly rendered in Defold (see bottom of my post). Would appreciate your insights on how I could get this thing working.

Relevant code

triangle.buffer:

[
    {
    "name": "position",
    "type": "float32",
    "count": 3,
    "data": [-0.5, -0.5, 0,
            0.5, -0.5, 0,
            0.0, 0.5, 0]
    },
    {
    "name": "color0",
    "type": "float32",
    "count": 4,
    "data": [0, 1, 0, 1,
            0, 1, 0, 1,
            0, 1, 0, 1]
    }
]

triangle.mesh:

material: "/main/triangle.material"
vertices: "/main/triangle.buffer"
position_stream: "position"
normal_stream: "position"

triangle.material

name: "triangle"
tags: "triangle"
vertex_program: "/main/triangle.vp"
fragment_program: "/main/triangle.fp"
vertex_constants {
  name: "mtx_proj"
  type: CONSTANT_TYPE_PROJECTION
}
vertex_constants {
  name: "mtx_worldview"
  type: CONSTANT_TYPE_WORLDVIEW
}

VP

uniform mediump mat4 mtx_worldview;
uniform mediump mat4 mtx_proj;

attribute mediump vec4 position;
attribute mediump vec4 color0;

varying mediump vec4 var_color;

void main()
{
	gl_Position = mtx_proj * mtx_worldview * vec4(position.xyz, 1.0);
	var_color = color0;
}

FP

varying mediump vec4 var_color;

void main()
{
	gl_FragColor = var_color;
}

main.collection

name: "main"
scale_along_z: 0
embedded_instances {
  id: "go"
  data: "components {\n"
  "  id: \"script\"\n"
  "  component: \"/main/triangle.script\"\n"
  "}\n"
  "components {\n"
  "  id: \"mesh\"\n"
  "  component: \"/main/triangle.mesh\"\n"
  "}\n"
  ""
}

1 Like

There is no triangle tag (predicate) in the default renderer. You can simply try to set the materials tag to model or you can modify the render script

2 Likes

Thanks for feedback! Yeah, I see now that only these are valid in default renderer: "tile", "gui", "particle", "model", "debug_text".

I went ahead and changed material’s tag to model as you suggested but still no luck. :neutral_face: Here’s the script file just to make sure I’m sharing everything. As stated - “Hello world!” renders correctly.

function init(self)
end

function update(self, dt)
	msg.post("@render:", "draw_text", { text = "Hello world!", position = vmath.vector3(50, 200, 0) })
end

Two changes:

  1. Set vertex_space: VERTEX_SPACE_LOCAL in your material
  2. You need to scale the game object holding the mesh for it to show. Otherwise it will be a 1x1px tiny thing.
3 Likes

Worked like a charm! Thanks a bunch.

1 Like