Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Post processing #3948

Merged
merged 58 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
4c26d5c
Draft, began working on mixin and new rendering
Beilinson Nov 8, 2022
0259d36
first attempt at mixin and rendering with only bloom effect
Beilinson Nov 8, 2022
af50f05
updated imports
Beilinson Nov 10, 2022
979993a
Merge branch 'master' into post-processing
Beilinson Nov 11, 2022
7153cc6
first attempt: working, but globally
Beilinson Nov 11, 2022
206dcaf
switched post-processing libraries
Beilinson Nov 12, 2022
2f6268b
better default settings
Beilinson Nov 12, 2022
acda054
First iteration of enabling/disabling effects per scene
Beilinson Nov 18, 2022
fc85c9a
Merge branch 'master' into post-processing
Beilinson Nov 29, 2022
05f91f5
added effect map
Beilinson Nov 29, 2022
5f9c53b
Added basic docs
Beilinson Nov 29, 2022
f4d27a2
revert scenegraph docs changes
Beilinson Nov 29, 2022
dd4e1f9
stash before pivot to webcomponent composition
Beilinson Mar 6, 2023
e8328f8
effects-composer, pixelate-effect webcomponents
Beilinson Mar 6, 2023
189925f
Created <model-viewer-effects> library, fixed bugs
Beilinson Mar 12, 2023
dcdfaa2
Merge branch 'post-processing' into master
Beilinson Mar 12, 2023
acf1d76
'master' -> 'postprocessing'
Beilinson Mar 12, 2023
c606f4b
updated package.json
Beilinson Mar 12, 2023
65da665
Tests passing, dirtyRender for glitch, better custom Passes
Beilinson Mar 14, 2023
a1765bd
added prepare back
Beilinson Mar 14, 2023
dd8255c
Improved Glitch API, added BlendMode mixin
Beilinson Mar 15, 2023
03dba81
removed prefix, fixed SSAOEffect
Beilinson Mar 16, 2023
ccc6fd5
Fixed SSAOEffect with transparency, tested three importmap
Beilinson Mar 18, 2023
9e2fb95
remove goldens from mve
Beilinson Mar 23, 2023
1e6ca51
Added no-three bundle to rollup, added test for image similarity
Beilinson Mar 23, 2023
8739e82
Better examples, easier to create custom effect component
Beilinson Mar 24, 2023
dc56b61
.
Beilinson Mar 24, 2023
8956b29
fixed example
Beilinson Mar 24, 2023
44ff444
updated readme
Beilinson Mar 24, 2023
a5c9f2f
depthWrite
Beilinson Mar 25, 2023
1141adc
added beforeRender callback
Beilinson Mar 25, 2023
090a8ff
removed accidental styling changes from model-viewer
Beilinson Mar 25, 2023
1bcce32
fixed double import issue
Beilinson Mar 27, 2023
b3571d1
CR fixes, added selective bloom example with rocketship
Beilinson Mar 30, 2023
95c9751
removed browserstack from karma conf
Beilinson Mar 30, 2023
af04a52
improved screenshot tests
Beilinson Mar 30, 2023
83b8d7b
Added selective bloom effect
Beilinson Mar 30, 2023
eefa555
disconnect bug, rename to @google
Beilinson Mar 31, 2023
86dc6eb
updated versions, removed unneeded comments
Beilinson Apr 3, 2023
53d841c
Bootstrap works locally
Beilinson Apr 3, 2023
1127d51
Merge branch 'master' of github.com:google/model-viewer into post-pro…
elalish Apr 3, 2023
046d71b
updating dependencies
elalish Apr 3, 2023
ef0e150
Tests passing, fixed bugs
Beilinson Apr 4, 2023
9c5d660
added mve-docs, added msaa to effect-composer
Beilinson Apr 4, 2023
d8e5baf
Completed docs, fixed bugs
Beilinson Apr 5, 2023
9534d81
minor styling
Beilinson Apr 5, 2023
1e37d9b
fixed test
Beilinson Apr 5, 2023
f1bc4fb
Merge branch 'master' into post-processing
Beilinson Apr 5, 2023
03d8b39
removed fxaa effect
Beilinson Apr 5, 2023
d2832dd
Merge branch 'post-processing' of https://github.com/Beilinson/model-…
Beilinson Apr 5, 2023
3f5fb42
update deps
Beilinson Apr 5, 2023
21ad92a
improved string literal type safety
Beilinson Apr 6, 2023
c4574de
removed clearPass, fixed "empty" effectComposer
Beilinson Apr 7, 2023
b4902ef
tonemapping + example (fixes bloom color)
Beilinson Apr 7, 2023
6196d1a
minor fixes all around
Beilinson Apr 12, 2023
ac77514
fixed package-lock
Beilinson Apr 15, 2023
f317808
Merge branch 'master' into post-processing
elalish Apr 19, 2023
b9c8a4e
Merge branch 'master' into post-processing
elalish Apr 25, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions packages/model-viewer/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions packages/model-viewer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@
"3d"
],
"dependencies": {
"three": "^0.146.0",
"lit": "^2.2.3"
"lit": "^2.2.3",
"postprocessing": "^6.29.1",
Beilinson marked this conversation as resolved.
Show resolved Hide resolved
"three": "^0.146.0"
},
"devDependencies": {
"@open-wc/karma-esm": "^4.0.0",
Expand Down
61 changes: 61 additions & 0 deletions packages/model-viewer/src/features/post-processing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* @license
* Copyright 2019 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { property } from 'lit/decorators.js';
import { EffectPass } from 'postprocessing';

import ModelViewerElementBase, { $needsRender } from '../model-viewer-base.js';
import { BLOOM_PASS, FXAA_PASS, OUTLINE_PASS, PIXELATE_PASS, SSAO_PASS } from '../three-components/PPRenderPipeline.js';
import { Constructor } from '../utilities.js';

export declare interface PostProcessingInterface {
fxaaEffect: boolean
ssaoEffect: boolean;
bloomEffect: boolean;
pixelateEffect: boolean;
outlineEffect: boolean;
}

export const EffectMap = new Map<string, EffectPass>([
['fxaaEffect', FXAA_PASS],
['ssaoEffect', SSAO_PASS],
['bloomEffect', BLOOM_PASS],
['outlineEffect', OUTLINE_PASS],
['pixelateEffect', PIXELATE_PASS],
]);

export const PostProcessingMixin = <T extends Constructor<ModelViewerElementBase>>(
ModelViewerElement: T): Constructor<PostProcessingInterface>&T => {
class PostProcessingModelViewerElement extends ModelViewerElement {
@property({type: Boolean, attribute: 'fxaa-effect'})
fxaaEffect: boolean = false;
@property({type: Boolean, attribute: 'ssao-effect'})
ssaoEffect: boolean = false;
@property({type: Boolean, attribute: 'bloom-effect'})
bloomEffect: boolean = false;
@property({type: Boolean, attribute: 'pixelate-effect'})
pixelateEffect: boolean = false;
@property({type: Boolean, attribute: 'outline-effect'})
outlineEffect: boolean = false;

updated(changedProperties: Map<string|number|symbol, unknown>) {
super.updated(changedProperties);
// TODO: check if any of the properties were updated, then re-render
this[$needsRender]()
}
}

return PostProcessingModelViewerElement;
};
5 changes: 3 additions & 2 deletions packages/model-viewer/src/model-viewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ import {SceneGraphMixin} from './features/scene-graph.js';
import {StagingMixin} from './features/staging.js';
import ModelViewerElementBase from './model-viewer-base.js';
import {FocusVisiblePolyfillMixin} from './utilities/focus-visible.js';
import {PostProcessingMixin} from './features/post-processing.js';

// Uncomment these lines to export PMREM textures in Glitch:
// export {default as TextureUtils} from './three-components/TextureUtils';
// export * from 'three';

export const ModelViewerElement = AnnotationMixin(SceneGraphMixin(StagingMixin(
export const ModelViewerElement = PostProcessingMixin(AnnotationMixin(SceneGraphMixin(StagingMixin(
EnvironmentMixin(ControlsMixin(ARMixin(LoadingMixin(AnimationMixin(
FocusVisiblePolyfillMixin(ModelViewerElementBase)))))))));
FocusVisiblePolyfillMixin(ModelViewerElementBase))))))))));

