webgpu_materials_sss.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - sss</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">three.js</a> webgpu - sss
  12. <br/>Fast subsurface scattering<br/>
  13. [Thanks for the art support from <a href="https://github.com/shaochun" target="_blank" rel="noopener">Shaochun Lin</a>]
  14. </div>
  15. <script type="importmap">
  16. {
  17. "imports": {
  18. "three": "../build/three.webgpu.js",
  19. "three/tsl": "../build/three.webgpu.js",
  20. "three/addons/": "./jsm/"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import Stats from 'three/addons/libs/stats.module.js';
  27. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  28. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  29. import { FBXLoader } from 'three/addons/loaders/FBXLoader.js';
  30. let container, stats;
  31. let camera, scene, renderer;
  32. let model;
  33. init();
  34. function init() {
  35. container = document.createElement( 'div' );
  36. document.body.appendChild( container );
  37. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 5000 );
  38. camera.position.set( 0.0, 300, 400 * 4 );
  39. scene = new THREE.Scene();
  40. // Lights
  41. scene.add( new THREE.AmbientLight( 0xc1c1c1 ) );
  42. const directionalLight = new THREE.DirectionalLight( 0xffffff, 0.03 );
  43. directionalLight.position.set( 0.0, 0.5, 0.5 ).normalize();
  44. scene.add( directionalLight );
  45. const pointLight1 = new THREE.Mesh( new THREE.SphereGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xc1c1c1 } ) );
  46. pointLight1.add( new THREE.PointLight( 0xc1c1c1, 4.0, 300, 0 ) );
  47. scene.add( pointLight1 );
  48. pointLight1.position.x = 0;
  49. pointLight1.position.y = - 50;
  50. pointLight1.position.z = 350;
  51. const pointLight2 = new THREE.Mesh( new THREE.SphereGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xc1c100 } ) );
  52. pointLight2.add( new THREE.PointLight( 0xc1c100, 0.75, 500, 0 ) );
  53. scene.add( pointLight2 );
  54. pointLight2.position.x = - 100;
  55. pointLight2.position.y = 20;
  56. pointLight2.position.z = - 260;
  57. renderer = new THREE.WebGPURenderer( { antialias: true } );
  58. renderer.setPixelRatio( window.devicePixelRatio );
  59. renderer.setSize( window.innerWidth, window.innerHeight );
  60. renderer.setAnimationLoop( animate );
  61. container.appendChild( renderer.domElement );
  62. //
  63. stats = new Stats();
  64. container.appendChild( stats.dom );
  65. const controls = new OrbitControls( camera, container );
  66. controls.minDistance = 500;
  67. controls.maxDistance = 3000;
  68. window.addEventListener( 'resize', onWindowResize );
  69. initMaterial();
  70. }
  71. function initMaterial() {
  72. const loader = new THREE.TextureLoader();
  73. const imgTexture = loader.load( 'models/fbx/white.jpg' );
  74. imgTexture.colorSpace = THREE.SRGBColorSpace;
  75. const thicknessTexture = loader.load( 'models/fbx/bunny_thickness.jpg' );
  76. imgTexture.wrapS = imgTexture.wrapT = THREE.RepeatWrapping;
  77. const material = new THREE.MeshSSSNodeMaterial();
  78. material.color = new THREE.Color( 1.0, 0.2, 0.2 );
  79. material.roughness = 0.3;
  80. material.thicknessColorNode = THREE.texture( thicknessTexture ).mul( THREE.vec3( 0.5, 0.3, 0.0 ) );
  81. material.thicknessDistortionNode = THREE.uniform( 0.1 );
  82. material.thicknessAmbientNode = THREE.uniform( 0.4 );
  83. material.thicknessAttenuationNode = THREE.uniform( 0.8 );
  84. material.thicknessPowerNode = THREE.uniform( 2.0 );
  85. material.thicknessScaleNode = THREE.uniform( 16.0 );
  86. // LOADER
  87. const loaderFBX = new FBXLoader();
  88. loaderFBX.load( 'models/fbx/stanford-bunny.fbx', function ( object ) {
  89. model = object.children[ 0 ];
  90. model.position.set( 0, 0, 10 );
  91. model.scale.setScalar( 1 );
  92. model.material = material;
  93. scene.add( model );
  94. } );
  95. initGUI( material );
  96. }
  97. function initGUI( material ) {
  98. const gui = new GUI( { title: 'Thickness Control' } );
  99. const ThicknessControls = function () {
  100. this.distortion = material.thicknessDistortionNode.value;
  101. this.ambient = material.thicknessAmbientNode.value;
  102. this.attenuation = material.thicknessAttenuationNode.value;
  103. this.power = material.thicknessPowerNode.value;
  104. this.scale = material.thicknessScaleNode.value;
  105. };
  106. const thicknessControls = new ThicknessControls();
  107. gui.add( thicknessControls, 'distortion' ).min( 0.01 ).max( 1 ).step( 0.01 ).onChange( function () {
  108. material.thicknessDistortionNode.value = thicknessControls.distortion;
  109. console.log( 'distortion' );
  110. } );
  111. gui.add( thicknessControls, 'ambient' ).min( 0.01 ).max( 5.0 ).step( 0.05 ).onChange( function () {
  112. material.thicknessAmbientNode.value = thicknessControls.ambient;
  113. } );
  114. gui.add( thicknessControls, 'attenuation' ).min( 0.01 ).max( 5.0 ).step( 0.05 ).onChange( function () {
  115. material.thicknessAttenuationNode.value = thicknessControls.attenuation;
  116. } );
  117. gui.add( thicknessControls, 'power' ).min( 0.01 ).max( 16.0 ).step( 0.1 ).onChange( function () {
  118. material.thicknessPowerNode.value = thicknessControls.power;
  119. } );
  120. gui.add( thicknessControls, 'scale' ).min( 0.01 ).max( 50.0 ).step( 0.1 ).onChange( function () {
  121. material.thicknessScaleNode.value = thicknessControls.scale;
  122. } );
  123. }
  124. function onWindowResize() {
  125. camera.aspect = window.innerWidth / window.innerHeight;
  126. camera.updateProjectionMatrix();
  127. renderer.setSize( window.innerWidth, window.innerHeight );
  128. }
  129. //
  130. function animate() {
  131. render();
  132. stats.update();
  133. }
  134. function render() {
  135. if ( model ) model.rotation.y = performance.now() / 5000;
  136. renderer.render( scene, camera );
  137. }
  138. </script>
  139. </body>
  140. </html>