forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebgl2_multisampled_renderbuffers.html
182 lines (126 loc) · 4.7 KB
/
webgl2_multisampled_renderbuffers.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js WebGL 2 - Multisampled Renderbuffers</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">
<style>
body {
background-color: #fff;
color: #222;
}
a {
color: #08f;
}
#container {
position: absolute;
top: 70px;
width: 100%;
bottom: 0px;
}
</style>
</head>
<body>
<div id="info">
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - Multisampled Renderbuffers<br />
Left: WebGLRenderTarget, Right: WebGLMultisampleRenderTarget.
</div>
<div id="container">
</div>
<script type="module">
import * as THREE from '../build/three.module.js';
import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
import { RenderPass } from './jsm/postprocessing/RenderPass.js';
import { ShaderPass } from './jsm/postprocessing/ShaderPass.js';
import { CopyShader } from './jsm/shaders/CopyShader.js';
import { WEBGL } from './jsm/WebGL.js';
if ( WEBGL.isWebGL2Available() === false ) {
document.body.appendChild( WEBGL.getWebGL2ErrorMessage() );
}
var camera, scene, renderer, clock, group, container;
var composer1, composer2;
init();
animate();
function init() {
container = document.getElementById( 'container' );
camera = new THREE.PerspectiveCamera( 45, container.offsetWidth / container.offsetHeight, 1, 2000 );
camera.position.z = 500;
scene = new THREE.Scene();
scene.background = new THREE.Color( 0xffffff );
scene.fog = new THREE.Fog( 0xcccccc, 100, 1500 );
clock = new THREE.Clock();
//
var hemiLight = new THREE.HemisphereLight( 0xffffff, 0x222222, 1.5 );
hemiLight.position.set( 1, 1, 1 );
scene.add( hemiLight );
//
group = new THREE.Group();
var geometry = new THREE.SphereBufferGeometry( 10, 64, 40 );
var material = new THREE.MeshLambertMaterial( { color: 0xee0808 } );
var material2 = new THREE.MeshBasicMaterial( { color: 0xffffff, wireframe: true } );
for ( var i = 0; i < 10; i ++ ) {
var mesh = new THREE.Mesh( geometry, material );
mesh.position.x = Math.random() * 600 - 300;
mesh.position.y = Math.random() * 600 - 300;
mesh.position.z = Math.random() * 600 - 300;
mesh.rotation.x = Math.random();
mesh.rotation.z = Math.random();
mesh.scale.setScalar( Math.random() * 5 + 5 );
group.add( mesh );
var mesh2 = new THREE.Mesh( geometry, material2 );
mesh2.position.copy( mesh.position );
mesh2.rotation.copy( mesh.rotation );
mesh2.scale.copy( mesh.scale );
group.add( mesh2 );
}
scene.add( group );
//
var canvas = document.createElement( 'canvas' );
var context = canvas.getContext( 'webgl2', { alpha: false, antialias: false } );
renderer = new THREE.WebGLRenderer( { canvas: canvas, context: context } );
renderer.autoClear = false;
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( container.offsetWidth, container.offsetHeight );
container.appendChild( renderer.domElement );
//
var parameters = {
format: THREE.RGBFormat,
stencilBuffer: false
};
var size = renderer.getDrawingBufferSize( new THREE.Vector2() );
var renderTarget = new THREE.WebGLMultisampleRenderTarget( size.width, size.height, parameters );
var renderPass = new RenderPass( scene, camera );
var copyPass = new ShaderPass( CopyShader );
//
composer1 = new EffectComposer( renderer );
composer1.addPass( renderPass );
composer1.addPass( copyPass );
//
composer2 = new EffectComposer( renderer, renderTarget );
composer2.addPass( renderPass );
composer2.addPass( copyPass );
//
window.addEventListener( 'resize', onWindowResize, false );
}
function onWindowResize() {
camera.aspect = container.offsetWidth / container.offsetHeight;
camera.updateProjectionMatrix();
renderer.setSize( container.offsetWidth, container.offsetHeight );
composer1.setSize( container.offsetWidth, container.offsetHeight );
composer2.setSize( container.offsetWidth, container.offsetHeight );
}
function animate() {
requestAnimationFrame( animate );
var halfWidth = container.offsetWidth / 2;
group.rotation.y += clock.getDelta() * 0.1;
renderer.setScissorTest( true );
renderer.setScissor( 0, 0, halfWidth - 1, container.offsetHeight );
composer1.render();
renderer.setScissor( halfWidth, 0, halfWidth, container.offsetHeight );
composer2.render();
renderer.setScissorTest( false );
}
</script>
</body>
</html>