Three.js: How to create new 'morphing' geometry if I have all necessary buffers?

后端 未结 1 1632
时光说笑
时光说笑 2021-01-14 13:10

I\'m using a web-worker to load a .json file of an animated 3D model. For each of the big arrays (vertices, normals, etc.), I\'m transferring an Float32Ar

1条回答
  •  再見小時候
    2021-01-14 13:52

    Here's an example based on the mesh loading portion of THREE.GLTF2Loader.

    // Create BufferGeometry and assign vertices and normals.
    var geometry = new THREE.BufferGeometry();
    geometry.addAttribute( 'position', new THREE.BufferAttribute( vertices, 3 ) );
    geometry.addAttribute( 'normal', new THREE.BufferAttribute( normals, 3 ) );
    geometry.setIndex( new THREE.BufferAttribute( faces, 3 ) );
    
    // Create material.
    var material = new THREE.MeshStandardMaterial({
      morphTargets: true,
      morphNormals: true
    });
    
    // Set up morph target attributes.
    var posAttr = new THREE.BufferAttribute( morphVertices, 3 );
    var normAttr = new THREE.BufferAttribute( morphNormals, 3 );
    for (var i = 0; i < posAttr.array.length; i++) {
      posAttr.array[i] += geometry.attributes.position.array[i];
    }
    for (var j = 0; j < normAttr.array.length; j++) {
      normAttr.array[j] += geometry.attributes.normal.array[j];
    }
    
    // Assign morph target attributes.
    geometry.morphAttributes.position = [ posAttr ];
    geometry.morphAttributes.normal = [ normAttr ];
    
    // Create Mesh.
    var mesh = new THREE.Mesh(geometry, material);
    mesh.updateMorphTargets();
    
    // Apply 50/50 blend of morph targets and default position/normals.
    mesh.morphTargetInfluences[0] = 0.5;
    

    three.js r86-dev.

    0 讨论(0)
提交回复
热议问题