webgl_materials_cubemap.html 4.0 KB

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