-
Notifications
You must be signed in to change notification settings - Fork 0
/
Methan.html
225 lines (194 loc) · 7.8 KB
/
Methan.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
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://getfirebug.com/firebug-lite-debug.js"></script>
<meta name="description" content="CS4406 Computer Graphics - Exercise #5" />
<meta charset="utf-8" />
<title>Assignment #5 for CS4406 Computer Graphics</title>
<style>
#container {
background: #000000;
width: 100%;
height: 100%;
}
#info {
position: absolute;
top: 670px;
width: 110%;
text-align: center;
z-index: 100;
display:block;
font-size: 250%;
font-weight: Bold;
}
</style>
<meta charset=utf-8 />
<title>CS4406 Computer Graphics - Exercise #5</title>
<style id="jsbin-css">
</style>
</head>
<body>
<br/>
<div id="Title Continer">
<div class="form-check form-check-inline">
<form id="Title">
<label class="Title" for="">CS4406 Computer Graphics - Unit 5</label>
</form>
</div>
<div id="info"> Methane CH<sub>4</sub></div>
<div id="container"></div>
<script type="text/javascript" src="https://getfirebug.com/firebug-lite-debug.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/108/three.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.6.5/dat.gui.min.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/mrdoob/three.js/master/examples/js/controls/OrbitControls.js"></script>
<script type="text/javascript" src="https://threejs.org/examples/js/controls/TransformControls.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/6.2.1/math.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tween.js/16.3.5/Tween.min.js"></script>
<script type="text/javascript">
// set the scene size
var WIDTH = 700, HEIGHT = 700;
// set some camera attributes
var VIEW_ANGLE = 45, ASPECT = WIDTH / HEIGHT, NEAR = 1, FAR = 1000;
// get the DOM element to attach to
var $container = $('#container');
// create a WebGL renderer, camera, and a scene
//Set up the renderer to calculate shadows.
var renderer = new THREE.WebGLRenderer();
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
var scene = new THREE.Scene();
var clock = new THREE.Clock();
var camera = new THREE.PerspectiveCamera(VIEW_ANGLE,ASPECT,NEAR,FAR);
var canvas = renderer.domElement;
//Create a listener for the canvas element to track the moving of the user's mouse.
canvas.addEventListener('mousemove', onMouseMove);
// the camera starts at 0,0,0 so pull it back
camera.position.z = 200;
// add the camera to the scene
scene.add(camera)
// start the renderer
renderer.setSize(WIDTH, HEIGHT);
// attach the render-supplied DOM element
$container.append(renderer.domElement);
// ----------------------------------------------------------------------------------------
// NOTE:
// TO ROTATE THE MOLECULE, MOVE YOUR MOUSE OVER THE MOLECULE IN THE OUTPUT WINDOW.
// THE MOLECULE IS NOT CONTROLLED BY CLICK-DRAGGING.
// ----------------------------------------------------------------------------------------
// Create a point light
var areaLight = new THREE.RectAreaLight( 0xffffff, 10);
//Create spotlight for illumination; areaLight does not affect shininess, etc.
var spotLight = new THREE.SpotLight( 0xffffff );
spotLight.position.set( 0, 80, 0 );
spotLight.castShadow = true;
// add to the scene
scene.add(areaLight, spotLight);
//Define the qualities of the shadows
spotLight.shadow.mapSize.width = 512;
spotLight.shadow.mapSize.height = 512;
spotLight.shadow.camera.near = 0.5;
spotLight.shadow.camera.far = 500
spotLight.shadow.focus = 1;
// create the materials
//CARBON
var cMaterial = new THREE.MeshPhongMaterial( {color: 0xab0000, emissive:0xaa2222, shininess: 8,
specular: 0x0fffff});
//HYDROGEN
var hMaterial = new THREE.MeshPhongMaterial( {color: 0x2080aa, emissive:0x000aa, shininess: 8,
specular: 0x0fffff});
//BONDS
var bGeometry = new THREE.CylinderGeometry( 3, 3, 60, 35 );
var bMaterial = new THREE.MeshPhongMaterial( {color: 0xffffff, emissive:0xaaaaaa} );
//PLANE
var pGeometry = new THREE.PlaneGeometry( 500, 500, 500 );
var pMaterial = new THREE.MeshPhongMaterial( {color: 0x80f080, emissive:0x001000,side: THREE.DoubleSide});
// set up the sphere vars
var cRadius = 20, cSegments = 35, cRings = 35;
var hRadius = 10, hSegments = 35, hRings = 35;
// create a new mesh with sphere geometry
var carbon = new THREE.Mesh(
new THREE.SphereGeometry(cRadius, cSegments, cRings),
cMaterial);
//HYDROGEN
var hydrogen1 = new THREE.Mesh(
new THREE.SphereGeometry(hRadius, hSegments, hRings),
hMaterial);
var hydrogen2 = new THREE.Mesh(
new THREE.SphereGeometry(hRadius, hSegments, hRings),
hMaterial);
var hydrogen3 = new THREE.Mesh(
new THREE.SphereGeometry(hRadius, hSegments, hRings),
hMaterial);
var hydrogen4 = new THREE.Mesh(
new THREE.SphereGeometry(hRadius, hSegments, hRings),
hMaterial);
//BONDS
var bond1 = new THREE.Mesh( bGeometry, bMaterial );
var bond2 = new THREE.Mesh( bGeometry, bMaterial );
var bond3 = new THREE.Mesh( bGeometry, bMaterial );
var bond4 = new THREE.Mesh( bGeometry, bMaterial );
//Group the carbon, hydrogen, and bonds together to make it easier to manipulate.
//This can be done simply with the Three.Group() Object.
var CH4 = new THREE.Group();
CH4.add(carbon, hydrogen1, hydrogen2, hydrogen3, hydrogen4, bond1, bond2, bond3, bond4);
//PLANE
var plane = new THREE.Mesh( pGeometry, pMaterial );
//Set each object's shadow casting/receiving properties
carbon.castShadow = true;
hydrogen1.castShadow = true;
hydrogen2.castShadow = true;
hydrogen3.castShadow = true;
hydrogen4.castShadow = true;
bond1.castShadow = true;
bond2.castShadow = true;
bond3.castShadow = true;
bond4.castShadow = true;
plane.receiveShadow = true;
// Add the meshes to the scene
scene.add(CH4, plane);
//Place the meshes
//NOTE: We are defining them before the animate function so they won't be constantly stuck in
//a single position when we try to spin it. Otherwise, the program will lock them to the given
//position each and every time it renders.
hydrogen1.position.y = 50;
hydrogen2.position.y = -25;
hydrogen2.position.z = 50;
hydrogen3.position.x = -50;
hydrogen3.position.y = -25;
hydrogen3.position.z = -10;
hydrogen4.position.x = 50;
hydrogen4.position.y = -25;
hydrogen4.position.z = -10;
bond1.position.y = 20;
bond2.position.y= -10;
bond2.position.z= 15;
bond2.rotation.x= -20;
bond3.position.x = -15;
bond3.position.y = -10;
bond3.position.z = -6;
bond3.rotation.y= -0.2;
bond3.rotation.z= -20;
bond4.position.x = 15;
bond4.position.y = -10;
bond4.position.z = -6;
bond4.rotation.y= 0.2;
bond4.rotation.z= 20;
plane.position.y = -55
plane.rotation.x = 300;
//This is the function that will determine how the CH4 reacts to being "moused over"
//In this case, it will rotate depending on what direction the mouse is moved; try it out!
function onMouseMove(event) {
CH4.rotation.x += event.movementY * 0.002;
CH4.rotation.y += event.movementX * 0.002;
}
function animate() {
requestAnimationFrame(animate);
render();
}
function render() {
renderer.render(scene, camera);
}
animate();
</script>
</html>