webgl_materials_cubemap.html 4.3 KB

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