[LayaAirIDE3]Laya3.2版本想自己用命令行生成astc压缩纹理,可以告知官方的方式吗

rt
Laya3.2版本想自己用命令行生成astc压缩纹理,可以告知官方的方式吗
已邀请:

狂蜂浪蝶

赞同来自:

# astcenc方面
和2.x不一样,需要把-cl改成-cs
 
# png源文件方面
需要先预乘alpha之后再进行astc转换,具体可以用node的sharp库
具体代码如下
 const sharpObj = sharp(inputFile); 
const sharpRaw = sharpObj.raw();
const rawBuffer = await sharpRaw.toBuffer();
const __width__ = sharpMetadata.width, __height__ = sharpMetadata.height;
let __bufferArray__ = new Float32Array(__width__ * __height__ * 4);
for (let __loop_i__ = 0; __loop_i__ < __height__; __loop_i__++) {
for (let r = 0; r < __width__; r++) {
let i = 4 * (__loop_i__ * __width__ + r)
, s = rawBuffer[i]
, o = rawBuffer[i + 1]
, l = rawBuffer[i + 2]
, c = rawBuffer[i + 3]
, u = c / 255;
__bufferArray__[i] = s * u,
__bufferArray__[i + 1] = o * u,
__bufferArray__[i + 2] = l * u,
__bufferArray__[i + 3] = c
}
}
let newSharpObj = sharp(Buffer.from(__bufferArray__), {
raw: {
width: __width__,
height: __height__,
channels: 4
}
});
await newSharpObj.toFile(tmp)

1567600631用户

赞同来自:

const sharp = require('sharp');
const fs = require('fs').promises;


/// 预乘Alpha操作 png才能使用 所以在使用前先判断通道是否是四个
async function manualPremultiplyAlpha(inputPath, outputPath) {
  try {
    // 读取图像为Raw格式
    const { data, info } = await sharp(inputPath).raw().toBuffer({ resolveWithObject: true }); 
    // 基于图像的宽度、高度和通道数进行遍历
    const width = info.width;
    const height = info.height;
    const channels = info.channels;
    if (channels !== 4) {
      throw new Error('Input image must have 4 channels (RGBA).');
    }
    for (let y = 0; y < height; y++) {
      for (let x = 0; x < width; x++) {
        // Calculate the offset of the current pixel in the data array.
        const pixelOffset = (y * width + x) * channels;
       
        // 获取Alpha值并进行归一化(假设8位精度)
        const alpha = data[pixelOffset + 3] / 255; 
        // 预乘Alpha操作
        for (let c = 0; c < 3; c++) {
          data[pixelOffset + c] = Math.round(data[pixelOffset + c] * alpha);
        }
      }
    } 
   // 使用sharp处理预乘Alpha后的Buffer并输出为PNG
   await sharp(Buffer.from(data), { raw: { width, height, channels } })
   .png()
   .toFile(outputPath); 
    console.log('Image processed and saved with manual pre-multiplied alpha.');
  } catch (error) {
    console.error('Error processing image:', error);
  }

// 使用函数处理图像
const inputFilePath = 'k.png';
const outputFilePath = 'output.png';
manualPremultiplyAlpha(inputFilePath, outputFilePath);

要回复问题请先

商务合作
商务合作