webgl_materials_translucency.html 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 { TranslucentShader } from './jsm/shaders/TranslucentShader.js';
  21. import { FBXLoader } from './jsm/loaders/FBXLoader.js';
  22. var container, stats;
  23. var camera, scene, renderer;
  24. var 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. var directionalLight = new THREE.DirectionalLight( 0xffffff, 0.03 );
  36. directionalLight.position.set( 0.0, 0.5, 0.5 ).normalize();
  37. scene.add( directionalLight );
  38. var pointLight1 = new THREE.Mesh( new THREE.SphereBufferGeometry( 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. var pointLight2 = new THREE.Mesh( new THREE.SphereBufferGeometry( 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. var controls = new OrbitControls( camera, container );
  59. window.addEventListener( 'resize', onWindowResize, false );
  60. initMaterial();
  61. }
  62. function initMaterial() {
  63. var loader = new THREE.TextureLoader();
  64. var imgTexture = loader.load( 'models/fbx/white.jpg' );
  65. var thicknessTexture = loader.load( 'models/fbx/bunny_thickness.jpg' );
  66. imgTexture.wrapS = imgTexture.wrapT = THREE.RepeatWrapping;
  67. var shader = TranslucentShader;
  68. var uniforms = THREE.UniformsUtils.clone( shader.uniforms );
  69. uniforms[ 'map' ].value = imgTexture;
  70. uniforms[ 'diffuse' ].value = new THREE.Vector3( 1.0, 0.2, 0.2 );
  71. uniforms[ 'shininess' ].value = 500;
  72. uniforms[ 'thicknessMap' ].value = thicknessTexture;
  73. uniforms[ 'thicknessColor' ].value = new THREE.Vector3( 0.5, 0.3, 0.0 );
  74. uniforms[ 'thicknessDistortion' ].value = 0.1;
  75. uniforms[ 'thicknessAmbient' ].value = 0.4;
  76. uniforms[ 'thicknessAttenuation' ].value = 0.8;
  77. uniforms[ 'thicknessPower' ].value = 2.0;
  78. uniforms[ 'thicknessScale' ].value = 16.0;
  79. var material = new THREE.ShaderMaterial( {
  80. uniforms: uniforms,
  81. vertexShader: shader.vertexShader,
  82. fragmentShader: shader.fragmentShader,
  83. lights: true
  84. } );
  85. material.extensions.derivatives = true;
  86. // LOADER
  87. var loader = new FBXLoader();
  88. loader.load( 'models/fbx/stanford-bunny.fbx', function ( object ) {
  89. model = object.children[ 0 ];
  90. model.position.set( 0, 0, 10 );
  91. model.scale.setScalar( 1 );
  92. model.material = material;
  93. scene.add( model );
  94. } );
  95. initGUI( uniforms );
  96. }
  97. function initGUI( uniforms ) {
  98. var gui = new GUI();
  99. var ThicknessControls = function () {
  100. this.distoration = uniforms[ 'thicknessDistortion' ].value;
  101. this.ambient = uniforms[ 'thicknessAmbient' ].value;
  102. this.attenuation = uniforms[ 'thicknessAttenuation' ].value;
  103. this.power = uniforms[ 'thicknessPower' ].value;
  104. this.scale = uniforms[ 'thicknessScale' ].value;
  105. };
  106. var thicknessControls = new ThicknessControls();
  107. var thicknessFolder = gui.addFolder( 'Thickness Control' );
  108. thicknessFolder.add( thicknessControls, 'distoration' ).min( 0.01 ).max( 1 ).step( 0.01 ).onChange( function () {
  109. uniforms[ 'thicknessDistortion' ].value = thicknessControls.distoration;
  110. } );
  111. thicknessFolder.add( thicknessControls, 'ambient' ).min( 0.01 ).max( 5.0 ).step( 0.05 ).onChange( function () {
  112. uniforms[ 'thicknessAmbient' ].value = thicknessControls.ambient;
  113. } );
  114. thicknessFolder.add( thicknessControls, 'attenuation' ).min( 0.01 ).max( 5.0 ).step( 0.05 ).onChange( function () {
  115. uniforms[ 'thicknessAttenuation' ].value = thicknessControls.attenuation;
  116. } );
  117. thicknessFolder.add( thicknessControls, 'power' ).min( 0.01 ).max( 16.0 ).step( 0.1 ).onChange( function () {
  118. uniforms[ 'thicknessPower' ].value = thicknessControls.power;
  119. } );
  120. thicknessFolder.add( thicknessControls, 'scale' ).min( 0.01 ).max( 50.0 ).step( 0.1 ).onChange( function () {
  121. uniforms[ 'thicknessScale' ].value = thicknessControls.scale;
  122. } );
  123. thicknessFolder.open();
  124. }
  125. function onWindowResize() {
  126. camera.aspect = window.innerWidth / window.innerHeight;
  127. camera.updateProjectionMatrix();
  128. renderer.setSize( window.innerWidth, window.innerHeight );
  129. }
  130. //
  131. function animate() {
  132. requestAnimationFrame( animate );
  133. render();
  134. stats.update();
  135. }
  136. function render() {
  137. if ( model ) model.rotation.y = performance.now() / 5000;
  138. renderer.render( scene, camera );
  139. }
  140. </script>
  141. </body>
  142. </html>