webgl_materials_cubemap.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. <meta property="og:title" content="three.js webgl - materials - cube reflection / refraction [Walt]">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_materials_cubemap.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_materials_cubemap.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. </head>
  13. <body>
  14. <div id="container"></div>
  15. <div id="info">
  16. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - cube mapping demo.<br />
  17. 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>
  18. </div>
  19. <script type="importmap">
  20. {
  21. "imports": {
  22. "three": "../build/three.module.js",
  23. "three/addons/": "./jsm/"
  24. }
  25. }
  26. </script>
  27. <script type="module">
  28. import * as THREE from 'three';
  29. import Stats from 'three/addons/libs/stats.module.js';
  30. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  31. import { OBJLoader } from 'three/addons/loaders/OBJLoader.js';
  32. let container, stats;
  33. let camera, scene, renderer;
  34. let pointLight;
  35. init();
  36. function init() {
  37. container = document.createElement( 'div' );
  38. document.body.appendChild( container );
  39. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 100 );
  40. camera.position.z = 13;
  41. //cubemap
  42. const path = 'textures/cube/SwedishRoyalCastle/';
  43. const format = '.jpg';
  44. const urls = [
  45. path + 'px' + format, path + 'nx' + format,
  46. path + 'py' + format, path + 'ny' + format,
  47. path + 'pz' + format, path + 'nz' + format
  48. ];
  49. const reflectionCube = new THREE.CubeTextureLoader().load( urls );
  50. const refractionCube = new THREE.CubeTextureLoader().load( urls );
  51. refractionCube.mapping = THREE.CubeRefractionMapping;
  52. scene = new THREE.Scene();
  53. scene.background = reflectionCube;
  54. //lights
  55. const ambient = new THREE.AmbientLight( 0xffffff, 3 );
  56. scene.add( ambient );
  57. pointLight = new THREE.PointLight( 0xffffff, 200 );
  58. scene.add( pointLight );
  59. //materials
  60. const cubeMaterial3 = new THREE.MeshLambertMaterial( { color: 0xffaa00, envMap: reflectionCube, combine: THREE.MixOperation, reflectivity: 0.3 } );
  61. const cubeMaterial2 = new THREE.MeshLambertMaterial( { color: 0xfff700, envMap: refractionCube, refractionRatio: 0.95 } );
  62. const cubeMaterial1 = new THREE.MeshLambertMaterial( { color: 0xffffff, envMap: reflectionCube } );
  63. //models
  64. const objLoader = new OBJLoader();
  65. objLoader.setPath( 'models/obj/walt/' );
  66. objLoader.load( 'WaltHead.obj', function ( object ) {
  67. const head = object.children[ 0 ];
  68. head.scale.setScalar( 0.1 );
  69. head.position.y = - 3;
  70. head.material = cubeMaterial1;
  71. const head2 = head.clone();
  72. head2.position.x = - 6;
  73. head2.material = cubeMaterial2;
  74. const head3 = head.clone();
  75. head3.position.x = 6;
  76. head3.material = cubeMaterial3;
  77. scene.add( head, head2, head3 );
  78. } );
  79. //renderer
  80. renderer = new THREE.WebGLRenderer( { antialias: true } );
  81. renderer.setPixelRatio( window.devicePixelRatio );
  82. renderer.setSize( window.innerWidth, window.innerHeight );
  83. renderer.setAnimationLoop( animate );
  84. container.appendChild( renderer.domElement );
  85. //controls
  86. const controls = new OrbitControls( camera, renderer.domElement );
  87. controls.enableZoom = false;
  88. controls.enablePan = false;
  89. controls.minPolarAngle = Math.PI / 4;
  90. controls.maxPolarAngle = Math.PI / 1.5;
  91. //stats
  92. stats = new Stats();
  93. container.appendChild( stats.dom );
  94. window.addEventListener( 'resize', onWindowResize );
  95. }
  96. function onWindowResize() {
  97. camera.aspect = window.innerWidth / window.innerHeight;
  98. camera.updateProjectionMatrix();
  99. renderer.setSize( window.innerWidth, window.innerHeight );
  100. }
  101. function animate() {
  102. renderer.render( scene, camera );
  103. stats.update();
  104. }
  105. </script>
  106. </body>
  107. </html>
粤ICP备19079148号