View3D QML Type

Provides a viewport on which to render a 3D scene. More...

Import Statement: import QtQuick3D

Properties

Methods

  • vector3d mapFrom3DScene(vector3d scenePos)
  • vector3d mapTo3DScene(vector3d viewPos)
  • pickResult pick(float x, float y)
  • pickResult pick(float x, float y, Model model) (since 6.8)
  • List<pickResult> pickAll(float x, float y) (since 6.2)
  • List<pickResult> pickSubset(float x, float y, list<Model> models) (since 6.8)
  • pickResult rayPick(vector3d origin, vector3d direction) (since 6.2)
  • List<pickResult> rayPickAll(vector3d origin, vector3d direction) (since 6.2)
  • setTouchpoint(Item target, point position, int pointId, bool pressed) (since 6.8)

Detailed Description

View3D provides a 2D surface on which a 3D scene can be rendered. This surface is a Qt Quick Item and can be placed in a Qt Quick scene.

There are two ways to define the 3D scene that is visualized on the View3D: If you define a hierarchy of Node-based items as children of the View3D directly, then this will become the implicit scene of the View3D.

It is also possible to reference an existing scene by using the importScene property and setting it to the root Node of the scene you want to visualize. This Node does not have to be an ancestor of the View3D, and you can have multiple View3Ds that import the same scene.

This is demonstrated in View3D example.

If the View3D both has child Nodes and the importScene property is set simultaneously, then both scenes will be rendered as if they were sibling subtrees in the same scene.

To control how a scene is rendered, you can set the environment property. The type SceneEnvironment has a number of visual properties that can be adjusted, such as background color, tone mapping, anti-aliasing and more. ExtendedSceneEnvironment in the QtQuick3D.Helpers module extends SceneEnvironment with even more features, adding common post-processing effects.

In addition, in order for anything to be rendered in the View3D, the scene needs a Camera. If there is only a single Camera in the scene, then this will automatically be picked. Otherwise, the camera property can be used to select the camera. The Camera decides which parts of the scene are visible, and how they are projected onto the 2D surface.

By default, the 3D scene will first be rendered into an off-screen buffer and then composited with the rest of the Qt Quick scene when it is done. This provides the maximum level of compatibility, but may have performance implications on some graphics hardware. If this is the case, the renderMode property can be used to switch how the View3D is rendered into the window.

A View3D with the default Offscreen renderMode is implicitly a texture provider as well. This means that ShaderEffect or Texture.sourceItem can reference the View3D directly as long as all items live within the same window. Like with any other Item, it is also possible to switch the View3D, or one of its ancestors, into a texture-based item layer.

See also Qt Quick 3D - View3D example.

Property Documentation

This property specifies which Camera is used to render the scene. If this property is not set, then the first enabled camera in the scene will be used.

Note: If this property contains a camera that's not visible then no further attempts to find a camera will be done.

See also PerspectiveCamera, OrthographicCamera, FrustumCamera, and CustomCamera.


effectiveTextureSize : size [since 6.7]

This property exposes the size, in pixels, of the underlying color (and depth/stencil) buffers. It is provided for use on the GUI (main) thread, in QML bindings or JavaScript.

This is a read-only property.

Note: This property is relevant only when renderMode is set to Offscreen.

This property was introduced in Qt 6.7.

See also explicitTextureWidth, explicitTextureHeight, and DebugView.


This property specifies the SceneEnvironment used to render the scene.

See also SceneEnvironment.


explicitTextureHeight : int [since 6.7]

The height, in pixels, of the item's associated texture. Relevant when a fixed texture size is desired that does not depend on the item's size. This size has no effect on the geometry of the item (its size and placement within the scene), which means the texture's content will appear scaled up or down (and possibly stretched) onto the item's area.

