webgl_materials_cubemap_balls_refraction.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - cube refraction [balls]</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. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl cube refraction demo.<br/>
  12. skybox by <a href="http://www.zfight.com/" target="_blank" rel="noopener">Jochum Skoglund</a>
  13. </div>
  14. <script type="module">
  15. import * as THREE from '../build/three.module.js';
  16. var container;
  17. var camera, scene, renderer;
  18. var spheres = [];
  19. var mouseX = 0, mouseY = 0;
  20. var windowHalfX = window.innerWidth / 2;
  21. var windowHalfY = window.innerHeight / 2;
  22. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  23. init();
  24. animate();
  25. function init() {
  26. container = document.createElement( 'div' );
  27. document.body.appendChild( container );
  28. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 100000 );
  29. camera.position.z = 3200;
  30. scene = new THREE.Scene();
  31. scene.background = new THREE.CubeTextureLoader()
  32. .setPath( 'textures/cube/Park3Med/' )
  33. .load( [ 'px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg' ] );
  34. var geometry = new THREE.SphereBufferGeometry( 100, 32, 16 );
  35. var material = new THREE.MeshBasicMaterial( { color: 0xffffff, envMap: scene.background, refractionRatio: 0.95 } );
  36. material.envMap.mapping = THREE.CubeRefractionMapping;
  37. for ( var i = 0; i < 500; i ++ ) {
  38. var mesh = new THREE.Mesh( geometry, material );
  39. mesh.position.x = Math.random() * 10000 - 5000;
  40. mesh.position.y = Math.random() * 10000 - 5000;
  41. mesh.position.z = Math.random() * 10000 - 5000;
  42. mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 3 + 1;
  43. scene.add( mesh );
  44. spheres.push( mesh );
  45. }
  46. //
  47. renderer = new THREE.WebGLRenderer();
  48. renderer.setPixelRatio( window.devicePixelRatio );
  49. renderer.setSize( window.innerWidth, window.innerHeight );
  50. container.appendChild( renderer.domElement );
  51. //
  52. window.addEventListener( 'resize', onWindowResize, false );
  53. }
  54. function onWindowResize() {
  55. windowHalfX = window.innerWidth / 2;
  56. windowHalfY = window.innerHeight / 2;
  57. camera.aspect = window.innerWidth / window.innerHeight;
  58. camera.updateProjectionMatrix();
  59. renderer.setSize( window.innerWidth, window.innerHeight );
  60. }
  61. function onDocumentMouseMove( event ) {
  62. mouseX = ( event.clientX - windowHalfX ) * 10;
  63. mouseY = ( event.clientY - windowHalfY ) * 10;
  64. }
  65. //
  66. function animate() {
  67. requestAnimationFrame( animate );
  68. render();
  69. }
  70. function render() {
  71. var timer = 0.0001 * Date.now();
  72. for ( var i = 0, il = spheres.length; i < il; i ++ ) {
  73. var sphere = spheres[ i ];
  74. sphere.position.x = 5000 * Math.cos( timer + i );
  75. sphere.position.y = 5000 * Math.sin( timer + i * 1.1 );
  76. }
  77. camera.position.x += ( mouseX - camera.position.x ) * .05;
  78. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  79. camera.lookAt( scene.position );
  80. renderer.render( scene, camera );
  81. }
  82. </script>
  83. </body>
  84. </html>