Phantom Objects (write only to the z-buffer)

Hi, I have this function that tries to turn an object into Phantom Object (for an AR application).
Phantom objects are those that are drawn to the depth buffer but not to the frame buffer.

Primitives.togglePhantom = function(entity)
{
    if (entity.isPhantom === undefined)
        entity.isPhantom = true;
    else if (entity.isPhantom === false)
        entity.isPhantom = true;
    else
        entity.isPhantom = false;
    
    var model = entity.model.model;
    
    if (model !== null) 
    {
        var materials = model.getMaterials();
        for (var i = 0; i < materials.length; i++) 
        {
            var material = materials[i];

            material.depthTest = true;
            material.depthWrite = true;

            if (entity.isPhantom === true)
            {
                material.blueWrite = false;
                material.redWrite = false;
                material.greenWrite = false;
                material.alphaWrite = false;
            }
            else
            {
                material.blueWrite = true;
                material.redWrite = true;
                material.greenWrite = true;
                material.alphaWrite = true;
            }
                
            material.update();
        }
    }
};

However, this function works ONLY for those objects created by the editor.

If I create the object programatically (and I need to do it) with:

    var entity = new pc.Entity();
    entity.name = "cube";
    entity.addComponent("model", { type: 'box',});
    root.addChild(entity);

and apply the togglePhantom function, then the object only occludes a set of lines created with the renderLine function (a grid that I have placed to serve as reference plane) but not the other objects of the scene.

Can someone give a hint/explanation and a solution for this??

Thanks in advance.

Can you link to a project example please?

Hi @Sergio_Casas this might help you:

Yes.
This is not my original project, but a simplfied one to illustrate the effect:

https://playcanvas.com/editor/scene/604430

@Sergio_Casas Is the project private? :stuck_out_tongue:

Oooooops.

Not now :slight_smile:

Stupid question time: What is it meant to look like and what do you see when it doesn’t work?

I expected the cube (one of the smaller ones) to occlude the objects behind (without drawing its geometry). It does not…

I’ve narrowed down the problem to the material. If I use the one that is created in the editor, it works as expected. There may be a setting/flag that is set to a default value when created in the editor but I can’t track down which one.

We have reached the same conclusion. It’s something in the material but can’t figure out what…

Anyone can give me a hint here??

Had another look at it and printed both the material that was created in code and the one created in the editor to the console. I compared the two using a diff tool and it highlighted a number of differences.

I’ve added the following lines to get this ‘working’

        material.blendSrc = 1;
        material.blendDst = 0;
        material.blend = false;

Project here: https://playcanvas.com/editor/scene/604439

Thanks,

I see that it works in your project but cannot make it work in mine.
I guess there are some other changes that I have to analyse…