webgl_materials_cubemap_refraction.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. <!-- Import maps polyfill -->
  16. <!-- Remove this when import maps will be widely supported -->
  17. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  18. <script type="importmap">
  19. {
  20. "imports": {
  21. "three": "../build/three.module.js",
  22. "three/addons/": "./jsm/"
  23. }
  24. }
  25. </script>
  26. <script type="module">
  27. import * as THREE from 'three';
  28. import Stats from 'three/addons/libs/stats.module.js';
  29. import { PLYLoader } from 'three/addons/loaders/PLYLoader.js';
  30. let container, stats;
  31. let camera, scene, renderer;
  32. let pointLight;
  33. let mouseX = 0, mouseY = 0;
  34. let windowHalfX = window.innerWidth / 2;
  35. let windowHalfY = window.innerHeight / 2;
  36. init();
  37. animate();
  38. function init() {
  39. container = document.createElement( 'div' );
  40. document.body.appendChild( container );
  41. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 100000 );
  42. camera.position.z = - 4000;
  43. //
  44. const r = 'textures/cube/Park3Med/';
  45. const urls = [
  46. r + 'px.jpg', r + 'nx.jpg',
  47. r + 'py.jpg', r + 'ny.jpg',
  48. r + 'pz.jpg', r + 'nz.jpg'
  49. ];
  50. const textureCube = new THREE.CubeTextureLoader().load( urls );
  51. textureCube.mapping = THREE.CubeRefractionMapping;
  52. scene = new THREE.Scene();
  53. scene.background = textureCube;
  54. // LIGHTS
  55. const ambient = new THREE.AmbientLight( 0xffffff );
  56. scene.add( ambient );
  57. pointLight = new THREE.PointLight( 0xffffff, 2 );
  58. scene.add( pointLight );
  59. // light representation
  60. const sphere = new THREE.SphereGeometry( 100, 16, 8 );
  61. const mesh = new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
  62. mesh.scale.set( 0.05, 0.05, 0.05 );
  63. pointLight.add( mesh );
  64. // material samples
  65. const cubeMaterial3 = new THREE.MeshPhongMaterial( { color: 0xccddff, envMap: textureCube, refractionRatio: 0.98, reflectivity: 0.9 } );
  66. const cubeMaterial2 = new THREE.MeshPhongMaterial( { color: 0xccfffd, envMap: textureCube, refractionRatio: 0.985 } );
  67. const cubeMaterial1 = new THREE.MeshPhongMaterial( { color: 0xffffff, envMap: textureCube, refractionRatio: 0.98 } );
  68. //
  69. renderer = new THREE.WebGLRenderer();
  70. renderer.setPixelRatio( window.devicePixelRatio );
  71. renderer.setSize( window.innerWidth, window.innerHeight );
  72. container.appendChild( renderer.domElement );
  73. stats = new Stats();
  74. container.appendChild( stats.dom );
  75. const loader = new PLYLoader();
  76. loader.load( 'models/ply/binary/Lucy100k.ply', function ( geometry ) {
  77. createScene( geometry, cubeMaterial1, cubeMaterial2, cubeMaterial3 );
  78. } );
  79. document.addEventListener( 'mousemove', onDocumentMouseMove );
  80. //
  81. window.addEventListener( 'resize', onWindowResize );
  82. }
  83. function onWindowResize() {
  84. windowHalfX = window.innerWidth / 2;
  85. windowHalfY = window.innerHeight / 2;
  86. camera.aspect = window.innerWidth / window.innerHeight;
  87. camera.updateProjectionMatrix();
  88. renderer.setSize( window.innerWidth, window.innerHeight );
  89. }
  90. function createScene( geometry, m1, m2, m3 ) {
  91. geometry.computeVertexNormals();
  92. const s = 1.5;
  93. let mesh = new THREE.Mesh( geometry, m1 );
  94. mesh.scale.x = mesh.scale.y = mesh.scale.z = s;
  95. scene.add( mesh );
  96. mesh = new THREE.Mesh( geometry, m2 );
  97. mesh.position.x = - 1500;
  98. mesh.scale.x = mesh.scale.y = mesh.scale.z = s;
  99. scene.add( mesh );
  100. mesh = new THREE.Mesh( geometry, m3 );
  101. mesh.position.x = 1500;
  102. mesh.scale.x = mesh.scale.y = mesh.scale.z = s;
  103. scene.add( mesh );
  104. }
  105. function onDocumentMouseMove( event ) {
  106. mouseX = ( event.clientX - windowHalfX ) * 4;
  107. mouseY = ( event.clientY - windowHalfY ) * 4;
  108. }
  109. //
  110. function animate() {
  111. requestAnimationFrame( animate );
  112. render();
  113. stats.update();
  114. }
  115. function render() {
  116. const timer = - 0.0002 * Date.now();
  117. camera.position.x += ( mouseX - camera.position.x ) * .05;
  118. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  119. camera.lookAt( scene.position );
  120. pointLight.position.x = 1500 * Math.cos( timer );
  121. pointLight.position.z = 1500 * Math.sin( timer );
  122. renderer.render( scene, camera );
  123. }
  124. </script>
  125. </body>
  126. </html>