setAssetTexture static method
Load an asset image, flip vertically and
add it to the shader uniform with the name uniformName
toReplace
if true replace the texture with another
one with different size.
Implementation
static Future<bool> setAssetTexture(String uniformName, String assetName,
{AddMethod method = AddMethod.replace}) async {
final Uint8List inputImg =
(await rootBundle.load(assetName)).buffer.asUint8List();
final decoder = img.PngDecoder();
img.Image? decodedImg = decoder.decode(inputImg);
img.Image rgba = decodedImg!.convert(
format: Format.uint8,
numChannels: 4,
withPalette: false,
);
// texture in OpenGL have the origin at top left, so flip it vertically
rgba = img.flipVertical(rgba);
final decodedBytes = rgba.getBytes(order: img.ChannelOrder.rgba);
bool ret = false;
switch (method) {
case AddMethod.add:
ret = OpenGLController().openglFFI.addSampler2DUniform(
uniformName, rgba.width, rgba.height, decodedBytes);
break;
case AddMethod.replace:
ret = OpenGLController().openglFFI.replaceSampler2DUniform(
uniformName, rgba.width, rgba.height, decodedBytes);
break;
case AddMethod.set:
ret = OpenGLController()
.openglFFI
.setSampler2DUniform(uniformName, decodedBytes);
break;
}
return ret;
}