-
-
Notifications
You must be signed in to change notification settings - Fork 36.4k
Expand file tree
/
Copy pathwebgpu_hdr.html
More file actions
202 lines (154 loc) · 6.26 KB
/
webgpu_hdr.html
File metadata and controls
202 lines (154 loc) · 6.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
<title>three.js webgpu - HDR Draw</title>
<link type="text/css" rel="stylesheet" href="example.css">
<style>
body {
background-color: #fff;
}
#no-hdr {
position: absolute;
font-family: monospace;
font-size: 11px;
font-weight: normal;
text-align: center;
background: #000;
color: #fff;
left: 50%;
transform: translateX(-50%);
padding: 1.5em;
max-width: 600px;
margin: 5em auto 0;
}
</style>
</head>
<body>
<div id="info" class="invert">
<a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
<div class="title-wrapper">
<a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>HDR</span>
</div>
<small>
The demo produces a color output intended for HDR monitors.
</small>
</div>
<div id="no-hdr" style="display: none">
<div>
The browser says your device or monitor doesn't support HDR.<br />
If you're on a laptop using an external monitor, try the built in
monitor<br />
or, try this site on your phone. Most phones support HDR.
</div>
</div>
<script type="importmap">
{
"imports": {
"three": "../build/three.webgpu.js",
"three/webgpu": "../build/three.webgpu.js",
"three/tsl": "../build/three.tsl.js",
"three/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three/webgpu';
import { pass, uv, uniform } from 'three/tsl';
import WebGPU from 'three/addons/capabilities/WebGPU.js';
import { afterImage } from 'three/addons/tsl/display/AfterImageNode.js';
import { Inspector } from 'three/addons/inspector/Inspector.js';
import { ExtendedSRGBColorSpace, ExtendedSRGBColorSpaceImpl } from 'three/addons/math/ColorSpaces.js';
const params = {
intensity: uniform( 4.0, 'float' ).setName( 'intensity' ),
hardness: uniform( 0.4, 'float' ).setName( 'hardness' ),
radius: uniform( 0.5, 'float' ).setName( 'radius' ),
afterImageDecay: uniform( 0.985, 'float' ).setName( 'afterImageDecay' ),
};
const hdrMediaQuery = window.matchMedia( '(dynamic-range: high)' );
function updateHDRWarning() {
const displayIsHDR = hdrMediaQuery.matches;
document.querySelector( '#no-hdr' ).style.display = displayIsHDR ? 'none' : '';
}
hdrMediaQuery.addEventListener( 'change', updateHDRWarning );
updateHDRWarning();
if ( WebGPU.isAvailable() === false ) {
document.body.appendChild( WebGPU.getErrorMessage() );
throw new Error( 'No WebGPU support' );
}
// Enable Extended sRGB output color space for HDR presentation
THREE.ColorManagement.define( { [ ExtendedSRGBColorSpace ]: ExtendedSRGBColorSpaceImpl } );
// Renderer (HalfFloat output + Extended sRGB)
const renderer = new THREE.WebGPURenderer( {
antialias: true,
outputType: THREE.HalfFloatType,
} );
renderer.outputColorSpace = ExtendedSRGBColorSpace;
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.inspector = new Inspector();
document.body.appendChild( renderer.domElement );
const camera = new THREE.OrthographicCamera( 0, window.innerWidth, window.innerHeight, 0, 1, 2 );
camera.position.z = 1;
// Brush scene (rendered into drawTarget)
const brushScene = new THREE.Scene();
brushScene.background = new THREE.Color( 0xffffff );
const brushMat = new THREE.MeshBasicNodeMaterial();
brushMat.transparent = true;
brushMat.depthTest = false;
brushMat.depthWrite = false;
brushMat.blending = THREE.AdditiveBlending; // additive to build HDR energy
const renderPipeline = new THREE.RenderPipeline( renderer );
const brushPass = pass( brushScene, camera, { type: THREE.HalfFloatType } );
brushPass.renderTarget.texture.colorSpace = ExtendedSRGBColorSpace;
renderPipeline.outputNode = afterImage( brushPass, params.afterImageDecay );
// HDR brush uniforms
const uColor = params.intensity;
const uHard = params.hardness;
const uRadius = params.radius;
// Radial falloff in TSL
const d = uv().sub( 0.5 ).length();
const t = d.div( uRadius );
const a = t.clamp().oneMinus().pow( uHard.mul( 8.0 ).add( 1.0 ) );
brushMat.colorNode = uColor.mul( a );
brushMat.opacityNode = a; // premultiplied style with additive blending
const brushMesh = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), brushMat );
brushMesh.scale.set( 300, 300, 1 ); // ~300px default brush size
brushScene.add( brushMesh );
function onPointerMove( e ) {
const rect = renderer.domElement.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
// camera has origin at bottom-left (0,0)
brushMesh.position.set( x, window.innerHeight - y, 0 );
}
window.addEventListener( 'pointermove', onPointerMove, { passive: false } );
// Prevent mobile scroll on touch
renderer.domElement.addEventListener( 'touchstart', ( e ) => e.preventDefault(), { passive: false } );
renderer.domElement.addEventListener( 'touchmove', ( e ) => e.preventDefault(), { passive: false } );
renderer.domElement.addEventListener( 'touchend', ( e ) => e.preventDefault(), { passive: false } );
// GUI setup
const gui = renderer.inspector.createParameters( 'Settings' );
const colorFolder = gui.addFolder( 'HDR' );
colorFolder.add( params.intensity, 'value', 0, 10, 0.1 ).name( 'Intensity' );
const brushFolder = gui.addFolder( 'Brush Settings' );
brushFolder.add( params.hardness, 'value', 0, 0.99, 0.01 ).name( 'Hardness' );
brushFolder.add( params.radius, 'value', 0.1, 2.0, 0.01 ).name( 'Radius' );
const effectFolder = gui.addFolder( 'Effects' );
effectFolder.add( params.afterImageDecay, 'value', 0.9, 0.999, 0.001 ).name( 'After Image Decay' );
// Resize handling
function onResize() {
renderer.setSize( window.innerWidth, window.innerHeight );
camera.right = window.innerWidth;
camera.top = window.innerHeight;
camera.updateProjectionMatrix();
}
window.addEventListener( 'resize', onResize );
// Main loop
renderer.setAnimationLoop( async () => {
renderPipeline.render();
} );
</script>
</body>
</html>