mirror of
https://github.com/PavelDoGreat/WebGL-Fluid-Simulation.git
synced 2025-10-03 17:31:53 +02:00
improve checking rendering to texture support
This commit is contained in:
47
script.js
47
script.js
@@ -40,10 +40,24 @@ function getWebGLContext (canvas) {
|
|||||||
|
|
||||||
gl.clearColor(0.0, 0.0, 0.0, 1.0);
|
gl.clearColor(0.0, 0.0, 0.0, 1.0);
|
||||||
|
|
||||||
const internalFormat = isWebGL2 ? gl.RGBA16F : gl.RGBA;
|
|
||||||
const internalFormatRG = isWebGL2 ? gl.RG16F : gl.RGBA;
|
|
||||||
const formatRG = isWebGL2 ? gl.RG : gl.RGBA;
|
|
||||||
const halfFloatTexType = isWebGL2 ? gl.HALF_FLOAT : halfFloat.HALF_FLOAT_OES;
|
const halfFloatTexType = isWebGL2 ? gl.HALF_FLOAT : halfFloat.HALF_FLOAT_OES;
|
||||||
|
const internalFormat = isWebGL2 ? gl.RGBA16F : gl.RGBA;
|
||||||
|
let internalFormatRG = isWebGL2 ? gl.RG16F : gl.RGBA;
|
||||||
|
let formatRG = isWebGL2 ? gl.RG : gl.RGBA;
|
||||||
|
|
||||||
|
if (isWebGL2) {
|
||||||
|
if (!supportRenderTextureFormat(gl, internalFormatRG, formatRG, halfFloatTexType)) {
|
||||||
|
internalFormatRG = gl.RGBA16F;
|
||||||
|
formatRG = gl.RGBA;
|
||||||
|
|
||||||
|
ga('send', 'event', 'webgl2', 'rg not supported');
|
||||||
|
if (!supportRenderTextureFormat(gl, internalFormatRG, formatRG, halfFloatTexType))
|
||||||
|
ga('send', 'event', 'webgl2', 'rgba not supported');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!supportRenderTextureFormat(gl, internalFormatRG, formatRG, halfFloatTexType))
|
||||||
|
ga('send', 'event', 'webgl', 'rgba not supported');
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
gl,
|
gl,
|
||||||
@@ -57,6 +71,25 @@ function getWebGLContext (canvas) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function supportRenderTextureFormat (gl, internalFormat, format, type) {
|
||||||
|
let texture = gl.createTexture();
|
||||||
|
gl.bindTexture(gl.TEXTURE_2D, texture);
|
||||||
|
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
|
||||||
|
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
|
||||||
|
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
||||||
|
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
||||||
|
gl.texImage2D(gl.TEXTURE_2D, 0, internalFormat, 4, 4, 0, format, type, null);
|
||||||
|
|
||||||
|
let fbo = gl.createFramebuffer();
|
||||||
|
gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
|
||||||
|
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
|
||||||
|
|
||||||
|
const status = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
|
||||||
|
if (status != gl.FRAMEBUFFER_COMPLETE)
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
function startGUI () {
|
function startGUI () {
|
||||||
var gui = new dat.GUI({ width: 300 });
|
var gui = new dat.GUI({ width: 300 });
|
||||||
gui.add(config, 'TEXTURE_DOWNSAMPLE', { Full: 0, Half: 1, Quarter: 2 }).name('resolution').onFinishChange(initFramebuffers);
|
gui.add(config, 'TEXTURE_DOWNSAMPLE', { Full: 0, Half: 1, Quarter: 2 }).name('resolution').onFinishChange(initFramebuffers);
|
||||||
@@ -438,14 +471,6 @@ function createFBO (texId, w, h, internalFormat, format, type, param) {
|
|||||||
gl.viewport(0, 0, w, h);
|
gl.viewport(0, 0, w, h);
|
||||||
gl.clear(gl.COLOR_BUFFER_BIT);
|
gl.clear(gl.COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
const status = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
|
|
||||||
if (status != gl.FRAMEBUFFER_COMPLETE) {
|
|
||||||
ga('send', 'pageview', {
|
|
||||||
'dimension1': status,
|
|
||||||
'dimension2': isWebGL2
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return [texture, fbo, texId];
|
return [texture, fbo, texId];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user