简体   繁体   English

快速(每50ms左右)用图像更新材质的纹理贴图会导致严重的卡顿

[英]Updating a material's texture map with images rapidly (every 50ms or so) causes huge stuttering

Question: How should I update a sphere's material texture map with images (stored in a HTML5 canvas) very rapidly (50ms ~ 200ms interval) without the browser choking? 问题:如何在不使浏览器阻塞的情况下,快速(以50ms〜200ms的间隔)使用图像(存储在HTML5画布中)更新球体的材质贴图?

I have a series of panoramic images to map onto a sphere. 我有一系列全景图像可以映射到一个球体上。 These images are updated every 50ms ~ 200ms. 这些图像每50ms〜200ms更新一次。 The code seems to work but in practice it chokes very badly - causing some re-draws to skip or wait for 2s ~ 3s to show up. 该代码似乎可以正常工作,但实际上它会非常窒息-导致一些重新绘制跳过或等待2s〜3s出现。

Here is the mesh code and the texture (first texture map is a placeholder): 这是网格代码和纹理(第一个纹理贴图是一个占位符):

var geometry = new THREE.SphereGeometry(100.0, 64, 64);
var material = new THREE.MeshBasicMaterial({
  map: THREE.ImageUtils.loadTexture(image_path), // placeholder
  side: THREE.DoubleSide,
});

When playPanos(0) is triggered, it tries to update the material texture map with the image drawn in a HTML5 canvas, stored in an array “ loaded_canvas ”: 触发playPanos(0)时 ,它将尝试使用HTML5画布中绘制的图像更新材质纹理贴图,该图像存储在数组“ load_canvas ”中:

function playPanos(curr_id) {
  if (curr_id >= loaded_canvas.length) return;

  paintPanos(curr_id);

  setTimeout(function() {
    playPanos(curr_id + 1);
  }, 100); // update every 100ms
}

function paintPanos(curr_id) {
  material.map = new THREE.Texture(loaded_canvas[curr_id]);
  material.map.needsUpdate = true;
}

Thanks. 谢谢。 Please pardon prototype style code. 请原谅原型样式代码。

A few tips: 一些提示:

  • You don't need to create new THREE.Texture() everytime you update the sphere texture. 您不需要在每次更新球体纹理时都创建new THREE.Texture() Create the texture once, and then update it using: material.map.needsUpdate = true; 创建一次纹理,然后使用以下material.map.needsUpdate = true;更新它: material.map.needsUpdate = true; each time you draw something new in the canvas context. 每次您在画布上下文中绘制新内容时。
  • I supose, since you store the canvases in an array, you don't have a dynamic animation, you have just a loop animation. 我想,因为您将画布存储在一个数组中,所以没有动态动画,只有循环动画。 In that case you could store the finished textures and just update material.map instead of updating the texture with the canvases stored in the array. 在这种情况下,您可以存储完成的纹理并仅更新material.map而不用存储在数组中的画布来更新纹理。
  • If your canvas resolution is too high, try to reduce the pixel dimensions as much as possible. 如果您的画布分辨率太高,请尝试尽可能减小像素尺寸。 I guess you allready done this. 我想你已经准备好了。
  • Do you really need: side: THREE.DoubleSide ? 您是否真的需要: side: THREE.DoubleSide Try using THREE.BackSide if you are using the sphere for a skybox. 如果THREE.BackSide球体用于天空盒,请尝试使用THREE.BackSide

With theese optimisations, you should be able to run texture updates at 60 fps. 通过这些优化,您应该能够以60 fps的速度运行纹理更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Setinterval似乎在1500毫秒而不是50毫秒上运行 - Setinterval seems to be running at 1500 ms instead of 50ms 将 setInterval 间隔从 100 毫秒更改为 50 毫秒 - change setInterval interval from 100ms to 50ms 节点js,为什么setTimeout用50ms比setTimeout 0快 - Node js, why is setTimeout with 50ms is faster than setTimeout 0 为什么AJAX请求所需的时间从2ms跳到50ms甚至更高? - Why does the time taken for AJAX request jump from 2ms to 50ms and higher? 为什么JS .focus()只能在50ms或更长的setTimeout内工作 - Why does JS .focus() only work inside a setTimeout of 50ms or longer 使用Data URI快速更新图像会导致缓存,内存泄漏 - Rapidly updating image with Data URI causes caching, memory leak 移相器-显示图形对象50毫秒,并在同时发生一次或多次按键时每次按键破坏 - Phaser - Show graphic objects for 50ms and destroy for each key press when one or more key presses occur simultaneously 在用户滚动到它们之前触发所有 `content-visibility: auto` 元素的渲染,而不会阻塞主线程超过 50 毫秒 - Trigger rendering of all `content-visibility: auto` elements BEFORE a user scrolls to them without blocking main thread for >50ms Java脚本每50毫秒显示一次变量 - Javascript show variable every 50 ms 重新计算风格:为什么如此口吃? - Recalculate Style: why so stuttering?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM