This repository has been archived by the owner on Nov 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
css3d_molecules.html
448 lines (290 loc) · 10.8 KB
/
css3d_molecules.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
<!DOCTYPE html>
<html>
<head>
<title>Verge3D css3d - molecules</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="main.css">
<style>
body {
background-color: #050505;
background: radial-gradient(ellipse at center, rgba(43,45,48,1) 0%,rgba(0,0,0,1) 100%);
}
.bond {
width: 5px;
height: 10px;
background: #eee;
display: block;
}
</style>
</head>
<body>
<div id="container"></div>
<div id="info"><a href="https://www.soft8soft.com/verge3d" target="_blank" rel="noopener">Verge3D</a> css3d - molecules</div>
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"v3d": "../build/v3d.module.js",
"v3d/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as v3d from 'v3d';
import { TrackballControls } from 'v3d/addons/controls/TrackballControls.js';
import { PDBLoader } from 'v3d/addons/loaders/PDBLoader.js';
import { CSS3DRenderer, CSS3DObject, CSS3DSprite } from 'v3d/addons/renderers/CSS3DRenderer.js';
import { GUI } from 'v3d/addons/libs/lil-gui.module.min.js';
let camera, scene, renderer;
let controls;
let root;
const objects = [];
const tmpVec1 = new v3d.Vector3();
const tmpVec2 = new v3d.Vector3();
const tmpVec3 = new v3d.Vector3();
const tmpVec4 = new v3d.Vector3();
const offset = new v3d.Vector3();
const VIZ_TYPE = {
'Atoms': 0,
'Bonds': 1,
'Atoms + Bonds': 2
};
const MOLECULES = {
'Ethanol': 'ethanol.pdb',
'Aspirin': 'aspirin.pdb',
'Caffeine': 'caffeine.pdb',
'Nicotine': 'nicotine.pdb',
'LSD': 'lsd.pdb',
'Cocaine': 'cocaine.pdb',
'Cholesterol': 'cholesterol.pdb',
'Lycopene': 'lycopene.pdb',
'Glucose': 'glucose.pdb',
'Aluminium oxide': 'Al2O3.pdb',
'Cubane': 'cubane.pdb',
'Copper': 'cu.pdb',
'Fluorite': 'caf2.pdb',
'Salt': 'nacl.pdb',
'YBCO superconductor': 'ybco.pdb',
'Buckyball': 'buckyball.pdb',
// 'Diamond': 'diamond.pdb',
'Graphite': 'graphite.pdb'
};
const params = {
vizType: 2,
molecule: 'caffeine.pdb'
};
const loader = new PDBLoader();
const colorSpriteMap = {};
const baseSprite = document.createElement('img');
init();
animate();
function init() {
camera = new v3d.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 5000);
camera.position.z = 1000;
scene = new v3d.Scene();
root = new v3d.Object3D();
scene.add(root);
//
renderer = new CSS3DRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.getElementById('container').appendChild(renderer.domElement);
//
controls = new TrackballControls(camera, renderer.domElement);
controls.rotateSpeed = 0.5;
//
baseSprite.onload = function() {
loadMolecule(params.molecule);
};
baseSprite.src = 'textures/sprites/ball.png';
//
window.addEventListener('resize', onWindowResize);
//
const gui = new GUI();
gui.add(params, 'vizType', VIZ_TYPE).onChange(changeVizType);
gui.add(params, 'molecule', MOLECULES).onChange(loadMolecule);
gui.open();
}
function changeVizType(value) {
if (value === 0) showAtoms();
else if (value === 1) showBonds();
else showAtomsBonds();
}
//
function showAtoms() {
for (let i = 0; i < objects.length; i++) {
const object = objects[i];
if (object instanceof CSS3DSprite) {
object.element.style.display = '';
object.visible = true;
} else {
object.element.style.display = 'none';
object.visible = false;
}
}
}
function showBonds() {
for (let i = 0; i < objects.length; i++) {
const object = objects[i];
if (object instanceof CSS3DSprite) {
object.element.style.display = 'none';
object.visible = false;
} else {
object.element.style.display = '';
object.element.style.height = object.userData.bondLengthFull;
object.visible = true;
}
}
}
function showAtomsBonds() {
for (let i = 0; i < objects.length; i++) {
const object = objects[i];
object.element.style.display = '';
object.visible = true;
if (!(object instanceof CSS3DSprite)) {
object.element.style.height = object.userData.bondLengthShort;
}
}
}
//
function colorify(ctx, width, height, color) {
const r = color.r, g = color.g, b = color.b;
const imageData = ctx.getImageData(0, 0, width, height);
const data = imageData.data;
for (let i = 0, l = data.length; i < l; i += 4) {
data[i + 0] *= r;
data[i + 1] *= g;
data[i + 2] *= b;
}
ctx.putImageData(imageData, 0, 0);
}
function imageToCanvas(image) {
const width = image.width;
const height = image.height;
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
const context = canvas.getContext('2d');
context.drawImage(image, 0, 0, width, height);
return canvas;
}
//
function loadMolecule(model) {
const url = 'models/pdb/' + model;
for (let i = 0; i < objects.length; i++) {
const object = objects[i];
object.parent.remove(object);
}
objects.length = 0;
loader.load(url, function(pdb) {
const geometryAtoms = pdb.geometryAtoms;
const geometryBonds = pdb.geometryBonds;
const json = pdb.json;
geometryAtoms.computeBoundingBox();
geometryAtoms.boundingBox.getCenter(offset).negate();
geometryAtoms.translate(offset.x, offset.y, offset.z);
geometryBonds.translate(offset.x, offset.y, offset.z);
const positionAtoms = geometryAtoms.getAttribute('position');
const colorAtoms = geometryAtoms.getAttribute('color');
const position = new v3d.Vector3();
const color = new v3d.Color();
for (let i = 0; i < positionAtoms.count; i++) {
position.fromBufferAttribute(positionAtoms, i);
color.fromBufferAttribute(colorAtoms, i);
const atomJSON = json.atoms[i];
const element = atomJSON[4];
if (!colorSpriteMap[element]) {
const canvas = imageToCanvas(baseSprite);
const context = canvas.getContext('2d');
colorify(context, canvas.width, canvas.height, color);
const dataUrl = canvas.toDataURL();
colorSpriteMap[element] = dataUrl;
}
const colorSprite = colorSpriteMap[element];
const atom = document.createElement('img');
atom.src = colorSprite;
const object = new CSS3DSprite(atom);
object.position.copy(position);
object.position.multiplyScalar(75);
object.matrixAutoUpdate = false;
object.updateMatrix();
root.add(object);
objects.push(object);
}
const positionBonds = geometryBonds.getAttribute('position');
const start = new v3d.Vector3();
const end = new v3d.Vector3();
for (let i = 0; i < positionBonds.count; i += 2) {
start.fromBufferAttribute(positionBonds, i);
end.fromBufferAttribute(positionBonds, i + 1);
start.multiplyScalar(75);
end.multiplyScalar(75);
tmpVec1.subVectors(end, start);
const bondLength = tmpVec1.length() -50;
//
let bond = document.createElement('div');
bond.className = 'bond';
bond.style.height = bondLength + 'px';
let object = new CSS3DObject(bond);
object.position.copy(start);
object.position.lerp(end, 0.5);
object.userData.bondLengthShort = bondLength + 'px';
object.userData.bondLengthFull = (bondLength + 55) + 'px';
//
const axis = tmpVec2.set(0, 1, 0).cross(tmpVec1);
const radians = Math.acos(tmpVec3.set(0, 1, 0).dot(tmpVec4.copy(tmpVec1).normalize()));
const objMatrix = new v3d.Matrix4().makeRotationAxis(axis.normalize(), radians);
object.matrix.copy(objMatrix);
object.quaternion.setFromRotationMatrix(object.matrix);
object.matrixAutoUpdate = false;
object.updateMatrix();
root.add(object);
objects.push(object);
//
bond = document.createElement('div');
bond.className = 'bond';
bond.style.height = bondLength + 'px';
const joint = new v3d.Object3D(bond);
joint.position.copy(start);
joint.position.lerp(end, 0.5);
joint.matrix.copy(objMatrix);
joint.quaternion.setFromRotationMatrix(joint.matrix);
joint.matrixAutoUpdate = false;
joint.updateMatrix();
object = new CSS3DObject(bond);
object.rotation.y = Math.PI / 2;
object.matrixAutoUpdate = false;
object.updateMatrix();
object.userData.bondLengthShort = bondLength + 'px';
object.userData.bondLengthFull = (bondLength + 55) + 'px';
object.userData.joint = joint;
joint.add(object);
root.add(joint);
objects.push(object);
}
//console.log("CSS3DObjects:", objects.length);
changeVizType(params.vizType);
});
}
//
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function animate() {
requestAnimationFrame(animate);
controls.update();
const time = Date.now() * 0.0004;
root.rotation.x = time;
root.rotation.y = time * 0.7;
render();
}
function render() {
renderer.render(scene, camera);
}
</script>
</body>
</html>