By default the value is 0. A value of 0 means that texture's size follows the item's size. (texture size in pixels = item's logical size * device pixel ratio).

Note: This property is relevant only when renderMode is set to Offscreen. Its value is ignored otherwise.

This property was introduced in Qt 6.7.

See also explicitTextureWidth, effectiveTextureSize, and DebugView.


explicitTextureWidth : int [since 6.7]

The width, in pixels, of the item's associated texture. Relevant when a fixed texture size is desired that does not depend on the item's size. This size has no effect on the geometry of the item (its size and placement within the scene), which means the texture's content will appear scaled up or down (and possibly stretched) onto the item's area.

By default the value is 0. A value of 0 means that texture's size follows the item's size. (texture size in pixels = item's logical size * device pixel ratio).

Note: This property is relevant only when renderMode is set to Offscreen. Its value is ignored otherwise.

This property was introduced in Qt 6.7.

See also explicitTextureHeight, effectiveTextureSize, and DebugView.


extensions : List<QtQuick3D::Object3D>

This property contains a list of user extensions that should be used with this View3D.

See also RenderExtension.


importScene : QtQuick3D::Node

This property defines the reference node of the scene to render to the viewport. The node does not have to be a child of the View3D. This referenced node becomes a sibling with child nodes of View3D, if there are any.

Note: This property can only be set once, and subsequent changes will have no effect.

You can also define a hierarchy of Node-based items as children of the View3D directly, then this will become the implicit scene of the View3D.

To return the current scene of the View3D, use the scene property.

See also Node.


renderFormat : enumeration [since 6.4]

This property determines the backing texture's format. Applicable only when the View3D is rendering to a texture, for example because the renderMode is View3D.Offscreen.

The default is ShaderEffectSource.RGBA8.

If the format is not supported by the underlying graphics driver at run time, RGBA8 is used.

This property was introduced in Qt 6.4.

See also QtQuick::ShaderEffectSource::format and QtQuick::Item::layer.format.


renderMode : enumeration

This property determines how the View3D is combined with the other parts of the Qt Quick scene.

By default, the scene will be rendered into an off-screen buffer as an intermediate step. This off-screen buffer is then rendered into the window (or render target) like any other Qt Quick Item.

For most users, there will be no need to change the render mode, and this property can safely be ignored. But on some graphics hardware, the use of an off-screen buffer can be a performance bottleneck. If this is the case, it might be worth experimenting with other modes.

ConstantDescription
View3D.OffscreenThe scene is rendered into an off-screen buffer as an intermediate step. This off-screen buffer is then composited with the rest of the Qt Quick scene.
View3D.UnderlayThe scene is rendered directly to the window before the rest of the Qt Quick scene is rendered. With this mode, the View3D cannot be placed on top of other Qt Quick items.
View3D.OverlayThe scene is rendered directly to the window after Qt Quick is rendered. With this mode, the View3D will always be on top of other Qt Quick items.
View3D.InlineThe View3D's scene graph is embedded into the main scene graph, and the same ordering semantics are applied as to any other Qt Quick Item. As this mode can lead to subtle issues, depending on the contents of the scene, due to injecting depth-based 3D content into a 2D scene graph, it is not recommended to be used, unless a specific need arises.

The default is View3D.Offscreen.

Note: When changing the render mode, it is important to note that View3D.Offscreen (the default) is the only mode which guarantees perfect graphics fidelity. The other modes all have limitations that can cause visual glitches, so it is important to check that the visual output still looks correct when changing this property.

Note: When using the Underlay, Overlay, or Inline modes, it can be useful, and in some cases, required, to disable the Qt Quick scene graph's depth buffer writes via QQuickGraphicsConfiguration::setDepthBufferFor2D() before showing the QQuickWindow or QQuickView hosting the View3D item.


renderStats : QtQuick3D::RenderStats [read-only]

This property provides statistics about the rendering of a frame, such as fps, frameTime, renderTime, syncTime, and maxFrameTime.


scene : QtQuick3D::Node [read-only]

Returns the root Node of the View3D's scene.

To define the 3D scene that is visualized in the View3D:

  • Define a hierarchy of Node-based items as children of the View3D directly, then this will become the implicit scene of the View3D.
  • Reference an existing scene by using the importScene property and set it to the root Node of the scene you want to visualize. This Node does not have to be an ancestor of the View3D, and you can have multiple View3Ds that import the same scene.

See also importScene.


Method Documentation

vector3d mapFrom3DScene(vector3d scenePos)

Transforms scenePos from scene space (3D) into view space (2D).

The returned x- and y-values will be be in view coordinates, with the top-left corner at [0, 0] and the bottom-right corner at [width, height]. The returned z-value contains the distance from the near clip plane of the frustum (clipNear) to scenePos in scene coordinates. If the distance is negative, that means the scenePos is behind the active camera. If scenePos cannot be mapped to a position in the scene, a position of [0, 0, 0] is returned.

This function requires that camera is assigned to the view.

See also mapTo3DScene() and Camera.mapToViewport().


vector3d mapTo3DScene(vector3d viewPos)

Transforms viewPos from view space (2D) into scene space (3D).

The x- and y-values of viewPos should be in view coordinates, with the top-left corner at [0, 0] and the bottom-right corner at [width, height]. The z-value is interpreted as the distance from the near clip plane of the frustum (clipNear) in scene coordinates.

If viewPos cannot successfully be mapped to a position in the scene, a position of [0, 0, 0] is returned.

This function requires that a camera is assigned to the view.

See also mapFrom3DScene() and Camera.mapFromViewport().


pickResult pick(float x, float y)

This method will "shoot" a ray into the scene from view coordinates x and y and return information about the nearest intersection with an object in the scene.

This can, for instance, be called with mouse coordinates to find the object under the mouse cursor.


[since 6.8] pickResult pick(float x, float y, Model model)

This method will "shoot" a ray into the scene from view coordinates x and y and return information about the intersection between the ray and the specified model.

This can, for instance, be called with mouse coordinates to find the object under the mouse cursor.

This method was introduced in Qt 6.8.


[since 6.2] List<pickResult> pickAll(float x, float y)

This method will "shoot" a ray into the scene from view coordinates x and y and return a list of information about intersections with objects in the scene. The returned list is sorted by distance from the camera with the nearest intersections appearing first and the furthest appearing last.

This can, for instance, be called with mouse coordinates to find the object under the mouse cursor.

This method was introduced in Qt 6.2.


[since 6.8] List<pickResult> pickSubset(float x, float y, list<Model> models)

This method will "shoot" a ray into the scene from view coordinates x and y and return information about the intersections with the passed in list of models. This will only check against the list of models passed in. The returned list is sorted by distance from the camera with the nearest intersections appearing first and the furthest appearing last.

This can, for instance, be called with mouse coordinates to find the object under the mouse cursor.

Works with both property list<Model> and dynamic JavaScript arrays of models.

This method was introduced in Qt 6.8.


[since 6.2] pickResult rayPick(vector3d origin, vector3d direction)

This method will "shoot" a ray into the scene starting at origin and in direction and return information about the nearest intersection with an object in the scene.

This can, for instance, be called with the position and forward vector of any object in a scene to see what object is in front of an item. This makes it possible to do picking from any point in the scene.

This method was introduced in Qt 6.2.


[since 6.2] List<pickResult> rayPickAll(vector3d origin, vector3d direction)

This method will "shoot" a ray into the scene starting at origin and in direction and return a list of information about the nearest intersections with objects in the scene. The list is presorted by distance from the origin along the direction vector with the nearest intersections appearing first and the furthest appearing last.

This can, for instance, be called with the position and forward vector of any object in a scene to see what objects are in front of an item. This makes it possible to do picking from any point in the scene.

This method was introduced in Qt 6.2.


[since 6.8] setTouchpoint(Item target, point position, int pointId, bool pressed)

Sends a synthetic touch event to target, moving the touch point with ID pointId to position, with pressed determining if the point is pressed. Also sends the appropriate touch release event if pointId was previously active on a different item.

This method was introduced in Qt 6.8.