1/* 2 * Copyright (c) 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 16import scene3d from '@ohos.graphics.scene' 17import router from '@ohos.router'; 18import Logger from '../util/Logger'; 19 20@Entry 21@Component 22struct container { 23 scene: scene3d.Scene | null = null; 24 @State sceneOpt: SceneOptions | null = null; 25 cam: scene3d.Camera | null = null; 26 private node: scene3d.Node | null | undefined = undefined; 27 private sceneNode: scene3d.Node | null = null; 28 @State hierarchy: string = ''; 29 30 traversal(node: scene3d.Node | null): void { 31 if (!node) { 32 return; 33 } 34 35 this.hierarchy += node.path + '/' + node.name + '\n'; 36 let container: scene3d.Container<scene3d.Node> = node.children; 37 let count: number = container.count(); 38 39 this.hierarchy += ' '; 40 for (let i = 0; i < count; i++) { 41 this.traversal(container.get(i)); 42 } 43 } 44 45 onPageShow(): void { 46 this.init(); 47 } 48 49 onPageHide(): void { 50 if (this.scene) { 51 this.scene.destroy(); 52 } 53 54 this.cam = null; 55 this.scene = null; 56 } 57 58 init(): void { 59 if (this.scene === null) { 60 scene3d.Scene.load($rawfile("gltf/DamagedHelmet/glTF/DamagedHelmet.glb")).then(async (result: scene3d.Scene) => { 61 this.scene = result; 62 this.sceneOpt = { scene: this.scene, modelType: ModelType.SURFACE } as SceneOptions; 63 let rf: scene3d.SceneResourceFactory = this.scene.getResourceFactory(); 64 this.cam = await rf.createCamera({ "name": "Camera1" }); 65 this.cam.enabled = true; 66 this.cam.position.z = 5; // move camera to position 5 on z 67 68 this.node = this.scene.getNodeByPath('rootNode_/Scene/node_damagedHelmet_-6514'); 69 this.traversal(this.scene.root); 70 this.sceneNode = this.scene.getNodeByPath('rootNode_/Scene'); 71 }).catch((reason: string) => { 72 Logger.error("init error", reason); 73 }); 74 } 75 } 76 77 build() { 78 Row() { 79 Column() { 80 Column() { 81 if (this.sceneOpt) { 82 Component3D(this.sceneOpt) 83 .renderWidth('60%') 84 .renderHeight('60%') 85 } 86 else { 87 Text("loading 1..."); 88 } 89 } 90 .height('30%') 91 92 Button('remove node').onClick(() => { 93 this.scene?.root?.children.remove(this.node); 94 }).id('remove_node') 95 96 Button('append node').onClick(() => { 97 this.scene?.root?.children.get(0)?.children.append(this.node); 98 }).id('append_node') 99 100 Button('insert node').onClick(() => { 101 this.scene?.root?.children.get(0)?.children.insertAfter(this.node, null); 102 }).id('insert_node') 103 104 Button('clear').onClick(() => { 105 this.scene?.root?.children.clear(); 106 }).id('clear_node') 107 108 Text(this.hierarchy) 109 Button('back').onClick(() => { 110 router.back() 111 }).id('container_back') 112 } 113 .width('100%') 114 } 115 .height('100%') 116 } 117}