Why is this cube still transparent? (SOLVED)

I know this might be a simple thing but I need help to figure out the problem. I created a cube using primitives and I want it to have a red colour but it is still transparent.

RedCube.zip (32.1 KB)

1 Like

Hello @tobi!

The are 2 issues:

  1. In the cube.material in the Vertex Constants the mtx_view is of type User. This is then used as a view matrix and therefore should be of type View, so change User to View:


    This is because, Defold automatically puts correct View matrix then for you for the script, otherwise you were using a vector full of 0, so practically cancelling output in vertex program (there is multiplication by mtx_view).

  2. After this, all shaders will be correct and you should see the color, but there is another issue with faces in the cube.buffer. When you start rotating the cube, you can see only few faces of the cube are “correct”, so facing outside:

  3. So I modified the cube.buffer a bit and got a correct result:

Use this:

[
    {
        "name": "position",
        "type": "float32",
        "count": 3,
        "data": [-0.5, -0.5, -0.5,
                0.5, -0.5, -0.5,
                0.5,  0.5, -0.5,
                0.5,  0.5, -0.5,
                -0.5,  0.5, -0.5,
                -0.5, -0.5, -0.5,

                -0.5, -0.5,  0.5,
                0.5, -0.5,  0.5,
                0.5,  0.5,  0.5,
                0.5,  0.5,  0.5,
                -0.5,  0.5,  0.5,
                -0.5, -0.5,  0.5,

                -0.5,  0.5,  0.5,
                -0.5,  0.5, -0.5,
                -0.5, -0.5, -0.5,
                -0.5, -0.5, -0.5,
                -0.5, -0.5,  0.5,
                -0.5,  0.5,  0.5,

                0.5,  0.5,  0.5,
                0.5,  0.5, -0.5,
                0.5, -0.5, -0.5,
                0.5, -0.5, -0.5,
                0.5, -0.5,  0.5,
                0.5,  0.5,  0.5,

                -0.5, -0.5, -0.5,
                0.5, -0.5, -0.5,
                0.5, -0.5,  0.5,
                0.5, -0.5,  0.5,
                -0.5, -0.5,  0.5,
                -0.5, -0.5, -0.5,

                -0.5,  0.5, -0.5,
                0.5,  0.5, -0.5,
                0.5,  0.5,  0.5,
                0.5,  0.5,  0.5,
                -0.5,  0.5,  0.5,
                -0.5,  0.5, -0.5]
    }
]

Some of the faces were just hidden because of something called “back-face culling” - that is happening by default for model p(models and meshes using built-in materials with tag model) in the default render script you’re using, because of some of them had “reversed winding”.

A face is “reversed winding” when its triangles list vertices in the opposite order than the renderer expects for “front faces”.

I’m not 100% sure, but it looks like in case of Defold counter-clockwise vertex order is treated as front facing. @jhonny.goransson could you please confirm if this is true or not by default?

1 Like

Thanks a lot for your answer @Pawel .

The backface culling still happens with the latest buffer.

1 Like

Here’s my full project and in this the whole cube is front faced (as in the screenshot above) - do you have a different result?

RedCube.zip (70.4 KB)

1 Like

It works now. Thanks a lot!

1 Like

Hello @Pawel , could you kindly explain why the texture mapping in colours.mesh in this project is scrambled? What am I getting wrong?

Lighting Maps.zip (1.1 MB)

Edit: Found my mistake.

1 Like