Skip to content

Commit fd73b3c

Browse files
committed
feat: add type sprite on babylonjs engine
1 parent 9cc394e commit fd73b3c

File tree

1 file changed

+59
-39
lines changed

1 file changed

+59
-39
lines changed

src/scripts/babylon.js

Lines changed: 59 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ class BabylonEngine extends Engine {
5454
// Optimizer
5555
const optimizer = new BABYLON.SceneOptimizer(this.scene, optimizerOptions);
5656

57+
// Spritemanager
58+
const spriteManager = new BABYLON.SpriteManager(
59+
'textures',
60+
'/sprite.png',
61+
1000000,
62+
{ width: 64, height: 64 },
63+
this.scene
64+
);
65+
5766
// Particle creation
5867
const particles = new Array(this.count);
5968
const rnd = [1, -1];
@@ -67,49 +76,60 @@ class BabylonEngine extends Engine {
6776
3 * Math.random() * rnd[Math.floor(Math.random() * 2)],
6877
];
6978

70-
// Create circles by drawing lines in a 360 degree radius
71-
let points = [];
72-
if (circles[size]) {
73-
points = circles[size];
79+
let circle;
80+
let filled;
81+
if (this.type === 'sprite') {
82+
circle = new BABYLON.Sprite('sprite', spriteManager);
83+
circle.width = 64;
84+
circle.height = 64;
85+
circle.angle = Math.PI;
86+
circle.position.x = -x;
87+
circle.position.z = -y;
88+
circle.position.y = -i;
7489
} else {
75-
const radius = size;
76-
for (let i = -Math.PI; i <= Math.PI; i += Math.PI / 360) {
77-
points.push(
78-
new BABYLON.Vector3(radius * Math.cos(i), 0, radius * Math.sin(i))
79-
);
90+
// Create circles by drawing lines in a 360 degree radius
91+
let points = [];
92+
if (circles[size]) {
93+
points = circles[size];
94+
} else {
95+
const radius = size;
96+
for (let i = -Math.PI; i <= Math.PI; i += Math.PI / 360) {
97+
points.push(
98+
new BABYLON.Vector3(radius * Math.cos(i), 0, radius * Math.sin(i))
99+
);
100+
}
101+
circles[size] = points;
80102
}
81-
circles[size] = points;
82-
}
83-
const circle = BABYLON.MeshBuilder.CreateLines(
84-
'circle',
85-
{ points: points, updatable: false },
86-
this.scene
87-
);
88-
circle.color = new BABYLON.Color3.White();
89-
circle.position.x = -x;
90-
circle.position.z = -y;
91-
circle.position.y = -i - 1;
92-
let filled;
93-
if (this.type === 'fill') {
94-
const mat = new BABYLON.StandardMaterial('mat1', this.scene);
95-
mat.alpha = 1;
96-
mat.diffuseColor = new BABYLON.Color3(1, 1, 1);
97-
mat.emissiveColor = new BABYLON.Color3.White();
98-
mat.backFaceCulling = false;
99-
filled = BABYLON.MeshBuilder.CreateRibbon(
100-
'filled_circle',
101-
{
102-
pathArray: [points],
103-
closePath: true,
104-
},
103+
circle = BABYLON.MeshBuilder.CreateLines(
104+
'circle',
105+
{ points: points, updatable: false },
105106
this.scene
106107
);
107-
filled.color = BABYLON.Color3.White();
108-
filled.material = mat;
109-
filled.position.x = -x;
110-
filled.position.z = -y;
111-
filled.position.y = -i;
112-
circle.color = new BABYLON.Color3.Black();
108+
circle.color = new BABYLON.Color3.White();
109+
circle.position.x = -x;
110+
circle.position.z = -y;
111+
circle.position.y = -i - 1;
112+
if (this.type === 'fill') {
113+
const mat = new BABYLON.StandardMaterial('mat1', this.scene);
114+
mat.alpha = 1;
115+
mat.diffuseColor = new BABYLON.Color3(1, 1, 1);
116+
mat.emissiveColor = new BABYLON.Color3.White();
117+
mat.backFaceCulling = false;
118+
filled = BABYLON.MeshBuilder.CreateRibbon(
119+
'filled_circle',
120+
{
121+
pathArray: [points],
122+
closePath: true,
123+
},
124+
this.scene
125+
);
126+
filled.color = BABYLON.Color3.White();
127+
filled.material = mat;
128+
filled.position.x = -x;
129+
filled.position.z = -y;
130+
filled.position.y = -i;
131+
circle.color = new BABYLON.Color3.Black();
132+
}
113133
}
114134

115135
particles[i] = { x, y, size: size, dx, dy, el: [circle, filled] };

0 commit comments

Comments
 (0)