温馨提示:本文翻译自stackoverflow.com,查看原文请点击:three.js - Rotate instance in vertex shader
three.js glsl vertex-shader

three.js - 在顶点着色器中旋转实例

发布于 2020-03-28 23:34:27

我正在学习实例缓冲区的几何形状,并尝试扩展LambertMaterial着色器以旋转每个实例。

  #define LAMBERT
  mat4 rotationX( in float angle ) {
    return mat4(    1.0,        0,          0,          0,
            0,  cos(angle), -sin(angle),        0,
            0,  sin(angle),  cos(angle),        0,
            0,          0,            0,        1);
  }

  mat4 rotationY( in float angle ) {
    return mat4(    cos(angle),     0,      sin(angle), 0,
                0,      1.0,             0, 0,
            -sin(angle),    0,      cos(angle), 0,
                0,      0,              0,  1);
  }

  mat4 rotationZ( in float angle ) {
    return mat4(    cos(angle),     -sin(angle),    0,  0,
            sin(angle),     cos(angle),     0,  0,
                0,              0,      1,  0,
                0,              0,      0,  1);
  }

        // instanced
        attribute vec3 instanceOffset;
        attribute vec3 instanceColor;
        attribute vec3 instanceRotation;
        varying vec3 vLightFront;
        varying vec3 vIndirectFront;
        #ifdef DOUBLE_SIDED
            varying vec3 vLightBack;
            varying vec3 vIndirectBack;
        #endif
        #include <common>
        #include <uv_pars_vertex>
        #include <uv2_pars_vertex>
        #include <envmap_pars_vertex>
        #include <bsdfs>
        #include <lights_pars_begin>
        #include <color_pars_vertex>
        #include <fog_pars_vertex>
        #include <morphtarget_pars_vertex>
        #include <skinning_pars_vertex>
        #include <shadowmap_pars_vertex>
        #include <logdepthbuf_pars_vertex>
        #include <clipping_planes_pars_vertex>
        void main() {
            #include <uv_vertex>
            #include <uv2_vertex>
            #include <color_vertex>
            // vertex colors instanced
            #include <beginnormal_vertex>
            #include <morphnormal_vertex>
            #include <skinbase_vertex>
            #include <skinnormal_vertex>
            #include <defaultnormal_vertex>
            #include <begin_vertex>
            // position instanced
            transformed += instanceOffset;
            #include <morphtarget_vertex>
            #include <skinning_vertex>
    #include <project_vertex>
    mvPosition =  rotationX(250.0) * rotationY(90.0) * rotationZ(25.0) * vec4(position, 1.0);
            #include <logdepthbuf_vertex>
            #include <clipping_planes_vertex>
            #include <worldpos_vertex>
            #include <envmap_vertex>
            #include <lights_lambert_vertex>
            #include <shadowmap_vertex>
    #include <fog_vertex>
        }
        `

在浏览完google之后,我使用了类似的方法,但它根本不会旋转它们。无论我输入多少数字,它们都位于相同的位置。

不知道这是否是正确的转换。如何正确扩展着色器以增加旋转度?

查看更多

查看更多

提问者
mjanisz1
被浏览
301
Mugen87 2020-01-31 17:50

使用THREE.InstancedMesh时,可以对所有内置材质使用实例渲染,并分别转换每个实例。查看下面的示例演示此方法:

https://threejs.org/examples/webgl_instancing_dynamic

这个想法是使用InstancedMesh.setMatrixAt()以便为动画循环中的每个实例设置本地转换。

如果InstancedBufferGeometry由于某些原因仍要使用,可以将以下示例用作代码模板:

https://threejs.org/examples/webgl_buffergeometry_instancing_lambert

它显示了如何MeshLambertMaterial使用实例渲染进行增强

three.js R113