在利用drawcommand繪製圖形的時候,會通過uniformMap傳遞變量到着色器,如果用到異步加載圖片生成紋理再傳遞給uniformMap,這個時候需要需要注意return的方式。應先判斷需要傳值的紋理是否爲null或者undefined,然後根據判斷結果是return 所傳的紋理,還是context的默認紋理。即使此時Resource異步加載圖片生成紋理沒有完成,此時應傳入context的默認紋理,等到Resource加載完畢,傳入的context的默認紋理會被Resource加載圖片生成的紋理所覆蓋。
1、生成紋理示例
//圖像紋理讀取與創建
let texture = undefined;
let imageUri = '../static/images/bonsai.raw.png';
let vtxfTexture = undefined;
Cesium.Resource.createIfNeeded(imageUri).fetchImage().then(function (image) {
console.log('image loaded!');
console.log(image);
//debugger
vtxfTexture = new Cesium.Texture({
context: context,
width: image.width,
height: image.height,
source: {
arrayBufferView: image.bufferView
},
hasMipmap: false,
sampler: sampler,
pixelDatatype: Cesium.PixelDatatype.UNSIGNED_BYTE
});
vtxfTexture.type = 'sampler2D';
//debugger
that._cubeTextures = vtxfTexture
});
2、uniformMap傳值示例
let uniformMapFront = {
tex: function () {
return that._framebuffer._colorTextures[0]
},
cubeTex: function () {
if (Cesium.defined(that._cubeTextures)) {
return that._cubeTextures;
} else {
return context.defaultTexture;
}
}
}