openharmony 鸿蒙 js-apis-inner-scene-nodes

2025-06-12 浏览 (1)

SceneNode

The SceneNode module provides the types and operation methods of scene nodes in 3D graphics.

NOTE

The initial APIs of this module are supported since API version 12. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import { LayerMask, NodeType, Container, Node, Geometry, LightType, Light, SpotLight, DirectionalLight,
  Camera } from '@kit.ArkGraphics3D';

LayerMask

Layer mask of the node.

getEnabled

getEnabled(index: number): boolean

Checks whether the mask is enabled for a layer of a given index.

System capability: SystemCapability.ArkUi.Graphics3D

Parameters

NameTypeMandatoryDescription
indexnumberYesIndex of the layer. The value is an integer greater than or equal to 0.

Return value

TypeDescription
booleanCheck result. The value true means that the layer mask is enabled, and false means the opposite.

Example

import { Image, Shader, MaterialType, Material, ShaderMaterial, Animation, Environment, Container, SceneNodeParameters,
  LightType, Light, Camera, SceneResourceParameters, SceneResourceFactory, Scene, Node } from '@kit.ArkGraphics3D';

function layerMask() : void {
  let scene: Promise<Scene> = Scene.load($rawfile("gltf/CubeWithFloor/glTF/AnimatedCube.gltf"));
  scene.then(async (result: Scene) => {
    if (result) {
      let node : Node|null = result.getNodeByPath("rootNode_");
      if (node) {
          // Obtain the enabled status of the mask.
          let enabled: Boolean = node.layerMask.getEnabled(1);
      }
    }
  });
}

setEnabled

setEnabled(index: number, enabled: boolean): void

Enables the mask of a layer of a given index.

System capability: SystemCapability.ArkUi.Graphics3D

Parameters

NameTypeMandatoryDescription
indexnumberYesIndex of the layer. The value is an integer greater than or equal to 0.
enabledbooleanYesEnabled status to set. The value true means to enable the layer mask, and false means the opposite.

Example

import { Image, Shader, MaterialType, Material, ShaderMaterial, Animation, Environment, Container, SceneNodeParameters,
  LightType, Light, Camera, SceneResourceParameters, SceneResourceFactory, Scene, Node } from '@kit.ArkGraphics3D';

function layerMask() : void {
  let scene: Promise<Scene> = Scene.load($rawfile("gltf/CubeWithFloor/glTF/AnimatedCube.gltf"));
  scene.then(async (result: Scene) => {
    if (result) {
      let node : Node|null = result.getNodeByPath("rootNode/Scene/");
      if (node) {
          // Set the enabled status of the mask.
          node.layerMask.setEnabled(1, true);
      }
    }
  });
}

NodeType

Enumerates the node types.

System capability: SystemCapability.ArkUi.Graphics3D

NameValueDescription
NODE1Empty node.
GEOMETRY2Geometry node.
CAMERA3Camera node.
LIGHT4Light node.

Container<T>

Container for defining scene nodes. It provides a way to group scene nodes into a hierarchy.

append

append(item: T): void

Appends a node to the container.

System capability: SystemCapability.ArkUi.Graphics3D

Parameters

NameTypeMandatoryDescription
itemTYesObject of the T type.

Example

import { Image, Shader, MaterialType, Material, ShaderMaterial, Animation, Environment, Container, SceneNodeParameters,
  LightType, Light, Camera, SceneResourceParameters, SceneResourceFactory, Scene, Node } from '@kit.ArkGraphics3D';

function append() : void {
  let scene: Promise<Scene> = Scene.load($rawfile("gltf/CubeWithFloor/glTF/AnimatedCube.gltf"));
  scene.then(async (result: Scene) => {
    if (result) {
      let node : Node|null = result.getNodeByPath("rootNode/Scene/");
      // Call append to add a node.
      result.root?.children.get(0)?.children.append(node);
    }
  });
}

insertAfter

insertAfter(item: T, sibling: T|null): void

Inserts a node after a sibling node.

System capability: SystemCapability.ArkUi.Graphics3D

Parameters

NameTypeMandatoryDescription
itemTYesNode to insert.
siblingT |nullYesSibling node.

Example

import { Image, Shader, MaterialType, Material, ShaderMaterial, Animation, Environment, Container, SceneNodeParameters,
  LightType, Light, Camera, SceneResourceParameters, SceneResourceFactory, Scene, Node } from '@kit.ArkGraphics3D';

function insertAfter() : void {
  let scene: Promise<Scene> = Scene.load($rawfile("gltf/CubeWithFloor/glTF/AnimatedCube.gltf"));
  scene.then(async (result: Scene) => {
    if (result) {
      let node : Node|null = result.getNodeByPath("rootNode/Scene/");
      // Call insertAfter to add a node.
      result.root?.children.get(0)?.children.insertAfter(node, null);
    }
  });
}

