webgl_loader_pdb.html 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - 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. #menu {
  10. position: absolute;
  11. bottom: 20px;
  12. width: 100%;
  13. text-align: center;
  14. padding: 0;
  15. margin: 0;
  16. }
  17. button {
  18. color: rgb(255,255,255);
  19. background: transparent;
  20. border: 0px;
  21. padding: 5px 10px;
  22. cursor: pointer;
  23. }
  24. button:hover {
  25. background-color: rgba(0,255,255,0.5);
  26. }
  27. button:active {
  28. color: #000000;
  29. background-color: rgba(0,255,255,1);
  30. }
  31. .label {
  32. text-shadow: -1px 1px 1px rgb(0,0,0);
  33. margin-left: 25px;
  34. font-size: 20px;
  35. }
  36. </style>
  37. </head>
  38. <body>
  39. <div id="container"></div>
  40. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js webgl</a> - molecules</div>
  41. <div id="menu"></div>
  42. <script type="module">
  43. import * as THREE from '../build/three.module.js';
  44. import { TrackballControls } from './jsm/controls/TrackballControls.js';
  45. import { PDBLoader } from './jsm/loaders/PDBLoader.js';
  46. import { CSS2DRenderer, CSS2DObject } from './jsm/renderers/CSS2DRenderer.js';
  47. var camera, scene, renderer, labelRenderer;
  48. var controls;
  49. var root;
  50. var MOLECULES = {
  51. "Ethanol": "ethanol.pdb",
  52. "Aspirin": "aspirin.pdb",
  53. "Caffeine": "caffeine.pdb",
  54. "Nicotine": "nicotine.pdb",
  55. "LSD": "lsd.pdb",
  56. "Cocaine": "cocaine.pdb",
  57. "Cholesterol": "cholesterol.pdb",
  58. "Lycopene": "lycopene.pdb",
  59. "Glucose": "glucose.pdb",
  60. "Aluminium oxide": "Al2O3.pdb",
  61. "Cubane": "cubane.pdb",
  62. "Copper": "cu.pdb",
  63. "Fluorite": "caf2.pdb",
  64. "Salt": "nacl.pdb",
  65. "YBCO superconductor": "ybco.pdb",
  66. "Buckyball": "buckyball.pdb",
  67. "Graphite": "graphite.pdb"
  68. };
  69. var loader = new PDBLoader();
  70. var offset = new THREE.Vector3();
  71. var menu = document.getElementById( 'menu' );
  72. init();
  73. animate();
  74. function init() {
  75. scene = new THREE.Scene();
  76. scene.background = new THREE.Color( 0x050505 );
  77. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 5000 );
  78. camera.position.z = 1000;
  79. scene.add( camera );
  80. var light = new THREE.DirectionalLight( 0xffffff, 0.8 );
  81. light.position.set( 1, 1, 1 );
  82. scene.add( light );
  83. var light = new THREE.DirectionalLight( 0xffffff, 0.5 );
  84. light.position.set( - 1, - 1, 1 );
  85. scene.add( light );
  86. root = new THREE.Group();
  87. scene.add( root );
  88. //
  89. renderer = new THREE.WebGLRenderer( { antialias: true } );
  90. renderer.setPixelRatio( window.devicePixelRatio );
  91. renderer.setSize( window.innerWidth, window.innerHeight );
  92. document.getElementById( 'container' ).appendChild( renderer.domElement );
  93. labelRenderer = new CSS2DRenderer();
  94. labelRenderer.setSize( window.innerWidth, window.innerHeight );
  95. labelRenderer.domElement.style.position = 'absolute';
  96. labelRenderer.domElement.style.top = '0';
  97. labelRenderer.domElement.style.pointerEvents = 'none';
  98. document.getElementById( 'container' ).appendChild( labelRenderer.domElement );
  99. //
  100. controls = new TrackballControls( camera, renderer.domElement );
  101. controls.minDistance = 500;
  102. controls.maxDistance = 2000;
  103. //
  104. loadMolecule( 'models/molecules/caffeine.pdb' );
  105. createMenu();
  106. //
  107. window.addEventListener( 'resize', onWindowResize, false );
  108. }
  109. //
  110. function generateButtonCallback( url ) {
  111. return function () {
  112. loadMolecule( url );
  113. };
  114. }
  115. function createMenu() {
  116. for ( var m in MOLECULES ) {
  117. var button = document.createElement( 'button' );
  118. button.innerHTML = m;
  119. menu.appendChild( button );
  120. var url = 'models/molecules/' + MOLECULES[ m ];
  121. button.addEventListener( 'click', generateButtonCallback( url ), false );
  122. }
  123. }
  124. //
  125. function loadMolecule( url ) {
  126. while ( root.children.length > 0 ) {
  127. var object = root.children[ 0 ];
  128. object.parent.remove( object );
  129. }
  130. loader.load( url, function ( pdb ) {
  131. var geometryAtoms = pdb.geometryAtoms;
  132. var geometryBonds = pdb.geometryBonds;
  133. var json = pdb.json;
  134. var boxGeometry = new THREE.BoxBufferGeometry( 1, 1, 1 );
  135. var sphereGeometry = new THREE.IcosahedronBufferGeometry( 1, 2 );
  136. geometryAtoms.computeBoundingBox();
  137. geometryAtoms.boundingBox.getCenter( offset ).negate();
  138. geometryAtoms.translate( offset.x, offset.y, offset.z );
  139. geometryBonds.translate( offset.x, offset.y, offset.z );
  140. var positions = geometryAtoms.getAttribute( 'position' );
  141. var colors = geometryAtoms.getAttribute( 'color' );
  142. var position = new THREE.Vector3();
  143. var color = new THREE.Color();
  144. for ( var i = 0; i < positions.count; i ++ ) {
  145. position.x = positions.getX( i );
  146. position.y = positions.getY( i );
  147. position.z = positions.getZ( i );
  148. color.r = colors.getX( i );
  149. color.g = colors.getY( i );
  150. color.b = colors.getZ( i );
  151. var material = new THREE.MeshPhongMaterial( { color: color } );
  152. var object = new THREE.Mesh( sphereGeometry, material );
  153. object.position.copy( position );
  154. object.position.multiplyScalar( 75 );
  155. object.scale.multiplyScalar( 25 );
  156. root.add( object );
  157. var atom = json.atoms[ i ];
  158. var text = document.createElement( 'div' );
  159. text.className = 'label';
  160. text.style.color = 'rgb(' + atom[ 3 ][ 0 ] + ',' + atom[ 3 ][ 1 ] + ',' + atom[ 3 ][ 2 ] + ')';
  161. text.textContent = atom[ 4 ];
  162. var label = new CSS2DObject( text );
  163. label.position.copy( object.position );
  164. root.add( label );
  165. }
  166. positions = geometryBonds.getAttribute( 'position' );
  167. var start = new THREE.Vector3();
  168. var end = new THREE.Vector3();
  169. for ( var i = 0; i < positions.count; i += 2 ) {
  170. start.x = positions.getX( i );
  171. start.y = positions.getY( i );
  172. start.z = positions.getZ( i );
  173. end.x = positions.getX( i + 1 );
  174. end.y = positions.getY( i + 1 );
  175. end.z = positions.getZ( i + 1 );
  176. start.multiplyScalar( 75 );
  177. end.multiplyScalar( 75 );
  178. var object = new THREE.Mesh( boxGeometry, new THREE.MeshPhongMaterial( 0xffffff ) );
  179. object.position.copy( start );
  180. object.position.lerp( end, 0.5 );
  181. object.scale.set( 5, 5, start.distanceTo( end ) );
  182. object.lookAt( end );
  183. root.add( object );
  184. }
  185. render();
  186. } );
  187. }
  188. //
  189. function onWindowResize() {
  190. camera.aspect = window.innerWidth / window.innerHeight;
  191. camera.updateProjectionMatrix();
  192. renderer.setSize( window.innerWidth, window.innerHeight );
  193. labelRenderer.setSize( window.innerWidth, window.innerHeight );
  194. render();
  195. }
  196. function animate() {
  197. requestAnimationFrame( animate );
  198. controls.update();
  199. var time = Date.now() * 0.0004;
  200. root.rotation.x = time;
  201. root.rotation.y = time * 0.7;
  202. render();
  203. }
  204. function render() {
  205. renderer.render( scene, camera );
  206. labelRenderer.render( scene, camera );
  207. }
  208. </script>
  209. </body>
  210. </html>