webgl_materials_cubemap.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. <!-- 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 { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  30. import { OBJLoader } from 'three/addons/loaders/OBJLoader.js';
  31. THREE.ColorManagement.enabled = false; // TODO: Confirm correct color management.
  32. let container, stats;
  33. let camera, scene, renderer;
  34. let pointLight;
  35. init();
  36. animate();
  37. function init() {
  38. container = document.createElement( 'div' );
  39. document.body.appendChild( container );
  40. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 5000 );
  41. camera.position.z = 2000;
  42. //cubemap
  43. const path = 'textures/cube/SwedishRoyalCastle/';
  44. const format = '.jpg';
  45. const urls = [
  46. path + 'px' + format, path + 'nx' + format,
  47. path + 'py' + format, path + 'ny' + format,
  48. path + 'pz' + format, path + 'nz' + format
  49. ];
  50. const reflectionCube = new THREE.CubeTextureLoader().load( urls );
  51. reflectionCube.colorSpace = THREE.SRGBColorSpace;
  52. const refractionCube = new THREE.CubeTextureLoader().load( urls );
  53. refractionCube.colorSpace = THREE.SRGBColorSpace;
  54. refractionCube.mapping = THREE.CubeRefractionMapping;
  55. scene = new THREE.Scene();
  56. scene.background = reflectionCube;
  57. //lights
  58. const ambient = new THREE.AmbientLight( 0xffffff );
  59. scene.add( ambient );
  60. pointLight = new THREE.PointLight( 0xffffff, 2 );
  61. scene.add( pointLight );
  62. //materials
  63. const cubeMaterial3 = new THREE.MeshLambertMaterial( { color: 0xff6600, envMap: reflectionCube, combine: THREE.MixOperation, reflectivity: 0.3 } );
  64. const cubeMaterial2 = new THREE.MeshLambertMaterial( { color: 0xffee00, envMap: refractionCube, refractionRatio: 0.95 } );
  65. const cubeMaterial1 = new THREE.MeshLambertMaterial( { color: 0xffffff, envMap: reflectionCube } );
  66. //models
  67. const objLoader = new OBJLoader();
  68. objLoader.setPath( 'models/obj/walt/' );
  69. objLoader.load( 'WaltHead.obj', function ( object ) {
  70. const head = object.children[ 0 ];
  71. head.scale.multiplyScalar( 15 );
  72. head.position.y = - 500;
  73. head.material = cubeMaterial1;
  74. const head2 = head.clone();
  75. head2.position.x = - 900;
  76. head2.material = cubeMaterial2;
  77. const head3 = head.clone();
  78. head3.position.x = 900;
  79. head3.material = cubeMaterial3;
  80. scene.add( head, head2, head3 );
  81. } );
  82. //renderer
  83. renderer = new THREE.WebGLRenderer();
  84. renderer.setPixelRatio( window.devicePixelRatio );
  85. renderer.setSize( window.innerWidth, window.innerHeight );
  86. container.appendChild( renderer.domElement );
  87. //controls
  88. const controls = new OrbitControls( camera, renderer.domElement );
  89. controls.enableZoom = false;
  90. controls.enablePan = false;
  91. controls.minPolarAngle = Math.PI / 4;
  92. controls.maxPolarAngle = Math.PI / 1.5;
  93. //stats
  94. stats = new Stats();
  95. container.appendChild( stats.dom );
  96. window.addEventListener( 'resize', onWindowResize );
  97. }
  98. function onWindowResize() {
  99. camera.aspect = window.innerWidth / window.innerHeight;
  100. camera.updateProjectionMatrix();
  101. renderer.setSize( window.innerWidth, window.innerHeight );
  102. }
  103. function animate() {
  104. requestAnimationFrame( animate );
  105. render();
  106. }
  107. function render() {
  108. renderer.render( scene, camera );
  109. stats.update();
  110. }
  111. </script>
  112. </body>
  113. </html>