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