webgl_materials_translucency.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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">Shaochun Lin</a>]
  29. </div>
  30. <script src="../build/three.js"></script>
  31. <script src="js/controls/OrbitControls.js"></script>
  32. <script src="js/Detector.js"></script>
  33. <script src="js/libs/stats.min.js"></script>
  34. <script src="js/loaders/FBXLoader.js"></script>
  35. <script src="js/ShaderTranslucent.js"></script>
  36. <script>
  37. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  38. var container, stats, font;
  39. var camera, scene, renderer, controls;
  40. init();
  41. animate();
  42. function init() {
  43. container = document.createElement( 'div' );
  44. document.body.appendChild( container );
  45. font = font;
  46. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 5000 );
  47. camera.position.set( 0.0, 300, 400 * 4 );
  48. scene = new THREE.Scene();
  49. // Lights
  50. scene.add( new THREE.AmbientLight( 0x888888 ) );
  51. var directionalLight = new THREE.DirectionalLight( 0xffffff, 0.03 );
  52. directionalLight.position.set( 0.0, 0.5, 0.5 ).normalize();
  53. scene.add( directionalLight );
  54. var pointLight1 = new THREE.Mesh( new THREE.SphereBufferGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0x888888 } ) );
  55. pointLight1.add( new THREE.PointLight( 0x888888, 7.0, 300 ) );
  56. scene.add( pointLight1 );
  57. pointLight1.position.x = 0;
  58. pointLight1.position.y = 0;
  59. pointLight1.position.z = 350;
  60. var pointLight2 = new THREE.Mesh( new THREE.SphereBufferGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0x888800 } ) );
  61. pointLight2.add( new THREE.PointLight( 0x888800, 1.0, 500 ) );
  62. scene.add( pointLight2 );
  63. pointLight2.position.x = 150;
  64. pointLight2.position.y = -100;
  65. pointLight2.position.z = -250;
  66. renderer = new THREE.WebGLRenderer( { antialias: true } );
  67. renderer.setPixelRatio( window.devicePixelRatio );
  68. renderer.setSize( window.innerWidth, window.innerHeight );
  69. container.appendChild( renderer.domElement );
  70. renderer.gammaInput = true;
  71. renderer.gammaOutput = true;
  72. //
  73. stats = new Stats();
  74. container.appendChild( stats.dom );
  75. controls = new THREE.OrbitControls( camera );
  76. window.addEventListener( 'resize', onWindowResize, false );
  77. initMaterial();
  78. }
  79. function initMaterial() {
  80. var shader = new THREE.TranslucentShader();
  81. var uniforms = THREE.UniformsUtils.clone( shader.uniforms );
  82. // Materials
  83. var loader = new THREE.TextureLoader();
  84. var imgTexture = loader.load("models/fbx/white.jpg");
  85. var thicknessTexture = loader.load("models/fbx/bunny_thickness.png");
  86. imgTexture.wrapS = imgTexture.wrapT = THREE.RepeatWrapping;
  87. uniforms[ "map" ].value = imgTexture;
  88. uniforms[ "diffuse" ].value = new THREE.Vector3(1.0, 0.2, 0.2);
  89. uniforms[ "shininess" ].value = 500;
  90. uniforms[ "thicknessMap" ].value = thicknessTexture;
  91. uniforms[ "thicknessColor" ].value = new THREE.Vector3(0.5, 0.3, 0.3);
  92. uniforms[ "thicknessDistortion" ].value = 0.1;
  93. uniforms[ "thicknessAmbient" ].value = 0.1;
  94. uniforms[ "thicknessAttenuation" ].value = 0.8;
  95. uniforms[ "thicknessPower" ].value = 2;
  96. uniforms[ "thicknessScale" ].value = 10.0;
  97. var parameters = { fragmentShader: shader.fragmentShader, vertexShader: shader.vertexShader, uniforms: uniforms, lights: true };
  98. var material = new THREE.ShaderMaterial( parameters );
  99. material.extensions.derivatives = true;
  100. // LOADER
  101. var loader = new THREE.FBXLoader();
  102. loader.load("models/fbx/stanford-bunny.fbx", function(object) {
  103. loadedModel(object, 1, material);
  104. });
  105. }
  106. function loadedModel(object, scale, material) {
  107. object.children[0].position.x = 0;
  108. object.children[0].position.y = 0;
  109. object.children[0].position.z = 10;
  110. object.children[0].scale.set(scale, scale, scale);
  111. object.children[0].material = material;
  112. scene.add(object);
  113. }
  114. function onWindowResize() {
  115. camera.aspect = window.innerWidth / window.innerHeight;
  116. camera.updateProjectionMatrix();
  117. renderer.setSize( window.innerWidth, window.innerHeight );
  118. }
  119. //
  120. function animate() {
  121. requestAnimationFrame( animate );
  122. render();
  123. stats.update();
  124. }
  125. function render() {
  126. var timer = Date.now() * 0.00025;
  127. camera.lookAt( scene.position );
  128. renderer.render( scene, camera );
  129. }
  130. </script>
  131. </body>
  132. </html>