webgl_materials_subsurface_scattering.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. function init() {
  35. container = document.createElement( 'div' );
  36. document.body.appendChild( container );
  37. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 5000 );
  38. camera.position.set( 0.0, 300, 400 * 4 );
  39. scene = new THREE.Scene();
  40. // Lights
  41. scene.add( new THREE.AmbientLight( 0xc1c1c1 ) );
  42. const directionalLight = new THREE.DirectionalLight( 0xffffff, 0.03 );
  43. directionalLight.position.set( 0.0, 0.5, 0.5 ).normalize();
  44. scene.add( directionalLight );
  45. const pointLight1 = new THREE.Mesh( new THREE.SphereGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xc1c1c1 } ) );
  46. pointLight1.add( new THREE.PointLight( 0xc1c1c1, 4.0, 300, 0 ) );
  47. scene.add( pointLight1 );
  48. pointLight1.position.x = 0;
  49. pointLight1.position.y = - 50;
  50. pointLight1.position.z = 350;
  51. const pointLight2 = new THREE.Mesh( new THREE.SphereGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xc1c100 } ) );
  52. pointLight2.add( new THREE.PointLight( 0xc1c100, 0.75, 500, 0 ) );
  53. scene.add( pointLight2 );
  54. pointLight2.position.x = - 100;
  55. pointLight2.position.y = 20;
  56. pointLight2.position.z = - 260;
  57. renderer = new THREE.WebGLRenderer( { antialias: true } );
  58. renderer.setPixelRatio( window.devicePixelRatio );
  59. renderer.setSize( window.innerWidth, window.innerHeight );
  60. renderer.setAnimationLoop( animate );
  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. // LOADER
  96. const loaderFBX = new FBXLoader();
  97. loaderFBX.load( 'models/fbx/stanford-bunny.fbx', function ( object ) {
  98. model = object.children[ 0 ];
  99. model.position.set( 0, 0, 10 );
  100. model.scale.setScalar( 1 );
  101. model.material = material;
  102. scene.add( model );
  103. } );
  104. initGUI( uniforms );
  105. }
  106. function initGUI( uniforms ) {
  107. const gui = new GUI( { title: 'Thickness Control' } );
  108. const ThicknessControls = function () {
  109. this.distortion = uniforms[ 'thicknessDistortion' ].value;
  110. this.ambient = uniforms[ 'thicknessAmbient' ].value;
  111. this.attenuation = uniforms[ 'thicknessAttenuation' ].value;
  112. this.power = uniforms[ 'thicknessPower' ].value;
  113. this.scale = uniforms[ 'thicknessScale' ].value;
  114. };
  115. const thicknessControls = new ThicknessControls();
  116. gui.add( thicknessControls, 'distortion' ).min( 0.01 ).max( 1 ).step( 0.01 ).onChange( function () {
  117. uniforms[ 'thicknessDistortion' ].value = thicknessControls.distortion;
  118. console.log( 'distortion' );
  119. } );
  120. gui.add( thicknessControls, 'ambient' ).min( 0.01 ).max( 5.0 ).step( 0.05 ).onChange( function () {
  121. uniforms[ 'thicknessAmbient' ].value = thicknessControls.ambient;
  122. } );
  123. gui.add( thicknessControls, 'attenuation' ).min( 0.01 ).max( 5.0 ).step( 0.05 ).onChange( function () {
  124. uniforms[ 'thicknessAttenuation' ].value = thicknessControls.attenuation;
  125. } );
  126. gui.add( thicknessControls, 'power' ).min( 0.01 ).max( 16.0 ).step( 0.1 ).onChange( function () {
  127. uniforms[ 'thicknessPower' ].value = thicknessControls.power;
  128. } );
  129. gui.add( thicknessControls, 'scale' ).min( 0.01 ).max( 50.0 ).step( 0.1 ).onChange( function () {
  130. uniforms[ 'thicknessScale' ].value = thicknessControls.scale;
  131. } );
  132. }
  133. function onWindowResize() {
  134. camera.aspect = window.innerWidth / window.innerHeight;
  135. camera.updateProjectionMatrix();
  136. renderer.setSize( window.innerWidth, window.innerHeight );
  137. }
  138. //
  139. function animate() {
  140. render();
  141. stats.update();
  142. }
  143. function render() {
  144. if ( model ) model.rotation.y = performance.now() / 5000;
  145. renderer.render( scene, camera );
  146. }
  147. </script>
  148. </body>
  149. </html>