我正在学习实例缓冲区的几何形状,并尝试扩展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之后,我使用了类似的方法,但它根本不会旋转它们。无论我输入多少数字,它们都位于相同的位置。
不知道这是否是正确的转换。如何正确扩展着色器以增加旋转度?
使用THREE.InstancedMesh时,可以对所有内置材质使用实例渲染,并分别转换每个实例。查看下面的示例演示此方法:
https://threejs.org/examples/webgl_instancing_dynamic
这个想法是使用InstancedMesh.setMatrixAt()以便为动画循环中的每个实例设置本地转换。
如果InstancedBufferGeometry
由于某些原因仍要使用,可以将以下示例用作代码模板:
https://threejs.org/examples/webgl_buffergeometry_instancing_lambert
它显示了如何MeshLambertMaterial
使用实例渲染进行增强。
three.js R113
我意识到这一点-只是想使其尽可能高效,然后尝试
InstancedBufferGeometry
您为什么认为使用
InstancedBufferGeometry
比效率更高THREE.InstancedMesh
?threejs.org/docs/#api/en/core/BufferGeometry
BufferGeometry An efficient representation of mesh, line, or point geometry.
;)InstancedMesh
内部使用BufferGeometry
。