首页 > 解决方案 > Update normals of animated skinned mesh in three.js?

问题描述

I have a simple animated model, animated throught skinning, and I am giving it a ShaderMaterial which displays its normals:

gl_FragColor = vec4(vNormal, 1.0);

https://codepen.io/marco_fugaro/pen/ZEEBjKz?editors=0010

The problem is the normals aren't updated with the skinning of the model, and they always stay the same from the object's non animated position (see VertexNormalsHelper).

How do I get the normals to update, or how do i get the animated vertices' normals?

model.geometry.computeVertexNormals() doesn't work

model

标签: animationthree.jsshadernormals

解决方案


我最终猴子修补了MeshNormalMaterial顶点着色器,并删除了法线相对于相机的逻辑,这是最终代码:

import * as THREE from 'three'
import normalVert from 'three/src/renderers/shaders/ShaderLib/normal_vert.glsl'

// like MeshNormalMaterial, but the normals are relative to world not camera
const normalMaterialGlobalvert = normalVert.replace(
  '#include <defaultnormal_vertex>',
  THREE.ShaderChunk['defaultnormal_vertex'].replace(
    'transformedNormal = normalMatrix * transformedNormal;',
    // take into consideration only the model matrix
    'transformedNormal =  mat3(modelMatrix) * transformedNormal;'
  )
)

然后像这样使用它:

mesh.material = new THREE.ShaderMaterial({
  vertexShader: normalMaterialGlobalvert,
  fragmentShader: `
    varying vec3 vNormal;

    void main() {
      gl_FragColor = vec4(vNormal, 1.0);
    }
  `,
})

感谢gman为我指明了正确的方向!


推荐阅读