使用 Web Worker 时未加载缓冲区几何网格

buffer geometry mesh not loading when using web worker

我可以将我的几何体转移到缓冲区几何体中,发现几何体和 material 的网格都很好,但模型没有出现在 window 中。

这是我的代码:

var myWorker = new Worker("js/respond.js");
myWorker.postMessage("horse.js");

myWorker.onmessage = function(e) {
    geometry = new THREE.BufferGeometry();
    var colorattribute = new THREE.BufferAttribute(e.data.color, 3, false);
    var uv = new THREE.BufferAttribute(e.data.uv, 3, false);//created buffer attribute with worker data
    var normal = new THREE.BufferAttribute(e.data.normal, 3, false);
    var position = new THREE.BufferAttribute(e.data.position, 3, false);
    var morph = new THREE.BufferAttribute(e.data.morphAttributes, 3, false);
    geometry.addAttribute('color', colorattribute);
    geometry.addAttribute('uv', uv);
    geometry.addAttribute('normal', normal);
    geometry.addAttribute('position', position);
    geometry.addGroup(e.data.groups[0].start, e.data.groups[0].count, e.data.groups[0].materialIndex);
    geometry.morphAttributes.position = [];
    geometry.morphAttributes.position.push(morph);
    //console.log(e.data.morphAttributes);

    // mixer = new THREE.AnimationMixer(mesh);
    // clip = THREE.AnimationClip.CreateFromMorphTargetSequence('static', geometry.morphAttributes, 30);
    console.log(geometry);
    mesh = new THREE.Mesh(geometry, new THREE.MeshLambertMaterial({color:  0xffffff, morphTargets: true}));
    console.log(mesh);
    scene.add(mesh);

无法在您的 Web Worker 中使用 THREE.js 等库。

可以 做的是在 worker 中构建几何图形,然后将其输入 THREE.js。从 web worker 中,您可以根据需要 return 一个包含 TypedArraysUInt32ArraysFloat32Arrays 的对象来填充所有属性。

这将导致性能提升,因为数据已经按照 THREE.BufferGeometry 想要的格式构建。