ScreenSpaceToWorldSpace but Z is a value from the Depth render target?

Options
Ines Hilz
Ines Hilz Posts: 38 🔥

Hi all :)
I'm working with the city landmarker right now and have to find a way to spawn objects on the building fasades of London.
I managed to get a depth render target and screen space normals, and i know that i can place objects in the world with this little script:

//@input SceneObject TestSphere
//@input Component.Camera camera

var event = script.createEvent("TouchStartEvent");
event.bind(function(eventData)
{
    var touchedPos = eventData.getTouchPosition();
    var objPos = script.camera.screenSpaceToWorldSpace(touchedPos, 40.0)
    script.TestSphere.getTransform().setWorldPosition(objPos);
});

I wonder if i can somehow use my normals/depth texture data instead of hard coding the distance in where i place the TestSphere?

Best Answer

  • Max
    Max Posts: 26 🔥
    #2 Answer ✓
    Options

    Yes! Using the Depth Texture controls, you can request the depth at a certain screen position.

    This will print the depth of the Depth Texture at the center of the screen (which has a screen position of 0.5, 0.5)

    //@input Asset.Texture depthTexture
    var depth = script.depthTexture.control.getDepth(new vec2(0.5, 0.5)); // distance (world units / cm)
    print(depth);
    

    And another example: this will print the depth at the position where you tap on the screen

    //@input Asset.Texture depthTexture
    
    function onTap(args){
        var screenPosition = args.getTapPosition();
        var depth = script.depthTexture.control.getDepth(screenPosition); // distance (world units / cm)
        print(depth);
    }
    var onTapEvent = script.createEvent('TapEvent');
    onTapEvent.bind(onTap);
    

Answers