-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for sRGB framebuffer on WebGPU (#6838)
* Support for sRGB framebuffer on WebGPU * lint * examples lint --------- Co-authored-by: Martin Valigursky <mvaligursky@snapchat.com>
- Loading branch information
1 parent
98301f9
commit 0ab8933
Showing
35 changed files
with
374 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
examples/src/examples/graphics/material-transparency.example.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
import * as pc from 'playcanvas'; | ||
import { deviceType, rootPath } from 'examples/utils'; | ||
|
||
const canvas = /** @type {HTMLCanvasElement} */ (document.getElementById('application-canvas')); | ||
window.focus(); | ||
|
||
const assets = { | ||
font: new pc.Asset('font', 'font', { url: rootPath + '/static/assets/fonts/arial.json' }) | ||
}; | ||
|
||
const gfxOptions = { | ||
deviceTypes: [deviceType], | ||
glslangUrl: rootPath + '/static/lib/glslang/glslang.js', | ||
twgslUrl: rootPath + '/static/lib/twgsl/twgsl.js', | ||
|
||
// disable anti-aliasing to make dithering more pronounced | ||
antialias: false, | ||
|
||
// use sRGB for display format (only supported on WebGPU, fallbacks to LDR on WebGL2) | ||
displayFormat: pc.DISPLAYFORMAT_LDR_SRGB | ||
}; | ||
|
||
const device = await pc.createGraphicsDevice(canvas, gfxOptions); | ||
|
||
// make dithering more pronounced by rendering to lower resolution | ||
device.maxPixelRatio = 1; | ||
|
||
const createOptions = new pc.AppOptions(); | ||
createOptions.graphicsDevice = device; | ||
|
||
createOptions.componentSystems = [ | ||
pc.RenderComponentSystem, | ||
pc.CameraComponentSystem, | ||
pc.LightComponentSystem, | ||
pc.ElementComponentSystem | ||
]; | ||
createOptions.resourceHandlers = [ | ||
pc.TextureHandler, | ||
pc.FontHandler | ||
]; | ||
|
||
const app = new pc.AppBase(canvas); | ||
app.init(createOptions); | ||
|
||
// Set the canvas to fill the window and automatically change resolution to be the same as the canvas size | ||
app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW); | ||
app.setCanvasResolution(pc.RESOLUTION_AUTO); | ||
|
||
// Ensure canvas is resized when window changes size | ||
const resize = () => app.resizeCanvas(); | ||
window.addEventListener('resize', resize); | ||
app.on('destroy', () => { | ||
window.removeEventListener('resize', resize); | ||
}); | ||
|
||
const assetListLoader = new pc.AssetListLoader(Object.values(assets), app.assets); | ||
assetListLoader.load(() => { | ||
app.start(); | ||
|
||
app.scene.rendering.toneMapping = pc.TONEMAP_LINEAR; | ||
|
||
// Create an entity with a camera component | ||
const camera = new pc.Entity(); | ||
camera.addComponent('camera', { | ||
clearColor: pc.Color.BLACK | ||
}); | ||
camera.translate(0, -0.5, 14); | ||
camera.rotate(0, 0, 0); | ||
app.root.addChild(camera); | ||
|
||
const NUM_SPHERES_X = 4; | ||
const NUM_SPHERES_Z = 10; | ||
|
||
const ditherOptions = [ | ||
pc.DITHER_NONE, | ||
pc.DITHER_BAYER8, | ||
pc.DITHER_BLUENOISE, | ||
pc.DITHER_IGNNOISE | ||
]; | ||
|
||
/** | ||
* @param {number} x - The x coordinate. | ||
* @param {number} z - The z coordinate. | ||
*/ | ||
const createSphere = function (x, z) { | ||
const material = new pc.StandardMaterial(); | ||
material.name = `material-${ditherOptions[x]}-${z}`; | ||
material.emissive = new pc.Color(1, 0, 0); | ||
material.specular = new pc.Color(1, 1, 1); | ||
material.metalness = 0.0; | ||
material.gloss = 0.5; | ||
material.useMetalness = true; | ||
|
||
if (ditherOptions[x] === pc.DITHER_NONE) { | ||
// alpha blending material | ||
material.blendType = pc.BLEND_NORMAL; | ||
} else { | ||
// alpha dithering material | ||
material.opacityDither = ditherOptions[x]; | ||
} | ||
|
||
// we want the spheres to seem to fade out in a linear fashion, so we need to convert | ||
// the perceived opacity value from sRGB to linear space | ||
const perceivedOpacity = (z + 1) / NUM_SPHERES_Z; | ||
const linearOpacity = Math.pow(perceivedOpacity, 2.2); | ||
material.opacity = linearOpacity; | ||
|
||
material.update(); | ||
|
||
const sphere = new pc.Entity(`entity-${ditherOptions[x]}-${z}`); | ||
sphere.addComponent('render', { | ||
material: material, | ||
type: 'sphere' | ||
}); | ||
sphere.setLocalPosition(1.5 * (x - (NUM_SPHERES_X - 1) * 0.5), z - (NUM_SPHERES_Z - 1) * 0.5, 0); | ||
sphere.setLocalScale(0.9, 0.9, 0.9); | ||
app.root.addChild(sphere); | ||
}; | ||
/** | ||
* @param {pc.Asset} fontAsset - The font asset. | ||
* @param {string} message - The message. | ||
* @param {number} x - The x coordinate. | ||
* @param {number} y - The y coordinate. | ||
*/ | ||
const createText = function (fontAsset, message, x, y) { | ||
// Create a text element-based entity | ||
const text = new pc.Entity(); | ||
text.addComponent('element', { | ||
anchor: [0.5, 0.5, 0.5, 0.5], | ||
fontAsset: fontAsset, | ||
fontSize: 0.3, | ||
pivot: [0.5, 0.5], | ||
text: message, | ||
type: pc.ELEMENTTYPE_TEXT | ||
}); | ||
text.setLocalPosition(x, y, 0); | ||
app.root.addChild(text); | ||
}; | ||
|
||
for (let i = 0; i < NUM_SPHERES_X; i++) { | ||
for (let j = 0; j < NUM_SPHERES_Z; j++) { | ||
createSphere(i, j); | ||
} | ||
} | ||
|
||
const y = (NUM_SPHERES_Z + 1) * -0.5; | ||
createText(assets.font, 'Alpha\nBlend', NUM_SPHERES_X * -0.6, y); | ||
createText(assets.font, 'Bayer8\nDither', NUM_SPHERES_X * -0.2, y); | ||
createText(assets.font, 'Blue-noise\nDither', NUM_SPHERES_X * 0.2, y); | ||
createText(assets.font, 'IGN-noise\nDither', NUM_SPHERES_X * 0.6, y); | ||
}); | ||
|
||
export { app }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
|
||
precision mediump float; | ||
uniform sampler2D uTexture; | ||
varying float vertOutTexCoord; | ||
varying vec2 texCoord; | ||
void main(void) | ||
{ | ||
float v = vertOutTexCoord; | ||
v = float(int(v * 6.0)) / 6.0; | ||
// vec4 color = texture2D (uTexture, texCoord); // try this to use the diffuse color. | ||
vec4 color = vec4(0.5, 0.47, 0.43, 1.0); | ||
gl_FragColor = color * vec4(v, v, v, 1.0); | ||
vec3 linearColor = vec3(0.218, 0.190, 0.156) * v; | ||
gl_FragColor.rgb = gammaCorrectOutput(linearColor.rgb); | ||
gl_FragColor.a = 1.0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.