Engine API Reference - v2.22.0-beta.28
    Preparing search index...

    Class SceneDepthReaderAlpha

    Reads the scene depth of a camera back to the CPU.

    A sample is the distance from the camera to the surface at that point, in world units, measured along the camera's view direction.

    Reads are asynchronous and land a frame or two later. Any number of them may be in flight at once, so a read can be issued every frame without waiting for the previous one to finish.

    Note that something has to be rendering the depth for there to be anything to read: an effect which consumes it, or CameraComponent#requestSceneDepthMap.

    const reader = new pc.SceneDepthReader(camera.camera);
    const rect = new pc.Vec4(0.45, 0.45, 0.1, 0.1);

    app.on('update', () => {
    reader.read(rect, 8, 8)?.then((samples) => {
    const hit = samples.filter(Number.isFinite);
    console.log(hit.length ? Math.min(...hit) : 'nothing in view');
    });
    });
    Index
    • Alpha

      Frees the resources the reader owns and stops reading for this camera. Reads still in flight report their region as empty.

      Returns void

    • Alpha

      Requests the depth of a region of the view, as width * height samples in row major order. The region is point sampled rather than averaged - one sample per cell, taken at its centre - so asking for more samples than the region resolves to repeats them.

      Samples where nothing was rendered read as Infinity.

      Parameters

      • rect: Vec4

        The region of the view to sample, normalized, with its origin in the bottom left as CameraComponent#rect.

      • width: number

        The number of samples across the region. Not pixels.

      • height: number

        The number of samples down the region.

      • Optionaltarget: Float32Array<ArrayBufferLike>

        An array to fill, at least width * height long. One is allocated when not given. It is filled when the returned promise resolves, so an array must not be shared between reads which overlap in time.

      Returns Promise<Float32Array<ArrayBufferLike>> | null

      The samples, in world units, or null when the camera is not rendering a scene depth, leaving nothing to read.