webgl_materials_subsurface_scattering.html 6.3 KB

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