webgl_materials_shaders_fresnel.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - shaders [Fresnel]</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 Fresnel shader demo.<br/>texture by <a href="http://www.humus.name/index.php?page=Textures" target="_blank" rel="noopener">Humus</a>
  12. </div>
  13. <script type="module">
  14. import * as THREE from '../build/three.module.js';
  15. import { FresnelShader } from './jsm/shaders/FresnelShader.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. //
  31. var path = "textures/cube/Park2/";
  32. var format = '.jpg';
  33. var urls = [
  34. path + 'posx' + format, path + 'negx' + format,
  35. path + 'posy' + format, path + 'negy' + format,
  36. path + 'posz' + format, path + 'negz' + format
  37. ];
  38. var textureCube = new THREE.CubeTextureLoader().load( urls );
  39. textureCube.format = THREE.RGBFormat;
  40. scene = new THREE.Scene();
  41. scene.background = textureCube;
  42. //
  43. var geometry = new THREE.SphereBufferGeometry( 100, 32, 16 );
  44. var shader = FresnelShader;
  45. var uniforms = THREE.UniformsUtils.clone( shader.uniforms );
  46. uniforms[ "tCube" ].value = textureCube;
  47. var material = new THREE.ShaderMaterial( {
  48. uniforms: uniforms,
  49. vertexShader: shader.vertexShader,
  50. fragmentShader: shader.fragmentShader
  51. } );
  52. for ( var i = 0; i < 500; i ++ ) {
  53. var mesh = new THREE.Mesh( geometry, material );
  54. mesh.position.x = Math.random() * 10000 - 5000;
  55. mesh.position.y = Math.random() * 10000 - 5000;
  56. mesh.position.z = Math.random() * 10000 - 5000;
  57. mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 3 + 1;
  58. scene.add( mesh );
  59. spheres.push( mesh );
  60. }
  61. //
  62. renderer = new THREE.WebGLRenderer();
  63. renderer.setPixelRatio( window.devicePixelRatio );
  64. renderer.setSize( window.innerWidth, window.innerHeight );
  65. container.appendChild( renderer.domElement );
  66. //
  67. window.addEventListener( 'resize', onWindowResize, false );
  68. }
  69. function onWindowResize() {
  70. windowHalfX = window.innerWidth / 2;
  71. windowHalfY = window.innerHeight / 2;
  72. camera.aspect = window.innerWidth / window.innerHeight;
  73. camera.updateProjectionMatrix();
  74. renderer.setSize( window.innerWidth, window.innerHeight );
  75. }
  76. function onDocumentMouseMove( event ) {
  77. mouseX = ( event.clientX - windowHalfX ) * 10;
  78. mouseY = ( event.clientY - windowHalfY ) * 10;
  79. }
  80. //
  81. function animate() {
  82. requestAnimationFrame( animate );
  83. render();
  84. }
  85. function render() {
  86. var timer = 0.0001 * Date.now();
  87. camera.position.x += ( mouseX - camera.position.x ) * .05;
  88. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  89. camera.lookAt( scene.position );
  90. for ( var i = 0, il = spheres.length; i < il; i ++ ) {
  91. var sphere = spheres[ i ];
  92. sphere.position.x = 5000 * Math.cos( timer + i );
  93. sphere.position.y = 5000 * Math.sin( timer + i * 1.1 );
  94. }
  95. renderer.render( scene, camera );
  96. }
  97. </script>
  98. </body>
  99. </html>