123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525 |
- <!DOCTYPE html>
- <html>
- <head>
- <title>three.js 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%);
- }
- #topmenu {
- position: absolute;
- top: 50px;
- width: 100%;
- padding: 10px;
- box-sizing: border-box;
- text-align: center;
- }
- #menu {
- position: absolute;
- bottom: 20px;
- width: 100%;
- padding: 10px;
- box-sizing: border-box;
- text-align: center;
- }
- button {
- color: rgb(255,255,255);
- background: rgb(255,255,255,0.1);
- border: 0px;
- padding: 5px 10px;
- margin: 2px;
- font-size: 14px;
- cursor: pointer;
- }
- button:hover {
- background-color: rgba(0,255,255,0.5);
- }
- button:active {
- color: #000000;
- background-color: rgba(0,255,255,1);
- }
- .bond {
- width: 5px;
- height: 10px;
- background: #eee;
- display: block;
- }
- </style>
- </head>
- <body>
- <div id="container"></div>
- <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> css3d - molecules</div>
- <div id="topmenu">
- <button id="b_a">Atoms</button><button id="b_b">Bonds</button><button id="b_ab">Atoms + Bonds</button>
- </div>
- <div id="menu"></div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import { TrackballControls } from './jsm/controls/TrackballControls.js';
- import { PDBLoader } from './jsm/loaders/PDBLoader.js';
- import { CSS3DRenderer, CSS3DObject, CSS3DSprite } from './jsm/renderers/CSS3DRenderer.js';
- var camera, scene, renderer;
- var controls;
- var root;
- var objects = [];
- var tmpVec1 = new THREE.Vector3();
- var tmpVec2 = new THREE.Vector3();
- var tmpVec3 = new THREE.Vector3();
- var tmpVec4 = new THREE.Vector3();
- var offset = new THREE.Vector3();
- var visualizationType = 2;
- var 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"
- };
- var loader = new PDBLoader();
- var colorSpriteMap = {};
- var baseSprite = document.createElement( 'img' );
- var menu = document.getElementById( "menu" );
- init();
- animate();
- function init() {
- camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 5000 );
- camera.position.z = 1500;
- scene = new THREE.Scene();
- root = new THREE.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( "models/molecules/caffeine.pdb" );
- createMenu();
- };
- baseSprite.src = 'textures/sprites/ball.png';
- //
- window.addEventListener( 'resize', onWindowResize, false );
- }
- //
- function generateButtonCallback( url ) {
- return function () {
- loadMolecule( url );
- };
- }
- function createMenu() {
- for ( var m in MOLECULES ) {
- var button = document.createElement( 'button' );
- button.innerHTML = m;
- menu.appendChild( button );
- var url = "models/molecules/" + MOLECULES[ m ];
- button.addEventListener( 'click', generateButtonCallback( url ), false );
- }
- var b_a = document.getElementById( "b_a" );
- var b_b = document.getElementById( "b_b" );
- var b_ab = document.getElementById( "b_ab" );
- b_a.addEventListener( 'click', function () {
- visualizationType = 0;
- showAtoms();
- } );
- b_b.addEventListener( 'click', function () {
- visualizationType = 1;
- showBonds();
- } );
- b_ab.addEventListener( 'click', function () {
- visualizationType = 2;
- showAtomsBonds();
- } );
- }
- //
- function showAtoms() {
- for ( var i = 0; i < objects.length; i ++ ) {
- var 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 ( var i = 0; i < objects.length; i ++ ) {
- var 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 ( var i = 0; i < objects.length; i ++ ) {
- var 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 ) {
- var r = color.r, g = color.g, b = color.b;
- var imageData = ctx.getImageData( 0, 0, width, height );
- var data = imageData.data;
- for ( var 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 ) {
- var width = image.width;
- var height = image.height;
- var canvas = document.createElement( 'canvas' );
- canvas.width = width;
- canvas.height = height;
- var context = canvas.getContext( '2d' );
- context.drawImage( image, 0, 0, width, height );
- return canvas;
- }
- //
- function loadMolecule( url ) {
- for ( var i = 0; i < objects.length; i ++ ) {
- var object = objects[ i ];
- object.parent.remove( object );
- }
- objects = [];
- loader.load( url, function ( pdb ) {
- var geometryAtoms = pdb.geometryAtoms;
- var geometryBonds = pdb.geometryBonds;
- var 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 );
- var positions = geometryAtoms.getAttribute( 'position' );
- var colors = geometryAtoms.getAttribute( 'color' );
- var position = new THREE.Vector3();
- var color = new THREE.Color();
- for ( var i = 0; i < positions.count; i ++ ) {
- position.x = positions.getX( i );
- position.y = positions.getY( i );
- position.z = positions.getZ( i );
- color.r = colors.getX( i );
- color.g = colors.getY( i );
- color.b = colors.getZ( i );
- var atom = json.atoms[ i ];
- var element = atom[ 4 ];
- if ( ! colorSpriteMap[ element ] ) {
- var canvas = imageToCanvas( baseSprite );
- var context = canvas.getContext( '2d' );
- colorify( context, canvas.width, canvas.height, color );
- var dataUrl = canvas.toDataURL();
- colorSpriteMap[ element ] = dataUrl;
- }
- var colorSprite = colorSpriteMap[ element ];
- var atom = document.createElement( 'img' );
- atom.src = colorSprite;
- var object = new CSS3DSprite( atom );
- object.position.copy( position );
- object.position.multiplyScalar( 75 );
- object.matrixAutoUpdate = false;
- object.updateMatrix();
- root.add( object );
- objects.push( object );
- }
- positions = geometryBonds.getAttribute( 'position' );
- var start = new THREE.Vector3();
- var end = new THREE.Vector3();
- for ( var i = 0; i < positions.count; i += 2 ) {
- start.x = positions.getX( i );
- start.y = positions.getY( i );
- start.z = positions.getZ( i );
- end.x = positions.getX( i + 1 );
- end.y = positions.getY( i + 1 );
- end.z = positions.getZ( i + 1 );
- start.multiplyScalar( 75 );
- end.multiplyScalar( 75 );
- tmpVec1.subVectors( end, start );
- var bondLength = tmpVec1.length() - 50;
- //
- var bond = document.createElement( 'div' );
- bond.className = "bond";
- bond.style.height = bondLength + "px";
- var 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";
- //
- var axis = tmpVec2.set( 0, 1, 0 ).cross( tmpVec1 );
- var radians = Math.acos( tmpVec3.set( 0, 1, 0 ).dot( tmpVec4.copy( tmpVec1 ).normalize() ) );
- var objMatrix = new THREE.Matrix4().makeRotationAxis( axis.normalize(), radians );
- object.matrix = objMatrix;
- object.quaternion.setFromRotationMatrix( object.matrix );
- object.matrixAutoUpdate = false;
- object.updateMatrix();
- root.add( object );
- objects.push( object );
- //
- var bond = document.createElement( 'div' );
- bond.className = "bond";
- bond.style.height = bondLength + "px";
- var joint = new THREE.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();
- var 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 );
- switch ( visualizationType ) {
- case 0:
- showAtoms();
- break;
- case 1:
- showBonds();
- break;
- case 2:
- showAtomsBonds();
- break;
- }
- } );
- }
- //
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function animate() {
- requestAnimationFrame( animate );
- controls.update();
- var 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>
|