webgpu_materials_envmaps.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - materials - environment maps</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="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgpu environment mapping example<br/>
  12. Equirectangular Map by <a href="http://www.flickr.com/photos/jonragnarsson/2294472375/" target="_blank" rel="noopener">J&oacute;n Ragnarsson</a>.
  13. </div>
  14. <script type="importmap">
  15. {
  16. "imports": {
  17. "three": "../build/three.webgpu.js",
  18. "three/webgpu": "../build/three.webgpu.js",
  19. "three/tsl": "../build/three.tsl.js",
  20. "three/addons/": "./jsm/"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  27. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  28. let controls, camera, scene, renderer;
  29. let textureEquirec, textureCube;
  30. let sphereMesh, sphereMaterial, params;
  31. init();
  32. function init() {
  33. // CAMERAS
  34. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 100 );
  35. camera.position.set( 0, 0, 2.5 );
  36. // SCENE
  37. scene = new THREE.Scene();
  38. // Textures
  39. const loader = new THREE.CubeTextureLoader();
  40. loader.setPath( 'textures/cube/Bridge2/' );
  41. textureCube = loader.load( [ 'posx.jpg', 'negx.jpg', 'posy.jpg', 'negy.jpg', 'posz.jpg', 'negz.jpg' ] );
  42. const textureLoader = new THREE.TextureLoader();
  43. textureEquirec = textureLoader.load( 'textures/2294472375_24a3b8ef46_o.jpg' );
  44. textureEquirec.mapping = THREE.EquirectangularReflectionMapping;
  45. textureEquirec.colorSpace = THREE.SRGBColorSpace;
  46. scene.background = textureCube;
  47. //
  48. const geometry = new THREE.IcosahedronGeometry( 1, 15 );
  49. sphereMaterial = new THREE.MeshBasicMaterial( { envMap: textureCube } );
  50. sphereMesh = new THREE.Mesh( geometry, sphereMaterial );
  51. scene.add( sphereMesh );
  52. //
  53. renderer = new THREE.WebGPURenderer();
  54. renderer.setPixelRatio( window.devicePixelRatio );
  55. renderer.setSize( window.innerWidth, window.innerHeight );
  56. renderer.setAnimationLoop( animate );
  57. document.body.appendChild( renderer.domElement );
  58. //
  59. controls = new OrbitControls( camera, renderer.domElement );
  60. controls.minDistance = 1.5;
  61. controls.maxDistance = 6;
  62. //
  63. params = {
  64. Cube: function () {
  65. scene.background = textureCube;
  66. sphereMaterial.envMap = textureCube;
  67. sphereMaterial.needsUpdate = true;
  68. },
  69. Equirectangular: function () {
  70. scene.background = textureEquirec;
  71. sphereMaterial.envMap = textureEquirec;
  72. sphereMaterial.needsUpdate = true;
  73. },
  74. Refraction: false,
  75. backgroundRotationX: false,
  76. backgroundRotationY: false,
  77. backgroundRotationZ: false,
  78. syncMaterial: false
  79. };
  80. const gui = new GUI( { width: 300 } );
  81. gui.add( params, 'Cube' );
  82. gui.add( params, 'Equirectangular' );
  83. gui.add( params, 'Refraction' ).onChange( function ( value ) {
  84. if ( value ) {
  85. textureEquirec.mapping = THREE.EquirectangularRefractionMapping;
  86. textureCube.mapping = THREE.CubeRefractionMapping;
  87. } else {
  88. textureEquirec.mapping = THREE.EquirectangularReflectionMapping;
  89. textureCube.mapping = THREE.CubeReflectionMapping;
  90. }
  91. sphereMaterial.needsUpdate = true;
  92. } );
  93. gui.add( params, 'backgroundRotationX' );
  94. gui.add( params, 'backgroundRotationY' );
  95. gui.add( params, 'backgroundRotationZ' );
  96. gui.add( params, 'syncMaterial' );
  97. gui.open();
  98. window.addEventListener( 'resize', onWindowResize );
  99. }
  100. function onWindowResize() {
  101. camera.aspect = window.innerWidth / window.innerHeight;
  102. camera.updateProjectionMatrix();
  103. renderer.setSize( window.innerWidth, window.innerHeight );
  104. }
  105. //
  106. function animate() {
  107. if ( params.backgroundRotationX ) {
  108. scene.backgroundRotation.x += 0.001;
  109. }
  110. if ( params.backgroundRotationY ) {
  111. scene.backgroundRotation.y += 0.001;
  112. }
  113. if ( params.backgroundRotationZ ) {
  114. scene.backgroundRotation.z += 0.001;
  115. }
  116. if ( params.syncMaterial ) {
  117. sphereMesh.material.envMapRotation.copy( scene.backgroundRotation );
  118. }
  119. camera.lookAt( scene.position );
  120. renderer.render( scene, camera );
  121. }
  122. </script>
  123. </body>
  124. </html>
粤ICP备19079148号