webgpu_materials_envmaps.html 4.8 KB

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