remove

remove(item: T): void

Removes a node.

System capability: SystemCapability.ArkUi.Graphics3D

Parameters

NameTypeMandatoryDescription
itemTYesNode to remove.

Example

import { Image, Shader, MaterialType, Material, ShaderMaterial, Animation, Environment, Container, SceneNodeParameters,
  LightType, Light, Camera, SceneResourceParameters, SceneResourceFactory, Scene, Node } from '@kit.ArkGraphics3D';

function remove() : void {
  let scene: Promise<Scene> = Scene.load($rawfile("gltf/CubeWithFloor/glTF/AnimatedCube.gltf"));
  scene.then(async (result: Scene) => {
    if (result) {
      let node : Node|null = result.getNodeByPath("rootNode/Scene/");
      // Call remove to remove a node.
      result.root?.children.remove(node);
    }
  });
}

get

get(index: number): T|null

Obtains a node of a given index. If no node is obtained, null is returned.

System capability: SystemCapability.ArkUi.Graphics3D

Parameters

NameTypeMandatoryDescription
indexnumberYesIndex of the node. The value is an integer greater than or equal to 0.

Return value

TypeDescription
T |nullObject obtained. If no object is obtained, null is returned.

Example

import { Image, Shader, MaterialType, Material, ShaderMaterial, Animation, Environment, Container, SceneNodeParameters,
  LightType, Light, Camera, SceneResourceParameters, SceneResourceFactory, Scene, Node } from '@kit.ArkGraphics3D';

function get() : void {
  let scene: Promise<Scene> = Scene.load($rawfile("gltf/CubeWithFloor/glTF/AnimatedCube.gltf"));
  scene.then(async (result: Scene) => {
    if (result) {
      let node : Node|null = result.getNodeByPath("rootNode/Scene/");
      // Get node 0 from children.
      result.root?.children.get(0)?.children.insertAfter(node, null);
    }
  });
}

clear

clear(): void

Clears all nodes in the container.

System capability: SystemCapability.ArkUi.Graphics3D

Example

import { Image, Shader, MaterialType, Material, ShaderMaterial, Animation, Environment, Container, SceneNodeParameters,
  LightType, Light, Camera, SceneResourceParameters, SceneResourceFactory, Scene, Node } from '@kit.ArkGraphics3D';

function clear() : void {
  let scene: Promise<Scene> = Scene.load($rawfile("gltf/CubeWithFloor/glTF/AnimatedCube.gltf"));
  scene.then(async (result: Scene) => {
    if (result) {
      let node : Node|null = result.getNodeByPath("rootNode/Scene/");
      // Clear the nodes in children.
      result.root?.children.clear();
    }
  });
}

count

count(): number

Obtains the number of nodes in the container.

System capability: SystemCapability.ArkUi.Graphics3D

Return value

TypeDescription
numberNumber of nodes in the container. The value is a non-negative integer.

Example

import { Image, Shader, MaterialType, Material, ShaderMaterial, Animation, Environment, Container, SceneNodeParameters,
  LightType, Light, Camera, SceneResourceParameters, SceneResourceFactory, Scene, Node } from '@kit.ArkGraphics3D';

function count() : void {
  let scene: Promise<Scene> = Scene.load($rawfile("gltf/CubeWithFloor/glTF/AnimatedCube.gltf"));
  scene.then(async (result: Scene) => {
    if (result) {
      let node : Node|null = result.getNodeByPath("rootNode_");
      if (node) {
        let container: Container<Node> = node.children;
        // Obtain the number of nodes in children.
        let count: number = container.count();
      }
    }
  });
}

Node

The 3D scene consists of nodes in a tree hierarchy, where each node implements a Node interface. This class inherits from SceneResource.

Properties

System capability: SystemCapability.ArkUi.Graphics3D

NameTypeRead OnlyOptionalDescription
positionPosition3NoNoPosition of the node.
rotationQuaternionNoNoRotation angle of the node.
scaleScale3NoNoScale factor of the node.
visiblebooleanNoNoWhether the node is visible. The value true means that the node is visible, and false means the opposite.
nodeTypeNodeTypeYesNoType of the node.
layerMaskLayerMaskYesNoLayer mask of the node.
pathstringYesNoPath of the node.
parentNode |nullYesNoParent node of the node. If the parent node does not exist, the value is null.
childrenContainer<Node>YesNoChildren of the node. If the node does not have a child, the value is null.

getNodeByPath

getNodeByPath(path: string): Node|null

Obtains a node by path. If no node is obtained, null is returned.

System capability: SystemCapability.ArkUi.Graphics3D

Parameters

NameTypeMandatoryDescription
pathstringYesPath in the scene node hierarchy. Each layer is separated by a slash (/).

Return value

TypeDescription
Node |nullNode object.

Example

