webgl_materials_cubemap.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - cube reflection / refraction [Walt]</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="container"></div>
  11. <div id="info">
  12. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - cube mapping demo.<br />
  13. Texture by <a href="http://www.humus.name/index.php?page=Textures" target="_blank" rel="noopener">Humus</a>, Walt Disney head by <a href="http://web.archive.org/web/20120903131400/http://davidoreilly.com/post/18087489343/disneyhead" target="_blank" rel="noopener">David OReilly</a>
  14. </div>
  15. <script type="importmap">
  16. {
  17. "imports": {
  18. "three": "../build/three.module.js",
  19. "three/addons/": "./jsm/"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import Stats from 'three/addons/libs/stats.module.js';
  26. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  27. import { OBJLoader } from 'three/addons/loaders/OBJLoader.js';
  28. let container, stats;
  29. let camera, scene, renderer;
  30. let pointLight;
  31. init();
  32. function init() {
  33. container = document.createElement( 'div' );
  34. document.body.appendChild( container );
  35. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 100 );
  36. camera.position.z = 13;
  37. //cubemap
  38. const path = 'textures/cube/SwedishRoyalCastle/';
  39. const format = '.jpg';
  40. const urls = [
  41. path + 'px' + format, path + 'nx' + format,
  42. path + 'py' + format, path + 'ny' + format,
  43. path + 'pz' + format, path + 'nz' + format
  44. ];
  45. const reflectionCube = new THREE.CubeTextureLoader().load( urls );
  46. const refractionCube = new THREE.CubeTextureLoader().load( urls );
  47. refractionCube.mapping = THREE.CubeRefractionMapping;
  48. scene = new THREE.Scene();
  49. scene.background = reflectionCube;
  50. //lights
  51. const ambient = new THREE.AmbientLight( 0xffffff, 3 );
  52. scene.add( ambient );
  53. pointLight = new THREE.PointLight( 0xffffff, 200 );
  54. scene.add( pointLight );
  55. //materials
  56. const cubeMaterial3 = new THREE.MeshLambertMaterial( { color: 0xffaa00, envMap: reflectionCube, combine: THREE.MixOperation, reflectivity: 0.3 } );
  57. const cubeMaterial2 = new THREE.MeshLambertMaterial( { color: 0xfff700, envMap: refractionCube, refractionRatio: 0.95 } );
  58. const cubeMaterial1 = new THREE.MeshLambertMaterial( { color: 0xffffff, envMap: reflectionCube } );
  59. //models
  60. const objLoader = new OBJLoader();
  61. objLoader.setPath( 'models/obj/walt/' );
  62. objLoader.load( 'WaltHead.obj', function ( object ) {
  63. const head = object.children[ 0 ];
  64. head.scale.setScalar( 0.1 );
  65. head.position.y = - 3;
  66. head.material = cubeMaterial1;
  67. const head2 = head.clone();
  68. head2.position.x = - 6;
  69. head2.material = cubeMaterial2;
  70. const head3 = head.clone();
  71. head3.position.x = 6;
  72. head3.material = cubeMaterial3;
  73. scene.add( head, head2, head3 );
  74. } );
  75. //renderer
  76. renderer = new THREE.WebGLRenderer( { antialias: true } );
  77. renderer.setPixelRatio( window.devicePixelRatio );
  78. renderer.setSize( window.innerWidth, window.innerHeight );
  79. renderer.setAnimationLoop( animate );
  80. container.appendChild( renderer.domElement );
  81. //controls
  82. const controls = new OrbitControls( camera, renderer.domElement );
  83. controls.enableZoom = false;
  84. controls.enablePan = false;
  85. controls.minPolarAngle = Math.PI / 4;
  86. controls.maxPolarAngle = Math.PI / 1.5;
  87. //stats
  88. stats = new Stats();
  89. container.appendChild( stats.dom );
  90. window.addEventListener( 'resize', onWindowResize );
  91. }
  92. function onWindowResize() {
  93. camera.aspect = window.innerWidth / window.innerHeight;
  94. camera.updateProjectionMatrix();
  95. renderer.setSize( window.innerWidth, window.innerHeight );
  96. }
  97. function animate() {
  98. renderer.render( scene, camera );
  99. stats.update();
  100. }
  101. </script>
  102. </body>
  103. </html>