forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebgpu_rtt.html
140 lines (92 loc) · 3.68 KB
/
webgpu_rtt.html
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
<html lang="en">
<head>
<title>three.js - WebGPU - RTT</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="main.css">
</head>
<body>
<div id="info">
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> WebGPU - RTT
</div>
<script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "../build/three.module.js",
"three/addons/": "./jsm/",
"three/nodes": "./jsm/nodes/Nodes.js"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import * as Nodes from 'three/nodes';
import WebGPU from 'three/addons/capabilities/WebGPU.js';
import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
import WebGPUTextureRenderer from 'three/addons/renderers/webgpu/WebGPUTextureRenderer.js';
let camera, scene, renderer;
const mouse = new THREE.Vector2();
let cameraFX, sceneFX, textureRenderer;
let box;
const dpr = window.devicePixelRatio;
init();
animate();
function init() {
if ( WebGPU.isAvailable() === false ) {
document.body.appendChild( WebGPU.getErrorMessage() );
throw new Error( 'No WebGPU support' );
}
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 10 );
camera.position.z = 4;
scene = new THREE.Scene();
scene.background = new THREE.Color( 0x222222 );
// textured mesh
const loader = new THREE.TextureLoader();
const texture = loader.load( './textures/uv_grid_opengl.jpg' );
const geometryBox = new THREE.BoxGeometry();
const materialBox = new Nodes.MeshBasicNodeMaterial();
materialBox.colorNode = new Nodes.TextureNode( texture );
//
box = new THREE.Mesh( geometryBox, materialBox );
scene.add( box );
//
renderer = new WebGPURenderer();
renderer.setPixelRatio( dpr );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
textureRenderer = new WebGPUTextureRenderer( renderer );
textureRenderer.setSize( window.innerWidth * dpr, window.innerHeight * dpr );
window.addEventListener( 'mousemove', onWindowMouseMove );
window.addEventListener( 'resize', onWindowResize );
// FX
cameraFX = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
sceneFX = new THREE.Scene();
const geometryFX = new THREE.PlaneGeometry( 2, 2 );
// modulate the final color based on the mouse position
const screenFXNode = new Nodes.OperatorNode( '+', new Nodes.UniformNode( mouse ), new Nodes.ConstNode( new THREE.Vector2( 0.5, 0.5 ) ) );
const materialFX = new Nodes.MeshBasicNodeMaterial();
materialFX.colorNode = new Nodes.OperatorNode( '*', new Nodes.TextureNode( textureRenderer.getTexture() ), screenFXNode );
const quad = new THREE.Mesh( geometryFX, materialFX );
sceneFX.add( quad );
}
function onWindowMouseMove( e ) {
mouse.x = e.offsetX / screen.width;
mouse.y = e.offsetY / screen.height;
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
textureRenderer.setSize( window.innerWidth * dpr, window.innerHeight * dpr );
}
function animate() {
requestAnimationFrame( animate );
box.rotation.x += 0.01;
box.rotation.y += 0.02;
textureRenderer.render( scene, camera );
renderer.render( sceneFX, cameraFX );
}
</script>
</body>
</html>