只有geometry.elementsNeedUpdate会导致内存分配 - 为什么? [英] Only geometry.elementsNeedUpdate results in memory allocation - why?

查看:103
本文介绍了只有geometry.elementsNeedUpdate会导致内存分配 - 为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个简单的动画功能

If I have a simple animate function

function animate() {
geometry.elementsNeedUpdate = true;  // allocates memory without geometry.faces[ 0 ].color = ...    
requestAnimationFrame( animate );
renderer.render( scene, camera ); }

即使没有更改任何元素,我也会获得内存分配。
然后我添加了脸部颜色的操作。

I get a memory allocation, even if no element is changed. Then I add the manipulation of the color of the face.

function animate() {

geometry.elementsNeedUpdate = true;  
// geometry.colorsNeedUpdate = true; // not necessary
// material.needsUpdate = true;      // not necessary
time = clock.getElapsedTime();  
if ( time * 16 % 10 < 5 ) { 
    geometry.faces[ 0 ].color = colorBlue;      
} else {    
    geometry.faces[ 0 ].color = colorRed;       
}
requestAnimationFrame( animate );
renderer.render( scene, camera ); } 

并保留在内存分配中。

and it remains in the memory allocation.

geometry.elementsNeedUpdate = true; 是更改面部颜色所必需的。

geometry.elementsNeedUpdate = true; is necessary to change the face colors.

如何避免内存分配?

完整示例可在 http://sandbox.threejs.hofk.de/memory/

推荐答案

您想要更改对象的面部颜色。

You want to change the face colors of your object.

您可以使用 copy()或 setHex()如下:

mesh.geometry.faces[ 0 ].color.copy( myColor ); // or .setHex( 0xff0000 )

mesh.geometry.colorsNeedUpdate = true; // required if geometry previously rendered

不要设置其他 needsUpdate 不必要的标记。

Do not set other needsUpdate flags unnecessarily.

不要分配新的 THREE.Color 对象,就像你现在所做的那样。

Do not assign a new THREE.Color object, as you are currently doing.

three.js r.84

three.js r.84

这篇关于只有geometry.elementsNeedUpdate会导致内存分配 - 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