webgpu_materials_transmission.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>threejs webgpu - transmission</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="container"></div>
  11. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">threejs</a> webgpu - transmission</div>
  12. <script type="importmap">
  13. {
  14. "imports": {
  15. "three": "../build/three.webgpu.js",
  16. "three/webgpu": "../build/three.webgpu.js",
  17. "three/tsl": "../build/three.tsl.js",
  18. "three/addons/": "./jsm/"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three/webgpu';
  24. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  25. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  26. import { HDRLoader } from 'three/addons/loaders/HDRLoader.js';
  27. const params = {
  28. color: 0xffffff,
  29. transmission: 1,
  30. opacity: 1,
  31. metalness: 0,
  32. roughness: 0,
  33. ior: 1.5,
  34. thickness: 0.01,
  35. specularIntensity: 1,
  36. specularColor: 0xffffff,
  37. envMapIntensity: 1,
  38. lightIntensity: 1,
  39. exposure: 1
  40. };
  41. let camera, scene, renderer;
  42. let mesh;
  43. const hdrEquirect = new HDRLoader()
  44. .setPath( 'textures/equirectangular/' )
  45. .load( 'royal_esplanade_1k.hdr', function () {
  46. hdrEquirect.mapping = THREE.EquirectangularReflectionMapping;
  47. init();
  48. render();
  49. } );
  50. function init() {
  51. renderer = new THREE.WebGPURenderer( { antialias: true } );
  52. renderer.setPixelRatio( window.devicePixelRatio );
  53. renderer.setSize( window.innerWidth, window.innerHeight );
  54. renderer.setAnimationLoop( render );
  55. document.body.appendChild( renderer.domElement );
  56. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  57. renderer.toneMappingExposure = params.exposure;
  58. scene = new THREE.Scene();
  59. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 2000 );
  60. camera.position.set( 0, 0, 120 );
  61. //
  62. scene.background = hdrEquirect;
  63. //
  64. const geometry = new THREE.SphereGeometry( 20, 64, 32 );
  65. const texture = new THREE.CanvasTexture( generateTexture() );
  66. texture.magFilter = THREE.NearestFilter;
  67. texture.wrapT = THREE.RepeatWrapping;
  68. texture.wrapS = THREE.RepeatWrapping;
  69. texture.repeat.set( 1, 3.5 );
  70. const material = new THREE.MeshPhysicalMaterial( {
  71. color: params.color,
  72. metalness: params.metalness,
  73. roughness: params.roughness,
  74. ior: params.ior,
  75. alphaMap: texture,
  76. envMap: hdrEquirect,
  77. envMapIntensity: params.envMapIntensity,
  78. transmission: params.transmission, // use material.transmission for glass materials
  79. specularIntensity: params.specularIntensity,
  80. specularColor: params.specularColor,
  81. opacity: params.opacity,
  82. side: THREE.DoubleSide,
  83. transparent: true
  84. } );
  85. mesh = new THREE.Mesh( geometry, material );
  86. scene.add( mesh );
  87. //
  88. const controls = new OrbitControls( camera, renderer.domElement );
  89. controls.minDistance = 10;
  90. controls.maxDistance = 150;
  91. window.addEventListener( 'resize', onWindowResize );
  92. //
  93. const gui = new GUI();
  94. gui.addColor( params, 'color' )
  95. .onChange( function () {
  96. material.color.set( params.color );
  97. } );
  98. gui.add( params, 'transmission', 0, 1, 0.01 )
  99. .onChange( function () {
  100. material.transmission = params.transmission;
  101. } );
  102. gui.add( params, 'opacity', 0, 1, 0.01 )
  103. .onChange( function () {
  104. material.opacity = params.opacity;
  105. } );
  106. gui.add( params, 'metalness', 0, 1, 0.01 )
  107. .onChange( function () {
  108. material.metalness = params.metalness;
  109. } );
  110. gui.add( params, 'roughness', 0, 1, 0.01 )
  111. .onChange( function () {
  112. material.roughness = params.roughness;
  113. } );
  114. gui.add( params, 'ior', 1, 2, 0.01 )
  115. .onChange( function () {
  116. material.ior = params.ior;
  117. } );
  118. gui.add( params, 'thickness', 0, 5, 0.01 )
  119. .onChange( function () {
  120. material.thickness = params.thickness;
  121. } );
  122. gui.add( params, 'specularIntensity', 0, 1, 0.01 )
  123. .onChange( function () {
  124. material.specularIntensity = params.specularIntensity;
  125. } );
  126. gui.addColor( params, 'specularColor' )
  127. .onChange( function () {
  128. material.specularColor.set( params.specularColor );
  129. } );
  130. gui.add( params, 'envMapIntensity', 0, 1, 0.01 )
  131. .name( 'envMap intensity' )
  132. .onChange( function () {
  133. material.envMapIntensity = params.envMapIntensity;
  134. } );
  135. gui.add( params, 'exposure', 0, 1, 0.01 )
  136. .onChange( function () {
  137. renderer.toneMappingExposure = params.exposure;
  138. } );
  139. gui.open();
  140. }
  141. function onWindowResize() {
  142. const width = window.innerWidth;
  143. const height = window.innerHeight;
  144. camera.aspect = width / height;
  145. camera.updateProjectionMatrix();
  146. renderer.setSize( width, height );
  147. }
  148. //
  149. function generateTexture() {
  150. const canvas = document.createElement( 'canvas' );
  151. canvas.width = 2;
  152. canvas.height = 2;
  153. const context = canvas.getContext( '2d' );
  154. context.fillStyle = 'white';
  155. context.fillRect( 0, 1, 2, 1 );
  156. return canvas;
  157. }
  158. function render() {
  159. renderer.renderAsync( scene, camera );
  160. }
  161. </script>
  162. </body>
  163. </html>
粤ICP备19079148号