[英]Create texture from Array THREE.js
我正在研究地形發生器,但我無法弄清楚如何做顏色。 我希望能夠生成一個占據整個PlaneGeometry的圖像。 我的問題是如何根據我的高度圖創建一個覆蓋整個PlaneGeometry(沒有包裝)的單個圖像? 我可以想到一種方法,但我不確定它是否會完全覆蓋PlaneGeometry,效率非常低。 我用畫布上的顏色繪制二維視圖。 然后我將畫布轉換為紋理是最佳/唯一的方式嗎?
更新:使用DataTexture,我遇到了一些錯誤。 我完全不知道我哪里出錯了。 這是我得到的錯誤:
WebGL: drawElements: texture bound to texture unit 0 is not renderable. It maybe non-power-of-2 and have incompatible texture filtering or is not 'texture complete'. Or the texture is Float or Half Float type with linear filtering while OES_float_linear or OES_half_float_linear extension is not enabled.
DataTexture和PlaneGeometry都具有512 ^ 2的大小。 我該怎么做才能解決這個問題?
這是我使用的一些代碼:
編輯:我修好了。 這是我使用的工作代碼。
function genDataTexture(){
//Set the size.
var dataMap = new Uint8Array(1 << (Math.floor(Math.log(map.length * map[0].length * 4) / Math.log(2))));
/* ... */
//Set the r,g,b for each pixel, color determined above
dataMap[count++] = color.r;
dataMap[count++] = color.g;
dataMap[count++] = color.b;
dataMap[count++] = 255;
}
var texture = new THREE.DataTexture(dataMap, map.length, map[0].length, THREE.RGBAFormat);
texture.needsUpdate = true;
return texture;
}
/* ... */
//Create the material
var material = new THREE.MeshBasicMaterial({map: genDataTexture()});
//Here, I mesh it and add it to scene. I don't change anything after this.
如果數據已經在您的Javascript代碼中,那么最佳方法是使用DataTexture
- 請參閱https://threejs.org/docs/#api/textures/DataTexture獲取常規文檔,或者查看THREE.ImageUtils.generateDataTexture()
用於制作它們的相當方便的方法。 http://threejs.org/docs/#Reference/Extras/ImageUtils
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.