webgl_materials_cubemap_refraction.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - cube refraction [Lucy]</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">Three.js</a> cube map refraction demo<br/>
  12. Lucy model from <a href="http://graphics.stanford.edu/data/3Dscanrep/">Stanford 3d scanning repository<br/>
  13. Texture by <a href="http://www.humus.name/index.php?page=Textures" target="_blank" rel="noopener">Humus</a>
  14. </div>
  15. <script type="module">
  16. import * as THREE from '../build/three.module.js';
  17. import Stats from './jsm/libs/stats.module.js';
  18. import { PLYLoader } from './jsm/loaders/PLYLoader.js';
  19. let container, stats;
  20. let camera, scene, renderer;
  21. let pointLight;
  22. let mouseX = 0, mouseY = 0;
  23. let windowHalfX = window.innerWidth / 2;
  24. let windowHalfY = window.innerHeight / 2;
  25. init();
  26. animate();
  27. function init() {
  28. container = document.createElement( 'div' );
  29. document.body.appendChild( container );
  30. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 100000 );
  31. camera.position.z = - 4000;
  32. //
  33. const r = "textures/cube/Park3Med/";
  34. const urls = [
  35. r + "px.jpg", r + "nx.jpg",
  36. r + "py.jpg", r + "ny.jpg",
  37. r + "pz.jpg", r + "nz.jpg"
  38. ];
  39. const textureCube = new THREE.CubeTextureLoader().load( urls );
  40. textureCube.mapping = THREE.CubeRefractionMapping;
  41. scene = new THREE.Scene();
  42. scene.background = textureCube;
  43. // LIGHTS
  44. const ambient = new THREE.AmbientLight( 0xffffff );
  45. scene.add( ambient );
  46. pointLight = new THREE.PointLight( 0xffffff, 2 );
  47. scene.add( pointLight );
  48. // light representation
  49. const sphere = new THREE.SphereGeometry( 100, 16, 8 );
  50. const mesh = new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
  51. mesh.scale.set( 0.05, 0.05, 0.05 );
  52. pointLight.add( mesh );
  53. // material samples
  54. const cubeMaterial3 = new THREE.MeshPhongMaterial( { color: 0xccddff, envMap: textureCube, refractionRatio: 0.98, reflectivity: 0.9 } );
  55. const cubeMaterial2 = new THREE.MeshPhongMaterial( { color: 0xccfffd, envMap: textureCube, refractionRatio: 0.985 } );
  56. const cubeMaterial1 = new THREE.MeshPhongMaterial( { color: 0xffffff, envMap: textureCube, refractionRatio: 0.98 } );
  57. //
  58. renderer = new THREE.WebGLRenderer();
  59. renderer.setPixelRatio( window.devicePixelRatio );
  60. renderer.setSize( window.innerWidth, window.innerHeight );
  61. container.appendChild( renderer.domElement );
  62. stats = new Stats();
  63. container.appendChild( stats.dom );
  64. const loader = new PLYLoader();
  65. loader.load( 'models/ply/binary/Lucy100k.ply', function ( geometry ) {
  66. createScene( geometry, cubeMaterial1, cubeMaterial2, cubeMaterial3 );
  67. } );
  68. document.addEventListener( 'mousemove', onDocumentMouseMove );
  69. //
  70. window.addEventListener( 'resize', onWindowResize );
  71. }
  72. function onWindowResize() {
  73. windowHalfX = window.innerWidth / 2;
  74. windowHalfY = window.innerHeight / 2;
  75. camera.aspect = window.innerWidth / window.innerHeight;
  76. camera.updateProjectionMatrix();
  77. renderer.setSize( window.innerWidth, window.innerHeight );
  78. }
  79. function createScene( geometry, m1, m2, m3 ) {
  80. geometry.computeVertexNormals();
  81. const s = 1.5;
  82. let mesh = new THREE.Mesh( geometry, m1 );
  83. mesh.scale.x = mesh.scale.y = mesh.scale.z = s;
  84. scene.add( mesh );
  85. mesh = new THREE.Mesh( geometry, m2 );
  86. mesh.position.x = - 1500;
  87. mesh.scale.x = mesh.scale.y = mesh.scale.z = s;
  88. scene.add( mesh );
  89. mesh = new THREE.Mesh( geometry, m3 );
  90. mesh.position.x = 1500;
  91. mesh.scale.x = mesh.scale.y = mesh.scale.z = s;
  92. scene.add( mesh );
  93. }
  94. function onDocumentMouseMove( event ) {
  95. mouseX = ( event.clientX - windowHalfX ) * 4;
  96. mouseY = ( event.clientY - windowHalfY ) * 4;
  97. }
  98. //
  99. function animate() {
  100. requestAnimationFrame( animate );
  101. render();
  102. stats.update();
  103. }
  104. function render() {
  105. const timer = - 0.0002 * Date.now();
  106. camera.position.x += ( mouseX - camera.position.x ) * .05;
  107. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  108. camera.lookAt( scene.position );
  109. pointLight.position.x = 1500 * Math.cos( timer );
  110. pointLight.position.z = 1500 * Math.sin( timer );
  111. renderer.render( scene, camera );
  112. }
  113. </script>
  114. </body>
  115. </html>