利用basis_universal工具对3D模型png格式贴图进行无损压缩
工具GitHub地址:https://github.com/BinomialLLC/basis_universal
1.先把代码clone到本地或者直接下载zip,放在你想放的路径下,我直接下载的zip,下完解压:
2.由于是用c++写的,根据官方文档,各个平台需先进行编译:
3.编译需要cmake,如果你的操作系统中没有,需要先安装:
cmake下载地址:https://cmake.org/download/,安装按照提示装就行。装完在windows cmd或mac终端中,或者直接在idea的terminal中验证是否安装成功:
cmake -version
4.确认安装成功后,运行:
cmake CMakeLists.txt
让它跑完,再运行
make
这一步时间会有点长,耐心等待一会会儿,make完成后会在工程目录的bin或者对应系统下生成basisu的可执行文件,比如我的是mac,生成在bin_osx目录下:
5.接下来可以进行测试了,找一个图片,放在某个路径下,输入命令:
basisu x.png
压缩完成后会在命令的当前目录下生成相应的后缀为.basis的压缩文件,可以指定文件目录与输出目录:
./basisu -file /mydoc/test/b.png -output_file /mydoc/baseu/aaa.basis
-file输入路径可以加也可以不加,在多个参数时最好加上,详细参考官网。前端进行展示时,拿到这个文件进行还原成png就行。
需要注意的是,这条命令只能压缩sRGB的png图片,不要传jpg什么的,可以设置压缩级别与压缩后的质量,详细的命令使用请参考官网或者输入命令:./basisu -help,有非常详细的介绍,这里不再多做介绍。
唯一需要提一点的是,各个环境编译后的命令不能通用,比如mac与Linux的就不能通用,这意味着什么呢,意味着在如果要在Linux下使用的话,要把下载的代码copy到Linux下进行编译,生成的可执行文件才可以在Linux下使用。
6.Java调用命令,这里给出我的压缩方法,做个参考:
package com.lianj.crm.common.utils.draco;
import com.lianj.commons.exception.BizException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class DracoEncoderUtils {
private Logger logger = LoggerFactory.getLogger(DracoEncoderUtils.class);
private boolean isDebug = false;
public static void main(String[] args) {
try {
DracoEncoderUtils encoderUtils = new DracoEncoderUtils();
// String shellPath = "/mydoc/workspace/idea/study/draco/draco_build/draco_encoder";
// String filePath = "/mydoc/objs/a.obj";
// encoderUtils.setDebug(true);
// encoderUtils.encodeObj(shellPath, filePath, null, null, null);
String shellPath = "/mydoc/workspace/idea/study/basis_universal-master/bin_osx/basisu";
String filePath = "/mydoc/test/e.png";
encoderUtils.setDebug(true);
encoderUtils.encodePng(shellPath, filePath, null, null, null);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 压缩png材质贴图文件,输出文件
*
* @param shellRootPath 脚本路径
* @param pngFilePath png源文件路径
* @param destination 输出路径
* @param level 压缩级别(高级选项),编码器使用“ -comp_level X”命令行选项支持多个压缩“工作量”级别,其中X的范围为[0,5],默认1。请注意,大多数用户不应该将-comp_level弄乱。
* @param q -q选项是控制.basis文件质量级别的主要选项。-comp_level主要用于难以处理的内容,例如纹理视频。它修改了许多内部编码器配置参数,从而减慢了速度,但允许其每个输出位的质量略高。
* -q X: Set quality level, 1-255, default is 128, lower=better compression/lower quality/faster,
* higher=less compression/higher quality/slower, default is 128. For even higher quality, use -max_endpoints/-max_selectors.
* 请注意,-comp_level 2等效于初始发行版的默认值,-comp_level 4等效于初始发行版的“ -slower”选项。另外,-slower现在等效于2级(而不是4级)。
* @throws Exception
*/
public EncodeResult encodePng(String shellRootPath, String pngFilePath, String destination, Integer level, Integer q) throws Exception {
EncodeResult result = new EncodeResult(2000, "png压缩失败!");
// 参数验证
if (StringUtils.isBlank(shellRootPath) || StringUtils.isBlank(pngFilePath)) {
System.out.println("=========== png压缩命令地址路径或源文件路径参数为空! ========>>>");
logger.error("=========== png压缩命令地址路径或源文件路径参数为空! ========>>>");
result.setMsg("png压缩命令地址路径或源文件路径参数为空!");
return result;
}
String shellPath = null;
if (OSinfo.isLinux()) {
shellPath = shellRootPath + "baseu" + File.separator + "linux" + File.separator + "basisu";
}
if (OSinfo.isMacOSX()) {
shellPath = shellRootPath + "baseu" + File.separator + "mac" + File.separator + "basisu";
}
if (OSinfo.isWindows()) {
shellPath = shellRootPath + "baseu" + File.separator + "windows" + File.separator + "basisu.exe";
}
if (isDebug) {
shellPath = shellRootPath;
}
if (StringUtils.isBlank(shellPath)) {
System.out.println("=========== 未知平台! ========>>>");
logger.error("=========== 未知平台! ==&