2
0

css3d_molecules.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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. <!-- Import maps polyfill -->
  61. <!-- Remove this when import maps will be widely supported -->
  62. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  63. <script type="importmap">
  64. {
  65. "imports": {
  66. "three": "../build/three.module.js"
  67. }
  68. }
  69. </script>
  70. <script type="module">
  71. import * as THREE from 'three';
  72. import { TrackballControls } from './jsm/controls/TrackballControls.js';
  73. import { PDBLoader } from './jsm/loaders/PDBLoader.js';
  74. import { CSS3DRenderer, CSS3DObject, CSS3DSprite } from './jsm/renderers/CSS3DRenderer.js';
  75. let camera, scene, renderer;
  76. let controls;
  77. let root;
  78. const objects = [];
  79. const tmpVec1 = new THREE.Vector3();
  80. const tmpVec2 = new THREE.Vector3();
  81. const tmpVec3 = new THREE.Vector3();
  82. const tmpVec4 = new THREE.Vector3();
  83. const offset = new THREE.Vector3();
  84. let visualizationType = 2;
  85. const MOLECULES = {
  86. 'Ethanol': 'ethanol.pdb',
  87. 'Aspirin': 'aspirin.pdb',
  88. 'Caffeine': 'caffeine.pdb',
  89. 'Nicotine': 'nicotine.pdb',
  90. 'LSD': 'lsd.pdb',
  91. 'Cocaine': 'cocaine.pdb',
  92. 'Cholesterol': 'cholesterol.pdb',
  93. 'Lycopene': 'lycopene.pdb',
  94. 'Glucose': 'glucose.pdb',
  95. 'Aluminium oxide': 'Al2O3.pdb',
  96. 'Cubane': 'cubane.pdb',
  97. 'Copper': 'cu.pdb',
  98. 'Fluorite': 'caf2.pdb',
  99. 'Salt': 'nacl.pdb',
  100. 'YBCO superconductor': 'ybco.pdb',
  101. 'Buckyball': 'buckyball.pdb',
  102. // 'Diamond': 'diamond.pdb',
  103. 'Graphite': 'graphite.pdb'
  104. };
  105. const loader = new PDBLoader();
  106. const colorSpriteMap = {};
  107. const baseSprite = document.createElement( 'img' );
  108. const menu = document.getElementById( 'menu' );
  109. init();
  110. animate();
  111. function init() {
  112. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 5000 );
  113. camera.position.z = 1000;
  114. scene = new THREE.Scene();
  115. root = new THREE.Object3D();
  116. scene.add( root );
  117. //
  118. renderer = new CSS3DRenderer();
  119. renderer.setSize( window.innerWidth, window.innerHeight );
  120. document.getElementById( 'container' ).appendChild( renderer.domElement );
  121. //
  122. controls = new TrackballControls( camera, renderer.domElement );
  123. controls.rotateSpeed = 0.5;
  124. //
  125. baseSprite.onload = function () {
  126. loadMolecule( 'models/pdb/caffeine.pdb' );
  127. createMenu();
  128. };
  129. baseSprite.src = 'textures/sprites/ball.png';
  130. //
  131. window.addEventListener( 'resize', onWindowResize );
  132. }
  133. //
  134. function generateButtonCallback( url ) {
  135. return function () {
  136. loadMolecule( url );
  137. };
  138. }
  139. function createMenu() {
  140. for ( const m in MOLECULES ) {
  141. const button = document.createElement( 'button' );
  142. button.innerHTML = m;
  143. menu.appendChild( button );
  144. const url = 'models/pdb/' + MOLECULES[ m ];
  145. button.addEventListener( 'click', generateButtonCallback( url ) );
  146. }
  147. const b_a = document.getElementById( 'b_a' );
  148. const b_b = document.getElementById( 'b_b' );
  149. const b_ab = document.getElementById( 'b_ab' );
  150. b_a.addEventListener( 'click', function () {
  151. visualizationType = 0;
  152. showAtoms();
  153. } );
  154. b_b.addEventListener( 'click', function () {
  155. visualizationType = 1;
  156. showBonds();
  157. } );
  158. b_ab.addEventListener( 'click', function () {
  159. visualizationType = 2;
  160. showAtomsBonds();
  161. } );
  162. }
  163. //
  164. function showAtoms() {
  165. for ( let i = 0; i < objects.length; i ++ ) {
  166. const object = objects[ i ];
  167. if ( object instanceof CSS3DSprite ) {
  168. object.element.style.display = '';
  169. object.visible = true;
  170. } else {
  171. object.element.style.display = 'none';
  172. object.visible = false;
  173. }
  174. }
  175. }
  176. function showBonds() {
  177. for ( let i = 0; i < objects.length; i ++ ) {
  178. const object = objects[ i ];
  179. if ( object instanceof CSS3DSprite ) {
  180. object.element.style.display = 'none';
  181. object.visible = false;
  182. } else {
  183. object.element.style.display = '';
  184. object.element.style.height = object.userData.bondLengthFull;
  185. object.visible = true;
  186. }
  187. }
  188. }
  189. function showAtomsBonds() {
  190. for ( let i = 0; i < objects.length; i ++ ) {
  191. const object = objects[ i ];
  192. object.element.style.display = '';
  193. object.visible = true;
  194. if ( ! ( object instanceof CSS3DSprite ) ) {
  195. object.element.style.height = object.userData.bondLengthShort;
  196. }
  197. }
  198. }
  199. //
  200. function colorify( ctx, width, height, color ) {
  201. const r = color.r, g = color.g, b = color.b;
  202. const imageData = ctx.getImageData( 0, 0, width, height );
  203. const data = imageData.data;
  204. for ( let i = 0, l = data.length; i < l; i += 4 ) {
  205. data[ i + 0 ] *= r;
  206. data[ i + 1 ] *= g;
  207. data[ i + 2 ] *= b;
  208. }
  209. ctx.putImageData( imageData, 0, 0 );
  210. }
  211. function imageToCanvas( image ) {
  212. const width = image.width;
  213. const height = image.height;
  214. const canvas = document.createElement( 'canvas' );
  215. canvas.width = width;
  216. canvas.height = height;
  217. const context = canvas.getContext( '2d' );
  218. context.drawImage( image, 0, 0, width, height );
  219. return canvas;
  220. }
  221. //
  222. function loadMolecule( url ) {
  223. for ( let i = 0; i < objects.length; i ++ ) {
  224. const object = objects[ i ];
  225. object.parent.remove( object );
  226. }
  227. objects.length = 0;
  228. loader.load( url, function ( pdb ) {
  229. const geometryAtoms = pdb.geometryAtoms;
  230. const geometryBonds = pdb.geometryBonds;
  231. const json = pdb.json;
  232. geometryAtoms.computeBoundingBox();
  233. geometryAtoms.boundingBox.getCenter( offset ).negate();
  234. geometryAtoms.translate( offset.x, offset.y, offset.z );
  235. geometryBonds.translate( offset.x, offset.y, offset.z );
  236. const positionAtoms = geometryAtoms.getAttribute( 'position' );
  237. const colorAtoms = geometryAtoms.getAttribute( 'color' );
  238. const position = new THREE.Vector3();
  239. const color = new THREE.Color();
  240. for ( let i = 0; i < positionAtoms.count; i ++ ) {
  241. position.fromBufferAttribute( positionAtoms, i );
  242. color.fromBufferAttribute( colorAtoms, i );
  243. const atomJSON = json.atoms[ i ];
  244. const element = atomJSON[ 4 ];
  245. if ( ! colorSpriteMap[ element ] ) {
  246. const canvas = imageToCanvas( baseSprite );
  247. const context = canvas.getContext( '2d' );
  248. colorify( context, canvas.width, canvas.height, color );
  249. const dataUrl = canvas.toDataURL();
  250. colorSpriteMap[ element ] = dataUrl;
  251. }
  252. const colorSprite = colorSpriteMap[ element ];
  253. const atom = document.createElement( 'img' );
  254. atom.src = colorSprite;
  255. const object = new CSS3DSprite( atom );
  256. object.position.copy( position );
  257. object.position.multiplyScalar( 75 );
  258. object.matrixAutoUpdate = false;
  259. object.updateMatrix();
  260. root.add( object );
  261. objects.push( object );
  262. }
  263. const positionBonds = geometryBonds.getAttribute( 'position' );
  264. const start = new THREE.Vector3();
  265. const end = new THREE.Vector3();
  266. for ( let i = 0; i < positionBonds.count; i += 2 ) {
  267. start.fromBufferAttribute( positionBonds, i );
  268. end.fromBufferAttribute( positionBonds, i + 1 );
  269. start.multiplyScalar( 75 );
  270. end.multiplyScalar( 75 );
  271. tmpVec1.subVectors( end, start );
  272. const bondLength = tmpVec1.length() - 50;
  273. //
  274. let bond = document.createElement( 'div' );
  275. bond.className = 'bond';
  276. bond.style.height = bondLength + 'px';
  277. let object = new CSS3DObject( bond );
  278. object.position.copy( start );
  279. object.position.lerp( end, 0.5 );
  280. object.userData.bondLengthShort = bondLength + 'px';
  281. object.userData.bondLengthFull = ( bondLength + 55 ) + 'px';
  282. //
  283. const axis = tmpVec2.set( 0, 1, 0 ).cross( tmpVec1 );
  284. const radians = Math.acos( tmpVec3.set( 0, 1, 0 ).dot( tmpVec4.copy( tmpVec1 ).normalize() ) );
  285. const objMatrix = new THREE.Matrix4().makeRotationAxis( axis.normalize(), radians );
  286. object.matrix.copy( objMatrix );
  287. object.quaternion.setFromRotationMatrix( object.matrix );
  288. object.matrixAutoUpdate = false;
  289. object.updateMatrix();
  290. root.add( object );
  291. objects.push( object );
  292. //
  293. bond = document.createElement( 'div' );
  294. bond.className = 'bond';
  295. bond.style.height = bondLength + 'px';
  296. const joint = new THREE.Object3D( bond );
  297. joint.position.copy( start );
  298. joint.position.lerp( end, 0.5 );
  299. joint.matrix.copy( objMatrix );
  300. joint.quaternion.setFromRotationMatrix( joint.matrix );
  301. joint.matrixAutoUpdate = false;
  302. joint.updateMatrix();
  303. object = new CSS3DObject( bond );
  304. object.rotation.y = Math.PI / 2;
  305. object.matrixAutoUpdate = false;
  306. object.updateMatrix();
  307. object.userData.bondLengthShort = bondLength + 'px';
  308. object.userData.bondLengthFull = ( bondLength + 55 ) + 'px';
  309. object.userData.joint = joint;
  310. joint.add( object );
  311. root.add( joint );
  312. objects.push( object );
  313. }
  314. //console.log( "CSS3DObjects:", objects.length );
  315. switch ( visualizationType ) {
  316. case 0:
  317. showAtoms();
  318. break;
  319. case 1:
  320. showBonds();
  321. break;
  322. case 2:
  323. showAtomsBonds();
  324. break;
  325. }
  326. } );
  327. }
  328. //
  329. function onWindowResize() {
  330. camera.aspect = window.innerWidth / window.innerHeight;
  331. camera.updateProjectionMatrix();
  332. renderer.setSize( window.innerWidth, window.innerHeight );
  333. }
  334. function animate() {
  335. requestAnimationFrame( animate );
  336. controls.update();
  337. const time = Date.now() * 0.0004;
  338. root.rotation.x = time;
  339. root.rotation.y = time * 0.7;
  340. render();
  341. }
  342. function render() {
  343. renderer.render( scene, camera );
  344. }
  345. </script>
  346. </body>
  347. </html>