webgl_materials_subsurface_scattering.html 6.2 KB

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