gpt4 book ai didi

performance - 在three.js中提高动画 Sprite 的性能

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

目前,我正在开发在线游戏,出于性能考虑,我决定使用webgl而不是HTML5的 Canvas 。我正在使用three.js框架,我的意图是拥有移动的动画 Sprite 。 Sprite 本身放置在 Sprite 板上,我使用UVOffset和UVScale来使用正确的图片,并在经过时切换 Sprite 的图片。我想知道是否有一种方法可以提高这段代码的性能,因为现在它同时开始在场上约300个“玩家”的位置上放慢速度。

问候

以下是我的代码中最重要的部分:

var image = THREE.ImageUtils.loadTexture( "img/idlew.png" );

function addPlayer(){
var mesh = new THREE.Sprite({map:image});//, affectedByDistance: false, useScreenCoordinates: false});
images.push(mesh);
mesh.position.set(Math.floor(Math.random() * 500), Math.floor(Math.random() * 500), 10);
scene.add(mesh);
mesh.uvScale.x = 96/1152;
mesh.scale.x = 0.1;
mesh.scale.y = 0.1;
}


var imgBackground = new THREE.MeshLambertMaterial({
map:THREE.ImageUtils.loadTexture('img/grass.jpg')
});


var background = new THREE.Mesh(new THREE.PlaneGeometry(1000, 1000),imgBackground);


scene.add(background);



scene.add(camera);
camera.rotation.x = -(Math.PI/2);
scene.add(new THREE.AmbientLight(0xFFFFFF));

addPlayer();

renderer.render(scene, camera);
var moveUp = false;
tick();
var ticker = 0;
var usedOne = 0;

function tick(){
ticker++;
if(ticker%10==0){
for (var i = 0; i < images.length; i++) {
images[i].uvOffset.x = usedOne * 0.0835;
};
usedOne++;
if(usedOne == 12) usedOne = 0;
addPlayer();
addPlayer();
addPlayer();
console.log(images.length);
}
requestAnimationFrame( tick );

renderer.render(scene, camera);
}

最佳答案

我已经编写了一个显示动画纹理的代码示例,并在以下位置提供了实时示例:

http://stemkoski.github.com/Three.js/Texture-Animation.html

来源可在以下位置获得:

http://github.com/stemkoski/stemkoski.github.com/blob/master/Three.js/Texture-Animation.html

有用的部分是我编写的用于自动处理偏移量的函数。该函数(从上面的链接中提取)如下:

function TextureAnimator(texture, tilesHoriz, tilesVert, numTiles, tileDispDuration) 
{
// note: texture passed by reference, will be updated by the update function.

this.tilesHorizontal = tilesHoriz;
this.tilesVertical = tilesVert;

// how many images does this spritesheet contain?
// usually equals tilesHoriz * tilesVert, but not necessarily,
// if there at blank tiles at the bottom of the spritesheet.
this.numberOfTiles = numTiles;
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set( 1 / this.tilesHorizontal, 1 / this.tilesVertical );

// how long should each image be displayed?
this.tileDisplayDuration = tileDispDuration;

// how long has the current image been displayed?
this.currentDisplayTime = 0;

// which image is currently being displayed?
this.currentTile = 0;

this.update = function( milliSec )
{
this.currentDisplayTime += milliSec;
while (this.currentDisplayTime > this.tileDisplayDuration)
{
this.currentDisplayTime -= this.tileDisplayDuration;
this.currentTile++;
if (this.currentTile == this.numberOfTiles)
this.currentTile = 0;
var currentColumn = this.currentTile % this.tilesHorizontal;
texture.offset.x = currentColumn / this.tilesHorizontal;
var currentRow = Math.floor( this.currentTile / this.tilesHorizontal );
texture.offset.y = currentRow / this.tilesVertical;
}
};
}

您可以使用(例如)初始化 Material :
var runnerTexture = new THREE.ImageUtils.loadTexture( 'images/run.png' );
// a texture with 10 frames arranged horizontally, display each for 75 millisec
annie = new TextureAnimator( runnerTexture, 10, 1, 10, 75 );
var runnerMaterial = new THREE.MeshBasicMaterial( { map: runnerTexture } );

并在每次渲染之前使用以下命令对其进行更新:
var delta = clock.getDelta(); 
annie.update(1000 * delta);

希望这可以帮助!

关于performance - 在three.js中提高动画 Sprite 的性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11284372/

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