For rendering many sprites with WebGL, I thought I will ask about performance.
Let's consider this:
for(let i = 0; i < 50000; i++){
let t = new Sprite();
t.scale(Math.random())
t.rotate(Math.random())
t.transform(Math.random(),Math.random())
With the following functions for scale
, rotate
and transform
translate(x, y) {
for (let i = 0; i < this.vertexData.length; i += 3) {
this.vertexData[i] += x;
this.vertexData[i + 1] += y;
}
}
rotate(alpha) {
this.translate(-this.position[0], -this.position[1]);
for (let i = 0; i < this.vertexData.length; i += 3) {
let new_x = this.vertexData[i] * Math.cos(alpha) - this.vertexData[i + 1] * Math.sin(alpha);
let new_y = this.vertexData[i + 1] * Math.cos(alpha) + this.vertexData[i] * Math.sin(alpha);
this.vertexData[i] = new_x;
this.vertexData[i + 1] = new_y;
}
this.translate(this.position[0], this.position[1]);
}
scale(factor) {
this.translate(-this.position[0], -this.position[1]);
for (let i = 0; i < this.vertexData.length; i += 3) {
this.vertexData[i] *= factor;
this.vertexData[i + 1] *= factor;
}
this.translate(this.position[0], this.position[1])
}
And this.vertexData=[-1, -1, 0, 1, -1, 0, -1, 1, 0, -1, 1, 0, 1, -1, 0, 1, 1, 0];
What are my possibilities to make the functions scale
, rotate
and transform
faster? Either depending on language or mathematically.