Skip to content

Commit

Permalink
feat(GUI): add sample to show how to use UIPanel/Billboard/UIMaterial…
Browse files Browse the repository at this point in the history
…, or change their properties. (Orillusion#206)

Add samples about GPU GUI.
render GUI box by GUIUtil, and modify docs.
Improve the cloning function of components.
Change UIPanel to extends UIImage.
  • Loading branch information
hellmor authored Jun 6, 2023
1 parent 1cc2567 commit 661a7f9
Show file tree
Hide file tree
Showing 55 changed files with 601 additions and 215 deletions.
156 changes: 156 additions & 0 deletions samples/gui/Sample_POI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import { GUIHelp } from "@orillusion/debug/GUIHelp";
import { createExampleScene, createSceneParam } from "@samples/utils/ExampleScene";
import { Scene3D, PropertyAnimation, Engine3D, Object3D, Object3DUtil, PropertyAnimClip, WrapMode, WorldPanel, BillboardType, TextAnchor, UIImage, UIShadow, UITextField, Vector3, Color, Time } from "@orillusion/core";
import { GUIUtil } from "@samples/utils/GUIUtil";

class Sample_POI {
scene: Scene3D;
panel: WorldPanel;
position: Vector3;

async run() {
Engine3D.setting.shadow.autoUpdate = true;
Engine3D.setting.shadow.updateFrameRate = 1;
Engine3D.setting.shadow.shadowBound = 20;
Engine3D.setting.shadow.shadowBias = 0.0001;

await Engine3D.init({ renderLoop: () => { this.loop(); } });
let param = createSceneParam();
param.camera.distance = 16;
let exampleScene = createExampleScene(param);

GUIHelp.init();

this.scene = exampleScene.scene;

Engine3D.startRenderView(exampleScene.view);

await this.initScene();
this.initDuckPOI();
this.initScenePOI();
}

private modelContainer: Object3D;

async initScene() {
// floor
let floor: Object3D = Object3DUtil.GetSingleCube(16, 0.1, 16, 1, 1, 1);
this.scene.addChild(floor);
await Engine3D.res.loadFont('fnt/0.fnt');

// load external model
let model = await Engine3D.res.loadGltf('PBR/Duck/Duck.gltf') as Object3D;
model.rotationY = 180;
this.modelContainer = new Object3D();
this.modelContainer.addChild(model);
this.scene.addChild(this.modelContainer);
model.scaleX = model.scaleY = model.scaleZ = 0.01;
await this.initPropertyAnim(this.modelContainer);

let chair = await Engine3D.res.loadGltf('PBR/SheenChair/SheenChair.gltf') as Object3D;
chair.scaleX = chair.scaleY = chair.scaleZ = 8;
this.scene.addChild(chair);
}

private async initPropertyAnim(owner: Object3D) {
// add PropertyAnimation
let animation = owner.addComponent(PropertyAnimation);

//load a animation clip
let json: any = await Engine3D.res.loadJSON('json/anim_0.json');
let animClip = new PropertyAnimClip();
animClip.parse(json);
animClip.wrapMode = WrapMode.Loop;
animation.defaultClip = animClip.name;
animation.autoPlay = true;

// register clip to animation
animation.appendClip(animClip);
animation.play(animation.defaultClip);
return animation;
}

private initDuckPOI() {
let canvas = this.scene.view.enableUICanvas();
//panel
this.panel = new Object3D().addComponent(WorldPanel);
this.panel.billboard = BillboardType.BillboardXYZ;
//add to canvas
canvas.addChild(this.panel.object3D);
this.panel.object3D.localScale = new Vector3(0.1, 0.1, 0.1);

//poi
let panelRoot = new Object3D();

this.panel.object3D.addChild(panelRoot);

let image = panelRoot.addComponent(UIImage);
image.uiTransform.resize(32, 6);
image.uiTransform.setXY(20, 20);

image.color = new Color(1, 1, 1, 0.5);
image.isShadowless = true;
let text = panelRoot.addComponent(UITextField);

text.text = 'Happy Duck';
text.fontSize = 4;
text.color = new Color(0, 0, 0, 1);
text.alignment = TextAnchor.MiddleCenter;
GUIUtil.renderUIPanel(this.panel, true);
}

private sceneText: UITextField;
private initScenePOI() {
let canvas = this.scene.view.enableUICanvas();
//panel
let panel = new Object3D().addComponent(WorldPanel);
panel.cullMode = "none";
//add to canvas
canvas.addChild(panel.object3D);
panel.object3D.localScale = new Vector3(0.1, 0.1, 0.1);

//poi
let panelRoot = new Object3D();
panel.transform.rotationX = -30;
panel.transform.y = 3.1;
panel.transform.x = 1;

panel.object3D.addChild(panelRoot);
let text = panelRoot.addComponent(UITextField);
text.uiTransform.resize(80, 16);
text.text = this.title;
text.fontSize = 10;
text.color = new Color(0.5, 1.0, 0.5, 1.0);
text.alignment = TextAnchor.MiddleLeft;

panelRoot.addComponent(UIShadow).shadowOffset.multiplyScaler(0.2);
this.sceneText = text;
}

private charCount = 0;
private title: string = 'Hello, Orillusion';
private lastTitle = this.title;
private loop(): void {
if (this.panel) {
this.position ||= new Vector3();
this.position.copyFrom(this.modelContainer.transform.worldPosition);
this.panel.object3D.localPosition = this.position;
}
if (this.sceneText) {
let count = 1 + Math.floor(Time.frame * 0.1) % 30;
if (this.charCount != count) {
this.charCount = count;
let newTitle = this.title.slice(0, this.charCount);
if (newTitle != this.lastTitle) {
this.sceneText.text = newTitle;
this.lastTitle = newTitle;
}

}

}
}

}

new Sample_POI().run();
2 changes: 1 addition & 1 deletion samples/gui/Sample_UIButton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class Sample_UIButton {
exampleScene.scene.addChild(floor);
floor.y = -40;

// enable ui canvas 0
// enable ui canvas at index 0
let canvas = exampleScene.view.enableUICanvas();

//create UI root
Expand Down
2 changes: 1 addition & 1 deletion samples/gui/Sample_UIChangeParent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class Sample_UIChangeParent {
exampleScene.scene.addChild(floor);
floor.y = -40;

// enable ui canvas 0
// enable ui canvas at index 0
let canvas = exampleScene.view.enableUICanvas();

//create UI root
Expand Down
2 changes: 1 addition & 1 deletion samples/gui/Sample_UIImageColor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class Sample_UIImageColor {
exampleScene.scene.addChild(floor);
floor.y = -40;

// enable ui canvas 0
// enable ui canvas at index 0
let canvas = exampleScene.view.enableUICanvas();

//create UI root
Expand Down
6 changes: 3 additions & 3 deletions samples/gui/Sample_UIImageGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ export class Sample_UIImageGroup {
}

async createImageGroup() {
// enable ui canvas i
let canvas = this.scene.view.enableUICanvas(0);
// enable ui canvas
let canvas = this.scene.view.enableUICanvas();
//create UI root
let panelRoot: Object3D = new Object3D();
//create panel
let panel = panelRoot.addComponent(ViewPanel, { billboard: true });
let panel = panelRoot.addComponent(ViewPanel);
canvas.addChild(panel.object3D);

let bitmapTexture2D = new BitmapTexture2D();
Expand Down
7 changes: 4 additions & 3 deletions samples/gui/Sample_UIImageShadow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createExampleScene } from "@samples/utils/ExampleScene";
import { Engine3D, Object3DUtil, Object3D, BitmapTexture2D, UIImage, makeAloneSprite, WorldPanel, UIShadow } from "@orillusion/core";
import { GUIUtil } from "@samples/utils/GUIUtil";

export class Sample_UIImageShadow {
class Sample_UIImageShadow {
private img: UIImage;

async run() {
Expand All @@ -24,8 +24,7 @@ export class Sample_UIImageShadow {
let panelRoot: Object3D = new Object3D();
panelRoot.scaleX = panelRoot.scaleY = panelRoot.scaleZ = 0.1;

// enable ui canvas 0

// enable ui canvas
let canvas = exampleScene.view.enableUICanvas();

let panel = panelRoot.addComponent(WorldPanel);
Expand All @@ -50,3 +49,5 @@ export class Sample_UIImageShadow {
}

}

new Sample_UIImageShadow().run();
58 changes: 58 additions & 0 deletions samples/gui/Sample_UIMultiCanvas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { createExampleScene } from "@samples/utils/ExampleScene";
import { Engine3D, Object3DUtil, Object3D, UIImage, ImageType, Color, UIPanel, ViewPanel, Scene3D, Vector2, UITextField, UIShadow } from "@orillusion/core";

class Sample_UIMultiCanvas {
async run() {
Engine3D.setting.shadow.autoUpdate = true;
Engine3D.setting.shadow.shadowBias = 0.002;

await Engine3D.init();
await Engine3D.res.loadFont('fnt/0.fnt');

let exampleScene = createExampleScene();
Engine3D.startRenderView(exampleScene.view);

// create floor
let floor = Object3DUtil.GetSingleCube(100, 2, 50, 0.5, 0.5, 0.5);
exampleScene.scene.addChild(floor);
floor.y = -40;

let total: number = 4;
for (let i = 0; i < total; i++) {
let size: Vector2 = new Vector2();
size.x = 500 - i * 100;
size.y = 400 - i * 100;
this.createPanel(exampleScene.scene, i, size);
}

}

private createPanel(scene: Scene3D, index: number, size: Vector2): UIPanel {
let panelRoot: Object3D = new Object3D();
// enable ui canvas at index
let canvas = scene.view.enableUICanvas(index);
let panel = panelRoot.addComponent(ViewPanel);
canvas.addChild(panel.object3D);
// create image
let obj3D = new Object3D();
panelRoot.addChild(obj3D);
let image = obj3D.addComponent(UIImage);
image.isShadowless = true;
image.imageType = ImageType.Sliced;
image.uiTransform.resize(size.x, size.y);
image.color = Color.random();

//text
let text = obj3D.addComponent(UITextField);
text.text = 'Canvas index: ' + index;
text.fontSize = 24;

//shadow
let shadow = obj3D.addComponent(UIShadow);
shadow.shadowOffset.multiplyScaler(0.4);
return panel;
}

}

new Sample_UIMultiCanvas().run();
7 changes: 4 additions & 3 deletions samples/gui/Sample_UIMultiPanel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { GUIHelp } from "@orillusion/debug/GUIHelp";
import { createExampleScene, createSceneParam } from "@samples/utils/ExampleScene";
import { GUIPanelBinder, sampleUIPanelDispatcher, sampleUIPanelClick } from "./panel/GUIBinder";
import { Camera3D, Scene3D, View3D, Engine3D, Object3DUtil, Object3D, Vector3, WorldPanel, Time, zSorterUtil } from "@orillusion/core";
import { Camera3D, Scene3D, View3D, Engine3D, Object3DUtil, Object3D, Vector3, WorldPanel, Time, zSorterUtil, BillboardType } from "@orillusion/core";

export class Sample_UIMultiPanel {
camera: Camera3D;
Expand All @@ -24,7 +24,7 @@ export class Sample_UIMultiPanel {
this.scene = exampleScene.scene;
this.camera = exampleScene.camera;
this.view = exampleScene.view;
// enable ui canvas 0
// enable ui canvas at index 0
let canvas = exampleScene.view.enableUICanvas();

let car = await Engine3D.res.loadGltf('gltfs/pbrCar/pbrCar.gltf');
Expand All @@ -51,7 +51,8 @@ export class Sample_UIMultiPanel {
for (let i = 0; i < 50; i++) {
//panel
let panelRoot: Object3D = new Object3D();
let panel = panelRoot.addComponent(WorldPanel, { billboard: true });
let panel = panelRoot.addComponent(WorldPanel);
panel.billboard = BillboardType.BillboardXYZ;
panel.needSortOnCameraZ = true;
canvas.addChild(panel.object3D);

Expand Down
19 changes: 8 additions & 11 deletions samples/gui/Sample_UIPanelOrder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GUIHelp } from "@orillusion/debug/GUIHelp";
import { createExampleScene } from "@samples/utils/ExampleScene";
import { Engine3D, Object3DUtil, Object3D, UIImage, ImageType, Color, WorldPanel, UIPanel, GUICanvas } from "@orillusion/core";
import { Engine3D, Object3DUtil, Object3D, Color, WorldPanel, UIPanel, GUICanvas, BillboardType } from "@orillusion/core";

export class Sample_UIPanelOrder {

Expand All @@ -20,7 +20,7 @@ export class Sample_UIPanelOrder {
exampleScene.scene.addChild(floor);
floor.y = -40;

// enable ui canvas 0
// enable ui canvas at index 0
let canvas = exampleScene.view.enableUICanvas();
//create UI root
let panelRoot: Object3D = new Object3D();
Expand All @@ -33,7 +33,6 @@ export class Sample_UIPanelOrder {

let panel1 = this.createPanel(panelRoot, canvas, new Color(1.0, 0, 0.0, 0.8));
let panel2 = this.createPanel(panelRoot2, canvas, new Color(0, 0, 1, 0.8));

panel1.needSortOnCameraZ = true;
panel2.needSortOnCameraZ = true;

Expand All @@ -48,15 +47,13 @@ export class Sample_UIPanelOrder {
}

private createPanel(panelRoot: Object3D, canvas: GUICanvas, color: Color): UIPanel {
let panel = panelRoot.addComponent(WorldPanel, { billboard: true });
let panel = panelRoot.addComponent(WorldPanel);
panel.billboard = BillboardType.BillboardXYZ;
canvas.addChild(panel.object3D);
// create image
let obj3D = new Object3D();
panelRoot.addChild(obj3D);
let image = obj3D.addComponent(UIImage);
image.imageType = ImageType.Sliced;
image.uiTransform.resize(400, 300);
image.color = color;
panel.uiTransform.resize(400, 300);
panel.visible = true;
panel.color = color;

return panel;
}

Expand Down
6 changes: 3 additions & 3 deletions samples/gui/Sample_UIPerformance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ export class Sample_UISpriteSheet {
}

addLotOfSprite() {
// enable ui canvas i
// enable ui canvas at index 0
let canvas = this.scene.view.enableUICanvas(0);
//create UI root
let panelRoot: Object3D = new Object3D();
//create panel
let panel = panelRoot.addComponent(ViewPanel, { billboard: true });
let panel = panelRoot.addComponent(ViewPanel);
canvas.addChild(panel.object3D);
//create sprite sheet list
this.createSpriteSheets(panelRoot);
Expand All @@ -113,7 +113,7 @@ export class Sample_UISpriteSheet {
//create UI root
let panelRoot: Object3D = new Object3D();
//create panel
let panel = panelRoot.addComponent(ViewPanel, { billboard: true });
let panel = panelRoot.addComponent(ViewPanel);
panel.panelOrder = 10000;
canvas.addChild(panel.object3D);
let textQuad = new Object3D();
Expand Down
2 changes: 1 addition & 1 deletion samples/gui/Sample_UIPerformance2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export class Sample_UIPerformance2 {
}

addLotOfSprite() {
// enable ui canvas i
// enable ui canvas at index 0
let canvas = this.scene.view.enableUICanvas(0);
//create UI root
let panelRoot: Object3D = new Object3D();
Expand Down
Loading

0 comments on commit 661a7f9

Please sign in to comment.