3 回答

TA貢獻(xiàn)1874條經(jīng)驗(yàn) 獲得超12個(gè)贊
您可以輕松地使用BufferGeometry和setDrawRange()方法為線設(shè)置動(dòng)畫(或增加渲染點(diǎn)的數(shù)量)。但是,您確實(shí)需要設(shè)置最大點(diǎn)數(shù)。
var MAX_POINTS = 500;
// geometry
var geometry = new THREE.BufferGeometry();
// attributes
var positions = new Float32Array( MAX_POINTS * 3 ); // 3 vertices per point
geometry.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
// draw range
drawCount = 2; // draw the first 2 points, only
geometry.setDrawRange( 0, drawCount );
// material
var material = new THREE.LineBasicMaterial( { color: 0xff0000 } );
// line
line = new THREE.Line( geometry, material );
scene.add( line );
您可以使用以下模式設(shè)置位置數(shù)據(jù):
var positions = line.geometry.attributes.position.array;
var x = y = z = index = 0;
for ( var i = 0, l = MAX_POINTS; i < l; i ++ ) {
positions[ index ++ ] = x;
positions[ index ++ ] = y;
positions[ index ++ ] = z;
x += ( Math.random() - 0.5 ) * 30;
y += ( Math.random() - 0.5 ) * 30;
z += ( Math.random() - 0.5 ) * 30;
}
如果要更改在第一次渲染后渲染的點(diǎn)數(shù),請(qǐng)執(zhí)行以下操作:
line.geometry.setDrawRange( 0, newValue );
如果要在第一次渲染后更改位置數(shù)據(jù)值,請(qǐng)按以下方式設(shè)置needsUpdate標(biāo)志:
line.geometry.attributes.position.needsUpdate = true; // required after the first render
這是一個(gè)小提琴,顯示了可以適應(yīng)您的用例的動(dòng)畫線。

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超5個(gè)贊
實(shí)時(shí)畫線
這里是一個(gè)更新的小提琴,我在其中優(yōu)化了 user3325025他的示例中的代碼;在這種情況下,絕對(duì)不需要在渲染時(shí)更新直線的所有點(diǎn)。僅需要onMouseMove更新(更新行尾)和onMouseDown(繪制新點(diǎn)):
// update line
function updateLine() {
positions[count * 3 - 3] = mouse.x;
positions[count * 3 - 2] = mouse.y;
positions[count * 3 - 1] = mouse.z;
line.geometry.attributes.position.needsUpdate = true;
}
// mouse move handler
function onMouseMove(event) {
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
mouse.z = 0;
mouse.unproject(camera);
if( count !== 0 ){
updateLine();
}
}
// add point
function addPoint(event){
positions[count * 3 + 0] = mouse.x;
positions[count * 3 + 1] = mouse.y;
positions[count * 3 + 2] = mouse.z;
count++;
line.geometry.setDrawRange(0, count);
updateLine();
}
添加回答
舉報(bào)