-
Notifications
You must be signed in to change notification settings - Fork 1
/
shaders.html
executable file
·199 lines (148 loc) · 5.08 KB
/
shaders.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
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript" src="three.js"></script>
<script type="text/javascript" src="dat.gui.min.js"></script>
<script src="TrackballControls.js"></script>
</head>
<body style="background:#ececec; margin:0px; padding:0px" onmousemove="update()">
<div id="container"></div>
<!-- ----- MAIN THREE.JS CODE ----- -->
<script type="text/javascript">
var camera, scene, renderer, container;
var light, ambientLight, pointLight, geometry, mesh, material;
var basic, lambert, phong;
function start() {
container = document.getElementById( 'container' );
// --- WebGl renderer
try {
renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.autoClear = false;
container.appendChild( renderer.domElement );
}
catch (e) {
alert(e);
}
scene = new THREE.Scene();
// --- Camera
var fov = 15; // camera field-of-view in degrees
var width = renderer.domElement.width;
var height = renderer.domElement.height;
var aspect = width / height; // view aspect ratio
camera = new THREE.PerspectiveCamera( fov, aspect );
camera.position.x = 300;
camera.position.y = -100;
camera.position.z = -1200;
camera.lookAt(scene.position);
camera.updateMatrix();
// set camera range for depth shader min and max
camera.near = 1100;
camera.far = 1300;
controls = new THREE.TrackballControls( camera, renderer.domElement );
controls.rotateSpeed = 3.0;
controls.zoomSpeed = 1.2;
controls.panSpeed = 0.8;
controls.noZoom = false;
controls.noPan = false;
controls.staticMoving = true;
controls.dynamicDampingFactor = 0.3;
controls.addEventListener( 'change', render );
// --- Lights
ambientLight = new THREE.AmbientLight( 0x050505, 0.0 );
scene.add( ambientLight );
pointLight = new THREE.PointLight( 0xffffff, 1.0 );
scene.add( pointLight );
pointLight.position.set(0, 100, -200);
var sphere = new THREE.SphereGeometry( 100, 8, 8 );
light = new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color:0xffffff } ) );
light.position = pointLight.position;
light.scale.x = light.scale.y = light.scale.z = 0.05;
scene.add(light);
// --- Material
basic = new THREE.MeshBasicMaterial( { color: 0x666666 } );
lambert = new THREE.MeshLambertMaterial( );
phong = new THREE.MeshPhongMaterial( { specular: 0xffffff } );
depth = new THREE.MeshDepthMaterial( { specular: 0xffffff } );
normal = new THREE.MeshNormalMaterial( { specular: 0xffffff } );
wireframe = new THREE.MeshBasicMaterial( { color: 0x666666, wireframe: true } );
// --- Geometry
geometry = new THREE.SphereGeometry(100, 16, 16);
if(typeof mesh != 'undefined') scene.remove(mesh);
mesh = new THREE.Mesh( geometry, basic );
scene.add(mesh);
setInterval(function() {
mesh.rotation.y += .01;
update();
}, 30);
update();
}
function shade(shader) {
if (shader == "basic") material = basic;
else if (shader == "lambert") material = lambert;
else if (shader == "phong") material = phong;
else if (shader == "depth") material = depth;
else if (shader == "normal") material = normal;
else if (shader == "wireframe") material = wireframe;
// check to make sure variables are defined
if (typeof mesh != "undefined") scene.remove(mesh);
geometry = new THREE.SphereGeometry(100, 16, 16);
mesh = new THREE.Mesh( geometry, material );
if (typeof scene != "undefined") scene.add(mesh);
update();
}
function update() {
render();
controls.update(); // trackball interaction
}
function render() {
renderer.clear();
renderer.render(scene, camera);
}
function log(n) {
console.log(n);
}
//
// GUI
//
var initDisp = function() {
this.basic = function() {
shade("basic");
}
this.lambert = function() {
shade("lambert");
}
this.phong = function() {
shade("phong");
}
this.depth = function() {
shade("depth");
}
this.normal = function() {
shade("normal");
}
this.wireframe = function() {
shade("wireframe");
}
}
window.onload = function() {
var disp = new initDisp();
var gui = new dat.GUI();
basic = new THREE.MeshBasicMaterial( { color: 0x666666 } );
lambert = new THREE.MeshLambertMaterial( );
phong = new THREE.MeshPhongMaterial( { specular: 0xffffff } );
depth = new THREE.MeshDepthMaterial( { specular: 0xffffff } );
normal = new THREE.MeshNormalMaterial( { specular: 0xffffff } );
wireframe = new THREE.MeshBasicMaterial( { color: 0x666666, wireframe: true } );
gui.add(disp, "basic");
gui.add(disp, "lambert");
gui.add(disp, "phong");
gui.add(disp, "depth");
gui.add(disp, "normal");
gui.add(disp, "wireframe");
start();
controls.update();
}
</script>
</body>
</html>