|
| 1 | +// @config DESCRIPTION This example shows how to customize the final compose pass by injecting a simple pixelation post-effect. Useful if no additional render passes are needed. Changes are applied globally to all CameraFrames. |
| 2 | +import { data } from 'examples/observer'; |
| 3 | +import { deviceType, rootPath } from 'examples/utils'; |
| 4 | +import * as pc from 'playcanvas'; |
| 5 | + |
| 6 | +const canvas = /** @type {HTMLCanvasElement} */ (document.getElementById('application-canvas')); |
| 7 | +window.focus(); |
| 8 | + |
| 9 | +const assets = { |
| 10 | + orbit: new pc.Asset('script', 'script', { url: `${rootPath}/static/scripts/camera/orbit-camera.js` }), |
| 11 | + apartment: new pc.Asset('apartment', 'container', { url: `${rootPath}/static/assets/models/apartment.glb` }), |
| 12 | + love: new pc.Asset('love', 'container', { url: `${rootPath}/static/assets/models/love.glb` }), |
| 13 | + helipad: new pc.Asset( |
| 14 | + 'helipad-env-atlas', |
| 15 | + 'texture', |
| 16 | + { url: `${rootPath}/static/assets/cubemaps/helipad-env-atlas.png` }, |
| 17 | + { type: pc.TEXTURETYPE_RGBP, mipmaps: false } |
| 18 | + ) |
| 19 | +}; |
| 20 | + |
| 21 | +const gfxOptions = { |
| 22 | + deviceTypes: [deviceType], |
| 23 | + glslangUrl: `${rootPath}/static/lib/glslang/glslang.js`, |
| 24 | + twgslUrl: `${rootPath}/static/lib/twgsl/twgsl.js`, |
| 25 | + |
| 26 | + // The scene is rendered to an antialiased texture, so we disable antialiasing on the canvas |
| 27 | + // to avoid the additional cost. This is only used for the UI which renders on top of the |
| 28 | + // post-processed scene, and we're typically happy with some aliasing on the UI. |
| 29 | + antialias: false |
| 30 | +}; |
| 31 | + |
| 32 | +const device = await pc.createGraphicsDevice(canvas, gfxOptions); |
| 33 | +device.maxPixelRatio = Math.min(window.devicePixelRatio, 2); |
| 34 | + |
| 35 | +const createOptions = new pc.AppOptions(); |
| 36 | +createOptions.graphicsDevice = device; |
| 37 | +createOptions.mouse = new pc.Mouse(document.body); |
| 38 | +createOptions.touch = new pc.TouchDevice(document.body); |
| 39 | +createOptions.keyboard = new pc.Keyboard(window); |
| 40 | + |
| 41 | +createOptions.componentSystems = [ |
| 42 | + pc.RenderComponentSystem, |
| 43 | + pc.CameraComponentSystem, |
| 44 | + pc.LightComponentSystem, |
| 45 | + pc.ScriptComponentSystem |
| 46 | +]; |
| 47 | +createOptions.resourceHandlers = [ |
| 48 | + pc.TextureHandler, |
| 49 | + pc.ContainerHandler, |
| 50 | + pc.ScriptHandler |
| 51 | +]; |
| 52 | + |
| 53 | +const app = new pc.AppBase(canvas); |
| 54 | +app.init(createOptions); |
| 55 | + |
| 56 | +// Set the canvas to fill the window and automatically change resolution to be the same as the canvas size |
| 57 | +app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW); |
| 58 | +app.setCanvasResolution(pc.RESOLUTION_AUTO); |
| 59 | + |
| 60 | +// Ensure canvas is resized when window changes size |
| 61 | +const resize = () => app.resizeCanvas(); |
| 62 | +window.addEventListener('resize', resize); |
| 63 | +app.on('destroy', () => { |
| 64 | + window.removeEventListener('resize', resize); |
| 65 | +}); |
| 66 | + |
| 67 | +const assetListLoader = new pc.AssetListLoader(Object.values(assets), app.assets); |
| 68 | +assetListLoader.load(() => { |
| 69 | + app.start(); |
| 70 | + |
| 71 | + // setup skydome with low intensity |
| 72 | + app.scene.envAtlas = assets.helipad.resource; |
| 73 | + app.scene.exposure = 1.2; |
| 74 | + |
| 75 | + // create an instance of the apartment and add it to the scene |
| 76 | + const platformEntity = assets.apartment.resource.instantiateRenderEntity(); |
| 77 | + platformEntity.setLocalScale(30, 30, 30); |
| 78 | + app.root.addChild(platformEntity); |
| 79 | + |
| 80 | + // load a love sign model and add it to the scene |
| 81 | + const loveEntity = assets.love.resource.instantiateRenderEntity(); |
| 82 | + loveEntity.setLocalPosition(-80, 30, -20); |
| 83 | + loveEntity.setLocalScale(130, 130, 130); |
| 84 | + loveEntity.rotate(0, -90, 0); |
| 85 | + app.root.addChild(loveEntity); |
| 86 | + |
| 87 | + // make the love sign emissive to bloom |
| 88 | + const loveMaterial = loveEntity.findByName('s.0009_Standard_FF00BB_0').render.meshInstances[0].material; |
| 89 | + loveMaterial.emissive = pc.Color.YELLOW; |
| 90 | + loveMaterial.emissiveIntensity = 200; |
| 91 | + loveMaterial.update(); |
| 92 | + |
| 93 | + // adjust all materials of the love sign to disable dynamic refraction |
| 94 | + loveEntity.findComponents('render').forEach((render) => { |
| 95 | + render.meshInstances.forEach((meshInstance) => { |
| 96 | + meshInstance.material.useDynamicRefraction = false; |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + // Create an Entity with a camera component |
| 101 | + const cameraEntity = new pc.Entity(); |
| 102 | + cameraEntity.addComponent('camera', { |
| 103 | + farClip: 1500, |
| 104 | + fov: 80 |
| 105 | + }); |
| 106 | + |
| 107 | + const focusPoint = new pc.Entity(); |
| 108 | + focusPoint.setLocalPosition(-80, 80, -20); |
| 109 | + |
| 110 | + // add orbit camera script with a mouse and a touch support |
| 111 | + cameraEntity.addComponent('script'); |
| 112 | + cameraEntity.script.create('orbitCamera', { |
| 113 | + attributes: { |
| 114 | + inertiaFactor: 0.2, |
| 115 | + focusEntity: focusPoint, |
| 116 | + distanceMax: 500, |
| 117 | + frameOnStart: false |
| 118 | + } |
| 119 | + }); |
| 120 | + cameraEntity.script.create('orbitCameraInputMouse'); |
| 121 | + cameraEntity.script.create('orbitCameraInputTouch'); |
| 122 | + |
| 123 | + cameraEntity.setLocalPosition(-50, 100, 220); |
| 124 | + cameraEntity.lookAt(0, 0, 100); |
| 125 | + app.root.addChild(cameraEntity); |
| 126 | + |
| 127 | + // ------ Custom shader chunks for the camera frame ------ |
| 128 | + |
| 129 | + // Note: Override these empty chunks with your own custom code. Available chunk names: |
| 130 | + // - composeDeclarationsPS: declarations for your custom code |
| 131 | + // - composeMainStartPS: code to run at the start of the compose code |
| 132 | + // - composeMainEndPS: code to run at the end of the compose code |
| 133 | + |
| 134 | + // Pixelation shader is based on this shadertoy shader: https://www.shadertoy.com/view/4dsXWs |
| 135 | + |
| 136 | + // Define the pixelation helper in declarations so it's available in main |
| 137 | + pc.ShaderChunks.get(device, pc.SHADERLANGUAGE_GLSL).set('composeDeclarationsPS', ` |
| 138 | + uniform float pixelationTilePixels; |
| 139 | + uniform float pixelationIntensity; |
| 140 | + vec3 pixelateResult(vec3 color, vec2 uv, vec2 invRes) { |
| 141 | + vec2 tileUV = vec2(pixelationTilePixels, pixelationTilePixels) * invRes; |
| 142 | + vec2 centerUv = (floor(uv / tileUV) + 0.5) * tileUV; |
| 143 | +
|
| 144 | + vec2 local = (uv - centerUv) / tileUV; |
| 145 | + float dist = length(local); |
| 146 | + float radius = 0.35; |
| 147 | + float edge = fwidth(dist) * 1.5; |
| 148 | + float mask = 1.0 - smoothstep(radius, radius + edge, dist); |
| 149 | + vec3 dotResult = mix(vec3(0.0), color, mask); |
| 150 | + return mix(color, dotResult, pixelationIntensity); |
| 151 | + } |
| 152 | + `); |
| 153 | + |
| 154 | + // WGSL equivalent declarations |
| 155 | + pc.ShaderChunks.get(device, pc.SHADERLANGUAGE_WGSL).set('composeDeclarationsPS', ` |
| 156 | + uniform pixelationTilePixels: f32; |
| 157 | + uniform pixelationIntensity: f32; |
| 158 | + fn pixelateResult(color: vec3f, uv: vec2f, invRes: vec2f) -> vec3f { |
| 159 | + let tileUV = vec2f(uniform.pixelationTilePixels, uniform.pixelationTilePixels) * invRes; |
| 160 | + let centerUv = (floor(uv / tileUV) + vec2f(0.5, 0.5)) * tileUV; |
| 161 | + let local = (uv - centerUv) / tileUV; |
| 162 | + let dist = length(local); |
| 163 | + let radius: f32 = 0.35; |
| 164 | + let edge: f32 = fwidth(dist) * 1.5; |
| 165 | + let mask: f32 = 1.0 - smoothstep(radius, radius + edge, dist); |
| 166 | + let dotResult = vec3f(0.0) * (1.0 - mask) + color * mask; |
| 167 | + return mix(color, dotResult, uniform.pixelationIntensity); |
| 168 | + } |
| 169 | + `); |
| 170 | + |
| 171 | + // Call the helper at the end of compose to apply on top of previous effects |
| 172 | + pc.ShaderChunks.get(device, pc.SHADERLANGUAGE_GLSL).set('composeMainEndPS', ` |
| 173 | + result = pixelateResult(result, uv, sceneTextureInvRes); |
| 174 | + `); |
| 175 | + |
| 176 | + // WGSL equivalent call |
| 177 | + pc.ShaderChunks.get(device, pc.SHADERLANGUAGE_WGSL).set('composeMainEndPS', ` |
| 178 | + result = pixelateResult(result, uv, uniform.sceneTextureInvRes); |
| 179 | + `); |
| 180 | + |
| 181 | + // ------ Custom render passes set up ------ |
| 182 | + |
| 183 | + const cameraFrame = new pc.CameraFrame(app, cameraEntity.camera); |
| 184 | + cameraFrame.rendering.samples = 4; |
| 185 | + cameraFrame.bloom.intensity = 0.03; |
| 186 | + cameraFrame.bloom.blurLevel = 7; |
| 187 | + cameraFrame.vignette.inner = 0.5; |
| 188 | + cameraFrame.vignette.outer = 1; |
| 189 | + cameraFrame.vignette.curvature = 0.5; |
| 190 | + cameraFrame.vignette.intensity = 0.8; |
| 191 | + |
| 192 | + cameraFrame.update(); |
| 193 | + |
| 194 | + // apply UI changes (tone mapping only) |
| 195 | + data.on('*:set', (/** @type {string} */ path, value) => { |
| 196 | + if (path === 'data.sceneTonemapping') { |
| 197 | + // postprocessing tone mapping |
| 198 | + cameraFrame.rendering.toneMapping = value; |
| 199 | + cameraFrame.update(); |
| 200 | + } |
| 201 | + |
| 202 | + if (path === 'data.pixelSize') { |
| 203 | + // global uniform for pixelation tile size |
| 204 | + device.scope.resolve('pixelationTilePixels').setValue(value); |
| 205 | + } |
| 206 | + |
| 207 | + if (path === 'data.pixelationIntensity') { |
| 208 | + device.scope.resolve('pixelationIntensity').setValue(value); |
| 209 | + } |
| 210 | + }); |
| 211 | + |
| 212 | + // set initial values |
| 213 | + data.set('data', { |
| 214 | + sceneTonemapping: pc.TONEMAP_NEUTRAL, |
| 215 | + pixelSize: 8, |
| 216 | + pixelationIntensity: 0.5 |
| 217 | + }); |
| 218 | +}); |
| 219 | + |
| 220 | +export { app }; |
0 commit comments