forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebgl_mirror_nodes.html
276 lines (208 loc) · 8.29 KB
/
webgl_mirror_nodes.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - mirror with nodes</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
color: #888888;
font-family:Monospace;
font-size:13px;
background-color: #000;
margin: 0px;
overflow: hidden;
}
#info {
position: absolute;
top: 0px;
width: 400px;
left: calc(50% - 200px);
text-align: center;
}
a {
color: #00f;
}
</style>
</head>
<body>
<div id="container"></div>
<div id="info"><a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - mirror node-based
</div>
<script src="../build/three.js"></script>
<script src="js/libs/dat.gui.min.js"></script>
<script src="js/objects/Reflector.js"></script>
<script src="js/objects/ReflectorRTT.js"></script>
<script src="js/controls/OrbitControls.js"></script>
<script type="module">
import './js/nodes/THREE.Nodes.js';
import './js/loaders/NodeMaterialLoader.js';
// scene size
var WIDTH = window.innerWidth;
var HEIGHT = window.innerHeight;
// camera
var VIEW_ANGLE = 45;
var ASPECT = WIDTH / HEIGHT;
var NEAR = 1;
var FAR = 500;
var decalNormal = new THREE.TextureLoader().load( 'textures/decal/decal-normal.jpg' );
var decalDiffuse = new THREE.TextureLoader().load( 'textures/decal/decal-diffuse.png' );
decalDiffuse.wrapS = decalDiffuse.wrapT = THREE.RepeatWrapping;
var camera, scene, renderer;
var clock = new THREE.Clock();
var cameraControls;
var gui = new dat.GUI();
var sphereGroup, smallSphere;
var groundMirrorMaterial;
var frame = new THREE.NodeFrame();
function init() {
// renderer
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( WIDTH, HEIGHT );
// scene
scene = new THREE.Scene();
// camera
camera = new THREE.PerspectiveCamera( VIEW_ANGLE, ASPECT, NEAR, FAR );
camera.position.set( 0, 75, 160 );
cameraControls = new THREE.OrbitControls( camera, renderer.domElement );
cameraControls.target.set( 0, 40, 0 );
cameraControls.maxDistance = 400;
cameraControls.minDistance = 10;
cameraControls.update();
var container = document.getElementById( 'container' );
container.appendChild( renderer.domElement );
}
function fillScene() {
var planeGeo = new THREE.PlaneBufferGeometry( 100.1, 100.1 );
// reflector/mirror plane
var geometry = new THREE.PlaneBufferGeometry( 100, 100 );
var groundMirror = new THREE.ReflectorRTT( geometry, { clipBias: 0.003, textureWidth: WIDTH, textureHeight: HEIGHT } );
var mask = new THREE.SwitchNode( new THREE.TextureNode( decalDiffuse ), 'w' );
var mirror = new THREE.ReflectorNode( groundMirror );
var normalMap = new THREE.TextureNode( decalNormal );
var normalXY = new THREE.SwitchNode( normalMap, 'xy' );
var normalXYFlip = new THREE.Math1Node(
normalXY,
THREE.Math1Node.INVERT
);
var offsetNormal = new THREE.OperatorNode(
normalXYFlip,
new THREE.FloatNode( .5 ),
THREE.OperatorNode.SUB
);
mirror.offset = new THREE.OperatorNode(
offsetNormal, // normal
new THREE.FloatNode( 6 ), // scale
THREE.OperatorNode.MUL
);
var blurMirror = new THREE.BlurNode( mirror );
blurMirror.size = new THREE.Vector2( WIDTH, HEIGHT );
blurMirror.uv = new THREE.ExpressionNode( "projCoord.xyz / projCoord.q", "vec3" );
blurMirror.uv.keywords[ "projCoord" ] = new THREE.OperatorNode( mirror.offset, mirror.uv, THREE.OperatorNode.ADD );
blurMirror.radius.x = blurMirror.radius.y = 0;
gui.add( { blur: blurMirror.radius.x }, "blur", 0, 25 ).onChange( function ( v ) {
blurMirror.radius.x = blurMirror.radius.y = v;
} );
groundMirrorMaterial = new THREE.PhongNodeMaterial();
groundMirrorMaterial.environment = blurMirror; // or add "mirror" variable to disable blur
groundMirrorMaterial.environmentAlpha = mask;
groundMirrorMaterial.normal = new THREE.NormalMapNode( normalMap );
//groundMirrorMaterial.normalScale = new THREE.FloatNode( 1 );
// test serialization
/*
var library = {};
library[ groundMirror.uuid ] = groundMirror;
library[ decalDiffuse.uuid ] = decalDiffuse;
library[ decalNormal.uuid ] = decalNormal;
library[ mirror.textureMatrix.uuid ] = mirror.textureMatrix; // use textureMatrix to projection
var json = groundMirrorMaterial.toJSON();
groundMirrorMaterial = new THREE.NodeMaterialLoader( null, library ).parse( json );
*/
//--
var mirrorMesh = new THREE.Mesh( planeGeo, groundMirrorMaterial );
// add all alternative mirror materials inside the ReflectorRTT to prevent:
// glDrawElements: Source and destination textures of the draw are the same.
groundMirror.add( mirrorMesh );
groundMirror.rotateX( - Math.PI / 2 );
scene.add( groundMirror );
sphereGroup = new THREE.Object3D();
scene.add( sphereGroup );
var geometry = new THREE.CylinderBufferGeometry( 0.1, 15 * Math.cos( Math.PI / 180 * 30 ), 0.1, 24, 1 );
var material = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x444444 } );
var sphereCap = new THREE.Mesh( geometry, material );
sphereCap.position.y = - 15 * Math.sin( Math.PI / 180 * 30 ) - 0.05;
sphereCap.rotateX( - Math.PI );
var geometry = new THREE.SphereBufferGeometry( 15, 24, 24, Math.PI / 2, Math.PI * 2, 0, Math.PI / 180 * 120 );
var halfSphere = new THREE.Mesh( geometry, material );
halfSphere.add( sphereCap );
halfSphere.rotateX( - Math.PI / 180 * 135 );
halfSphere.rotateZ( - Math.PI / 180 * 20 );
halfSphere.position.y = 7.5 + 15 * Math.sin( Math.PI / 180 * 30 );
sphereGroup.add( halfSphere );
var geometry = new THREE.IcosahedronBufferGeometry( 5, 0 );
var material = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x333333, flatShading: true } );
smallSphere = new THREE.Mesh( geometry, material );
scene.add( smallSphere );
// walls
var planeTop = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xffffff } ) );
planeTop.position.y = 100;
planeTop.rotateX( Math.PI / 2 );
scene.add( planeTop );
var planeBack = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xffffff } ) );
planeBack.position.z = - 50;
planeBack.position.y = 50;
scene.add( planeBack );
var planeFront = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x7f7fff } ) );
planeFront.position.z = 50;
planeFront.position.y = 50;
planeFront.rotateY( Math.PI );
scene.add( planeFront );
var planeRight = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x00ff00 } ) );
planeRight.position.x = 50;
planeRight.position.y = 50;
planeRight.rotateY( - Math.PI / 2 );
scene.add( planeRight );
var planeLeft = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xff0000 } ) );
planeLeft.position.x = - 50;
planeLeft.position.y = 50;
planeLeft.rotateY( Math.PI / 2 );
scene.add( planeLeft );
// lights
var mainLight = new THREE.PointLight( 0xcccccc, 1.5, 250 );
mainLight.position.y = 60;
scene.add( mainLight );
var greenLight = new THREE.PointLight( 0x00ff00, 0.25, 1000 );
greenLight.position.set( 550, 50, 0 );
scene.add( greenLight );
var redLight = new THREE.PointLight( 0xff0000, 0.25, 1000 );
redLight.position.set( - 550, 50, 0 );
scene.add( redLight );
var blueLight = new THREE.PointLight( 0x7f7fff, 0.25, 1000 );
blueLight.position.set( 0, 50, 550 );
scene.add( blueLight );
}
function render() {
renderer.render( scene, camera );
}
function update() {
requestAnimationFrame( update );
var delta = clock.getDelta();
var timer = Date.now() * 0.01;
sphereGroup.rotation.y -= 0.002;
smallSphere.position.set(
Math.cos( timer * 0.1 ) * 30,
Math.abs( Math.cos( timer * 0.2 ) ) * 20 + 5,
Math.sin( timer * 0.1 ) * 30
);
smallSphere.rotation.y = ( Math.PI / 2 ) - timer * 0.1;
smallSphere.rotation.z = timer * 0.8;
frame.update( delta ).updateNode( groundMirrorMaterial );
render();
}
init();
fillScene();
update();
</script>
</body>
</html>