webgl_materials_translucency.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. <style>
  8. body {
  9. color: #fff;
  10. font-family: Monospace;
  11. font-size: 13px;
  12. text-align: center;
  13. background-color: #000;
  14. margin: 0px;
  15. overflow: hidden;
  16. }
  17. #info {
  18. position: absolute;
  19. top: 0px; width: 100%;
  20. padding: 5px;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <div id="container"></div>
  26. <div id="info"><a href="http://threejs.org" target="_blank" rel="noopener">three.js</a>
  27. <br/>Fast subsurface scattering in Blinn-Phong shading demo<br/>
  28. [Thanks for the art support from <a href="https://github.com/shaochun" target="_blank" rel="noopener">Shaochun Lin</a>]
  29. </div>
  30. <script src="../build/three.js"></script>
  31. <script src="js/controls/OrbitControls.js"></script>
  32. <script src="js/WebGL.js"></script>
  33. <script src="js/libs/stats.min.js"></script>
  34. <script src="js/libs/dat.gui.min.js"></script>
  35. <script src="js/loaders/FBXLoader.js"></script>
  36. <script src="js/ShaderTranslucent.js"></script>
  37. <script>
  38. if ( WEBGL.isWebGLAvailable() === false ) {
  39. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  40. }
  41. var container, stats;
  42. var camera, scene, renderer;
  43. var model;
  44. init();
  45. animate();
  46. function init() {
  47. container = document.createElement( 'div' );
  48. document.body.appendChild( container );
  49. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 5000 );
  50. camera.position.set( 0.0, 300, 400 * 4 );
  51. scene = new THREE.Scene();
  52. // Lights
  53. scene.add( new THREE.AmbientLight( 0x888888 ) );
  54. var directionalLight = new THREE.DirectionalLight( 0xffffff, 0.03 );
  55. directionalLight.position.set( 0.0, 0.5, 0.5 ).normalize();
  56. scene.add( directionalLight );
  57. var pointLight1 = new THREE.Mesh( new THREE.SphereBufferGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0x888888 } ) );
  58. pointLight1.add( new THREE.PointLight( 0x888888, 7.0, 300 ) );
  59. scene.add( pointLight1 );
  60. pointLight1.position.x = 0;
  61. pointLight1.position.y = - 50;
  62. pointLight1.position.z = 350;
  63. var pointLight2 = new THREE.Mesh( new THREE.SphereBufferGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0x888800 } ) );
  64. pointLight2.add( new THREE.PointLight( 0x888800, 1.0, 500 ) );
  65. scene.add( pointLight2 );
  66. pointLight2.position.x = - 100;
  67. pointLight2.position.y = 20;
  68. pointLight2.position.z = - 260;
  69. renderer = new THREE.WebGLRenderer( { antialias: true } );
  70. renderer.setPixelRatio( window.devicePixelRatio );
  71. renderer.setSize( window.innerWidth, window.innerHeight );
  72. container.appendChild( renderer.domElement );
  73. renderer.gammaInput = true;
  74. renderer.gammaOutput = true;
  75. //
  76. stats = new Stats();
  77. container.appendChild( stats.dom );
  78. var controls = new THREE.OrbitControls( camera, container );
  79. window.addEventListener( 'resize', onWindowResize, false );
  80. initMaterial();
  81. }
  82. function initMaterial() {
  83. var loader = new THREE.TextureLoader();
  84. var imgTexture = loader.load( 'models/fbx/white.jpg' );
  85. var thicknessTexture = loader.load( 'models/fbx/bunny_thickness.jpg' );
  86. imgTexture.wrapS = imgTexture.wrapT = THREE.RepeatWrapping;
  87. var shader = new THREE.TranslucentShader();
  88. var uniforms = THREE.UniformsUtils.clone( shader.uniforms );
  89. uniforms[ 'map' ].value = imgTexture;
  90. uniforms[ 'diffuse' ].value = new THREE.Vector3( 1.0, 0.2, 0.2 );
  91. uniforms[ 'shininess' ].value = 500;
  92. uniforms[ 'thicknessMap' ].value = thicknessTexture;
  93. uniforms[ 'thicknessColor' ].value = new THREE.Vector3( 0.5, 0.3, 0.0 );
  94. uniforms[ 'thicknessDistortion' ].value = 0.1;
  95. uniforms[ 'thicknessAmbient' ].value = 0.4;
  96. uniforms[ 'thicknessAttenuation' ].value = 0.8;
  97. uniforms[ 'thicknessPower' ].value = 2.0;
  98. uniforms[ 'thicknessScale' ].value = 16.0;
  99. var material = new THREE.ShaderMaterial( {
  100. uniforms: uniforms,
  101. vertexShader: shader.vertexShader,
  102. fragmentShader: shader.fragmentShader,
  103. lights: true
  104. } );
  105. material.extensions.derivatives = true;
  106. // LOADER
  107. var loader = new THREE.FBXLoader();
  108. loader.load( 'models/fbx/stanford-bunny.fbx', function ( object ) {
  109. model = object.children[ 0 ];
  110. model.position.set( 0, 0, 10 );
  111. model.scale.setScalar( 1 );
  112. model.material = material;
  113. scene.add( model );
  114. } );
  115. initGUI( uniforms );
  116. }
  117. function initGUI( uniforms ) {
  118. var gui = new dat.GUI();
  119. var ThicknessControls = function () {
  120. this.distoration = uniforms[ 'thicknessDistortion' ].value;
  121. this.ambient = uniforms[ 'thicknessAmbient' ].value;
  122. this.attenuation = uniforms[ 'thicknessAttenuation' ].value;
  123. this.power = uniforms[ 'thicknessPower' ].value;
  124. this.scale = uniforms[ 'thicknessScale' ].value;
  125. };
  126. var thicknessControls = new ThicknessControls();
  127. var thicknessFolder = gui.addFolder( 'Thickness Control' );
  128. thicknessFolder.add( thicknessControls, 'distoration' ).min( 0.01 ).max( 1 ).step( 0.01 ).onChange( function () {
  129. uniforms[ 'thicknessDistortion' ].value = thicknessControls.distoration;
  130. } );
  131. thicknessFolder.add( thicknessControls, 'ambient' ).min( 0.01 ).max( 5.0 ).step( 0.05 ).onChange( function () {
  132. uniforms[ 'thicknessAmbient' ].value = thicknessControls.ambient;
  133. } );
  134. thicknessFolder.add( thicknessControls, 'attenuation' ).min( 0.01 ).max( 5.0 ).step( 0.05 ).onChange( function () {
  135. uniforms[ 'thicknessAttenuation' ].value = thicknessControls.attenuation;
  136. } );
  137. thicknessFolder.add( thicknessControls, 'power' ).min( 0.01 ).max( 16.0 ).step( 0.1 ).onChange( function () {
  138. uniforms[ 'thicknessPower' ].value = thicknessControls.power;
  139. } );
  140. thicknessFolder.add( thicknessControls, 'scale' ).min( 0.01 ).max( 50.0 ).step( 0.1 ).onChange( function () {
  141. uniforms[ 'thicknessScale' ].value = thicknessControls.scale;
  142. } );
  143. thicknessFolder.open();
  144. }
  145. function onWindowResize() {
  146. camera.aspect = window.innerWidth / window.innerHeight;
  147. camera.updateProjectionMatrix();
  148. renderer.setSize( window.innerWidth, window.innerHeight );
  149. }
  150. //
  151. function animate() {
  152. requestAnimationFrame( animate );
  153. render();
  154. stats.update();
  155. }
  156. function render() {
  157. if ( model ) model.rotation.y = performance.now() / 5000;
  158. renderer.render( scene, camera );
  159. }
  160. </script>
  161. </body>
  162. </html>