webgpu_materials_envmaps_bpcem.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - materials - bpcem</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 - box projected cube environment mapping (BPCEM)<br/>
  12. </div>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.webgpu.js",
  17. "three/tsl": "../build/three.webgpu.js",
  18. "three/addons/": "./jsm/"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import { bumpMap, float, getParallaxCorrectNormal, pmremTexture, reflectVector, texture, uniform, vec3 } from 'three/tsl';
  25. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  26. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  27. import { RectAreaLightHelper } from 'three/addons/helpers/RectAreaLightHelper.js';
  28. import { RectAreaLightTexturesLib } from 'three/addons/lights/RectAreaLightTexturesLib.js';
  29. let camera, scene, renderer;
  30. let controls, cubeCamera;
  31. let groundPlane, wallMat;
  32. init();
  33. function init() {
  34. THREE.RectAreaLightNode.setLTC( RectAreaLightTexturesLib.init() );
  35. // scene
  36. scene = new THREE.Scene();
  37. // camera
  38. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 1000 );
  39. camera.position.set( 0, 200, - 200 );
  40. // cube camera for environment map
  41. const renderTarget = new THREE.WebGLCubeRenderTarget( 512 );
  42. renderTarget.texture.type = THREE.HalfFloatType;
  43. renderTarget.texture.minFilter = THREE.LinearMipmapLinearFilter;
  44. renderTarget.texture.magFilter = THREE.LinearFilter;
  45. renderTarget.texture.generateMipmaps = true;
  46. renderTarget.texture.mapping = THREE.CubeReflectionMapping;
  47. cubeCamera = new THREE.CubeCamera( 1, 1000, renderTarget );
  48. cubeCamera.position.set( 0, - 100, 0 );
  49. // ground floor ( with box projected environment mapping )
  50. const loader = new THREE.TextureLoader();
  51. const rMap = loader.load( 'textures/lava/lavatile.jpg' );
  52. rMap.wrapS = THREE.RepeatWrapping;
  53. rMap.wrapT = THREE.RepeatWrapping;
  54. rMap.repeat.set( 2, 1 );
  55. const roughnessUniform = uniform( 0.25 );
  56. const defaultMat = new THREE.MeshStandardNodeMaterial();
  57. defaultMat.envNode = pmremTexture( renderTarget.texture );
  58. defaultMat.roughnessNode = texture( rMap ).mul( roughnessUniform );
  59. defaultMat.metalnessNode = float( 1 );
  60. const boxProjectedMat = new THREE.MeshStandardNodeMaterial();
  61. boxProjectedMat.envNode = pmremTexture( renderTarget.texture, getParallaxCorrectNormal( reflectVector, vec3( 200, 100, 100 ), vec3( 0, - 50, 0 ) ) );
  62. boxProjectedMat.roughnessNode = texture( rMap ).mul( roughnessUniform );
  63. boxProjectedMat.metalnessNode = float( 1 );
  64. groundPlane = new THREE.Mesh( new THREE.PlaneGeometry( 200, 100, 100 ), boxProjectedMat );
  65. groundPlane.rotateX( - Math.PI / 2 );
  66. groundPlane.position.set( 0, - 49, 0 );
  67. scene.add( groundPlane );
  68. // walls
  69. const diffuseTex = loader.load( 'textures/brick_diffuse.jpg' );
  70. diffuseTex.colorSpace = THREE.SRGBColorSpace;
  71. const bumpTex = loader.load( 'textures/brick_bump.jpg' );
  72. wallMat = new THREE.MeshStandardNodeMaterial();
  73. wallMat.colorNode = texture( diffuseTex );
  74. wallMat.normalNode = bumpMap( texture( bumpTex ), float( 5 ) );
  75. const planeGeo = new THREE.PlaneGeometry( 100, 100 );
  76. const planeBack1 = new THREE.Mesh( planeGeo, wallMat );
  77. planeBack1.position.z = - 50;
  78. planeBack1.position.x = - 50;
  79. scene.add( planeBack1 );
  80. const planeBack2 = new THREE.Mesh( planeGeo, wallMat );
  81. planeBack2.position.z = - 50;
  82. planeBack2.position.x = 50;
  83. scene.add( planeBack2 );
  84. const planeFront1 = new THREE.Mesh( planeGeo, wallMat );
  85. planeFront1.position.z = 50;
  86. planeFront1.position.x = - 50;
  87. planeFront1.rotateY( Math.PI );
  88. scene.add( planeFront1 );
  89. const planeFront2 = new THREE.Mesh( planeGeo, wallMat );
  90. planeFront2.position.z = 50;
  91. planeFront2.position.x = 50;
  92. planeFront2.rotateY( Math.PI );
  93. scene.add( planeFront2 );
  94. const planeRight = new THREE.Mesh( planeGeo, wallMat );
  95. planeRight.position.x = 100;
  96. planeRight.rotateY( - Math.PI / 2 );
  97. scene.add( planeRight );
  98. const planeLeft = new THREE.Mesh( planeGeo, wallMat );
  99. planeLeft.position.x = - 100;
  100. planeLeft.rotateY( Math.PI / 2 );
  101. scene.add( planeLeft );
  102. // area lights
  103. const width = 50;
  104. const height = 50;
  105. const intensity = 5;
  106. const blueRectLight = new THREE.RectAreaLight( 0x9aaeff, intensity, width, height );
  107. blueRectLight.position.set( - 99, 5, 0 );
  108. blueRectLight.lookAt( 0, 5, 0 );
  109. scene.add( blueRectLight );
  110. const blueRectLightHelper = new RectAreaLightHelper( blueRectLight, 0xffffff );
  111. blueRectLight.add( blueRectLightHelper );
  112. const redRectLight = new THREE.RectAreaLight( 0xf3aaaa, intensity, width, height );
  113. redRectLight.position.set( 99, 5, 0 );
  114. redRectLight.lookAt( 0, 5, 0 );
  115. scene.add( redRectLight );
  116. const redRectLightHelper = new RectAreaLightHelper( redRectLight, 0xffffff );
  117. redRectLight.add( redRectLightHelper );
  118. // renderer
  119. renderer = new THREE.WebGPURenderer( { antialias: true } );
  120. renderer.setPixelRatio( window.devicePixelRatio );
  121. renderer.setSize( window.innerWidth, window.innerHeight );
  122. renderer.setAnimationLoop( animate );
  123. document.body.appendChild( renderer.domElement );
  124. window.addEventListener( 'resize', onWindowResize );
  125. // controls
  126. controls = new OrbitControls( camera, renderer.domElement );
  127. controls.target.set( 0, - 10, 0 );
  128. controls.maxDistance = 400;
  129. controls.minDistance = 10;
  130. controls.update();
  131. // gui
  132. const gui = new GUI();
  133. const params = {
  134. 'box projected': true
  135. };
  136. gui.add( params, 'box projected' ).onChange( ( value ) => {
  137. groundPlane.material = ( value ) ? boxProjectedMat : defaultMat;
  138. } );
  139. gui.add( roughnessUniform, 'value', 0, 1 ).name( 'roughness' );
  140. }
  141. function onWindowResize() {
  142. camera.aspect = window.innerWidth / window.innerHeight;
  143. camera.updateProjectionMatrix();
  144. renderer.setSize( window.innerWidth, window.innerHeight );
  145. }
  146. function updateCubeMap() {
  147. groundPlane.visible = false;
  148. cubeCamera.position.copy( groundPlane.position );
  149. cubeCamera.update( renderer, scene );
  150. groundPlane.visible = true;
  151. }
  152. function animate() {
  153. updateCubeMap();
  154. renderer.render( scene, camera );
  155. }
  156. </script>
  157. </body>
  158. </html>
粤ICP备19079148号