forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebgpu_compute_texture.html
134 lines (88 loc) · 3.34 KB
/
webgpu_compute_texture.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
<html lang="en">
<head>
<title>three.js - WebGPU - Compute Texture</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 - Compute Texture
</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';
import { texture, textureStore, Fn, instanceIndex, float, uvec2, vec4 } from 'three/tsl';
import WebGPU from 'three/addons/capabilities/WebGPU.js';
let camera, scene, renderer;
init();
render();
function init() {
if ( WebGPU.isAvailable() === false ) {
document.body.appendChild( WebGPU.getErrorMessage() );
throw new Error( 'No WebGPU support' );
}
const aspect = window.innerWidth / window.innerHeight;
camera = new THREE.OrthographicCamera( - aspect, aspect, 1, - 1, 0, 2 );
camera.position.z = 1;
scene = new THREE.Scene();
// texture
const width = 512, height = 512;
const storageTexture = new THREE.StorageTexture( width, height );
//storageTexture.minFilter = THREE.LinearMipMapLinearFilter;
// create function
const computeTexture = Fn( ( { storageTexture } ) => {
const posX = instanceIndex.mod( width );
const posY = instanceIndex.div( width );
const indexUV = uvec2( posX, posY );
// https://www.shadertoy.com/view/Xst3zN
const x = float( posX ).div( 50.0 );
const y = float( posY ).div( 50.0 );
const v1 = x.sin();
const v2 = y.sin();
const v3 = x.add( y ).sin();
const v4 = x.mul( x ).add( y.mul( y ) ).sqrt().add( 5.0 ).sin();
const v = v1.add( v2, v3, v4 );
const r = v.sin();
const g = v.add( Math.PI ).sin();
const b = v.add( Math.PI ).sub( 0.5 ).sin();
textureStore( storageTexture, indexUV, vec4( r, g, b, 1 ) ).toWriteOnly();
} );
// compute
const computeNode = computeTexture( { storageTexture } ).compute( width * height );
const material = new THREE.MeshBasicNodeMaterial( { color: 0x00ff00 } );
material.colorNode = texture( storageTexture );
const plane = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), material );
scene.add( plane );
renderer = new THREE.WebGPURenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
// compute texture
renderer.computeAsync( computeNode );
window.addEventListener( 'resize', onWindowResize );
}
function onWindowResize() {
renderer.setSize( window.innerWidth, window.innerHeight );
const aspect = window.innerWidth / window.innerHeight;
const frustumHeight = camera.top - camera.bottom;
camera.left = - frustumHeight * aspect / 2;
camera.right = frustumHeight * aspect / 2;
camera.updateProjectionMatrix();
render();
}
function render() {
renderer.renderAsync( scene, camera );
}
</script>
</body>
</html>