1
0

css3d_molecules.html 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. .bond {
  14. width: 5px;
  15. height: 10px;
  16. background: #eee;
  17. display: block;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div id="container"></div>
  23. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> css3d - molecules</div>
  24. <script type="importmap">
  25. {
  26. "imports": {
  27. "three": "../build/three.module.js",
  28. "three/addons/": "./jsm/"
  29. }
  30. }
  31. </script>
  32. <script type="module">
  33. import * as THREE from 'three';
  34. import { TrackballControls } from 'three/addons/controls/TrackballControls.js';
  35. import { PDBLoader } from 'three/addons/loaders/PDBLoader.js';
  36. import { CSS3DRenderer, CSS3DObject, CSS3DSprite } from 'three/addons/renderers/CSS3DRenderer.js';
  37. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  38. let camera, scene, renderer;
  39. let controls;
  40. let root;
  41. const objects = [];
  42. const tmpVec1 = new THREE.Vector3();
  43. const tmpVec2 = new THREE.Vector3();
  44. const tmpVec3 = new THREE.Vector3();
  45. const tmpVec4 = new THREE.Vector3();
  46. const offset = new THREE.Vector3();
  47. const VIZ_TYPE = {
  48. 'Atoms': 0,
  49. 'Bonds': 1,
  50. 'Atoms + Bonds': 2
  51. };
  52. const MOLECULES = {
  53. 'Ethanol': 'ethanol.pdb',
  54. 'Aspirin': 'aspirin.pdb',
  55. 'Caffeine': 'caffeine.pdb',
  56. 'Nicotine': 'nicotine.pdb',
  57. 'LSD': 'lsd.pdb',
  58. 'Cocaine': 'cocaine.pdb',
  59. 'Cholesterol': 'cholesterol.pdb',
  60. 'Lycopene': 'lycopene.pdb',
  61. 'Glucose': 'glucose.pdb',
  62. 'Aluminium oxide': 'Al2O3.pdb',
  63. 'Cubane': 'cubane.pdb',
  64. 'Copper': 'cu.pdb',
  65. 'Fluorite': 'caf2.pdb',
  66. 'Salt': 'nacl.pdb',
  67. 'YBCO superconductor': 'ybco.pdb',
  68. 'Buckyball': 'buckyball.pdb',
  69. // 'Diamond': 'diamond.pdb',
  70. 'Graphite': 'graphite.pdb'
  71. };
  72. const params = {
  73. vizType: 2,
  74. molecule: 'caffeine.pdb'
  75. };
  76. const loader = new PDBLoader();
  77. const colorSpriteMap = {};
  78. const baseSprite = document.createElement( 'img' );
  79. init();
  80. animate();
  81. function init() {
  82. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 5000 );
  83. camera.position.z = 1000;
  84. scene = new THREE.Scene();
  85. root = new THREE.Object3D();
  86. scene.add( root );
  87. //
  88. renderer = new CSS3DRenderer();
  89. renderer.setSize( window.innerWidth, window.innerHeight );
  90. document.getElementById( 'container' ).appendChild( renderer.domElement );
  91. //
  92. controls = new TrackballControls( camera, renderer.domElement );
  93. controls.rotateSpeed = 0.5;
  94. //
  95. baseSprite.onload = function () {
  96. loadMolecule( params.molecule );
  97. };
  98. baseSprite.src = 'textures/sprites/ball.png';
  99. //
  100. window.addEventListener( 'resize', onWindowResize );
  101. //
  102. const gui = new GUI();
  103. gui.add( params, 'vizType', VIZ_TYPE ).onChange( changeVizType );
  104. gui.add( params, 'molecule', MOLECULES ).onChange( loadMolecule );
  105. gui.open();
  106. }
  107. function changeVizType( value ) {
  108. if ( value === 0 ) showAtoms();
  109. else if ( value === 1 ) showBonds();
  110. else showAtomsBonds();
  111. }
  112. //
  113. function showAtoms() {
  114. for ( let i = 0; i < objects.length; i ++ ) {
  115. const object = objects[ i ];
  116. if ( object instanceof CSS3DSprite ) {
  117. object.element.style.display = '';
  118. object.visible = true;
  119. } else {
  120. object.element.style.display = 'none';
  121. object.visible = false;
  122. }
  123. }
  124. }
  125. function showBonds() {
  126. for ( let i = 0; i < objects.length; i ++ ) {
  127. const object = objects[ i ];
  128. if ( object instanceof CSS3DSprite ) {
  129. object.element.style.display = 'none';
  130. object.visible = false;
  131. } else {
  132. object.element.style.display = '';
  133. object.element.style.height = object.userData.bondLengthFull;
  134. object.visible = true;
  135. }
  136. }
  137. }
  138. function showAtomsBonds() {
  139. for ( let i = 0; i < objects.length; i ++ ) {
  140. const object = objects[ i ];
  141. object.element.style.display = '';
  142. object.visible = true;
  143. if ( ! ( object instanceof CSS3DSprite ) ) {
  144. object.element.style.height = object.userData.bondLengthShort;
  145. }
  146. }
  147. }
  148. //
  149. function colorify( ctx, width, height, color ) {
  150. const r = color.r, g = color.g, b = color.b;
  151. const imageData = ctx.getImageData( 0, 0, width, height );
  152. const data = imageData.data;
  153. for ( let i = 0, l = data.length; i < l; i += 4 ) {
  154. data[ i + 0 ] *= r;
  155. data[ i + 1 ] *= g;
  156. data[ i + 2 ] *= b;
  157. }
  158. ctx.putImageData( imageData, 0, 0 );
  159. }
  160. function imageToCanvas( image ) {
  161. const width = image.width;
  162. const height = image.height;
  163. const canvas = document.createElement( 'canvas' );
  164. canvas.width = width;
  165. canvas.height = height;
  166. const context = canvas.getContext( '2d' );
  167. context.drawImage( image, 0, 0, width, height );
  168. return canvas;
  169. }
  170. //
  171. function loadMolecule( model ) {
  172. const url = 'models/pdb/' + model;
  173. for ( let i = 0; i < objects.length; i ++ ) {
  174. const object = objects[ i ];
  175. object.parent.remove( object );
  176. }
  177. objects.length = 0;
  178. loader.load( url, function ( pdb ) {
  179. const geometryAtoms = pdb.geometryAtoms;
  180. const geometryBonds = pdb.geometryBonds;
  181. const json = pdb.json;
  182. geometryAtoms.computeBoundingBox();
  183. geometryAtoms.boundingBox.getCenter( offset ).negate();
  184. geometryAtoms.translate( offset.x, offset.y, offset.z );
  185. geometryBonds.translate( offset.x, offset.y, offset.z );
  186. const positionAtoms = geometryAtoms.getAttribute( 'position' );
  187. const colorAtoms = geometryAtoms.getAttribute( 'color' );
  188. const position = new THREE.Vector3();
  189. const color = new THREE.Color();
  190. for ( let i = 0; i < positionAtoms.count; i ++ ) {
  191. position.fromBufferAttribute( positionAtoms, i );
  192. color.fromBufferAttribute( colorAtoms, i );
  193. const atomJSON = json.atoms[ i ];
  194. const element = atomJSON[ 4 ];
  195. if ( ! colorSpriteMap[ element ] ) {
  196. const canvas = imageToCanvas( baseSprite );
  197. const context = canvas.getContext( '2d' );
  198. colorify( context, canvas.width, canvas.height, color );
  199. const dataUrl = canvas.toDataURL();
  200. colorSpriteMap[ element ] = dataUrl;
  201. }
  202. const colorSprite = colorSpriteMap[ element ];
  203. const atom = document.createElement( 'img' );
  204. atom.src = colorSprite;
  205. const object = new CSS3DSprite( atom );
  206. object.position.copy( position );
  207. object.position.multiplyScalar( 75 );
  208. object.matrixAutoUpdate = false;
  209. object.updateMatrix();
  210. root.add( object );
  211. objects.push( object );
  212. }
  213. const positionBonds = geometryBonds.getAttribute( 'position' );
  214. const start = new THREE.Vector3();
  215. const end = new THREE.Vector3();
  216. for ( let i = 0; i < positionBonds.count; i += 2 ) {
  217. start.fromBufferAttribute( positionBonds, i );
  218. end.fromBufferAttribute( positionBonds, i + 1 );
  219. start.multiplyScalar( 75 );
  220. end.multiplyScalar( 75 );
  221. tmpVec1.subVectors( end, start );
  222. const bondLength = tmpVec1.length() - 50;
  223. //
  224. let bond = document.createElement( 'div' );
  225. bond.className = 'bond';
  226. bond.style.height = bondLength + 'px';
  227. let object = new CSS3DObject( bond );
  228. object.position.copy( start );
  229. object.position.lerp( end, 0.5 );
  230. object.userData.bondLengthShort = bondLength + 'px';
  231. object.userData.bondLengthFull = ( bondLength + 55 ) + 'px';
  232. //
  233. const axis = tmpVec2.set( 0, 1, 0 ).cross( tmpVec1 );
  234. const radians = Math.acos( tmpVec3.set( 0, 1, 0 ).dot( tmpVec4.copy( tmpVec1 ).normalize() ) );
  235. const objMatrix = new THREE.Matrix4().makeRotationAxis( axis.normalize(), radians );
  236. object.matrix.copy( objMatrix );
  237. object.quaternion.setFromRotationMatrix( object.matrix );
  238. object.matrixAutoUpdate = false;
  239. object.updateMatrix();
  240. root.add( object );
  241. objects.push( object );
  242. //
  243. const joint = new THREE.Object3D();
  244. joint.position.copy( start );
  245. joint.position.lerp( end, 0.5 );
  246. joint.matrix.copy( objMatrix );
  247. joint.quaternion.setFromRotationMatrix( joint.matrix );
  248. joint.matrixAutoUpdate = false;
  249. joint.updateMatrix();
  250. bond = document.createElement( 'div' );
  251. bond.className = 'bond';
  252. bond.style.height = bondLength + 'px';
  253. object = new CSS3DObject( bond );
  254. object.rotation.y = Math.PI / 2;
  255. object.matrixAutoUpdate = false;
  256. object.updateMatrix();
  257. object.userData.bondLengthShort = bondLength + 'px';
  258. object.userData.bondLengthFull = ( bondLength + 55 ) + 'px';
  259. object.userData.joint = joint;
  260. joint.add( object );
  261. root.add( joint );
  262. objects.push( object );
  263. }
  264. //console.log( "CSS3DObjects:", objects.length );
  265. changeVizType( params.vizType );
  266. } );
  267. }
  268. //
  269. function onWindowResize() {
  270. camera.aspect = window.innerWidth / window.innerHeight;
  271. camera.updateProjectionMatrix();
  272. renderer.setSize( window.innerWidth, window.innerHeight );
  273. }
  274. function animate() {
  275. requestAnimationFrame( animate );
  276. controls.update();
  277. const time = Date.now() * 0.0004;
  278. root.rotation.x = time;
  279. root.rotation.y = time * 0.7;
  280. render();
  281. }
  282. function render() {
  283. renderer.render( scene, camera );
  284. }
  285. </script>
  286. </body>
  287. </html>