css3d_molecules.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>three.js css3d - molecules</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. body {
  10. background-color: #050505;
  11. background: radial-gradient(ellipse at center, rgba(43,45,48,1) 0%,rgba(0,0,0,1) 100%);
  12. }
  13. #topmenu {
  14. position: absolute;
  15. top: 50px;
  16. width: 100%;
  17. padding: 10px;
  18. box-sizing: border-box;
  19. text-align: center;
  20. }
  21. #menu {
  22. position: absolute;
  23. bottom: 20px;
  24. width: 100%;
  25. padding: 10px;
  26. box-sizing: border-box;
  27. text-align: center;
  28. }
  29. button {
  30. color: rgb(255,255,255);
  31. background: rgb(255,255,255,0.1);
  32. border: 0px;
  33. padding: 5px 10px;
  34. margin: 2px;
  35. font-size: 14px;
  36. cursor: pointer;
  37. }
  38. button:hover {
  39. background-color: rgba(0,255,255,0.5);
  40. }
  41. button:active {
  42. color: #000000;
  43. background-color: rgba(0,255,255,1);
  44. }
  45. .bond {
  46. width: 5px;
  47. height: 10px;
  48. background: #eee;
  49. display: block;
  50. }
  51. </style>
  52. </head>
  53. <body>
  54. <div id="container"></div>
  55. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> css3d - molecules</div>
  56. <div id="topmenu">
  57. <button id="b_a">Atoms</button><button id="b_b">Bonds</button><button id="b_ab">Atoms + Bonds</button>
  58. </div>
  59. <div id="menu"></div>
  60. <script type="module">
  61. import * as THREE from '../build/three.module.js';
  62. import { TrackballControls } from './jsm/controls/TrackballControls.js';
  63. import { PDBLoader } from './jsm/loaders/PDBLoader.js';
  64. import { CSS3DRenderer, CSS3DObject, CSS3DSprite } from './jsm/renderers/CSS3DRenderer.js';
  65. var camera, scene, renderer;
  66. var controls;
  67. var root;
  68. var objects = [];
  69. var tmpVec1 = new THREE.Vector3();
  70. var tmpVec2 = new THREE.Vector3();
  71. var tmpVec3 = new THREE.Vector3();
  72. var tmpVec4 = new THREE.Vector3();
  73. var offset = new THREE.Vector3();
  74. var visualizationType = 2;
  75. var MOLECULES = {
  76. "Ethanol": "ethanol.pdb",
  77. "Aspirin": "aspirin.pdb",
  78. "Caffeine": "caffeine.pdb",
  79. "Nicotine": "nicotine.pdb",
  80. "LSD": "lsd.pdb",
  81. "Cocaine": "cocaine.pdb",
  82. "Cholesterol": "cholesterol.pdb",
  83. "Lycopene": "lycopene.pdb",
  84. "Glucose": "glucose.pdb",
  85. "Aluminium oxide": "Al2O3.pdb",
  86. "Cubane": "cubane.pdb",
  87. "Copper": "cu.pdb",
  88. "Fluorite": "caf2.pdb",
  89. "Salt": "nacl.pdb",
  90. "YBCO superconductor": "ybco.pdb",
  91. "Buckyball": "buckyball.pdb",
  92. //"Diamond": "diamond.pdb",
  93. "Graphite": "graphite.pdb"
  94. };
  95. var loader = new PDBLoader();
  96. var colorSpriteMap = {};
  97. var baseSprite = document.createElement( 'img' );
  98. var menu = document.getElementById( "menu" );
  99. init();
  100. animate();
  101. function init() {
  102. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 5000 );
  103. camera.position.z = 1500;
  104. scene = new THREE.Scene();
  105. root = new THREE.Object3D();
  106. scene.add( root );
  107. //
  108. renderer = new CSS3DRenderer();
  109. renderer.setSize( window.innerWidth, window.innerHeight );
  110. document.getElementById( 'container' ).appendChild( renderer.domElement );
  111. //
  112. controls = new TrackballControls( camera, renderer.domElement );
  113. controls.rotateSpeed = 0.5;
  114. //
  115. baseSprite.onload = function () {
  116. loadMolecule( "models/molecules/caffeine.pdb" );
  117. createMenu();
  118. };
  119. baseSprite.src = 'textures/sprites/ball.png';
  120. //
  121. window.addEventListener( 'resize', onWindowResize, false );
  122. }
  123. //
  124. function generateButtonCallback( url ) {
  125. return function () {
  126. loadMolecule( url );
  127. };
  128. }
  129. function createMenu() {
  130. for ( var m in MOLECULES ) {
  131. var button = document.createElement( 'button' );
  132. button.innerHTML = m;
  133. menu.appendChild( button );
  134. var url = "models/molecules/" + MOLECULES[ m ];
  135. button.addEventListener( 'click', generateButtonCallback( url ), false );
  136. }
  137. var b_a = document.getElementById( "b_a" );
  138. var b_b = document.getElementById( "b_b" );
  139. var b_ab = document.getElementById( "b_ab" );
  140. b_a.addEventListener( 'click', function () {
  141. visualizationType = 0;
  142. showAtoms();
  143. } );
  144. b_b.addEventListener( 'click', function () {
  145. visualizationType = 1;
  146. showBonds();
  147. } );
  148. b_ab.addEventListener( 'click', function () {
  149. visualizationType = 2;
  150. showAtomsBonds();
  151. } );
  152. }
  153. //
  154. function showAtoms() {
  155. for ( var i = 0; i < objects.length; i ++ ) {
  156. var object = objects[ i ];
  157. if ( object instanceof CSS3DSprite ) {
  158. object.element.style.display = "";
  159. object.visible = true;
  160. } else {
  161. object.element.style.display = "none";
  162. object.visible = false;
  163. }
  164. }
  165. }
  166. function showBonds() {
  167. for ( var i = 0; i < objects.length; i ++ ) {
  168. var object = objects[ i ];
  169. if ( object instanceof CSS3DSprite ) {
  170. object.element.style.display = "none";
  171. object.visible = false;
  172. } else {
  173. object.element.style.display = "";
  174. object.element.style.height = object.userData.bondLengthFull;
  175. object.visible = true;
  176. }
  177. }
  178. }
  179. function showAtomsBonds() {
  180. for ( var i = 0; i < objects.length; i ++ ) {
  181. var object = objects[ i ];
  182. object.element.style.display = "";
  183. object.visible = true;
  184. if ( ! ( object instanceof CSS3DSprite ) ) {
  185. object.element.style.height = object.userData.bondLengthShort;
  186. }
  187. }
  188. }
  189. //
  190. function colorify( ctx, width, height, color ) {
  191. var r = color.r, g = color.g, b = color.b;
  192. var imageData = ctx.getImageData( 0, 0, width, height );
  193. var data = imageData.data;
  194. for ( var i = 0, l = data.length; i < l; i += 4 ) {
  195. data[ i + 0 ] *= r;
  196. data[ i + 1 ] *= g;
  197. data[ i + 2 ] *= b;
  198. }
  199. ctx.putImageData( imageData, 0, 0 );
  200. }
  201. function imageToCanvas( image ) {
  202. var width = image.width;
  203. var height = image.height;
  204. var canvas = document.createElement( 'canvas' );
  205. canvas.width = width;
  206. canvas.height = height;
  207. var context = canvas.getContext( '2d' );
  208. context.drawImage( image, 0, 0, width, height );
  209. return canvas;
  210. }
  211. //
  212. function loadMolecule( url ) {
  213. for ( var i = 0; i < objects.length; i ++ ) {
  214. var object = objects[ i ];
  215. object.parent.remove( object );
  216. }
  217. objects = [];
  218. loader.load( url, function ( pdb ) {
  219. var geometryAtoms = pdb.geometryAtoms;
  220. var geometryBonds = pdb.geometryBonds;
  221. var json = pdb.json;
  222. geometryAtoms.computeBoundingBox();
  223. geometryAtoms.boundingBox.getCenter( offset ).negate();
  224. geometryAtoms.translate( offset.x, offset.y, offset.z );
  225. geometryBonds.translate( offset.x, offset.y, offset.z );
  226. var positions = geometryAtoms.getAttribute( 'position' );
  227. var colors = geometryAtoms.getAttribute( 'color' );
  228. var position = new THREE.Vector3();
  229. var color = new THREE.Color();
  230. for ( var i = 0; i < positions.count; i ++ ) {
  231. position.x = positions.getX( i );
  232. position.y = positions.getY( i );
  233. position.z = positions.getZ( i );
  234. color.r = colors.getX( i );
  235. color.g = colors.getY( i );
  236. color.b = colors.getZ( i );
  237. var atom = json.atoms[ i ];
  238. var element = atom[ 4 ];
  239. if ( ! colorSpriteMap[ element ] ) {
  240. var canvas = imageToCanvas( baseSprite );
  241. var context = canvas.getContext( '2d' );
  242. colorify( context, canvas.width, canvas.height, color );
  243. var dataUrl = canvas.toDataURL();
  244. colorSpriteMap[ element ] = dataUrl;
  245. }
  246. var colorSprite = colorSpriteMap[ element ];
  247. var atom = document.createElement( 'img' );
  248. atom.src = colorSprite;
  249. var object = new CSS3DSprite( atom );
  250. object.position.copy( position );
  251. object.position.multiplyScalar( 75 );
  252. object.matrixAutoUpdate = false;
  253. object.updateMatrix();
  254. root.add( object );
  255. objects.push( object );
  256. }
  257. positions = geometryBonds.getAttribute( 'position' );
  258. var start = new THREE.Vector3();
  259. var end = new THREE.Vector3();
  260. for ( var i = 0; i < positions.count; i += 2 ) {
  261. start.x = positions.getX( i );
  262. start.y = positions.getY( i );
  263. start.z = positions.getZ( i );
  264. end.x = positions.getX( i + 1 );
  265. end.y = positions.getY( i + 1 );
  266. end.z = positions.getZ( i + 1 );
  267. start.multiplyScalar( 75 );
  268. end.multiplyScalar( 75 );
  269. tmpVec1.subVectors( end, start );
  270. var bondLength = tmpVec1.length() - 50;
  271. //
  272. var bond = document.createElement( 'div' );
  273. bond.className = "bond";
  274. bond.style.height = bondLength + "px";
  275. var object = new CSS3DObject( bond );
  276. object.position.copy( start );
  277. object.position.lerp( end, 0.5 );
  278. object.userData.bondLengthShort = bondLength + "px";
  279. object.userData.bondLengthFull = ( bondLength + 55 ) + "px";
  280. //
  281. var axis = tmpVec2.set( 0, 1, 0 ).cross( tmpVec1 );
  282. var radians = Math.acos( tmpVec3.set( 0, 1, 0 ).dot( tmpVec4.copy( tmpVec1 ).normalize() ) );
  283. var objMatrix = new THREE.Matrix4().makeRotationAxis( axis.normalize(), radians );
  284. object.matrix = objMatrix;
  285. object.quaternion.setFromRotationMatrix( object.matrix );
  286. object.matrixAutoUpdate = false;
  287. object.updateMatrix();
  288. root.add( object );
  289. objects.push( object );
  290. //
  291. var bond = document.createElement( 'div' );
  292. bond.className = "bond";
  293. bond.style.height = bondLength + "px";
  294. var joint = new THREE.Object3D( bond );
  295. joint.position.copy( start );
  296. joint.position.lerp( end, 0.5 );
  297. joint.matrix.copy( objMatrix );
  298. joint.quaternion.setFromRotationMatrix( joint.matrix );
  299. joint.matrixAutoUpdate = false;
  300. joint.updateMatrix();
  301. var object = new CSS3DObject( bond );
  302. object.rotation.y = Math.PI / 2;
  303. object.matrixAutoUpdate = false;
  304. object.updateMatrix();
  305. object.userData.bondLengthShort = bondLength + "px";
  306. object.userData.bondLengthFull = ( bondLength + 55 ) + "px";
  307. object.userData.joint = joint;
  308. joint.add( object );
  309. root.add( joint );
  310. objects.push( object );
  311. }
  312. //console.log( "CSS3DObjects:", objects.length );
  313. switch ( visualizationType ) {
  314. case 0:
  315. showAtoms();
  316. break;
  317. case 1:
  318. showBonds();
  319. break;
  320. case 2:
  321. showAtomsBonds();
  322. break;
  323. }
  324. } );
  325. }
  326. //
  327. function onWindowResize() {
  328. camera.aspect = window.innerWidth / window.innerHeight;
  329. camera.updateProjectionMatrix();
  330. renderer.setSize( window.innerWidth, window.innerHeight );
  331. }
  332. function animate() {
  333. requestAnimationFrame( animate );
  334. controls.update();
  335. var time = Date.now() * 0.0004;
  336. root.rotation.x = time;
  337. root.rotation.y = time * 0.7;
  338. render();
  339. }
  340. function render() {
  341. renderer.render( scene, camera );
  342. }
  343. </script>
  344. </body>
  345. </html>