webgl_materials_subsurface_scattering.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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( 0x888888 ) );
  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: 0x888888 } ) );
  50. pointLight1.add( new THREE.PointLight( 0x888888, 7.0, 300 ) );
  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: 0x888800 } ) );
  56. pointLight2.add( new THREE.PointLight( 0x888800, 1.0, 500 ) );
  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. container.appendChild( renderer.domElement );
  65. renderer.outputEncoding = THREE.sRGBEncoding;
  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. const thicknessTexture = loader.load( 'models/fbx/bunny_thickness.jpg' );
  79. imgTexture.wrapS = imgTexture.wrapT = THREE.RepeatWrapping;
  80. const shader = SubsurfaceScatteringShader;
  81. const uniforms = THREE.UniformsUtils.clone( shader.uniforms );
  82. uniforms[ 'map' ].value = imgTexture;
  83. uniforms[ 'diffuse' ].value = new THREE.Vector3( 1.0, 0.2, 0.2 );
  84. uniforms[ 'shininess' ].value = 500;
  85. uniforms[ 'thicknessMap' ].value = thicknessTexture;
  86. uniforms[ 'thicknessColor' ].value = new THREE.Vector3( 0.5, 0.3, 0.0 );
  87. uniforms[ 'thicknessDistortion' ].value = 0.1;
  88. uniforms[ 'thicknessAmbient' ].value = 0.4;
  89. uniforms[ 'thicknessAttenuation' ].value = 0.8;
  90. uniforms[ 'thicknessPower' ].value = 2.0;
  91. uniforms[ 'thicknessScale' ].value = 16.0;
  92. const material = new THREE.ShaderMaterial( {
  93. uniforms: uniforms,
  94. vertexShader: shader.vertexShader,
  95. fragmentShader: shader.fragmentShader,
  96. lights: true
  97. } );
  98. material.extensions.derivatives = true;
  99. // LOADER
  100. const loaderFBX = new FBXLoader();
  101. loaderFBX.load( 'models/fbx/stanford-bunny.fbx', function ( object ) {
  102. model = object.children[ 0 ];
  103. model.position.set( 0, 0, 10 );
  104. model.scale.setScalar( 1 );
  105. model.material = material;
  106. scene.add( model );
  107. } );
  108. initGUI( uniforms );
  109. }
  110. function initGUI( uniforms ) {
  111. const gui = new GUI( { title: 'Thickness Control' } );
  112. const ThicknessControls = function () {
  113. this.distortion = uniforms[ 'thicknessDistortion' ].value;
  114. this.ambient = uniforms[ 'thicknessAmbient' ].value;
  115. this.attenuation = uniforms[ 'thicknessAttenuation' ].value;
  116. this.power = uniforms[ 'thicknessPower' ].value;
  117. this.scale = uniforms[ 'thicknessScale' ].value;
  118. };
  119. const thicknessControls = new ThicknessControls();
  120. gui.add( thicknessControls, 'distortion' ).min( 0.01 ).max( 1 ).step( 0.01 ).onChange( function () {
  121. uniforms[ 'thicknessDistortion' ].value = thicknessControls.distortion;
  122. console.log( 'distortion' );
  123. } );
  124. gui.add( thicknessControls, 'ambient' ).min( 0.01 ).max( 5.0 ).step( 0.05 ).onChange( function () {
  125. uniforms[ 'thicknessAmbient' ].value = thicknessControls.ambient;
  126. } );
  127. gui.add( thicknessControls, 'attenuation' ).min( 0.01 ).max( 5.0 ).step( 0.05 ).onChange( function () {
  128. uniforms[ 'thicknessAttenuation' ].value = thicknessControls.attenuation;
  129. } );
  130. gui.add( thicknessControls, 'power' ).min( 0.01 ).max( 16.0 ).step( 0.1 ).onChange( function () {
  131. uniforms[ 'thicknessPower' ].value = thicknessControls.power;
  132. } );
  133. gui.add( thicknessControls, 'scale' ).min( 0.01 ).max( 50.0 ).step( 0.1 ).onChange( function () {
  134. uniforms[ 'thicknessScale' ].value = thicknessControls.scale;
  135. } );
  136. }
  137. function onWindowResize() {
  138. camera.aspect = window.innerWidth / window.innerHeight;
  139. camera.updateProjectionMatrix();
  140. renderer.setSize( window.innerWidth, window.innerHeight );
  141. }
  142. //
  143. function animate() {
  144. requestAnimationFrame( animate );
  145. render();
  146. stats.update();
  147. }
  148. function render() {
  149. if ( model ) model.rotation.y = performance.now() / 5000;
  150. renderer.render( scene, camera );
  151. }
  152. </script>
  153. </body>
  154. </html>