drawSprite method

void drawSprite(
  1. Sprite sprite,
  2. Transform transform
)

Implementation

void drawSprite(Sprite sprite, Transform transform) {
  if(!sprite.sheetTexture.loaded) return;

  transform.updateTransform();

  Vector3 distanceToCam = (transform.pos - camera.getTransform().pos)..absolute();
  if(distanceToCam.length > camera.drawDistance) return;

  gl.useProgram(testShader.program);
  gl.bindTexture(WebGL.WebGL.TEXTURE_2D, sprite.sheetTexture.texture);

  Matrix4 worldMatrix = transform.transformMatrix.clone();

  Matrix4 textureOffset = Matrix4.identity();
  if(sprite.textureSize.x != sprite.textureSize.y) {
    if(sprite.textureSize.y > sprite.textureSize.x) {
      worldMatrix.scale(1.0, 1.0 + (scale), 1.0);
    } else  if(sprite.textureSize.y < sprite.textureSize.x) {
      worldMatrix.scale(1.0, 1.0 - (scale), 1.0);
    }
  }

  textureOffset.scale(1.0 / sprite.sheetTexture.width,1.0 / sprite.sheetTexture.height, 0.0);
  textureOffset.translate(sprite.texturePos.x + 0.25, sprite.texturePos.y + 0.25, 0.0);
  textureOffset.scale((sprite.textureSize.x - 0.5), (sprite.textureSize.y - 0.5), 0.0);

  if(renderingMode == RenderingMode.SIX_BIT_COLOR) {
    if(sprite.colors != null) {
      int col = Util.getColor(sprite.colors!.x.toInt(), sprite.colors!.y.toInt(), sprite.colors!.z.toInt(), sprite.colors!.w.toInt());
      gl.uniform3fv(colorLocation1, Util.returnColor((col) & 255).storage);
      gl.uniform3fv(colorLocation2, Util.returnColor((col >> 8) & 255).storage);
      gl.uniform3fv(colorLocation3, Util.returnColor((col >> 16) & 255).storage);
      gl.uniform3fv(colorLocation4, Util.returnColor((col >> 24) & 255).storage);
    } else {
      throw "Sprite colors not set! ";
    }
  } else {
    gl.uniform3fv(colorLocation1, Vector3.all(-1).storage);
    gl.uniform3fv(colorLocation2, Vector3.all(-1).storage);
    gl.uniform3fv(colorLocation3, Vector3.all(-1).storage);
    gl.uniform3fv(colorLocation4, Vector3.all(-1).storage);
  }

  gl.uniformMatrix4fv(worldTransformLocation, false, worldMatrix.storage);
  gl.uniformMatrix4fv(viewTransformLocation, false, camera.viewMatrix.storage);
  gl.uniformMatrix4fv(projTransformLocation, false, camera.projMatrix.storage);
  gl.uniformMatrix4fv(texTransformLocation, false, textureOffset.storage);

  gl.drawElements(WebGL.WebGL.TRIANGLES, 6, WebGL.WebGL.UNSIGNED_SHORT, 0);
}