import { Image, Shader, MaterialType, Material, ShaderMaterial, Animation, Environment, Container, SceneNodeParameters,
  LightType, Light, Camera, SceneResourceParameters, SceneResourceFactory, Scene, Node } from '@kit.ArkGraphics3D';

function getNode() : void {
  let scene: Promise<Scene> = Scene.load($rawfile("gltf/CubeWithFloor/glTF/AnimatedCube.gltf"));
  scene.then(async (result: Scene) => {
    if (result && result.root) {
      // Search for a node.
      let geo : Node|null = result.root.getNodeByPath("scene/node");
    }
  });
}

Geometry

Geometric node type that holds renderable mesh data and supports optional deformation features. It inherits from Node.

Properties

System capability: SystemCapability.ArkUi.Graphics3D

NameTypeRead OnlyOptionalDescription
meshMeshYesNoMesh attribute.
morpher20+MorpherYesYesOptional morpher that adds vertex-based deformation or animation effects to the geometry. If this parameter is not specified, the geometry does not support deformation.

LightType

Enumerates the light types.

System capability: SystemCapability.ArkUi.Graphics3D

NameValueDescription
DIRECTIONAL1Directional light.
SPOT2Spot light.

Light

Light node, which inherits from Node.

Properties

System capability: SystemCapability.ArkUi.Graphics3D

NameTypeRead OnlyOptionalDescription
lightTypeLightTypeYesNoLight type.
colorColorNoNoColor.
intensitynumberNoNoLight intensity. The value is a real number greater than 0.
shadowEnabledbooleanNoNoWhether the shadow effect is enabled. The value true means that the shadow effect is enabled, and false means the opposite.
enabledbooleanNoNoWhether the light is used. The value true means that the light is used, and false means the opposite.

SpotLight

Spot light, which inherits from Light.

System capability: SystemCapability.ArkUi.Graphics3D

DirectionalLight

Directional light, which inherits from Light.

System capability: SystemCapability.ArkUi.Graphics3D

Camera

Camera node, which inherits from Node.

Properties

System capability: SystemCapability.ArkUi.Graphics3D

NameTypeRead OnlyOptionalDescription
fovnumberNoNoField of view. The value ranges from 0 to π radians.
nearPlanenumberNoNoNear plane. The value is greater than 0.
farPlanenumberNoNoRemote plane. The value must be greater than that of nearPlane.
enabledbooleanNoNoWhether the camera is enabled. The value true means that the camera is enabled, and false means the opposite.
postProcessPostProcessSettings |nullNoNoPost-processing settings.
clearColorColor |nullNoNoColor after the render target is cleared.

raycast20+

raycast(viewPosition: Vec2, params: RaycastParameters): Promise<RaycastResult[]>

Casts a ray from a specific position on the screen to detect and retrieve information about all hit 3D objects. This API uses a promise to return the result.

System capability: SystemCapability.ArkUi.Graphics3D

Parameters

NameTypeMandatoryDescription
viewPositionVec2YesStandardized Device Coordinates (NDC). The value range is [-1, 1]. The bottom-left corner of the screen is (-1, -1), and the top-right corner is (1, 1).
paramsRaycastParametersYesConfiguration parameters for raycasting, such as detection range and filtered nodes.

Return value

TypeDescription
Promise<RaycastResult[]>An array of hit objects sorted by distance (from nearest to farthest). If no objects are hit, an empty array is returned.

Example

import { Image, Shader, MaterialType, Material, ShaderMaterial, Animation, Environment, Container, SceneNodeParameters,
  LightType, Light, Camera, SceneResourceParameters, SceneResourceFactory, Scene, Node, Vec2, Vec3, RaycastParameters,
  RaycastResult } from '@kit.ArkGraphics3D';

function Raycast() : void {
  let scene: Promise<Scene> = Scene.load($rawfile("gltf/CubeWithFloor/glTF/AnimatedCube.gltf"));
  scene.then(async (result: Scene) => {
    if (result) {
      let sceneFactory: SceneResourceFactory = result.getResourceFactory();
      let sceneCameraParameter: SceneNodeParameters = { name: "camera1" };
      // Create a camera.
      let camera: Promise<Camera> = sceneFactory.createCamera(sceneCameraParameter);
      camera.enabled = true;
      lookAt(this.cam, { x: 15, y: 10, z: 20 }, { x: 0, y: 0, z: 0 }, { x: 0, y: 1, z: 0 });
      let viewPos: scene3d.Vec2 = { x: 0.5, y: 0.5 };
      return camera?.raycast(viewPos, result.root);
    }
  });
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙ArkGraphics 3D

harmony 鸿蒙ScenePostProcessSettings

harmony 鸿蒙SceneResource

harmony 鸿蒙SceneType

harmony 鸿蒙Scene

harmony 鸿蒙@ohos.graphics.scene (ArkGraphics 3D)

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/7OTdC9