webgl_materials_cubemap.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. <style>
  8. body {
  9. font-family: Monospace;
  10. margin: 0px;
  11. overflow: hidden;
  12. }
  13. #info {
  14. color: #fff;
  15. position: absolute;
  16. top: 10px;
  17. width: 100%;
  18. text-align: center;
  19. z-index: 100;
  20. display:block;
  21. }
  22. #info a {
  23. color: #ff0080;
  24. font-weight: bold;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <div id="container"></div>
  30. <div id="info">
  31. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - cube mapping demo.<br />
  32. 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>
  33. </div>
  34. <script src="../build/three.js"></script>
  35. <script src="js/controls/OrbitControls.js"></script>
  36. <script src="js/loaders/OBJLoader.js"></script>
  37. <script src="js/WebGL.js"></script>
  38. <script src="js/libs/stats.min.js"></script>
  39. <script>
  40. if ( WEBGL.isWebGLAvailable() === false ) {
  41. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  42. }
  43. var container, stats;
  44. var camera, scene, renderer;
  45. var mesh, geometry;
  46. var pointLight;
  47. init();
  48. animate();
  49. function init() {
  50. container = document.createElement( 'div' );
  51. document.body.appendChild( container );
  52. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 5000 );
  53. camera.position.z = 2000;
  54. //controls
  55. var controls = new THREE.OrbitControls( camera );
  56. controls.enableZoom = false;
  57. controls.enablePan = false;
  58. controls.minPolarAngle = Math.PI / 4;
  59. controls.maxPolarAngle = Math.PI / 1.5;
  60. //cubemap
  61. var path = "textures/cube/SwedishRoyalCastle/";
  62. var format = '.jpg';
  63. var urls = [
  64. path + 'px' + format, path + 'nx' + format,
  65. path + 'py' + format, path + 'ny' + format,
  66. path + 'pz' + format, path + 'nz' + format
  67. ];
  68. var reflectionCube = new THREE.CubeTextureLoader().load( urls );
  69. reflectionCube.format = THREE.RGBFormat;
  70. var refractionCube = new THREE.CubeTextureLoader().load( urls );
  71. refractionCube.mapping = THREE.CubeRefractionMapping;
  72. refractionCube.format = THREE.RGBFormat;
  73. scene = new THREE.Scene();
  74. scene.background = reflectionCube;
  75. //lights
  76. var ambient = new THREE.AmbientLight( 0xffffff );
  77. scene.add( ambient );
  78. pointLight = new THREE.PointLight( 0xffffff, 2 );
  79. scene.add( pointLight );
  80. //materials
  81. var cubeMaterial3 = new THREE.MeshLambertMaterial( { color: 0xff6600, envMap: reflectionCube, combine: THREE.MixOperation, reflectivity: 0.3 } );
  82. var cubeMaterial2 = new THREE.MeshLambertMaterial( { color: 0xffee00, envMap: refractionCube, refractionRatio: 0.95 } );
  83. var cubeMaterial1 = new THREE.MeshLambertMaterial( { color: 0xffffff, envMap: reflectionCube } );
  84. //models
  85. var objLoader = new THREE.OBJLoader();
  86. objLoader.setPath( 'models/obj/walt/' );
  87. objLoader.load( 'WaltHead.obj', function ( object ) {
  88. var head = object.children[0];
  89. head.scale.multiplyScalar( 15 );
  90. head.position.y = -500;
  91. head.material = cubeMaterial1;
  92. var head2 = head.clone();
  93. head2.position.x = - 900;
  94. head2.material = cubeMaterial2;
  95. var head3 = head.clone();
  96. head3.position.x = 900;
  97. head3.material = cubeMaterial3;
  98. scene.add( head, head2, head3 );
  99. } );
  100. //renderer
  101. renderer = new THREE.WebGLRenderer();
  102. renderer.setPixelRatio( window.devicePixelRatio );
  103. renderer.setSize( window.innerWidth, window.innerHeight );
  104. container.appendChild( renderer.domElement );
  105. //stats
  106. stats = new Stats();
  107. container.appendChild( stats.dom );
  108. window.addEventListener( 'resize', onWindowResize, false );
  109. }
  110. function onWindowResize() {
  111. camera.aspect = window.innerWidth / window.innerHeight;
  112. camera.updateProjectionMatrix();
  113. renderer.setSize( window.innerWidth, window.innerHeight );
  114. }
  115. function animate() {
  116. requestAnimationFrame( animate );
  117. render();
  118. }
  119. function render() {
  120. renderer.render( scene, camera );
  121. stats.update();
  122. }
  123. </script>
  124. </body>
  125. </html>