• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16/**
17 * @file Defines 3D scene related interfaces
18 * @kit ArkGraphics3D
19 */
20
21import { Shader, MaterialType, Material, Animation, Environment, Image, MeshResource, Sampler, SceneResource } from './SceneResources';
22import { Camera, LightType, Light, Node, NodeType, Geometry } from './SceneNodes';
23import { Position3, Color, GeometryDefinition, Vec2, Vec3, Vec4 } from './SceneTypes';
24
25/**
26 * The scene resource parameters type.
27 *
28 * @typedef SceneResourceParameters
29 * @syscap SystemCapability.ArkUi.Graphics3D
30 * @since 12
31 */
32export interface SceneResourceParameters {
33  /**
34   * The name of the scene resource parameters.
35   *
36   * @type { string }
37   * @syscap SystemCapability.ArkUi.Graphics3D
38   * @since 12
39   */
40  name: string;
41
42  /**
43   * The resource uri of the scene resource parameters.
44   *
45   * @type { ?ResourceStr }
46   * @syscap SystemCapability.ArkUi.Graphics3D
47   * @since 12
48   */
49  uri?: ResourceStr;
50}
51
52/**
53 * The scene node parameters type.
54 *
55 * @typedef SceneNodeParameters
56 * @syscap SystemCapability.ArkUi.Graphics3D
57 * @since 12
58 */
59export interface SceneNodeParameters {
60  /**
61   * The name of the scene node parameters.
62   *
63   * @type { string }
64   * @syscap SystemCapability.ArkUi.Graphics3D
65   * @since 12
66   */
67  name: string;
68
69  /**
70   * The path of the scene node parameters.
71   *
72   * @type { ?string }
73   * @syscap SystemCapability.ArkUi.Graphics3D
74   * @since 12
75   */
76  path?: string;
77}
78
79/**
80 * The result of a ray cast hit.
81 *
82 * @typedef RaycastResult
83 * @syscap SystemCapability.ArkUi.Graphics3D
84 * @since 20
85 */
86export interface RaycastResult {
87  /**
88   * The node that was hit.
89   *
90   * @type { Node }
91   * @syscap SystemCapability.ArkUi.Graphics3D
92   * @since 20
93   */
94  node: Node;
95
96  /**
97   * The distance to the center of the axis-aligned bounding box.
98   *
99   * @type { number }
100   * @syscap SystemCapability.ArkUi.Graphics3D
101   * @since 20
102   */
103  centerDistance: number;
104
105  /**
106   * The position of the hit in world coordinates.
107   *
108   * @type { Position3 }
109   * @syscap SystemCapability.ArkUi.Graphics3D
110   * @since 20
111   */
112  hitPosition: Position3;
113}
114
115/**
116 * How a raycast should be performed.
117 *
118 * @interface RaycastParameters
119 * @syscap SystemCapability.ArkUi.Graphics3D
120 * @since 20
121 */
122export interface RaycastParameters {
123  /**
124   * If defined, search only the nodes in the hierarchy under this node
125   * If undefined, search all the nodes in the scene
126   *
127   * @type { ?Node }
128   * @syscap SystemCapability.ArkUi.Graphics3D
129   * @since 20
130   */
131  rootNode?: Node;
132}
133
134/**
135 * The render resource factory. RenderResourceFactory is used to create resources that can be shared
136 * across Scenes that share a RenderContext
137 *
138 * @interface RenderResourceFactory
139 * @syscap SystemCapability.ArkUi.Graphics3D
140 * @since 20
141 */
142export interface RenderResourceFactory {
143  /**
144   * Create a shader.
145   *
146   * @param { SceneResourceParameters } params - the param of creating a shader
147   * @returns { Promise<Shader> } promise a shader
148   * @syscap SystemCapability.ArkUi.Graphics3D
149   * @since 20
150   */
151  createShader(params: SceneResourceParameters): Promise<Shader>;
152
153  /**
154    * Create a image.
155    *
156    * @param { SceneResourceParameters } params - the param of creating a image
157    * @returns { Promise<Image> } promise a image
158    * @syscap SystemCapability.ArkUi.Graphics3D
159    * @since 20
160    */
161  createImage(params: SceneResourceParameters): Promise<Image>;
162
163  /**
164   * Create a Mesh from an array of vertices.
165   *
166   * @param { SceneResourceParameters } params - the param of creating a Mesh object
167   * @param { GeometryDefinition } geometry - what sort of a geometric shape to create
168   * @returns { Promise<MeshResource> } promise a Mesh
169   * @syscap SystemCapability.ArkUi.Graphics3D
170   * @since 20
171   */
172  createMesh(params: SceneResourceParameters, geometry: GeometryDefinition): Promise<MeshResource>;
173
174  /**
175   * create a Sampler
176   *
177   * @param { SceneResourceParameters } params - the param of create a sampler
178   * @returns { Promise<Sampler> } - promise a scene
179   * @syscap SystemCapability.ArkUi.Graphics3D
180   * @since 20
181   */
182  createSampler(params:SceneResourceParameters): Promise<Sampler>
183
184  /**
185   * Create a new scene from a Resource.
186   * If uri is not provided, will return an empty scene.
187   *
188   * @param { ResourceStr } [uri] - the resource of creating a scene
189   * @returns { Promise<Scene> } promise a scene
190   * @syscap SystemCapability.ArkUi.Graphics3D
191   * @since 20
192   */
193  createScene(uri?: ResourceStr): Promise<Scene>;
194}
195
196/**
197 * The scene resource factory.
198 *
199 * @extends RenderResourceFactory
200 * @interface SceneResourceFactory
201 * @syscap SystemCapability.ArkUi.Graphics3D
202 * @since 12
203 */
204export interface SceneResourceFactory extends RenderResourceFactory {
205  /**
206   * Create a camera.
207   *
208   * @param { SceneNodeParameters } params - the param of creating a camera
209   * @returns { Promise<Camera> } promise a camera
210   * @syscap SystemCapability.ArkUi.Graphics3D
211   * @since 12
212   */
213  createCamera(params: SceneNodeParameters): Promise<Camera>;
214
215  /**
216   * Create a light.
217   *
218   * @param { SceneNodeParameters } params - the param of creating a light
219   * @param { LightType } lightType - the type of the light
220   * @returns { Promise<Light> } promise a light
221   * @syscap SystemCapability.ArkUi.Graphics3D
222   * @since 12
223   */
224  createLight(params: SceneNodeParameters, lightType: LightType): Promise<Light>;
225
226  /**
227   * Create a node.
228   *
229   * @param { SceneNodeParameters } params - the param of creating a node
230   * @returns { Promise<Node> } promise a node
231   * @syscap SystemCapability.ArkUi.Graphics3D
232   * @since 12
233   */
234  createNode(params: SceneNodeParameters): Promise<Node>;
235
236  /**
237   * Create a material.
238   *
239   * @param { SceneResourceParameters } params - the param of creating a material
240   * @param { MaterialType } materialType - the type of the material
241   * @returns { Promise<Material> } promise a material
242   * @syscap SystemCapability.ArkUi.Graphics3D
243   * @since 12
244   */
245  createMaterial(params: SceneResourceParameters, materialType: MaterialType): Promise<Material>;
246
247  /**
248   * Create a environment.
249   *
250   * @param { SceneResourceParameters } params - the param of creating a Environment object
251   * @returns { Promise<Environment> } promise a Environment
252   * @syscap SystemCapability.ArkUi.Graphics3D
253   * @since 12
254   */
255  createEnvironment(params: SceneResourceParameters): Promise<Environment>;
256
257  /**
258   * Create a geometry node.
259   *
260   * @param { SceneNodeParameters } params - the param of creating a geometry
261   * @param { MeshResource } mesh resource - The mesh data for the geometry
262   * @returns { Promise<Geometry> } promise a geometry
263   * @syscap SystemCapability.ArkUi.Graphics3D
264   * @since 18
265   */
266  createGeometry(params: SceneNodeParameters, mesh:MeshResource): Promise<Geometry>;
267}
268
269/**
270 * Define underlying scene component
271 *
272 * @interface SceneComponent
273 * @syscap SystemCapability.ArkUi.Graphics3D
274 * @since 20
275 */
276export interface SceneComponent {
277  /**
278   * Scene component name
279   *
280   * @type { string }
281   * @syscap SystemCapability.ArkUi.Graphics3D
282   * @since 20
283   */
284  name: string;
285
286  /**
287   * Component properties
288   *
289   * @type { Record<string, string | number | Vec2 | Vec3 | Vec4 | SceneResource | boolean | number[] | string[] | SceneResource[] | Vec2[] | Vec3[] | Vec4[] | null | undefined> }
290   * @readonly
291   * @syscap SystemCapability.ArkUi.Graphics3D
292   * @since 20
293   */
294  readonly property: Record<string, string | number | Vec2 | Vec3 | Vec4 | SceneResource | boolean | number[] | string[] | SceneResource[] | Vec2[] | Vec3[] | Vec4[] | null | undefined>;
295}
296
297/**
298 * Render context defines the context for all rendering resources. Resources within the same render context
299 * may be shared between scenes created within the same render context.
300 *
301 * @interface RenderContext
302 * @syscap SystemCapability.ArkUi.Graphics3D
303 * @since 20
304 */
305export interface RenderContext {
306  /**
307   * Get resource factory.
308   *
309   * @returns { RenderResourceFactory } -- RenderResourceFactory instance
310   * @syscap SystemCapability.ArkUi.Graphics3D
311   * @since 20
312   */
313  getRenderResourceFactory() : RenderResourceFactory;
314
315  /**
316   * Load external plugin
317   *
318   * @param {string} name - Name of the plugin
319   * @returns { Promise<boolean> } - Promise a boolean to show if the plugin load is successful
320   * @syscap SystemCapability.ArkUi.Graphics3D
321   * @since 20
322   */
323  loadPlugin(name: string): Promise<boolean>;
324
325  /**
326   * Register resource path
327   *
328   * @param { string } protocol - Protocol of the uri
329   * @param { string } uri - Path to register
330   * @returns { boolean } - True if registration success, false indicates the protocol has already been registered
331   * @syscap SystemCapability.ArkUi.Graphics3D
332   * @since 20
333   */
334  registerResourcePath(protocol: string, uri: string): boolean;
335}
336
337/**
338 * Defines parameters for manual rendering.
339 *
340 * @interface RenderParameters
341 * @syscap SystemCapability.ArkUi.Graphics3D
342 * @since 15
343 */
344export interface RenderParameters {
345  /**
346   * If true, the renderer should always render even if there have been no changes in the scene
347   * since the previous frame. If false, renderer may omit rendering if no changes have been made.
348   *
349   * @type { ?boolean }
350   * @syscap SystemCapability.ArkUi.Graphics3D
351   * @since 15
352   */
353  alwaysRender?: boolean;
354}
355
356/**
357 * Defines the 3d scene.
358 *
359 * @syscap SystemCapability.ArkUi.Graphics3D
360 * @since 12
361 */
362export declare class Scene {
363  /**
364   * Get default render context
365   *
366   * @returns { RenderContext | null } -- The default RenderContext instance
367   * @static
368   * @syscap SystemCapability.ArkUi.Graphics3D
369   * @since 20
370   */
371  static getDefaultRenderContext(): RenderContext | null;
372
373  /**
374   * Create a new scene from a ResourceStr.
375   * If uri is not provided, will return an empty scene.
376   *
377   * @param { ResourceStr } [uri] - the resource of creating a scene
378   * @returns { Promise<Scene> } promise a scene
379   * @static
380   * @syscap SystemCapability.ArkUi.Graphics3D
381   * @since 12
382   */
383  static load(uri? : ResourceStr): Promise<Scene>;
384
385  /**
386   * The environment of the scene.
387   *
388   * @type { Environment }
389   * @syscap SystemCapability.ArkUi.Graphics3D
390   * @since 12
391   */
392  environment: Environment;
393
394  /**
395   * The animations of the scene.
396   *
397   * @type { Animation[] }
398   * @readonly
399   * @syscap SystemCapability.ArkUi.Graphics3D
400   * @since 12
401   */
402  readonly animations: Animation[];
403
404  /**
405   * The root node of the scene.
406   *
407   * @type { Node | null }
408   * @readonly
409   * @syscap SystemCapability.ArkUi.Graphics3D
410   * @since 12
411   */
412  readonly root: Node | null;
413
414  /**
415   * Get a node by path.
416   *
417   * @param { string } path - the path of the node
418   * @param { NodeType } type - verify the type of node, if it does not match, return null
419   * @returns { Node | null } if the node is found by it's path
420   * @syscap SystemCapability.ArkUi.Graphics3D
421   * @since 12
422   */
423  getNodeByPath(path: string, type?: NodeType): Node | null;
424
425  /**
426   * Get resource factory.
427   *
428   * @returns { SceneResourceFactory } if the node is found by it's path
429   * @syscap SystemCapability.ArkUi.Graphics3D
430   * @since 12
431   */
432  getResourceFactory(): SceneResourceFactory;
433
434  /**
435   * Release all native scene resources. All TS references will be undefined.
436   *
437   * @syscap SystemCapability.ArkUi.Graphics3D
438   * @since 12
439   */
440  destroy(): void;
441
442  /**
443   * Import node into the scene. The original node may come from separate Scene.
444   * The node will be cloned and any modifications to the old node will not be visible after the import.
445   *
446   * @param { string } name - The name of the newly created node.
447   * @param { Node } node - The node to be imported.
448   * @param { Node | null} parent - The parent node or null for root
449   * @returns { Node } The newly created node.
450   * @syscap SystemCapability.ArkUi.Graphics3D
451   * @since 18
452   */
453  importNode(name: string, node: Node, parent: Node | null): Node;
454
455  /**
456   * Import scene into the scene as a node. The node hierarchy will appear under the parent node.
457   * All animations from the scene will be duplicated in the scene.
458   *
459   * @param { string } name - The name of the newly created node
460   * @param { Scene } scene - The scene to be imported.
461   * @param { Node | null } parent - The parent node or null for root
462   * @returns { Node } The newly created node.
463   * @syscap SystemCapability.ArkUi.Graphics3D
464   * @since 18
465   */
466  importScene(name: string, scene: Scene, parent: Node | null): Node;
467
468   /**
469   * A new frame is rendered for all active camera.
470   *
471   * @param { RenderParameters } params - Rendering parameters
472   * @returns { boolean } True if rendering was scheduled, false otherwise
473   * @syscap SystemCapability.ArkUi.Graphics3D
474   * @since 15
475   */
476  renderFrame(params?: RenderParameters): boolean;
477
478  /**
479   * Create a new component.
480   *
481   * @param { Node } node - The node the component is attached to
482   * @param { string } name - The name of the component to load. Valid names are defined by each plugin.
483   * @returns { Promise<SceneComponent> } - The newly added component.
484   * @syscap SystemCapability.ArkUi.Graphics3D
485   * @since 20
486   */
487  createComponent(node: Node, name: string): Promise<SceneComponent>;
488
489  /**
490    * Get component by name.
491    *
492    * @param { Node } node - The node component is attached to.
493    * @param { string } name - name of the component
494    * @returns { SceneComponent | null }
495    * @syscap SystemCapability.ArkUi.Graphics3D
496    * @since 20
497    */
498  getComponent(node: Node, name: string): SceneComponent | null;
499}
500