目录

Creating and Using Image Resources

Image: a two-dimensional texture resource in 3D rendering, essentially a memory buffer that stores pixel data. It provides the data required for rendering object surfaces, including maps such as base color, normal, metallic, roughness, and ambient occlusion. It can also serve as the texture input of a material or a custom shader, ultimately determining the appearance of an object surface.

ArkGraphics 3D provides the capability of creating Image resources in JPEG, PNG, and KTX formats and supports customizing the Image resources you need. The support for each format is described in the following table:

Format Description
JPEG (.jpg/.jpeg)

Supports JPEG files with JFIF, Exif, or ICC Profile markers in the header.

On devices running HarmonyOS 7.0.0 or later, JPEG files with DQT, XMP, MPF, or Adobe markers in the header are also supported.

PNG (.png) Supports standard PNG files.
KTX (.ktx) Supports files in the KTX format. KTX (Khronos Texture) is a texture container format defined by the Khronos Group, used to store texture data that can be read directly by the GPU.

How to Develop

  1. Import the required modules.

    Import the core types provided by ArkGraphics 3D in the page script to create objects like scenes, cameras, materials, and images.

    import { Camera, Environment, Geometry, Image, Material, MaterialType, Scene, SceneResourceFactory,
      SceneResourceParameters, Shader, ShaderMaterial, EnvironmentBackgroundType } from '@kit.ArkGraphics3D';
  2. Load the scene and configure rendering parameters.

    Call Scene.load() to load model files in .glb or .gltf format, and obtain a scene object upon completion. Then, construct a SceneOptions object to specify the scene and rendering mode for rendering the scene content via Component3D.

    if (this.scene === null) {
      // Switch from .gltf to .glb; the content is the same, but the format is different.
      Scene.load($rawfile('gltf/CubeWithFloor/glTF/AnimatedCube.glb'))
        .then(async (result: Scene) => {
          // Assign the loaded scene to globalScene for unified resource creation.
          globalScene = result;
          this.scene = result;
          this.sceneOpt = { scene: this.scene, modelType: ModelType.SURFACE } as SceneOptions;
          this.rf = this.scene.getResourceFactory();
          // ...
        })
        .catch((error: string) => {
          console.error('init error: ' + error + '.');
        });
    }
  3. Initialize the camera.

    Create a camera object, and set its enabled state and viewing position for subsequent model display.

    this.cam = await this.rf.createCamera({ name: 'Camera1' });
    this.cam.enabled = true;
    this.cam.position.z = 5;
  4. Obtain the geometry node.

    Call Scene.getNodeByPath() to obtain the geometry node of the target model, and record its original material. This enables rollback or recovery in case the material is later modified.

    this.geom = this.scene.getNodeByPath('rootNode_/Unnamed Node 1/AnimatedCube') as Geometry;
    
    // Record the original material.
    this.originalMat = this.geom.mesh.subMeshes[0].material;
  5. Create an image resource.

    Use SceneResourceFactory.createImage() to create an image resource.

    function createImagePromise(): Promise<Image> {
      return new Promise((resolve, reject) => {
        // Ensure the scene is loaded before accessing sceneFactory.
        if (globalScene) {
          let sceneFactory: SceneResourceFactory = globalScene.getResourceFactory();
    
          let sceneImageParameter: SceneResourceParameters = {
            name: 'image',
            uri: $rawfile('image/Cube_BaseColor.png')
          };
    
          let image: Promise<Image> = sceneFactory.createImage(sceneImageParameter);
          image.then((imageEntity: Image) => {
            resolve(imageEntity);
          }).catch((err: string) => {
            console.error('Image load failed: ' + err + '.');
            reject(err);
          });
        } else {
          reject('Scene is not loaded yet.');
        }
      });
    }
  6. Apply the image material to the model node.

    In the button click callback, create a shader material using createShader() and createMaterial(), call createImagePromise() to obtain the image resource and bind it to the shader input property BASE_COLOR_Image, and then apply the material to the model geometry to make the surface texture take effect and replace the texture.

    Button('Replace with a Image material')
      // ...
      .onClick(async (): Promise<void> => {
        console.info('Start to replace with a material of image');
    
        if (!this.scene || !this.cam || !this.rf) {
          return;
        }
    
        // Create a shader.
        this.shader = await this.rf.createShader({
          name: 'shaderResource',
          uri: $rawfile('shaders/custom_shader/custom_material_sample.shader')
        });
    
        // Create an image material.
        this.imageMat = await this.rf.createMaterial({ name: 'imageMat' }, MaterialType.SHADER) as ShaderMaterial;
    
        // Bind the shader and the image material.
        this.imageMat.colorShader = this.shader;
        let createdImage =  await createImagePromise();
        if (createdImage) {
          this.imageMat.colorShader.inputs['BASE_COLOR_Image'] = createdImage;
        }
    
        this.geom = this.scene.getNodeByPath('rootNode_/Unnamed Node 1/AnimatedCube') as Geometry;
    
        this.geom.mesh.materialOverride = undefined;
        this.geom.mesh.subMeshes[0].material = this.imageMat;
      })