css3d_molecules.html 10.0 KB

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