forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebgl_materials_compile.html
259 lines (173 loc) · 5.3 KB
/
webgl_materials_compile.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - node material</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: #fff;
font-family:Monospace;
font-size:13px;
margin: 0px;
text-align:center;
overflow: hidden;
}
#info {
color: #fff;
position: absolute;
top: 10px;
width: 100%;
text-align: center;
display:block;
}
#waitScreen {
color: #000;
position: absolute;
top: 50%;
left: 50%;
margin-top: -50px;
margin-left: -50px;
width: 100px;
height: 100px;
}
.hide {
display:none;
}
a { color: white }
</style>
</head>
<body>
<div id="container"></div>
<div id="info">
<a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - Node-Based Material
<br /><span class="button" id="preload">change preload</span>
</div>
<div id="waitScreen">
Loading ...
</div>
<script src="../build/three.js"></script>
<script src='js/geometries/TeapotBufferGeometry.js'></script>
<script src="js/controls/OrbitControls.js"></script>
<script src="js/libs/dat.gui.min.js"></script>
<script type="module">
import './js/nodes/THREE.Nodes.js';
var container = document.getElementById( 'container' );
var renderer, scene, camera, clock = new THREE.Clock(), fov = 50;
var frame = new THREE.NodeFrame();
var teapot;
var controls;
var rtTexture, rtMaterial;
var meshes = [];
document.getElementById( "preload" ).addEventListener( 'click', function () {
var hash = document.location.hash.substr( 1 );
if ( hash.length === 0 ) {
window.location.hash = "#NoPreLoad";
} else {
window.location.hash = "";
}
location.reload( true );
}, false );
window.addEventListener( 'load', init );
function init() {
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera( fov, window.innerWidth / window.innerHeight, 1, 1000 );
camera.position.x = 0;
camera.position.z = - 300;
camera.position.y = 200;
camera.target = new THREE.Vector3();
controls = new THREE.OrbitControls( camera, renderer.domElement );
controls.minDistance = 50;
controls.maxDistance = 400;
scene.add( new THREE.AmbientLight( 0x464646 ) );
var light = new THREE.DirectionalLight( 0xffddcc, 1 );
light.position.set( 1, 0.75, 0.5 );
scene.add( light );
var light = new THREE.DirectionalLight( 0xccccff, 1 );
light.position.set( - 1, 0.75, - 0.5 );
scene.add( light );
teapot = new THREE.TeapotBufferGeometry( 15, 18 );
var itemsonrow = 10;
for ( var i = 0; i < itemsonrow * itemsonrow; i ++ ) {
var mesh = new THREE.Mesh( teapot );
mesh.position.x = 50 * ( i % itemsonrow ) - 50 * itemsonrow / 2;
mesh.position.z = 50 * Math.floor( i / itemsonrow ) - 150;
updateMaterial( mesh );
scene.add( mesh );
meshes.push( mesh );
}
window.addEventListener( 'resize', onWindowResize, false );
var hash = document.location.hash.substr( 1 );
if ( hash.length === 0 ) {
renderer.compile( scene, camera );
}
document.getElementById( "waitScreen" ).className = "hide";
setTimeout( function () {
onWindowResize();
animate();
}, 1 );
}
function updateMaterial( mesh ) {
if ( mesh.material ) mesh.material.dispose();
if ( rtTexture ) {
rtTexture.dispose();
rtTexture = null;
}
if ( rtMaterial ) {
rtMaterial.dispose();
rtMaterial = null;
}
var mtl = new THREE.PhongNodeMaterial();
var time = new THREE.TimerNode();
var speed = new THREE.FloatNode( Math.random() );
var color = new THREE.ColorNode( Math.random() * 0xFFFFFF );
var timeSpeed = new THREE.OperatorNode(
time,
speed,
THREE.OperatorNode.MUL
);
var sinCycleInSecs = new THREE.OperatorNode(
timeSpeed,
new THREE.ConstNode( THREE.ConstNode.PI2 ),
THREE.OperatorNode.MUL
);
var cycle = new THREE.Math1Node( sinCycleInSecs, THREE.Math1Node.SIN );
var cycleColor = new THREE.OperatorNode(
cycle,
color,
THREE.OperatorNode.MUL
);
var cos = new THREE.Math1Node( cycleColor, THREE.Math1Node.SIN );
mtl.color = new THREE.ColorNode( 0 );
mtl.emissive = cos;
var transformer = new THREE.ExpressionNode( "position + 0.0 * " + Math.random(), "vec3", [ ] );
mtl.transform = transformer;
// build shader ( ignore auto build )
mtl.build();
// set material
mesh.material = mtl;
}
function onWindowResize() {
var width = window.innerWidth, height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize( width, height );
if ( rtTexture ) rtTexture.setSize( width, height );
}
function animate() {
var delta = clock.getDelta();
frame.update( delta );
for ( var i = 0; i < meshes.length; i ++ ) {
var mesh = meshes[ i ];
frame.updateNode( mesh.material );
}
renderer.render( scene, camera );
requestAnimationFrame( animate );
}
</script>
</body>
</html>