1# Scene 2<!--Kit: ArkGraphics 3D--> 3<!--Subsystem: Graphics--> 4<!--Owner: @zzhao0--> 5<!--SE: @zdustc--> 6<!--TSE: @zhangyue283--> 7 8The module is the basic module of ArkGraphics 3D and provides common data types such as **SceneResourceParameters** and **SceneNodeParameters**. It also provides basic methods such as glTF model loading, scene creation, and resource creation. 9 10> **NOTE** 11> 12> 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. 13 14## Modules to Import 15```ts 16import { SceneResourceParameters, SceneNodeParameters, RaycastResult, RaycastParameters,RenderResourceFactory, 17 SceneResourceFactory, SceneComponent, RenderContext, RenderParameters, Scene } from '@kit.ArkGraphics3D'; 18``` 19 20## SceneResourceParameters 21Describes the scene resource parameters (**name** and **uri**), which are used to provide the name of a scene resource and the path of the resource file required in the 3D scene. 22 23**System capability**: SystemCapability.ArkUi.Graphics3D 24| Name| Type| Read Only| Optional| Description| 25| ---- | ---- | ---- | ---- | ---- | 26| name | string | No| No| Name of the scene resource. It is customizable.| 27| uri | [ResourceStr](../apis-arkui/arkui-ts/ts-types.md#resourcestr) | No| Yes| Path of the resource file required in the 3D scene. The default value is undefined.| 28 29**Example** 30```ts 31import { Image, Shader, MaterialType, Material, ShaderMaterial, Animation, Environment, Container, SceneNodeParameters, 32 LightType, Light, Camera, SceneResourceParameters, SceneResourceFactory, Scene, Node } from '@kit.ArkGraphics3D'; 33 34function createShaderPromise() : Promise<Shader> { 35 return new Promise(() => { 36 let scene: Promise<Scene> = Scene.load($rawfile("gltf/CubeWithFloor/glTF/AnimatedCube.glb")); 37 scene.then(async (result: Scene) => { 38 let sceneFactory: SceneResourceFactory = result.getResourceFactory(); 39 40 // Create a variable of the SceneResourceParameters type and use it to create a shader. 41 let sceneResourceParameter: SceneResourceParameters = { name: "shaderResource", 42 uri: $rawfile("shaders/custom_shader/custom_material_sample.shader") }; 43 let shader: Promise<Shader> = sceneFactory.createShader(sceneResourceParameter); 44 return shader; 45 }); 46 }); 47} 48``` 49 50## SceneNodeParameters 51Describes the scene node parameters, which are used to provide the name and path in the scene node tree. 52 53**System capability**: SystemCapability.ArkUi.Graphics3D 54| Name| Type| Read Only| Optional| Description| 55| ---- | ---- | ---- | ---- | ---- | 56| name | string | No| No| Name of the scene node. It is customizable.| 57| path | string | No| Yes| Path in the scene node tree. It specifies the position of the created camera, light, or node in the scene node tree. Each layer is separated by a slash (/). If no path is provided, the node is set as a child node of the root node. The default value is undefined.| 58 59**Example** 60```ts 61import { Image, Shader, MaterialType, Material, ShaderMaterial, Animation, Environment, Container, SceneNodeParameters, 62 LightType, Light, Camera, SceneResourceParameters, SceneResourceFactory, Scene, Node } from '@kit.ArkGraphics3D'; 63 64function createNodePromise() : Promise<Node> { 65 return new Promise(() => { 66 let scene: Promise<Scene> = Scene.load($rawfile("gltf/CubeWithFloor/glTF/AnimatedCube.glb")); 67 scene.then(async (result: Scene) => { 68 let sceneFactory: SceneResourceFactory = result.getResourceFactory(); 69 70 // Create a variable of the SceneNodeParameters type and use it to create a node. 71 let sceneNodeParameter: SceneNodeParameters = { name: "empty_node", 72 path:"/rootNode_/empty_node" }; 73 let node: Promise<Node> = sceneFactory.createNode(sceneNodeParameter); 74 return node; 75 }); 76 }); 77} 78``` 79 80## RaycastResult<sup>20+</sup> 81Describes a result object from raycasting, containing details about the 3D object hit by the ray. 82 83**System capability**: SystemCapability.ArkUi.Graphics3D 84| Name| Type| Read Only| Optional| Description| 85| ---- | ---- | ---- | ---- | ---- | 86| node | [Node](js-apis-inner-scene-nodes.md#node) | Yes| No| 3D scene node hit by the ray. You can use this node to manipulate the target object (for example, moving, rotating, or hiding the object).| 87| centerDistance | number | Yes| No| Distance from the center of the bounding box of the hit object to the center of the camera. The value must be greater than 0.| 88| hitPosition | [Position3](js-apis-inner-scene-types.md#position3) | Yes| No| Precise world coordinates ({x: number, y: number, z: number}) of the point where the ray hit the object.| 89 90 91## RaycastParameters<sup>20+</sup> 92Describes the configuration parameters for raycasting, defining the behavior of raycasting. 93 94**System capability**: SystemCapability.ArkUi.Graphics3D 95| Name| Type| Read Only| Optional| Description| 96| ---- | ---- | ---- | ---- | ---- | 97| rootNode | [Node](js-apis-inner-scene-nodes.md#node) | No| Yes| Limits the detection scope to this node and its child nodes. If this parameter is not specified, the entire scene is detected.| 98 99 100## RenderResourceFactory<sup>20+</sup> 101Provides APIs to create rendering resources that can be shared among multiple scenes with a shared RenderContext. 102 103### createShader 104createShader(params: SceneResourceParameters): Promise\<Shader> 105 106Creates a shader based on the scene resource parameters. This API uses a promise to return the result. 107 108**System capability**: SystemCapability.ArkUi.Graphics3D 109 110**Parameters** 111| Name| Type| Mandatory| Description| 112| ---- | ---- | ---- | ---- | 113| params | [SceneResourceParameters](#sceneresourceparameters) | Yes| Parameters for creating the shader.| 114 115**Return value** 116| Type| Description| 117| ---- | ---- | 118| Promise\<[Shader](js-apis-inner-scene-resources.md#shader)> | Promise used to return the Shader object created.| 119 120**Example** 121```ts 122import { Image, Shader, MaterialType, Material, ShaderMaterial, Animation, Environment, Container, SceneNodeParameters, 123 LightType, Light, Camera, SceneResourceParameters, SceneResourceFactory, Scene, Node, RenderContext, RenderResourceFactory } from '@kit.ArkGraphics3D'; 124 125function createShaderResource(): Promise<Shader> { 126 const renderContext: RenderContext | null = Scene.getDefaultRenderContext(); 127 if (!renderContext) { 128 return Promise.reject(new Error("RenderContext is null")); 129 } 130 const renderResourceFactory: RenderResourceFactory = renderContext.getRenderResourceFactory(); 131 let shaderParams: SceneResourceParameters = { 132 name: "custom_shader", 133 uri: $rawfile("shaders/custom_shader/custom_material_sample.shader") 134 }; 135 return renderResourceFactory.createShader(shaderParams); 136} 137``` 138### createImage 139createImage(params: SceneResourceParameters): Promise\<Image> 140 141Creates an image based on the scene resource parameters. This API uses a promise to return the result. 142 143**System capability**: SystemCapability.ArkUi.Graphics3D 144 145**Parameters** 146| Name| Type| Mandatory| Description| 147| ---- | ---- | ---- | ---- | 148| params | [SceneResourceParameters](#sceneresourceparameters) | Yes| Parameters for creating the image.| 149 150**Return value** 151| Type| Description| 152| ---- | ---- | 153| Promise\<[Image](js-apis-inner-scene-resources.md#image)> | Promise used to return the Image object created.| 154 155**Example** 156```ts 157import { Image, Shader, MaterialType, Material, ShaderMaterial, Animation, Environment, Container, SceneNodeParameters, 158 LightType, Light, Camera, SceneResourceParameters, SceneResourceFactory, Scene, Node, RenderContext, RenderResourceFactory } from '@kit.ArkGraphics3D'; 159 160function createImageResource(): Promise<Image> { 161 const renderContext: RenderContext | null = Scene.getDefaultRenderContext(); 162 if (!renderContext) { 163 return Promise.reject(new Error("RenderContext is null")); 164 } 165 const renderResourceFactory: RenderResourceFactory = renderContext.getRenderResourceFactory(); 166 let imageParams: SceneResourceParameters = { 167 name: "sampleImage", 168 uri: $rawfile("image/Cube_BaseColor.png") 169 }; 170 return renderResourceFactory.createImage(imageParams); 171} 172``` 173 174### createMesh 175createMesh(params: SceneResourceParameters, geometry: GeometryDefinition): Promise\<MeshResource> 176 177Creates a mesh based on the scene resource parameters and geometry definition. This API uses a promise to return the result. 178 179**System capability**: SystemCapability.ArkUi.Graphics3D 180 181**Parameters** 182| Name| Type| Mandatory| Description| 183| ---- | ---- | ---- | ---- | 184| params | [SceneResourceParameters](#sceneresourceparameters) | Yes| Parameters for creating the mesh.| 185| geometry | [GeometryDefinition](js-apis-inner-scene-types.md#geometrydefinition18) | Yes| Geometry of the mesh to create.| 186 187**Return value** 188| Type| Description| 189| ---- | ---- | 190| Promise\<[MeshResource](js-apis-inner-scene-resources.md#meshresource18)> | Promise used to return the Mesh object created.| 191 192**Example** 193```ts 194import { Image, Shader, MaterialType, Material, ShaderMaterial, Animation, Environment, Container, SceneNodeParameters, 195 LightType, Light, Camera, SceneResourceParameters, SceneResourceFactory, Scene, Node } from '@kit.ArkGraphics3D'; 196import { CustomGeometry, PrimitiveTopology, RenderContext, RenderResourceFactory, 197 MeshResource } from '@ohos.graphics.scene'; 198 199function createMeshResource(): Promise<MeshResource> { 200 const renderContext: RenderContext | null = Scene.getDefaultRenderContext(); 201 if (!renderContext) { 202 return Promise.reject(new Error("RenderContext is null")); 203 } 204 const renderResourceFactory: RenderResourceFactory = renderContext.getRenderResourceFactory(); 205 const geometry = new CustomGeometry(); 206 geometry.vertices = [ 207 { x: 0, y: 0, z: 0 }, 208 { x: 1, y: 0, z: 0 }, 209 { x: 1, y: 1, z: 0 }, 210 { x: 0, y: 1, z: 0 }, 211 { x: 0, y: 0, z: 1 }, 212 { x: 1, y: 0, z: 1 }, 213 { x: 1, y: 1, z: 1 }, 214 { x: 0, y: 1, z: 1 } 215 ]; 216 geometry.indices = [ 217 0, 1, 2, 2, 3, 0, // front 218 4, 5, 6, 6, 7, 4, // back 219 0, 4, 5, 5, 1, 0, // bottom 220 1, 5, 6, 6, 2, 1, // right 221 3, 2, 6, 6, 7, 3, // top 222 3, 7, 4, 4, 0, 3 // left 223 ]; 224 geometry.topology = PrimitiveTopology.TRIANGLE_LIST; 225 geometry.normals = [ 226 { x: 0, y: 0, z: 1 }, 227 { x: 0, y: 0, z: 1 }, 228 { x: 0, y: 0, z: 1 }, 229 { x: 0, y: 0, z: 1 }, 230 { x: 0, y: 0, z: 1 }, 231 { x: 0, y: 0, z: 1 }, 232 { x: 0, y: 0, z: 1 }, 233 { x: 0, y: 0, z: 1 } 234 ]; 235 geometry.uvs = [ 236 { x: 0, y: 0 }, 237 { x: 1, y: 0 }, 238 { x: 1, y: 1 }, 239 { x: 0, y: 1 }, 240 { x: 0, y: 0 }, 241 { x: 1, y: 0 }, 242 { x: 1, y: 1 }, 243 { x: 0, y: 1 } 244 ]; 245 geometry.colors = [ 246 { r: 1, g: 0, b: 0, a: 1 }, 247 { r: 0, g: 1, b: 0, a: 1 }, 248 { r: 0, g: 0, b: 1, a: 1 }, 249 { r: 1, g: 1, b: 0, a: 1 }, 250 { r: 1, g: 0, b: 1, a: 1 }, 251 { r: 0, g: 1, b: 1, a: 1 }, 252 { r: 1, g: 1, b: 1, a: 1 }, 253 { r: 0, g: 0, b: 0, a: 1 } 254 ]; 255 let sceneResourceParameter: SceneResourceParameters = { 256 name: "cubeMesh", 257 uri: $rawfile("image/Cube_BaseColor.png") 258 }; 259 return renderResourceFactory.createMesh(sceneResourceParameter, geometry); 260} 261``` 262 263### createSampler 264createSampler(params:SceneResourceParameters): Promise\<Sampler> 265 266Creates a sampler based on the scene resource parameters. This API uses a promise to return the result. 267 268**System capability**: SystemCapability.ArkUi.Graphics3D 269 270**Parameters** 271| Name| Type| Mandatory| Description| 272| ---- | ---- | ---- | ---- | 273| params | [SceneResourceParameters](#sceneresourceparameters) | Yes| Parameters for creating the sampler.| 274 275**Return value** 276| Type| Description| 277| ---- | ---- | 278| Promise\<[Sampler](js-apis-inner-scene-resources.md#sampler20)> | Promise used to return the Sampler object created.| 279 280**Example** 281```ts 282import { Image, Shader, MaterialType, Material, ShaderMaterial, Animation, Environment, Container, SceneNodeParameters, 283 LightType, Light, Camera, SceneResourceParameters, SceneResourceFactory, Scene, Node, RenderContext, RenderResourceFactory, 284 Sampler } from '@kit.ArkGraphics3D'; 285 286function createSamplerResource(): Promise<Sampler> { 287 const renderContext: RenderContext | null = Scene.getDefaultRenderContext(); 288 if (!renderContext) { 289 return Promise.reject(new Error("RenderContext is null")); 290 } 291 const renderResourceFactory: RenderResourceFactory = renderContext.getRenderResourceFactory(); 292 let samplerParams: SceneResourceParameters = { 293 name: "sampler1", 294 uri: $rawfile("image/Cube_BaseColor.png") 295 }; 296 return renderResourceFactory.createSampler(samplerParams); 297} 298``` 299 300### createScene 301createScene(uri?: ResourceStr): Promise\<Scene> 302 303Creates a scene from the specified resource URI. If no URI is specified, an empty scene is created. This API uses a promise to return the result. 304 305**System capability**: SystemCapability.ArkUi.Graphics3D 306 307**Parameters** 308| Name| Type| Mandatory| Description| 309| ---- | ---- | ---- | ---- | 310| uri | [ResourceStr](../apis-arkui/arkui-ts/ts-types.md#resourcestr) | No| Resource path used for creating the scene. If no resource path is passed, an empty scene is created.| 311 312**Return value** 313| Type| Description| 314| ---- | ---- | 315| Promise\<[Scene](#scene-1)> | Promise used to return the Scene object created.| 316 317**Example** 318```ts 319import { Image, Shader, MaterialType, Material, ShaderMaterial, Animation, Environment, Container, SceneNodeParameters, 320 LightType, Light, Camera, SceneResourceParameters, SceneResourceFactory, Scene, Node } from '@kit.ArkGraphics3D'; 321import { RenderContext, RenderResourceFactory } from '@ohos.graphics.scene'; 322 323// fromFile=true: loads a scene from the specified GLB file. fromFile=false: creates an empty scene. This parameter illustrates two typical methods for creating scenes. 324function createScenePromise(fromFile: boolean = false): Promise<Scene> { 325 const renderContext: RenderContext | null = Scene.getDefaultRenderContext(); 326 if (!renderContext) { 327 return Promise.reject(new Error("RenderContext is null")); 328 } 329 330 const renderResourceFactory: RenderResourceFactory = renderContext.getRenderResourceFactory(); 331 if (fromFile) { 332 // Create a scene from a file. 333 return renderResourceFactory.createScene($rawfile("gltf/CubeWithFloor/glTF/AnimatedCube.glb")); 334 } else { 335 // Create an empty scene. 336 return renderResourceFactory.createScene(); 337 } 338} 339``` 340 341## SceneResourceFactory 342Provides APIs for creating resources, such as cameras and light sources, used in 3D scenes. This class inherits from [RenderResourceFactory](#renderresourcefactory20). 343 344### createCamera 345createCamera(params: SceneNodeParameters): Promise\<Camera> 346 347Creates a camera based on scene node parameters. This API uses a promise to return the result. 348 349**System capability**: SystemCapability.ArkUi.Graphics3D 350 351**Parameters** 352| Name| Type| Mandatory| Description| 353| ---- | ---- | ---- | ---- | 354| params | [SceneNodeParameters](#scenenodeparameters) | Yes| Scene node parameters.| 355 356**Return value** 357| Type| Description| 358| ---- | ---- | 359| Promise\<[Camera](js-apis-inner-scene-nodes.md#camera)> | Promise used to return the Camera object created.| 360 361**Example** 362```ts 363import { Image, Shader, MaterialType, Material, ShaderMaterial, Animation, Environment, Container, SceneNodeParameters, 364 LightType, Light, Camera, SceneResourceParameters, SceneResourceFactory, Scene, Node } from '@kit.ArkGraphics3D'; 365 366function createCameraPromise() : Promise<Camera> { 367 return new Promise(() => { 368 let scene: Promise<Scene> = Scene.load($rawfile("gltf/CubeWithFloor/glTF/AnimatedCube.glb")); 369 scene.then(async (result: Scene) => { 370 let sceneFactory: SceneResourceFactory = result.getResourceFactory(); 371 let sceneCameraParameter: SceneNodeParameters = { name: "camera1" }; 372 // Create a camera. 373 let camera: Promise<Camera> = sceneFactory.createCamera(sceneCameraParameter); 374 return camera; 375 }); 376 }); 377} 378``` 379 380### createLight 381createLight(params: SceneNodeParameters, lightType: LightType): Promise\<Light> 382 383Creates a light based on the scene node parameters and light type. This API uses a promise to return the result. 384 385**System capability**: SystemCapability.ArkUi.Graphics3D 386 387**Parameters** 388| Name| Type| Mandatory| Description| 389| ---- | ---- | ---- | ---- | 390| params | [SceneNodeParameters](#scenenodeparameters) | Yes| Scene node parameters.| 391| lightType | [LightType](js-apis-inner-scene-nodes.md#lighttype) | Yes| Light type.| 392 393**Return value** 394| Type| Description| 395| ---- | ---- | 396| Promise\<[Light](js-apis-inner-scene-nodes.md#light)> | Promise used to return the Light object created.| 397 398**Example** 399```ts 400import { Image, Shader, MaterialType, Material, ShaderMaterial, Animation, Environment, Container, SceneNodeParameters, 401 LightType, Light, Camera, SceneResourceParameters, SceneResourceFactory, Scene, Node } from '@kit.ArkGraphics3D'; 402 403function createLightPromise() : Promise<Light> { 404 return new Promise(() => { 405 let scene: Promise<Scene> = Scene.load($rawfile("gltf/CubeWithFloor/glTF/AnimatedCube.glb")); 406 scene.then(async (result: Scene) => { 407 let sceneFactory: SceneResourceFactory = result.getResourceFactory(); 408 let sceneLightParameter: SceneNodeParameters = { name: "light" }; 409 // Create directional light. 410 let light: Promise<Light> = sceneFactory.createLight(sceneLightParameter, LightType.DIRECTIONAL); 411 return light; 412 }); 413 }); 414} 415``` 416 417### createNode 418createNode(params: SceneNodeParameters): Promise\<Node> 419 420Creates a node. This API uses a promise to return the result. 421 422**System capability**: SystemCapability.ArkUi.Graphics3D 423 424**Parameters** 425| Name| Type| Mandatory| Description| 426| ---- | ---- | ---- | ---- | 427| params | [SceneNodeParameters](#scenenodeparameters) | Yes| Scene node parameters.| 428 429**Return value** 430| Type| Description| 431| ---- | ---- | 432| Promise\<[Node](js-apis-inner-scene-nodes.md#node)> | Promise used to return the Node object.| 433 434**Example** 435```ts 436import { Image, Shader, MaterialType, Material, ShaderMaterial, Animation, Environment, Container, SceneNodeParameters, 437 LightType, Light, Camera, SceneResourceParameters, SceneResourceFactory, Scene, Node } from '@kit.ArkGraphics3D'; 438 439function createNodePromise() : Promise<Node> { 440 return new Promise(() => { 441 let scene: Promise<Scene> = Scene.load($rawfile("gltf/CubeWithFloor/glTF/AnimatedCube.glb")); 442 scene.then(async (result: Scene) => { 443 let sceneFactory: SceneResourceFactory = result.getResourceFactory(); 444 let sceneNodeParameter: SceneNodeParameters = { name: "empty_node", 445 path:"/rootNode_/empty_node" }; 446 // Create a node. 447 let node: Promise<Node> = sceneFactory.createNode(sceneNodeParameter); 448 return node; 449 }); 450 }); 451} 452``` 453 454### createMaterial 455createMaterial(params: SceneResourceParameters, materialType: MaterialType): Promise\<Material> 456 457Creates a material based on the scene resource parameters and material type. This API uses a promise to return the result. 458 459**System capability**: SystemCapability.ArkUi.Graphics3D 460 461**Parameters** 462| Name| Type| Mandatory| Description| 463| ---- | ---- | ---- | ---- | 464| params | [SceneResourceParameters](#sceneresourceparameters) | Yes| Scene resource parameters.| 465| materialType | [MaterialType](js-apis-inner-scene-resources.md#materialtype) | Yes| Material type.| 466 467**Return value** 468| Type| Description| 469| ---- | ---- | 470| Promise\<[Material](js-apis-inner-scene-resources.md#material)> | Promise used to return the Material object.| 471 472**Example** 473```ts 474import { Image, Shader, MaterialType, Material, ShaderMaterial, Animation, Environment, Container, SceneNodeParameters, 475 LightType, Light, Camera, SceneResourceParameters, SceneResourceFactory, Scene, Node } from '@kit.ArkGraphics3D'; 476 477function createMaterialPromise() : Promise<Material> { 478 return new Promise(() => { 479 let scene: Promise<Scene> = Scene.load($rawfile("gltf/CubeWithFloor/glTF/AnimatedCube.glb")); 480 scene.then(async (result: Scene) => { 481 let sceneFactory: SceneResourceFactory = result.getResourceFactory(); 482 let sceneMaterialParameter: SceneResourceParameters = { name: "material" }; 483 // Create a material. 484 let material: Promise<Material> = sceneFactory.createMaterial(sceneMaterialParameter, MaterialType.SHADER); 485 return material; 486 }); 487 }); 488} 489``` 490 491### createEnvironment 492createEnvironment(params: SceneResourceParameters): Promise\<Environment> 493 494Creates an environment based on the scene resource parameters. This API uses a promise to return the result. 495 496**System capability**: SystemCapability.ArkUi.Graphics3D 497 498**Parameters** 499| Name| Type| Mandatory| Description| 500| ---- | ---- | ---- | ---- | 501| params | [SceneResourceParameters](#sceneresourceparameters) | Yes| Scene resource parameters.| 502 503**Return value** 504| Type| Description| 505| ---- | ---- | 506| Promise\<[Environment](js-apis-inner-scene-resources.md#environment)> | Promise used to return the Environment object created.| 507 508**Example** 509```ts 510import { Image, Shader, MaterialType, Material, ShaderMaterial, Animation, Environment, Container, SceneNodeParameters, 511 LightType, Light, Camera, SceneResourceParameters, SceneResourceFactory, Scene, Node } from '@kit.ArkGraphics3D'; 512 513function createEnvironmentPromise() : Promise<Environment> { 514 return new Promise(() => { 515 let scene: Promise<Scene> = Scene.load($rawfile("gltf/CubeWithFloor/glTF/AnimatedCube.glb")); 516 scene.then(async (result: Scene) => { 517 let sceneFactory: SceneResourceFactory = result.getResourceFactory(); 518 let sceneEnvironmentParameter: SceneResourceParameters = { name: "env", uri: $rawfile("KTX/quarry_02_2k_radiance.ktx") }; 519 // Create an environment. 520 let env: Promise<Environment> = sceneFactory.createEnvironment(sceneEnvironmentParameter); 521 return env; 522 }); 523 }); 524} 525``` 526 527### createGeometry<sup>18+</sup> 528createGeometry(params: SceneNodeParameters, mesh:MeshResource): Promise\<Geometry> 529 530Creates a geometry object based on the scene node parameters and mesh data. This API uses a promise to return the result. 531 532**System capability**: SystemCapability.ArkUi.Graphics3D 533 534**Parameters** 535| Name| Type| Mandatory| Description| 536| ---- | ---- | ---- | ---- | 537| params | [SceneNodeParameters](#scenenodeparameters) | Yes| Scene node parameters.| 538| mesh | [MeshResource](js-apis-inner-scene-resources.md#meshresource18) | Yes| Mesh data parameters.| 539 540**Return value** 541| Type| Description| 542| ---- | ---- | 543| Promise\<[Geometry](js-apis-inner-scene-nodes.md#geometry)> | Promise used to return the Geometry object created.| 544 545**Example** 546```ts 547import { Image, Shader, MaterialType, Material, ShaderMaterial, Animation, Environment, Container, SceneNodeParameters, 548 LightType, Light, Camera, SceneResourceParameters, SceneResourceFactory, Scene, Node, Geometry, CubeGeometry, MeshResource} from '@kit.ArkGraphics3D'; 549 550function createGeometryPromise() : Promise<Geometry> { 551 return new Promise(() => { 552 let scene: Promise<Scene> = Scene.load(); 553 scene.then(async (result: Scene | undefined) => { 554 if (!result) { 555 return; 556 } 557 let sceneFactory: SceneResourceFactory = result.getResourceFactory(); 558 let cubeGeom = new CubeGeometry(); 559 cubeGeom.size = { x: 1, y: 1, z: 1 }; 560 let meshRes = await sceneFactory.createMesh({ name: "MeshName" }, cubeGeom); 561 console.info("TEST createGeometryPromise"); 562 let geometry: Promise<Geometry> = sceneFactory.createGeometry({ name: "GeometryName" }, meshRes); 563 return geometry; 564 }); 565 }); 566} 567``` 568 569## SceneComponent<sup>20+</sup> 570Represents a basic scene component, which is used to describe the component information of a scene node, including the component name and its properties. 571 572### Properties 573 574**System capability**: SystemCapability.ArkUi.Graphics3D 575 576| Name| Type| Read Only| Optional| Description| 577| ---- | ---- | ---- | ---- | ---- | 578| name | string | No| No| Name of the scene component, which is customizable.| 579| property | Record<string, string \| number \| Vec2 \| Vec3 \| Vec4 \| Image \| boolean \| number[] \| string[] \| Image[]> | Yes| No| A set of component properties stored in key-value pairs. It supports various basic and complex types to describe various properties of the scene component.| 580 581## RenderContext<sup>20+</sup> 582Defines the context of all rendering resources. Multiple scenes created within the same render context can share rendering resources. 583 584### getRenderResourceFactory 585getRenderResourceFactory() : RenderResourceFactory 586 587Obtains the rendering resource factory, which provides APIs for creating different rendering resources. 588 589**System capability**: SystemCapability.ArkUi.Graphics3D 590 591**Return value** 592| Type| Description| 593| ---- | ---- | 594| [RenderResourceFactory](#renderresourcefactory20) | RenderResourceFactory instance for creating rendering resources.| 595 596**Example** 597```ts 598import { Image, Shader, MaterialType, Material, ShaderMaterial, Animation, Environment, Container, SceneNodeParameters, 599 LightType, Light, Camera, SceneResourceParameters, SceneResourceFactory, Scene, Node, RenderContext, 600 RenderResourceFactory } from '@kit.ArkGraphics3D'; 601 602function getRenderResourceFactory(): void { 603 const renderContext: RenderContext | null = Scene.getDefaultRenderContext(); 604 if (!renderContext) { 605 console.error("RenderContext is null"); 606 return; 607 } 608 const renderResourceFactory: RenderResourceFactory = renderContext.getRenderResourceFactory(); 609 console.info("TEST getRenderResourceFactory"); 610} 611``` 612 613### loadPlugin 614loadPlugin(name: string): Promise\<boolean> 615 616Loads a plugin by name. The API locates and loads the corresponding plugin resource using the provided plugin name. It uses a promise to return the result. 617 618**System capability**: SystemCapability.ArkUi.Graphics3D 619 620**Parameters** 621| Name| Type| Mandatory| Description| 622| ---- | ---- | ---- | ---- | 623| name | string | Yes| Name of the plugin to load, which must be a system predefined or registered and available plugin name, and follow the naming conventions.| 624 625**Return value** 626| Type| Description| 627| ---- | ---- | 628| Promise\<boolean> | Promise used to return a Boolean value, indicating whether the plugin is loaded. The value **true** means that the plugin is loaded, and **false** means the opposite.| 629 630**Example** 631```ts 632import { Image, Shader, MaterialType, Material, ShaderMaterial, Animation, Environment, Container, SceneNodeParameters, 633 LightType, Light, Camera, SceneResourceParameters, SceneResourceFactory, Scene, Node, RenderContext, 634 RenderResourceFactory } from '@kit.ArkGraphics3D'; 635 636function loadPlugin(): Promise<boolean> { 637 const renderContext: RenderContext | null = Scene.getDefaultRenderContext(); 638 if (!renderContext) { 639 console.error("RenderContext is null"); 640 return Promise.resolve(false); 641 } 642 return renderContext.loadPlugin("pluginName"); 643} 644``` 645 646### registerResourcePath<sup>20+</sup> 647registerResourcePath(protocol: string, uri: string): boolean 648 649Registers the directory path and retrieval name for asset files, such as shaders. It allows the system to find and replace the path descriptions of related files within the shaders using the retrieval name. This ensures that the correct paths for assets and their associated files are located and loaded properly. 650 651**System capability**: SystemCapability.ArkUi.Graphics3D 652 653**Parameters** 654| Name| Type| Mandatory| Description| 655| ---- | ---- | ---- | ---- | 656| protocol | string | Yes| Retrieval name for the path. It must be a non-empty string that is not already predefined or registered by the system.| 657| uri | string | Yes| Directory path of the assets, which corresponds to the retrieval name. It must be the path to the folder containing the asset files.| 658 659**Return value** 660| Type| Description| 661| ---- | ---- | 662| boolean | Result indicating whether the registration is successful. **true** if successful, and **false** otherwise. The possible cause of a registration failure is that the retrieval name has been registered or an input parameter is invalid.| 663 664**Example** 665```ts 666import { Image, Shader, MaterialType, Material, ShaderMaterial, Animation, Environment, Container, SceneNodeParameters, 667 LightType, Light, Camera, SceneResourceParameters, SceneResourceFactory, Scene, Node, RenderContext, 668 RenderResourceFactory } from '@kit.ArkGraphics3D'; 669 670function registerResourcePath(): void { 671 Scene.load($rawfile("shaders/custom_shader/custom_material_sample.shader")) 672 .then(scene => { 673 const renderContext: RenderContext | null = Scene.getDefaultRenderContext(); 674 if (!renderContext) { 675 console.error("RenderContext is null"); 676 return false; 677 } 678 return renderContext.registerResourcePath("protocol", "OhosRawFile://uri"); 679 }) 680 .then(result => { 681 if (result) { 682 console.info("resource path registration success"); 683 } else { 684 console.error("resource path registration failed"); 685 } 686 }); 687} 688``` 689 690## RenderParameters<sup>15+</sup> 691Describes the rendering parameters. 692 693**System capability**: SystemCapability.ArkUi.Graphics3D 694| Name| Type| Read Only| Optional| Description| 695| ---- | ---- | ---- | ---- | ---- | 696| alwaysRender<sup>15+</sup> | boolean | No| Yes| Whether to render every frame. The value **true** means to render every frame, and **false** means to render frames on demand. The default value is **true**.| 697 698 699## Scene 700Describes a scene. 701 702### Properties 703 704**System capability**: SystemCapability.ArkUi.Graphics3D 705 706| Name| Type| Read Only| Optional| Description| 707| ---- | ---- | ---- | ---- | ---- | 708| environment | [Environment](js-apis-inner-scene-resources.md#environment) | No| No| Environment object.| 709| animations | [Animation](js-apis-inner-scene-resources.md#animation)[] | Yes| No| Animation objects in the 3D scene.| 710| root | [Node](js-apis-inner-scene-nodes.md#node) \| null | Yes| No| Root node in the 3D scene tree.| 711 712### load 713static load(uri?: ResourceStr): Promise\<Scene> 714 715Loads a resource by path. This API uses a promise to return the result. 716 717**System capability**: SystemCapability.ArkUi.Graphics3D 718 719**Parameters** 720| Name| Type| Mandatory| Description| 721| ---- | ---- | ---- | ---- | 722| uri | [ResourceStr](../apis-arkui/arkui-ts/ts-types.md#resourcestr) | No| Path of the model file resource to load. The default value is undefined.| 723 724**Return value** 725| Type| Description| 726| ---- | ---- | 727| Promise\<[Scene](#scene-1)> | Promise used to return the Scene object created.| 728 729**Example** 730```ts 731import { Image, Shader, MaterialType, Material, ShaderMaterial, Animation, Environment, Container, SceneNodeParameters, 732 LightType, Light, Camera, SceneResourceParameters, SceneResourceFactory, Scene, Node } from '@kit.ArkGraphics3D'; 733 734function loadModel() : void { 735 // Load the model. 736 let scene: Promise<Scene> = Scene.load($rawfile("gltf/CubeWithFloor/glTF/AnimatedCube.glb")); 737 scene.then(async (result: Scene) => {}); 738} 739``` 740 741### getNodeByPath 742getNodeByPath(path: string, type?: NodeType): Node | null 743 744Obtains a node by path. 745 746**System capability**: SystemCapability.ArkUi.Graphics3D 747 748**Parameters** 749| Name| Type| Mandatory| Description| 750| ---- | ---- | ---- | ---- | 751| path | string | Yes| Path in the scene node tree. Each layer is separated by a slash (/).| 752| type | [NodeType](js-apis-inner-scene-nodes.md#nodetype) | No| Type of the node expected. The default value is null.| 753 754**Return value** 755| Type| Description| 756| ---- | ---- | 757| [Node](js-apis-inner-scene-nodes.md#node) \| null | Returns the Node object requested. If no node is found in the specified path or the found node type does not match the expected type, null is returned.| 758 759**Example** 760```ts 761import { Image, Shader, MaterialType, Material, ShaderMaterial, Animation, Environment, Container, SceneNodeParameters, 762 LightType, Light, Camera, SceneResourceParameters, SceneResourceFactory, Scene, Node } from '@kit.ArkGraphics3D'; 763 764function getNode() : void { 765 let scene: Promise<Scene> = Scene.load($rawfile("gltf/CubeWithFloor/glTF/AnimatedCube.glb")); 766 scene.then(async (result: Scene) => { 767 if (result) { 768 // Search for a node in the specified path. 769 let node : Node | null = result.getNodeByPath("rootNode_"); 770 } 771 }); 772} 773``` 774 775### getResourceFactory 776getResourceFactory(): SceneResourceFactory 777 778Obtains the scene resource factory. 779 780**System capability**: SystemCapability.ArkUi.Graphics3D 781 782**Return value** 783| Type| Description| 784| ---- | ---- | 785| [SceneResourceFactory](js-apis-inner-scene.md#sceneresourcefactory)| Scene resource factory.| 786 787**Example** 788```ts 789import { Image, Shader, MaterialType, Material, ShaderMaterial, Animation, Environment, Container, SceneNodeParameters, 790 LightType, Light, Camera, SceneResourceParameters, SceneResourceFactory, Scene, Node } from '@kit.ArkGraphics3D'; 791 792function getFactory() : void { 793 let scene: Promise<Scene> = Scene.load($rawfile("gltf/CubeWithFloor/glTF/AnimatedCube.glb")); 794 scene.then(async (result: Scene) => { 795 if (result) { 796 // Obtain a SceneResourceFactory object. 797 let sceneFactory: SceneResourceFactory = result.getResourceFactory(); 798 } 799 }); 800} 801``` 802 803### destroy 804destroy(): void 805 806Destroys this scene and releases all scene resources. 807 808**System capability**: SystemCapability.ArkUi.Graphics3D 809 810**Example** 811```ts 812import { Image, Shader, MaterialType, Material, ShaderMaterial, Animation, Environment, Container, SceneNodeParameters, 813 LightType, Light, Camera, SceneResourceParameters, SceneResourceFactory, Scene, Node } from '@kit.ArkGraphics3D'; 814 815function destroy() : void { 816 let scene: Promise<Scene> = Scene.load($rawfile("gltf/CubeWithFloor/glTF/AnimatedCube.glb")); 817 scene.then(async (result: Scene) => { 818 if (result) { 819 // Destroy the scene. 820 result.destroy(); 821 } 822 }); 823} 824``` 825 826### importNode<sup>18+</sup> 827importNode(name: string, node: Node, parent: Node | null): Node 828 829Imports a node from another scene. 830 831**System capability**: SystemCapability.ArkUi.Graphics3D 832 833**Parameters** 834| Name| Type| Mandatory| Description| 835| ---- | ---- | ---- | ---- | 836| name | string | Yes| Name of the imported node, which can be customized without specific constraints.| 837| node | [Node](js-apis-inner-scene-nodes.md#node) | Yes| Node to import.| 838| parent | [Node](js-apis-inner-scene-nodes.md#node) \| null | Yes| Parent node of the imported node in the new scene.| 839 840**Return value** 841| Type| Description| 842| ---- | ---- | 843| [Node](js-apis-inner-scene-nodes.md#node) | Node imported.| 844 845**Example** 846```ts 847import { Image, Shader, MaterialType, Material, ShaderMaterial, Animation, Environment, Container, SceneNodeParameters, 848 LightType, Light, Camera, SceneResourceParameters, SceneResourceFactory, Scene, Node, Geometry, CubeGeometry, MeshResource} from '@kit.ArkGraphics3D'; 849 850function ImportNodeTest() { 851 Scene.load().then(async (result: Scene | undefined) => { 852 if (!result) { 853 return; 854 } 855 Scene.load($rawfile("gltf/AnimatedCube/glTF/AnimatedCube.glb")) 856 .then(async (extScene: Scene) => { 857 let extNode = extScene.getNodeByPath("rootNode_/Unnamed Node 1/AnimatedCube"); 858 console.info("TEST ImportNodeTest"); 859 let node = result.importNode("scene", extNode, result.root); 860 if (node) { 861 node.position.x = 5; 862 } 863 }); 864 }); 865} 866``` 867 868### importScene<sup>18+</sup> 869importScene(name: string, scene: Scene, parent: Node | null): Node 870 871Imports another scene into the current one. 872 873**System capability**: SystemCapability.ArkUi.Graphics3D 874 875**Parameters** 876| Name| Type| Mandatory| Description| 877| ---- | ---- | ---- | ---- | 878| name | string | Yes| Root node name of the imported scene, which can be customized without specific constraints.| 879| scene | [Scene](#scene-1) | Yes| Scene to import.| 880| parent | [Node](js-apis-inner-scene-nodes.md#node) \| null | Yes| Parent node of the imported scene in the new scene.| 881 882**Return value** 883| Type| Description| 884| ---- | ---- | 885| [Node](js-apis-inner-scene-nodes.md#node) | Root node of the imported scene.| 886 887**Example** 888```ts 889import { Image, Shader, MaterialType, Material, ShaderMaterial, Animation, Environment, Container, SceneNodeParameters, 890 LightType, Light, Camera, SceneResourceParameters, SceneResourceFactory, Scene, Node, Geometry, CubeGeometry, MeshResource} from '@kit.ArkGraphics3D'; 891 892function ImportSceneTest() { 893 Scene.load().then(async (result: Scene | undefined) => { 894 if (!result) { 895 return; 896 } 897 let content = await result.getResourceFactory().createScene($rawfile("gltf/DamagedHelmet/glTF/DamagedHelmet.glb")) 898 console.info("TEST ImportSceneTest"); 899 result.importScene("helmet", content, null); 900 }); 901} 902``` 903 904### renderFrame<sup>15+</sup> 905renderFrame(params?: RenderParameters): boolean 906 907Renders frames on demand, such as controlling the frame rate. 908 909**System capability**: SystemCapability.ArkUi.Graphics3D 910 911**Parameters** 912| Name| Type| Mandatory| Description| 913| ---- | ---- | ---- | ---- | 914| params | [RenderParameters](#renderparameters15) | No| Rendering parameters. The default value is undefined.| 915 916**Return value** 917| Type| Description| 918| ---- | ---- | 919| boolean | Rendering result. The value **true** is returned if rendering is successfully scheduled; returns **false** otherwise.| 920 921**Example** 922```ts 923import { Image, Shader, MaterialType, Material, ShaderMaterial, Animation, Environment, Container, SceneNodeParameters, 924 LightType, Light, Camera, SceneResourceParameters, SceneResourceFactory, Scene, Node, Geometry, CubeGeometry, MeshResource} from '@kit.ArkGraphics3D'; 925 926function RenderFrameTest() { 927 Scene.load($rawfile("gltf/DamagedHelmet/glTF/DamagedHelmet.glb")) 928 .then(async (result: Scene | undefined) => { 929 if (!result) { 930 return; 931 } 932 console.info("TEST RenderFrameTest"); 933 result.renderFrame({ alwaysRender: true }); 934 }); 935} 936``` 937 938### createComponent<sup>20+</sup> 939createComponent(node: Node, name: string): Promise\<SceneComponent> 940 941Creates a component and attaches it to a node. This API uses a promise to return the result. 942 943**System capability**: SystemCapability.ArkUi.Graphics3D 944 945**Parameters** 946| Name| Type| Mandatory| Description| 947| ---- | ---- | ---- | ---- | 948| node | [Node](js-apis-inner-scene-nodes.md#node) | Yes| Node to which the component will be attached.| 949| name | string | Yes| Name of the component to create, which is defined by individual plugins.| 950 951**Return value** 952| Type| Description| 953| ---- | ---- | 954| Promise\<[SceneComponent](#scenecomponent20)> | Promise used to return the SceneComponent object created.| 955 956**Example** 957```ts 958import { Image, Shader, MaterialType, Material, ShaderMaterial, Animation, Environment, Container, SceneNodeParameters, 959 LightType, Light, Camera, SceneResourceParameters, SceneResourceFactory, Scene, Node } from '@kit.ArkGraphics3D'; 960import { SceneComponent } from '@ohos.graphics.scene'; 961 962 963function createComponentTest(): Promise<SceneComponent> { 964 return Scene.load($rawfile("gltf/DamagedHelmet/glTF/DamagedHelmet.glb")) 965 .then(scene => { 966 if (!scene) { 967 return Promise.reject(new Error("Scene load failed")); 968 } 969 // RenderConfigurationComponent is an internal component of the engine. You do not need to install plugins when creating the component. 970 return scene.createComponent(scene.root, "RenderConfigurationComponent"); 971 }) 972 .then(component => { 973 if (!component) { 974 return Promise.reject(new Error("createComponent failed")); 975 } 976 return component; 977 }); 978} 979``` 980 981### getComponent<sup>20+</sup> 982getComponent(node: Node, name: string): SceneComponent | null 983 984Obtains the component instance from a node based on the component name. 985 986**System capability**: SystemCapability.ArkUi.Graphics3D 987 988**Parameters** 989| Name| Type| Mandatory| Description| 990| ---- | ---- | ---- | ---- | 991| node | [Node](js-apis-inner-scene-nodes.md#node) | Yes| Node to which the component is attached.| 992| name | string | Yes| Name of the component to obtain. The value must be a system predefined or registered custom component name, and follow the naming conventions.| 993 994**Return value** 995| Type| Description| 996| ---- | ---- | 997| [SceneComponent](#scenecomponent20) \| null | SceneComponent object corresponding to the given name, or **null** if not found.| 998 999**Example** 1000```ts 1001import { Image, Shader, MaterialType, Material, ShaderMaterial, Animation, Environment, Container, SceneNodeParameters, 1002 LightType, Light, Camera, SceneResourceParameters, SceneResourceFactory, Scene, Node, Geometry, CubeGeometry, MeshResource, SceneComponent } from '@kit.ArkGraphics3D'; 1003 1004function getComponentTest() { 1005 Scene.load($rawfile("gltf/DamagedHelmet/glTF/DamagedHelmet.glb")) 1006 .then(async (result: Scene | undefined) => { 1007 if (!result) { 1008 console.error("Scene load failed"); 1009 return; 1010 } 1011 console.info("TEST getComponentTest"); 1012 let component = result.getComponent(result.root, "myComponent"); 1013 if (component) { 1014 console.info("getComponent success"); 1015 } else { 1016 console.warn("Component not found"); 1017 } 1018 }); 1019} 1020``` 1021 1022### getDefaultRenderContext<sup>20+</sup> 1023static getDefaultRenderContext(): RenderContext | null 1024 1025Obtains the rendering context associated with the current graphics object. 1026 1027**Return value** 1028| Type| Description| 1029| ---- | ---- | 1030| [RenderContext](#rendercontext20) \| null | Rendering context associated with the current object, or **null** if no rendering context is associated.| 1031 1032**Example** 1033```ts 1034import { Image, Shader, MaterialType, Material, ShaderMaterial, Animation, Environment, Container, SceneNodeParameters, 1035 LightType, Light, Camera, SceneResourceParameters, SceneResourceFactory, Scene, Node, Geometry, CubeGeometry, MeshResource, SceneComponent, RenderContext } from '@kit.ArkGraphics3D'; 1036 1037function getDefaultRenderContextTest() { 1038 console.info("TEST getDefaultRenderContextTest"); 1039 const renderContext: RenderContext | null = Scene.getDefaultRenderContext(); 1040 if (renderContext) { 1041 console.info("getDefaultRenderContext success"); 1042 } else { 1043 console.error("RenderContext is null"); 1044 } 1045} 1046``` 1047