Skip to content

Commit

Permalink
Merge pull request #28 from galacean/feat/addPixi
Browse files Browse the repository at this point in the history
Add sprte and text benchmark for pixi.js
  • Loading branch information
gz65555 authored Jul 11, 2024
2 parents 5639782 + 47bda61 commit 41ed02a
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 289 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@types/three": "^0.164.0",
"babylonjs": "^7.6.0",
"dat.gui": "^0.7.9",
"pixi.js": "^8.1.8",
"stats.js": "^0.17.0",
"three": "^0.164.1",
"three-nebula": "^10.0.3",
Expand Down
72 changes: 0 additions & 72 deletions src/babylon/sprite-benchmark.ts

This file was deleted.

63 changes: 0 additions & 63 deletions src/babylon/text-benchmark.ts

This file was deleted.

23 changes: 13 additions & 10 deletions src/galacean/sprite-benchmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import {
Camera,
Script,
Sprite,
SpriteAtlas,
SpriteRenderer,
Texture2D,
WebGLEngine,
} from "@galacean/engine";

class Rotate extends Script {
speed = 30;
speed = 60;
onUpdate(dt: number): void {
this.entity.transform.rotation.z += this.speed * dt;
}
Expand All @@ -35,16 +36,17 @@ WebGLEngine.create({
camera.orthographicSize = (engine.canvas.height * 0.5) / 100;

engine.resourceManager
.load<Texture2D>({
url: "https://mdn.alipayobjects.com/huamei_w6ifet/afts/img/A*YwyMRq8_O-4AAAAAAAAAAAAADjCHAQ/original",
type: AssetType.Texture2D,
.load<SpriteAtlas>({
url: "https://mdn.alipayobjects.com/oasis_be/afts/file/A*s_FUSawb0sgAAAAAAAAAAAAADkp5AQ/SpriteAtlas.json",
type: AssetType.SpriteAtlas,
})
.then((texture: Texture2D) => {
const sprite = new Sprite(engine, texture);
.then((atlas: SpriteAtlas) => {
const sprite1 = atlas.getSprite("/tex1-spr.png");
const sprite2 = atlas.getSprite("/tex2-spr.png");

const col = 22;
const col = 88;
const row = 90;
const offsetX = 0.38;
const offsetX = 0.1;
const offsetY = 0.2;
// 中心点所在行列
const centerX = col * 0.5;
Expand All @@ -55,10 +57,11 @@ WebGLEngine.create({
const entity = rootEntity.createChild(`sprite-${i}`);
const sr = entity.addComponent(SpriteRenderer);
entity.addComponent(Rotate);
sr.sprite = sprite;
sr.sprite = i % 2 === 0 ? sprite1 : sprite2;
sr && (sr.priority = index++);
const transform = entity.transform;
transform.setScale(0.2, 0.2, 0.2);
const scale = 0.075;
transform.setScale(scale, scale, scale);
transform.setPosition(
(j - centerX) * offsetX,
(i - centerY) * offsetY,
Expand Down
63 changes: 63 additions & 0 deletions src/pixi/sprite-benchmark.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* @title Sprite
* @category Benchmark
*/

import * as PIXI from "pixi.js";

const canvas = document.getElementById("canvas") as HTMLCanvasElement;
const app = new PIXI.Application();
app
.init({
canvas: canvas,
resizeTo: window,
width: canvas.width,
height: canvas.height,
})
.then(() => {
PIXI.Assets.load([
{
src: "https://mdn.alipayobjects.com/huamei_w6ifet/afts/img/A*YwyMRq8_O-4AAAAAAAAAAAAADjCHAQ/original",
loadParser: "loadTextures",
},
{
src: "https://mdn.alipayobjects.com/huamei_w6ifet/afts/img/A*ClJLS6U4HtsAAAAAAAAAAAAADjCHAQ/original",
loadParser: "loadTextures",
},
]).then((textureRecord) => {
const col = 88;
const row = 90;
const offsetX = 5;
const offsetY = 10;
// 中心点所在行列
const centerX = col * 0.5;
const centerY = row * 0.5;
// 屏幕宽高
const screen = app.screen;
const halfWidth = screen.width * 0.5;
const halfHeight = screen.height * 0.5;
const sprites = [];
const texture1 =
textureRecord[
"https://mdn.alipayobjects.com/huamei_w6ifet/afts/img/A*YwyMRq8_O-4AAAAAAAAAAAAADjCHAQ/original"
];
const texture2 = textureRecord['https://mdn.alipayobjects.com/huamei_w6ifet/afts/img/A*ClJLS6U4HtsAAAAAAAAAAAAADjCHAQ/original'];
for (let i = 0; i < row; ++i) {
for (let j = 0; j < col; ++j) {
const sprite = new PIXI.Sprite(i % 2 === 0 ? texture1 : texture2);
app.stage.addChild(sprite);
sprite.scale = 0.045;
sprite.anchor.set(0.5, 0.5);
sprite.x = halfWidth + (j - centerX) * offsetX + 5;
sprite.y = halfHeight + (i - centerY) * offsetY + 10;
sprites.push(sprite);
}
}

app.ticker.add((time) => {
for (let i = 0, n = sprites.length; i < n; ++i) {
sprites[i].rotation += 0.01 * time.deltaTime;
}
});
});
});
55 changes: 55 additions & 0 deletions src/pixi/text-benchmark.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* @title Text
* @category Benchmark
*/

import * as PIXI from "pixi.js";

const canvas = document.getElementById("canvas") as HTMLCanvasElement;
const app = new PIXI.Application();
app
.init({
canvas: canvas,
resizeTo: window,
width: canvas.width,
height: canvas.height,
})
.then(() => {
const col = 22;
const row = 90;
const offsetX = 20;
const offsetY = 10;
// 中心点所在行列
const centerX = col * 0.5;
const centerY = row * 0.5;
// 屏幕宽高
const screen = app.screen;
const halfWidth = screen.width * 0.5;
const halfHeight = screen.height * 0.5;
const texts = [];
const style = new PIXI.TextStyle({
fontFamily: "Arial",
fontSize: 20,
fill: 0xffffff,
});
for (let i = 0; i < row; ++i) {
for (let j = 0; j < col; ++j) {
const text = new PIXI.Text({
text: "hello",
style,
});
app.stage.addChild(text);
text.anchor.set(0.5, 0.5);
text.scale = 0.8;
text.x = halfWidth + (j - centerX) * offsetX + 10;
text.y = halfHeight + (i - centerY) * offsetY + 10;
texts.push(text);
}
}

app.ticker.add((time) => {
for (let i = 0, n = texts.length; i < n; ++i) {
texts[i].rotation += 0.01 * time.deltaTime;
}
});
});
73 changes: 0 additions & 73 deletions src/three/sprite-benchmark.ts

This file was deleted.

Loading

0 comments on commit 41ed02a

Please sign in to comment.