【发布时间】:2017-11-27 13:39:39
【问题描述】:
我在 THREE.js 中有数千个点的动画,它们会及时改变它们的位置。我想使用着色器使动画更快。由于我对着色器完全陌生,我想从只为一个点设置动画开始。在每个时间步骤中,我想分配保存在向量数组(positions)中的点预定义位置。所以如果time = 1,点的位置就等于positions [1]
我尝试将 positions 保存为统一并将其传递给顶点着色器,但它不起作用。
我应该做些什么不同的事情?这是正确的做法吗?
点声明
//example array with only three vectors but I would like to use arrays which thousands of vectors
var positions = [
new THREE.Vector3(1.6, 2.5, 4.0),
new THREE.Vector3(1.8, 2.5, 3.8),
new THREE.Vector3(1.2, 2.0, 3.0)
]
var geometry = new THREE.BufferGeometry();
geometry.addAttribute("position", new THREE.Float32BufferAttribute([1.6, 2.5, 4.0],3))
var material = new THREE.ShaderMaterial({
vertexShader: document.getElementById( 'vertexShader' ).textContent,
fragmentShader: document.getElementById( 'fragmentShader' ).textContent,
uniforms: {
time: { type: 'f', value: 0 },
positionArray: {type: "v3v", value: positions}
}
})
var point = new THREE.Points(geometry, material);
scene.add(point)
动画
function animate() {
requestAnimationFrame(animate);
time = time + 1
material.uniforms.time.value = time;
controls.update();
renderer.render(scene, camera);
stats.update();
}
顶点着色器
uniform vec3 positionArray[3];
uniform float time;
void main () {
vec3 newPosition;
newPosition= positionArray[time];
gl_PointSize = 10.0;
gl_Position = projectionMatrix * modelViewMatrix * vec4(newPosition, 1.0);
}
【问题讨论】:
-
我在我的代码中发现了问题,在制服定义中我应该将时间设置为 int 而不是 float
time: { type: 'i', value: 0 }无论如何这种方法并不能解决动画缓慢的问题...... -
你能创建一个工作代码 sn-p 或 jsfiddle 吗?
标签: three.js glsl webgl vertex-shader