重绘对象时如何清除屏幕
How can I clear the screen when I redraw an object?
我正在尝试实现 boids 算法,一切都很顺利,直到使用 three.js 绘制鸟类(或立方体,在我的情况下)。
重绘后屏幕未清除,因此我什至会在屏幕上获得旧的立方体。
这是我的代码:
var scene;
var camera;
var aspect = window.innerWidth / window.innerHeight;
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera( 45, aspect, 0.1, 1000 );
camera.position.z = 200;
var geometry = new THREE.BoxGeometry( 10, 10, 10 );
var loader = new THREE.TextureLoader();
function init()
{
var posx, posy, posz;
var velx, vely, velz;
var rotx = 0;
var roty = 0;
var rotz = 0;
var boid;
loader.load('images/picture.jpg', function ( texture ) {
var material = new THREE.MeshBasicMaterial( {
map: texture
} );
for ( var i = 0; i < 5; i ++ ) {
posx = (Math.random() - 0.5) * 100;
posy = (Math.random() - 0.5) * 100;
posz = (Math.random() - 0.5) * 100;
velx = (Math.random() - 0.7) * 100;
vely = (Math.random() - 0.7) * 100;
velz = (Math.random() - 0.7) * 100;
var boidPosition = new THREE.Vector3(posx, posy, posz);
var boidRotation = new THREE.Vector3(rotx, roty, rotz);
var boidVelocity = new THREE.Vector3(velx, vely, velz);
var boidDesign = new THREE.Mesh( geometry, material );
boidDesign.position.x = boidPosition.x;
boidDesign.position.y = boidPosition.y;
boidDesign.position.z = boidPosition.z;
boid = new Boid(boidPosition, boidVelocity);
boids[i] = boid;
boidDesign.name = "test_name" + i;
scene.add( boidDesign );
}
},
function ( xhr ) {
console.log( (xhr.loaded / xhr.total * 100) + '% loaded' );
},
function ( xhr ) {
console.log( 'An error happened' );
}
);
}
function redraw() {
var boid;
loader.load('images/picture.jpg', function ( texture ) {
var material = new THREE.MeshBasicMaterial( {
map: texture
} );
for ( var i = 0; i < boids.length; i ++ ) {
boid = boids[i];
var boidPosition = new THREE.Vector3(boid.position.x, boid.position.y, boid.position.z);
var boidDesign = new THREE.Mesh( geometry, material );
boidDesign.position.x = boidPosition.x;
boidDesign.position.y = boidPosition.y;
boidDesign.position.z = boidPosition.z;
boidDesign.name = "test_name" + i;
scene.add( boidDesign );
}
},
function ( xhr ) {
console.log( (xhr.loaded / xhr.total * 100) + '% loaded' );
},
function ( xhr ) {
console.log( 'An error happened' );
}
);
}
function removeEntity(object) {
var selectedObject = scene.getObjectByName(object.name);
scene.remove( selectedObject );
}
function drawAfterUpdate()
{
var boid;
for(var i = 0; i < boids.length; i++)
{
boid = boids[i];
boid.update(boids);
removeEntity(boid);
}
redraw(boids);
}
var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setClearColor( 0x000000 );
document.body.appendChild( renderer.domElement );
var render = function()
{
requestAnimationFrame( render );
drawAfterUpdate();
renderer.render( scene, camera );
};
init();
render();
我只是找不到解决方案。
如果需要更多详细信息,请询问。提前谢谢。:)
因此,我找到了一个解决方案:我在重绘和初始化中删除了loader.load调用,并在这些函数之外定义了纹理和材料,结果是相同的,但它似乎在页面上加载得更快。另外,我没有删除我认为的对象,所以我更改了这个:boidDesign.name = "boid"+i;
这个:boid.name = "boid" + i; boidDesign.name = boid.name;
它按预期工作。
相关文章:
- document.open/document.write没有正确地清除chrome中的文档——这是chrome的错误吗
- 单击jquery清除输入值
- 如何将屏幕分辨率乘以 80%,然后在代码中使用
- 在不打开聊天屏幕的情况下制作Zopim-ding代理
- 使用图像数组列表的simplegallary.js显示适合桌面屏幕的图像
- 导航栏没有调整到浏览器屏幕的大小
- 调整屏幕大小后不显示子菜单
- 如何在p5.js中管理多智力竞赛游戏的多个屏幕
- 如何在Microsoft VirtualEarth 6.3中使用纯javascript清除整个形状层
- 重绘对象时如何清除屏幕
- 清除屏幕插件中的 setInterval 以在视口中更改类
- 如何擦除、清除、擦除保存在iPad主屏幕上的网络应用程序的HTML5本地存储
- 如何使用javascript清除屏幕
- 如何清除主屏幕上IOS Web应用程序的缓存
- 在three.js的下一帧之前清除屏幕
- 意想不到的屏幕清除WebGL
- 显示javascript变量而不清除屏幕
- 创建模型可以清除屏幕
- 如何从控制台清除集合或屏幕上的注释和名称
- 如何在Node.js中清除函数调用之间的Windows 7控制台屏幕?