webgpu_materials_sss.html 6.0 KB

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