export type ModelViewerElement = InstanceType<typeof ModelViewerElement>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ export default class EnvironmentScene extends Scene {
box6.scale.set(3.875, 3.487, 2.986);
this.add(box6);


// -x right
const light1 = new Mesh(geometry, this.createAreaLightMaterial(50));
light1.position.set(-16.116, 14.37, 8.208);
Expand Down
46 changes: 46 additions & 0 deletions packages/model-viewer/src/three-components/PPRenderPipeline.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { EffectComposer, RenderPass, EffectPass, BloomEffect, BlendFunction, KernelSize, FXAAEffect, SSAOEffect, PixelationEffect, OutlineEffect, ToneMappingEffect, ToneMappingMode } from 'postprocessing';
import { WebGLRenderer } from 'three';

export const RENDER_PASS = new RenderPass(null, null);

export const FXAA_EFFECT = new FXAAEffect();

export const SSAO_EFFECT = new SSAOEffect();

export const BLOOM_EFFECT = new BloomEffect({
blendFunction: BlendFunction.ADD,
mipmapBlur: true,
luminanceThreshold: 0.85,
luminanceSmoothing: 0.025,
intensity: 3,
kernelSize: KernelSize.LARGE
Beilinson marked this conversation as resolved.
Show resolved Hide resolved
});

export const PIXELATE_EFFECt = new PixelationEffect(6);

export const OUTLINE_EFFECT = new OutlineEffect();
// export const TONEMAPPING_EFFECT = new ToneMappingEffect({mode: ToneMappingMode.OPTIMIZED_CINEON});

export const FXAA_PASS = new EffectPass(null, FXAA_EFFECT);
export const SSAO_PASS = new EffectPass(null, SSAO_EFFECT);
export const BLOOM_PASS = new EffectPass(null, BLOOM_EFFECT);
export const OUTLINE_PASS = new EffectPass(null, OUTLINE_EFFECT);
export const PIXELATE_PASS = new EffectPass(null, PIXELATE_EFFECt);

FXAA_PASS.enabled = false;
SSAO_PASS.enabled = false;
BLOOM_PASS.enabled = false;
OUTLINE_PASS.enabled = false;
PIXELATE_PASS.enabled = false;

export const CreateEffectComposer = (threeRenderer: WebGLRenderer) => {
const effectComposer = new EffectComposer(threeRenderer);

effectComposer.addPass(RENDER_PASS);
effectComposer.addPass(FXAA_PASS);
effectComposer.addPass(SSAO_PASS);
effectComposer.addPass(BLOOM_PASS);
effectComposer.addPass(OUTLINE_PASS);
effectComposer.addPass(PIXELATE_PASS);
// effectComposer.addPass(POST_PROCESSING_PIPELINE);
};
27 changes: 25 additions & 2 deletions packages/model-viewer/src/three-components/Renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/

import {ACESFilmicToneMapping, Event, EventDispatcher, sRGBEncoding, Vector2, WebGLRenderer} from 'three';
import { RenderPass, EffectComposer } from 'postprocessing';

import {$updateEnvironment} from '../features/environment.js';
import {ModelViewerGlobalConfig} from '../features/loading.js';
Expand All @@ -26,6 +27,8 @@ import {Debugger} from './Debugger.js';
import {ModelViewerGLTFInstance} from './gltf-instance/ModelViewerGLTFInstance.js';
import {ModelScene} from './ModelScene.js';
import TextureUtils from './TextureUtils.js';
import { CreateEffectComposer, RENDER_PASS } from './PPRenderPipeline.js';
import { EffectMap } from '../features/post-processing.js';

export interface RendererOptions {
powerPreference: string;
Expand Down Expand Up @@ -92,6 +95,8 @@ export class Renderer extends EventDispatcher {

public threeRenderer!: WebGLRenderer;
public canvas3D: HTMLCanvasElement;
public renderPass!: RenderPass;
public effectComposer!: EffectComposer;
public textureUtils: TextureUtils|null;
public arRenderer: ARRenderer;
public loader = new CachingGLTFLoader(ModelViewerGLTFInstance);
Expand Down Expand Up @@ -143,10 +148,13 @@ export class Renderer extends EventDispatcher {
alpha: true,
antialias: true,
powerPreference: options.powerPreference as WebGLPowerPreference,
preserveDrawingBuffer: true
preserveDrawingBuffer: true,
stencil: false,
depth: false
});
this.threeRenderer.autoClear = true;
this.threeRenderer.outputEncoding = sRGBEncoding;

this.threeRenderer.physicallyCorrectLights = true;
this.threeRenderer.setPixelRatio(1); // handle pixel ratio externally

Expand All @@ -156,6 +164,9 @@ export class Renderer extends EventDispatcher {
// ACESFilmicToneMapping appears to be the most "saturated",
// and similar to Filament's gltf-viewer.
this.threeRenderer.toneMapping = ACESFilmicToneMapping;

this.effectComposer = CreateEffectComposer(this.threeRenderer);
this.renderPass = RENDER_PASS;
} catch (error) {
console.warn(error);
}
Expand Down Expand Up @@ -281,6 +292,10 @@ export class Renderer extends EventDispatcher {

if (this.canRender) {
this.threeRenderer.setSize(width, height, false);
this.effectComposer.setSize(width, height, false);
this.renderPass.setSize(width, height);
this.effectComposer.passes[1].setSize(width, height);
// this.effectComposer.setSize(width, height);
}

// Each scene's canvas must match the renderer size. In general they can be
Expand Down Expand Up @@ -418,6 +433,14 @@ export class Renderer extends EventDispatcher {
const exposureIsNumber =
typeof exposure === 'number' && !Number.isNaN(exposure);
this.threeRenderer.toneMappingExposure = exposureIsNumber ? exposure : 1.0;

for (const [effectName, pass] of EffectMap.entries()) {
pass.enabled = scene[effectName];
}
for (const pass of this.effectComposer.passes) {
pass.mainScene = scene;
pass.mainCamera = scene.getCamera();
}
}

render(t: number, frame?: XRFrame) {
Expand Down Expand Up @@ -491,7 +514,7 @@ export class Renderer extends EventDispatcher {
this.threeRenderer.setRenderTarget(null);
this.threeRenderer.setViewport(
0, Math.ceil(this.height * this.dpr) - height, width, height);
this.threeRenderer.render(scene, scene.camera);
this.effectComposer.render();

if (this.multipleScenesVisible || scene.renderCount === 0) {
this.copyPixels(scene, width, height);
Expand Down
4 changes: 2 additions & 2 deletions packages/model-viewer/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"sourceMap": true,
"inlineSources": true,
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noUnusedLocals": false,
"noUnusedParameters": false,
Beilinson marked this conversation as resolved.
Show resolved Hide resolved
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"outDir": "./lib",
Expand Down
30 changes: 30 additions & 0 deletions packages/modelviewer.dev/data/docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -1052,5 +1052,35 @@
]
}
]
},
{
"Title": "PostProcessing Effects",
"htmlName": "postprocessing",
"Attributes": [
Beilinson marked this conversation as resolved.
Show resolved Hide resolved
{
"name": "bloom-effect",
"htmlName": "bloomEffect",
"description": "Toggle the Bloom effect.",
"links": [
"<a href=\"../examples/postprocessing/#effects\">Related examples</a>"
]
},
{
"name": "ssao-effect",
"htmlName": "ssaoEffect",
"description": "Toggle the SSAO effect.",
"links": [
"<a href=\"../examples/postprocessing/#effects\">Related examples</a>"
]
},
{
"name": "pixelate-effect",
"htmlName": "pixelateEffect",
"description": "Toggle the Pixelate effect.",
"links": [
"<a href=\"../examples/postprocessing/#effects\">Related examples</a>"
]
},
]
}
]
12 changes: 11 additions & 1 deletion packages/modelviewer.dev/data/examples.json
Original file line number Diff line number Diff line change
Expand Up @@ -256,5 +256,15 @@
"name": "Exporter"
}
]
}
},
{
"name": "PostProcessing Effects",
"htmlName": "postprocessing",
"examples": [
{
"htmlId": "effects",
"name": "Toggle Effects"
}
]
}
]
1 change: 1 addition & 0 deletions packages/modelviewer.dev/docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<div id="lightingandenv-docs"></div>
<div id="animation-docs"></div>
<div id="scenegraph-docs"></div>
<div id="postprocessing-docs"></div>
<div class="footerDocumentation">
<div class="footer">
<div style="margin-top:24px;" class="copyright">©Copyright 2018-2020 Google Inc. Licensed under the Apache License 2.0.</div>
Expand Down
Loading