-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.html
203 lines (152 loc) · 6.8 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<title>Yeah!</title>
<script type="text/javascript" src="three.js"></script>
</head>
<body>
<div id="container"></div>
<a href="particles.html">Particles</a>
<script type="x-shader/x-vertex" id="vertexShader">
uniform float time;
varying vec3 transformedNormal;
varying vec3 lighting;
varying vec4 vPosition;
void main() {
vPosition = modelViewMatrix * vec4(position, 1.0);
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
transformedNormal = normalMatrix * normal;
}
</script>
<script type="x-shader/x-fragment" id="cubeFragmentShader">
uniform float time;
varying vec3 transformedNormal;
varying vec3 lighting;
varying vec4 vPosition;
void main() {
vec3 directionalVector = vec3(0.0, 5.0, 0.0);
vec3 lightDirection = normalize(directionalVector - vPosition.xyz);
vec3 lightColor = vec3(sin(time), cos(time), tan(time));//vec3(0.15, 0.9, 0.45);
vec3 ambientLight = vec3(0.16, 0.16, 0.16);
float directionalLightWeighting = max(dot(normalize(transformedNormal), lightDirection), 0.0);
vec3 lightWeighting = ambientLight + (lightColor * directionalLightWeighting);
vec4 fragmentColor = vec4(1.0, 1.0, 1.0, 1.0);
gl_FragColor = vec4(fragmentColor.rgb * lightWeighting, fragmentColor.a);
}
</script>
<script type="x-shader/x-fragment" id="sphereFragmentShader">
uniform float time;
varying vec3 transformedNormal;
varying vec3 lighting;
varying vec4 vPosition;
void main() {
vec3 directionalVector = vec3(0.0, 105.0, 0.0);
vec3 lightDirection = normalize(directionalVector - vPosition.xyz);
vec3 normal = normalize(transformedNormal);
vec3 directionalLightColor = vec3(0.80, 0.80, 0.0);
vec3 ambientLight = vec3(0.05, 0.05, 0.05);
float directionalLightWeighting = max(dot(normal, lightDirection), 0.0);
vec3 specularLightColor = directionalLightColor;
vec3 eyeDirection = normalize(-vPosition.xyz);
vec3 reflectionDirection = reflect(-lightDirection, normal);
float specularLightWeighting = pow(max(dot(reflectionDirection, eyeDirection), 0.0), 20.0);
vec3 lightWeighting = ambientLight +
(specularLightColor * specularLightWeighting) +
(directionalLightColor * directionalLightWeighting);
vec4 fragmentColor = vec4(1.0, 1.0, 1.0, 1.0);
gl_FragColor = vec4(fragmentColor.rgb * lightWeighting, fragmentColor.a);
}
</script>
<script type="text/javascript" id="display-logic">
var WIDTH = 1000,
HEIGHT = 600;
VIEW_ANGLE = 45,
ASPECT_RATIO = WIDTH / HEIGHT,
NEAR = 0.1,
FAR = 10000;
var camera = new THREE.PerspectiveCamera(VIEW_ANGLE, ASPECT_RATIO, NEAR, FAR);
var scene = new THREE.Scene();
camera.position.z = 300;
var renderer = new THREE.WebGLRenderer();
renderer.setSize(WIDTH, HEIGHT);
var crateTexture = THREE.ImageUtils.loadTexture('http://i.imgur.com/eH3YvZw.jpg');
var sphereMaterial = new THREE.MeshPhongMaterial({ color: 0x0000CC }),
cubeMaterial = new THREE.MeshPhongMaterial({map: crateTexture, color: 0xFFFFFF}),
uniforms = {
time: {
type: 'f', // a float
value: 0
}
},
shaderMaterial = new THREE.ShaderMaterial({
uniforms: uniforms,
vertexShader: document.getElementById('vertexShader').textContent,
fragmentShader: document.getElementById('cubeFragmentShader').textContent
}),
sphereShaderMaterial = new THREE.ShaderMaterial({
uniforms: uniforms,
vertexShader: document.getElementById('vertexShader').textContent,
fragmentShader: document.getElementById('sphereFragmentShader').textContent
});
var radius = 50,
segments = 46,
rings = 16,
sphere = new THREE.Mesh(new THREE.SphereGeometry(radius, segments, rings), sphereShaderMaterial),
cube = new THREE.Mesh(new THREE.CubeGeometry(radius, radius, radius), cubeMaterial),
shadedCube = new THREE.Mesh(new THREE.CubeGeometry(radius, radius, radius), shaderMaterial);
sphere.position.set(50, 120, -150)
cube.position.set(-50, 0, 0);
cube.rotation.x = 2.0;
shadedCube.position.set(50, 0, 0);
shadedCube.rotation.x = 2.0;
shadedCube.rotation.y = 25.0;
var pointLight = new THREE.PointLight(0xFFFFFF);
pointLight.position.x = 10;
pointLight.position.y = 50;
pointLight.position.z = 130;
scene.add(pointLight);
var ambientLight = new THREE.AmbientLight(0x666666);
//scene.add(ambientLight);
var frame = 0,
moveablePointLight = new THREE.PointLight(0xFFFFFF);
//scene.add(new THREE.AmbientLight(0x0000CC));
var directionLight = new THREE.DirectionalLight(0xffffff, 0.5);
directionLight.position.set(0, 1, 100);
scene.add(directionLight);
// var spotLight = new THREE.SpotLight(0xffffff);
// spotLight.position.set(100, 1000, 100);
// spotLight.castShadow = true;
// spotLight.shadowMapWidth = 1024;
// spotLight.shadowMapHeight = 1024;
// spotLight.shadowCameraNear = 500;
// spotLight.shadowCameraFar = 4000;
// spotLight.shadowCameraFov = 30;
// scene.add( spotLight );
renderer.setClearColor(0x000000);
var render = function() {
requestAnimationFrame(render);
scene.remove(sphere);
scene.remove(cube);
scene.remove(shadedCube);
// scene.remove(moveablePointLight);
// moveablePointLight.position.x = frame;
// moveablePointLight.position.y = 50;
// moveablePointLight.position.z = 1530;
// scene.add(moveablePointLight);
cube.rotation.y -= 0.025;
sphere.rotation.x += 0.025;
shadedCube.rotation.x += 0.01;
shadedCube.rotation.z += 0.075;
scene.add(sphere);
scene.add(cube);
scene.add(shadedCube);
scene.add(camera);
renderer.render(scene, camera);
uniforms.time.value = frame / 100;
frame++;
};
render();
document.getElementById('container').appendChild(renderer.domElement);
</script>
</body>
</html>