Three.js 更新三点几何图形的最佳方法
我有一个搅拌机模型,上面有一些形状键。我导出这个模型并在threejs中使用它。假设这是一个立方体。然后我创建三个点,在这个模型(即立方体)的顶点上绘制小球体 现在使用变形目标,我为立方体设置动画。到目前为止还不错Three.js 更新三点几何图形的最佳方法,three.js,blender,gltf,Three.js,Blender,Gltf,我有一个搅拌机模型,上面有一些形状键。我导出这个模型并在threejs中使用它。假设这是一个立方体。然后我创建三个点,在这个模型(即立方体)的顶点上绘制小球体 现在使用变形目标,我为立方体设置动画。到目前为止还不错 //Some code that uses the shapee keys cube.morphTargetInfluences[0] = 0.2; // <-- I animate this value //一些使用shapee键的代码 cube.morphTargetF
//Some code that uses the shapee keys
cube.morphTargetInfluences[0] = 0.2; // <-- I animate this value
//一些使用shapee键的代码
cube.morphTargetFluence[0]=0.2;// 回答我自己的问题,以防这对将来的其他人有帮助
我能够找到两种解决方案:
基于CPU(慢速)-使用函数将变形复制到位置
function updatePointsGeometry(points,weight) {
if (!points.geometry.attributes.morphTarget0) {
return;
}
for (var i = 0; i < points.geometry.attributes.position.count; i++) {
points.geometry.attributes.position.array[i+0] = weight * points.geometry.attributes.morphTarget0.array[i+0];
points.geometry.attributes.position.array[i+1] = weight * points.geometry.attributes.morphTarget0.array[i+1];
points.geometry.attributes.position.array[i+2] = weight * points.geometry.attributes.morphTarget0.array[i+2];
}
points.geometry.attributes.position.needsUpdate = true;
}
这是顶点着色器
uniform float size;
uniform float scale;
uniform float morphTargetInfluences[ 4 ];
#include <common>
#include <color_pars_vertex>
#include <fog_pars_vertex>
#include <shadowmap_pars_vertex>
#include <logdepthbuf_pars_vertex>
#include <clipping_planes_pars_vertex>
void main() {
#include <color_vertex>
#include <begin_vertex>
#include <project_vertex>
#ifdef USE_SIZEATTENUATION
gl_PointSize = size * ( scale / - mvPosition.z );
#else
gl_PointSize = size;
#endif
vec3 morphed = vec3( 0.0 , 0.0 , 0.0 );
morphed += ( morphTarget0 - position ) * morphTargetInfluences[0];
morphed += position;
gl_Position = projectionMatrix * modelViewMatrix * vec4( morphed, 1.0 );
#include <logdepthbuf_vertex>
#include <clipping_planes_vertex>
#include <worldpos_vertex>
#include <shadowmap_vertex>
#include <fog_vertex>
}
统一浮点数;
均匀浮标;
均匀浮点数注量[4];
#包括
#包括
#包括
#包括
#包括
#包括
void main(){
#包括
#包括
#包括
#ifdef使用尺寸评估
gl_PointSize=尺寸*(比例/-mvPosition.z);
#否则
gl_PointSize=大小;
#恩迪夫
vec3变形=vec3(0.0,0.0,0.0);
变形+=(变形目标0-位置)*变形目标通量[0];
变形+=位置;
gl_位置=投影矩阵*模型视图矩阵*向量4(变形,1.0);
#包括
#包括
#包括
#包括
#包括
}
点材质
现在变形目标。@WestLangley这只对点有效?我有一个基本网格和表面上的另一个小网格。如何移动较小的网格,使其在变形基础网格时保留在基础网格的曲面上?
function updatePointsGeometry(points,weight) {
if (!points.geometry.attributes.morphTarget0) {
return;
}
for (var i = 0; i < points.geometry.attributes.position.count; i++) {
points.geometry.attributes.position.array[i+0] = weight * points.geometry.attributes.morphTarget0.array[i+0];
points.geometry.attributes.position.array[i+1] = weight * points.geometry.attributes.morphTarget0.array[i+1];
points.geometry.attributes.position.array[i+2] = weight * points.geometry.attributes.morphTarget0.array[i+2];
}
points.geometry.attributes.position.needsUpdate = true;
}
var shaderPoint = THREE.ShaderLib.points;
var uniforms = THREE.UniformsUtils.clone(shaderPoint.uniforms);
uniforms.map.value = imageTexture;
uniforms.size.value = 10;
//Custom Shader
customPointMaterial = new THREE.ShaderMaterial({
uniforms: uniforms,
morphTargets: true,
defines: {
USE_MAP: ""
},
transparent: false,
opacity: 1,
alphaTest: 0.5,
fog: false,
vertexShader: document.getElementById( 'vertexShader' ).textContent,
fragmentShader: shaderPoint.fragmentShader
});
//copy over the morph data from original geometry to the points
myPoints = new THREE.Points(mesh.geometry, customPointMaterial);
myPoints.morphTargetInfluences = mesh.morphTargetInfluences;
myPoints.morphTargetDictionary = mesh.morphTargetDictionary;
uniform float size;
uniform float scale;
uniform float morphTargetInfluences[ 4 ];
#include <common>
#include <color_pars_vertex>
#include <fog_pars_vertex>
#include <shadowmap_pars_vertex>
#include <logdepthbuf_pars_vertex>
#include <clipping_planes_pars_vertex>
void main() {
#include <color_vertex>
#include <begin_vertex>
#include <project_vertex>
#ifdef USE_SIZEATTENUATION
gl_PointSize = size * ( scale / - mvPosition.z );
#else
gl_PointSize = size;
#endif
vec3 morphed = vec3( 0.0 , 0.0 , 0.0 );
morphed += ( morphTarget0 - position ) * morphTargetInfluences[0];
morphed += position;
gl_Position = projectionMatrix * modelViewMatrix * vec4( morphed, 1.0 );
#include <logdepthbuf_vertex>
#include <clipping_planes_vertex>
#include <worldpos_vertex>
#include <shadowmap_vertex>
#include <fog_vertex>
}