webgl_materials_cubemap_refraction.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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.colorSpace = THREE.SRGBColorSpace;
  52. textureCube.mapping = THREE.CubeRefractionMapping;
  53. scene = new THREE.Scene();
  54. scene.background = textureCube;
  55. // LIGHTS
  56. const ambient = new THREE.AmbientLight( 0xffffff );
  57. scene.add( ambient );
  58. pointLight = new THREE.PointLight( 0xffffff, 2 );
  59. scene.add( pointLight );
  60. // light representation
  61. const sphere = new THREE.SphereGeometry( 100, 16, 8 );
  62. const mesh = new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
  63. mesh.scale.set( 0.05, 0.05, 0.05 );
  64. pointLight.add( mesh );
  65. // material samples
  66. const cubeMaterial3 = new THREE.MeshPhongMaterial( { color: 0xccddff, envMap: textureCube, refractionRatio: 0.98, reflectivity: 0.9 } );
  67. const cubeMaterial2 = new THREE.MeshPhongMaterial( { color: 0xccfffd, envMap: textureCube, refractionRatio: 0.985 } );
  68. const cubeMaterial1 = new THREE.MeshPhongMaterial( { color: 0xffffff, envMap: textureCube, refractionRatio: 0.98 } );
  69. //
  70. renderer = new THREE.WebGLRenderer();
  71. renderer.setPixelRatio( window.devicePixelRatio );
  72. renderer.setSize( window.innerWidth, window.innerHeight );
  73. container.appendChild( renderer.domElement );
  74. stats = new Stats();
  75. container.appendChild( stats.dom );
  76. const loader = new PLYLoader();
  77. loader.load( 'models/ply/binary/Lucy100k.ply', function ( geometry ) {
  78. createScene( geometry, cubeMaterial1, cubeMaterial2, cubeMaterial3 );
  79. } );
  80. document.addEventListener( 'mousemove', onDocumentMouseMove );
  81. //
  82. window.addEventListener( 'resize', onWindowResize );
  83. }
  84. function onWindowResize() {
  85. windowHalfX = window.innerWidth / 2;
  86. windowHalfY = window.innerHeight / 2;
  87. camera.aspect = window.innerWidth / window.innerHeight;
  88. camera.updateProjectionMatrix();
  89. renderer.setSize( window.innerWidth, window.innerHeight );
  90. }
  91. function createScene( geometry, m1, m2, m3 ) {
  92. geometry.computeVertexNormals();
  93. const s = 1.5;
  94. let mesh = new THREE.Mesh( geometry, m1 );
  95. mesh.scale.x = mesh.scale.y = mesh.scale.z = s;
  96. scene.add( mesh );
  97. mesh = new THREE.Mesh( geometry, m2 );
  98. mesh.position.x = - 1500;
  99. mesh.scale.x = mesh.scale.y = mesh.scale.z = s;
  100. scene.add( mesh );
  101. mesh = new THREE.Mesh( geometry, m3 );
  102. mesh.position.x = 1500;
  103. mesh.scale.x = mesh.scale.y = mesh.scale.z = s;
  104. scene.add( mesh );
  105. }
  106. function onDocumentMouseMove( event ) {
  107. mouseX = ( event.clientX - windowHalfX ) * 4;
  108. mouseY = ( event.clientY - windowHalfY ) * 4;
  109. }
  110. //
  111. function animate() {
  112. requestAnimationFrame( animate );
  113. render();
  114. stats.update();
  115. }
  116. function render() {
  117. const timer = - 0.0002 * Date.now();
  118. camera.position.x += ( mouseX - camera.position.x ) * .05;
  119. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  120. camera.lookAt( scene.position );
  121. pointLight.position.x = 1500 * Math.cos( timer );
  122. pointLight.position.z = 1500 * Math.sin( timer );
  123. renderer.render( scene, camera );
  124. }
  125. </script>
  126. </body>
  127. </html>