1
0

webgpu_materials_envmaps.html 5.1 KB

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