1
0

webgpu_materials_sss.html 6.3 KB

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