webgl_materials_cubemap.html 4.1 KB

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