javascript – 使用Three.js提高性能
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 使用Three.js提高性能,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5294字,纯文字阅读大概需要8分钟。
内容图文
![javascript – 使用Three.js提高性能](/upload/InfoBanner/zyjiaocheng/725/1d04f8d0c48841b39fad08b27717efdd.jpg)
我目前正在编写我的第一个Three.js / WebGL应用程序,它在我的PC(Chrome)上运行得非常好.遗憾的是,在许多其他PC上,帧速率通常会低于每秒30帧.
由于应用程序实际上并不复杂,我想询问一些与应用程序相关的提示,以提高性能.
该应用程序的一个版本可以在这里找到:
www.wrodewald.de/StackOverflowExample/
该应用程序包含一个使用64²顶点的动态(变形)平面.矩阵用于存储静态高度图和波形图.每个帧都会更新波形图以重新计算自身,一些滤波器用于与其顶点相比“均匀”每个顶点.
因此,必须更新平面的每个帧(颜色和顶点位置),这可能是性能问题的原因
第二个对象(菱形)不应该是一个问题,静止,移动一点但没有什么特别的.
有三个灯(环境,方向,球形),没有阴影,倾斜移位着色器和晕影着色器.
以下是每帧调用的函数:
var render = function() {
requestAnimationFrame( render );
var time = clock.getDelta();
world.updateWorld(time);
diamond.rotate(time);
diamond.update(time);
control.updateCamera(camera, time);
composer.render();
stats.update();
}
这就是world.updateWorld(时间)的作用
//in world.updateWorld(time) where
// accmap: stores acceleration and wavemap stores position
// this.mapSize stores the size of the plane in vertices (64)
// UPDATE ACCELERATION MAP
for(var iX = 1; iX < (this.mapSize-1); iX++) {
for(var iY = 1; iY < (this.mapSize-1); iY++) {
accmap[iX][iY] -= dT * (wavemap[iX][iY]) * Math.abs(wavemap[iX][iY]);
}
}
// SMOOTH ACCELERATION MAP
for(var iX = 1; iX < (this.mapSize-1); iX++) {
for(var iY = 1; iY < (this.mapSize-1); iY++) {
tempmap[iX][iY] = accmap[iX-1][iY-1] * 0.0625
+ accmap[iX-1][iY ] * 0.125
+ accmap[iX-1][iY+1] * 0.0625
+ accmap[iX ][iY-1] * 0.125
+ accmap[iX ][iY ] * 0.25
+ accmap[iX ][iY+1] * 0.125
+ accmap[iX+1][iY-1] * 0.0625
+ accmap[iX+1][iY ] * 0.125
+ accmap[iX+1][iY+1] * 0.0625;
accmap[iX][iY] = tempmap[iX][iY];
}
}
// UPDATE WAVE MAP
for(var iX = 1; iX < (this.mapSize-1); iX++) {
for(var iY = 1; iY < (this.mapSize-1); iY++) {
wavemap[iX][iY] += dT * accmap[iX][iY];
}
}
for(var i = 0; i < this.mapSize; i++) {
for(var k = 0; k < this.mapSize; k++) {
geometry.vertices[ i * this.mapSize + k ].y = wavemap[i][k] + heightmap[i][k];
}
}
for(var i = 0; i < geometry.faces.length; i++) {
var vertexA = geometry.vertices[geometry.faces[i].a];
var vertexB = geometry.vertices[geometry.faces[i].b];
var vertexC = geometry.vertices[geometry.faces[i].c];
var val = (vertexA.y + vertexB.y + vertexC.y) / 3.0;
val = (val / 200.) + 0.5;
geometry.faces[i].color.r = val;
geometry.faces[i].color.g = val;
geometry.faces[i].color.b = val;
}
geometry.colorsNeedUpdate = true;
geometry.verticesNeedUpdate = true;
这些是“钻石”功能
this.rotate = function(dT) {
counter += 0.5 * dT;
counter % 1;
var x = 0.0025 * (Math.sin((counter) * 2 * Math.PI));
var y = 0.0025 * (Math.cos((counter) * 2 * Math.PI));
this.mesh.rotateOnAxis(new THREE.Vector3(1,0,0), x);
this.mesh.rotateOnAxis(new THREE.Vector3(0,0,1), y);
}
this.update = function(dT) {
for(var i = 0; i < geometry.faces.length; i++) {
geometry.faces[i].color.lerp(color, dT*(0.9));
}
geometry.colorsNeedUpdate = true;
}
您是否发现帧速率如此不一致的任何原因?
解决方法:
编辑:
我发现了你需要改进的两件大事:
使用GPU更新平面
加速:高
让我们从plane.js中选择你的代码
timer += dT;
if(timer > 0.1) {
var x = 2 + Math.floor(Math.random() * (this.mapSize - 4));
var y = 2 + Math.floor(Math.random() * (this.mapSize - 4));
//accmap[x][y] += 30000 * Math.random () - 15000
}
// UPDATE ACCELERATION MAP
for(var iX = 1; iX < (this.mapSize-1); iX++) {
for(var iY = 1; iY < (this.mapSize-1); iY++) {
accmap[iX][iY] -= dT * (wavemap[iX][iY]) * Math.abs(wavemap[iX][iY]);
}
}
所以你有4096个顶点,你想用CPU每17毫秒更新一次.请注意,您没有使用任何GPU优势.该怎么做:
>首先创建缓冲区,顶点位置,法线,纹理坐标,索引….这一起称为网格.
>然后你创建一个模型.模型由一个或多个网格组成.和modelViewMatrix.这是非常重要的部分,矩阵4×4是该模型的位置,旋转和比例的代数表示.
>使用每个渲染,您可以在顶点着色器中完成此操作:
“gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0);”,
它来自你的cg / shaders / VerticalTiltShiftShader.js
>如果要旋转平面,则不要将每个顶点相乘,但只需将模型矩阵乘以一次(js与THREE.js函数):
projectionMatrix.makeRotationY(DT);
然后每个顶点在顶点着色器中与此矩阵相乘,这样更快.
Javascript风格
加速:无 – 中等,但它可以让你更快地编码
让我们选择你的plane.js作为例子.
// this is your interface
function PlaneWorld () {
this.init = function() {};
this.updateVertices = function() {};
this.updateWorld = function(dT) {};
// ... and more
}
// and somewhere else:
var world = new PlaneWorld();
如果您的项目中只有一个平面,您可以将其视为单例并且实现正常.但是如果您想创建2个或更多平面,则会为每个实例重新创建所有函数(new PlaneWorld()).如何做到这一点的正确方法是:
function PlaneWorld () {
...
}
PlaneWorld.prototype.init = function() {};
PlaneWorld.prototype.updateVertices = function() {};
PlaneWorld.prototype.updateWorld = function(dT) {};
// ... and more
var world = new PlaneWorld();
// calling methods works same
world.updateVertices();
或更复杂的匿名函数版本:
var PlaneWorld = (function() {
// something like private static variables here
var PlaneWorld = function () {
...
}
PlaneWorld.prototype = {
init: function() {},
updateVertices: function() {},
updateWorld: function(dT) {}
// ... and more
}
return PlaneWorld();
})();
var world = new PlaneWorld();
// calling methods works same
world.updateVertices();
然后降低新的实例成本.现在可能连接的东西,每个实例应共享相同的网格,但有自己的modelViewMatrix.
内容总结
以上是互联网集市为您收集整理的javascript – 使用Three.js提高性能全部内容,希望文章能够帮你解决javascript – 使用Three.js提高性能所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。