webgl_materials_subsurface_scattering.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - Fast subsurface scattering in Blinn-Phong shading demo</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 in Blinn-Phong shading demo<br/>
  13. [Thanks for the art support from <a href="https://github.com/shaochun" target="_blank" rel="noopener">Shaochun Lin</a>]
  14. </div>
  15. <!-- Import maps polyfill -->
  16. <!-- Remove this when import maps will be widely supported -->
  17. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  18. <script type="importmap">
  19. {
  20. "imports": {
  21. "three": "../build/three.module.js",
  22. "three/addons/": "./jsm/"
  23. }
  24. }
  25. </script>
  26. <script type="module">
  27. import * as THREE from 'three';
  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 { SubsurfaceScatteringShader } from 'three/addons/shaders/SubsurfaceScatteringShader.js';
  32. import { FBXLoader } from 'three/addons/loaders/FBXLoader.js';
  33. let container, stats;
  34. let camera, scene, renderer;
  35. let model;
  36. init();
  37. animate();
  38. function init() {
  39. container = document.createElement( 'div' );
  40. document.body.appendChild( container );
  41. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 5000 );
  42. camera.position.set( 0.0, 300, 400 * 4 );
  43. scene = new THREE.Scene();
  44. // Lights
  45. scene.add( new THREE.AmbientLight( 0xc1c1c1 ) );
  46. const directionalLight = new THREE.DirectionalLight( 0xffffff, 0.03 );
  47. directionalLight.position.set( 0.0, 0.5, 0.5 ).normalize();
  48. scene.add( directionalLight );
  49. const pointLight1 = new THREE.Mesh( new THREE.SphereGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xc1c1c1 } ) );
  50. pointLight1.add( new THREE.PointLight( 0xc1c1c1, 4.0, 300, 0 ) );
  51. scene.add( pointLight1 );
  52. pointLight1.position.x = 0;
  53. pointLight1.position.y = - 50;
  54. pointLight1.position.z = 350;
  55. const pointLight2 = new THREE.Mesh( new THREE.SphereGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xc1c100 } ) );
  56. pointLight2.add( new THREE.PointLight( 0xc1c100, 0.75, 500, 0 ) );
  57. scene.add( pointLight2 );
  58. pointLight2.position.x = - 100;
  59. pointLight2.position.y = 20;
  60. pointLight2.position.z = - 260;
  61. renderer = new THREE.WebGLRenderer( { antialias: true } );
  62. renderer.setPixelRatio( window.devicePixelRatio );
  63. renderer.setSize( window.innerWidth, window.innerHeight );
  64. renderer.useLegacyLights = false;
  65. container.appendChild( renderer.domElement );
  66. //
  67. stats = new Stats();
  68. container.appendChild( stats.dom );
  69. const controls = new OrbitControls( camera, container );
  70. controls.minDistance = 500;
  71. controls.maxDistance = 3000;
  72. window.addEventListener( 'resize', onWindowResize );
  73. initMaterial();
  74. }
  75. function initMaterial() {
  76. const loader = new THREE.TextureLoader();
  77. const imgTexture = loader.load( 'models/fbx/white.jpg' );
  78. imgTexture.colorSpace = THREE.SRGBColorSpace;
  79. const thicknessTexture = loader.load( 'models/fbx/bunny_thickness.jpg' );
  80. imgTexture.wrapS = imgTexture.wrapT = THREE.RepeatWrapping;
  81. const shader = SubsurfaceScatteringShader;
  82. const uniforms = THREE.UniformsUtils.clone( shader.uniforms );
  83. uniforms[ 'map' ].value = imgTexture;
  84. uniforms[ 'diffuse' ].value = new THREE.Vector3( 1.0, 0.2, 0.2 );
  85. uniforms[ 'shininess' ].value = 500;
  86. uniforms[ 'thicknessMap' ].value = thicknessTexture;
  87. uniforms[ 'thicknessColor' ].value = new THREE.Vector3( 0.5, 0.3, 0.0 );
  88. uniforms[ 'thicknessDistortion' ].value = 0.1;
  89. uniforms[ 'thicknessAmbient' ].value = 0.4;
  90. uniforms[ 'thicknessAttenuation' ].value = 0.8;
  91. uniforms[ 'thicknessPower' ].value = 2.0;
  92. uniforms[ 'thicknessScale' ].value = 16.0;
  93. const material = new THREE.ShaderMaterial( {
  94. uniforms: uniforms,
  95. vertexShader: shader.vertexShader,
  96. fragmentShader: shader.fragmentShader,
  97. lights: true
  98. } );
  99. material.extensions.derivatives = true;
  100. // LOADER
  101. const loaderFBX = new FBXLoader();
  102. loaderFBX.load( 'models/fbx/stanford-bunny.fbx', function ( object ) {
  103. model = object.children[ 0 ];
  104. model.position.set( 0, 0, 10 );
  105. model.scale.setScalar( 1 );
  106. model.material = material;
  107. scene.add( model );
  108. } );
  109. initGUI( uniforms );
  110. }
  111. function initGUI( uniforms ) {
  112. const gui = new GUI( { title: 'Thickness Control' } );
  113. const ThicknessControls = function () {
  114. this.distortion = uniforms[ 'thicknessDistortion' ].value;
  115. this.ambient = uniforms[ 'thicknessAmbient' ].value;
  116. this.attenuation = uniforms[ 'thicknessAttenuation' ].value;
  117. this.power = uniforms[ 'thicknessPower' ].value;
  118. this.scale = uniforms[ 'thicknessScale' ].value;
  119. };
  120. const thicknessControls = new ThicknessControls();
  121. gui.add( thicknessControls, 'distortion' ).min( 0.01 ).max( 1 ).step( 0.01 ).onChange( function () {
  122. uniforms[ 'thicknessDistortion' ].value = thicknessControls.distortion;
  123. console.log( 'distortion' );
  124. } );
  125. gui.add( thicknessControls, 'ambient' ).min( 0.01 ).max( 5.0 ).step( 0.05 ).onChange( function () {
  126. uniforms[ 'thicknessAmbient' ].value = thicknessControls.ambient;
  127. } );
  128. gui.add( thicknessControls, 'attenuation' ).min( 0.01 ).max( 5.0 ).step( 0.05 ).onChange( function () {
  129. uniforms[ 'thicknessAttenuation' ].value = thicknessControls.attenuation;
  130. } );
  131. gui.add( thicknessControls, 'power' ).min( 0.01 ).max( 16.0 ).step( 0.1 ).onChange( function () {
  132. uniforms[ 'thicknessPower' ].value = thicknessControls.power;
  133. } );
  134. gui.add( thicknessControls, 'scale' ).min( 0.01 ).max( 50.0 ).step( 0.1 ).onChange( function () {
  135. uniforms[ 'thicknessScale' ].value = thicknessControls.scale;
  136. } );
  137. }
  138. function onWindowResize() {
  139. camera.aspect = window.innerWidth / window.innerHeight;
  140. camera.updateProjectionMatrix();
  141. renderer.setSize( window.innerWidth, window.innerHeight );
  142. }
  143. //
  144. function animate() {
  145. requestAnimationFrame( animate );
  146. render();
  147. stats.update();
  148. }
  149. function render() {
  150. if ( model ) model.rotation.y = performance.now() / 5000;
  151. renderer.render( scene, camera );
  152. }
  153. </script>
  154. </body>
  155. </html>