gpt4 book ai didi

javascript - 更新数据纹理统一时,所有点都变为 0,0

转载 作者:行者123 更新时间:2023-12-03 05:18:17 25 4
gpt4 key购买 nike

当用户单击时,我将交换新的位置数据以塑造点云(在高云和宽云之间交替)。我做了这个fiddle ,到目前为止,我可以让云第一次正确渲染(一朵高高的云),但在单击事件中,所有点位置似乎都变成 0,0,而不是变成一朵宽云。我在这里错过了什么?

以下是一些供检查的片段:

function makeCloud() {

var cloudWidth, cloudHeight;

if ( isTallCloud ) {
cloudHeight = 100;
cloudWidth = 25;
} else {
cloudHeight = 25;
cloudWidth = 100;
}

isTallCloud = !isTallCloud;

var positions = new Float32Array( particles * 4 );
var offsets = new Float32Array( particles * 4 );

for ( var i = 0, i4 = 0; i < particles; i ++, i4 +=4 ) {

positions[ i4 + 0 ] = ( Math.random() * 2 - 1 ) * cloudWidth; // x
positions[ i4 + 1 ] = ( Math.random() * 2 - 1 ) * cloudHeight; // y
positions[ i4 + 2 ] = 0.0; // velocity
positions[ i4 + 3 ] = 0.0; // velocity

offsets[ i4 + 0 ] = positions[ i4 + 0 ]; // width offset
offsets[ i4 + 1 ] = positions[ i4 + 1 ]; // height offset
offsets[ i4 + 2 ] = stateChange; // this will hold the change state ( 0.0 or 1.0 )
offsets[ i4 + 3 ] = 0.0; // not used

}

cloudData = {
"positions" : positions,
"offsets" : offsets
}
}

function updateStateChange() {

for ( var i = 0, i4 = 0; i < particles; i ++, i4 +=4 ) {
cloudData.offsets[ i4 + 2 ] = stateChange; // this will hold the change state ( 0.0 or 1.0 )
}
}

function updateOffsets() {
offsetsTexture = new THREE.DataTexture( cloudData.offsets, width, height, THREE.RGBAFormat, THREE.FloatType );
positionUniforms.tOffsets.value = offsetsTexture;
positionUniforms.tOffsets.needsUpdate = true;
}

function onDocumentClick() {
event.preventDefault();
stateChange = 1.0;
makeCloud();
updateOffsets();
}

function animate() {
requestAnimationFrame( animate );
update();
render();
}

function render() {
renderer.render( scene, camera );
}

function update() {
positionUniforms.uTime.value += 0.01;
gpuCompute.compute();

particleUniforms.tPositions.value = gpuCompute.getCurrentRenderTarget( positionVariable ).texture;

if ( stateChange === 1.0 ) {
stateChange = 0.0;
console.log("swapped state!");
updateStateChange();
updateOffsets();
}
}

着色器代码片段:

vec4 offsets = texture2D( tOffsets, uv ).xyzw;
float swapState = offsets.z;
vec4 nowPos;
vec2 velocity;

if ( swapState == 0.0 ) {
nowPos = texture2D( tPositions, uv ).xyzw;
velocity = vec2(nowPos.z, nowPos.w);
} else { // if swapState == 1.0
nowPos = vec4( offsets.x, offsets.y, 0.0, 0.0 );
velocity = vec2(0.0, 0.0);
}

背景:

我意识到只需单击一下即可交换所有数据并获得我正在寻找的结果就足够容易了;但是,我尝试通过在纹理中传递这些偏移值来进行更新的原因是,在我的较大程序中,有更多的点云,而我只想更改选定的云(如着色器中的 swapState 所示) ),同时让物理着色器继续控制剩余未选中的。更新所有云会扰乱着色器控制的物理的连续性。

最佳答案

我在我的代码中发现了错误 - 该缺陷在于我为 DataTexture 设置 needsUpdate 属性的方式。请参阅固定 fiddle here 。这是使其最终按预期工作的修订版:

function updateOffsets() {
offsetsTexture = new THREE.DataTexture( cloudData.offsets, width, height, THREE.RGBAFormat, THREE.FloatType );
offsetsTexture.needsUpdate = true; // **using this way of updating the texture produced the behavior I expected
positionUniforms.tOffsets.value = offsetsTexture;
// positionUniforms.tOffsets.needsUpdate = true; **this form of updating caused the faulty behavior
}

关于javascript - 更新数据纹理统一时,所有点都变为 0,0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41514510/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com