全栈笔记

模型批量压缩

2022-11-15  本文已影响0人  小贤笔记

压缩

npm install -g gltf-pipeline
// .gltf 压缩
for %i in ('dir *.gltf /s /b') do gltf-pipeline -i %i -o %i -d

// .glb 压缩
for %i in ('dir *.glb /s /b') do gltf-pipeline -i %i -o %i -d

// .gltf 转 .glb, 并压缩
for %i in ('dir ./  *.gltf /s /b') do gltf-pipeline -i %i -o %~ni.glb -d

导入

官方示例:

// Instantiate a loader
const loader = new GLTFLoader();

// Optional: Provide a DRACOLoader instance to decode compressed mesh data
const dracoLoader = new DRACOLoader();
dracoLoader.setDecoderPath( '/examples/js/libs/draco/' );
loader.setDRACOLoader( dracoLoader );

// Load a glTF resource
loader.load(
    // resource URL
    'models/gltf/duck/duck.gltf',
    // called when the resource is loaded
    function ( gltf ) {

        scene.add( gltf.scene );

        gltf.animations; // Array<THREE.AnimationClip>
        gltf.scene; // THREE.Group
        gltf.scenes; // Array<THREE.Group>
        gltf.cameras; // Array<THREE.Camera>
        gltf.asset; // Object

    },
    // called while loading is progressing
    function ( xhr ) {

        console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );

    },
    // called when loading has errors
    function ( error ) {

        console.log( 'An error happened' );

    }
);

注: setDecoderPath() 的参数为 "/examples/js/libs/draco/", 但这样写会报错:
"Uncaught SyntaxError: Unexpected token '<'"

可做如下修改:

const dracoLoader = new DRACOLoader()

dracoLoader.setDecoderPath('/draco/')
loader.setDRACOLoader(dracoLoader)
上一篇下一篇

猜你喜欢

热点阅读