webgl_materials_physical_sheen.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <html lang="en">
  2. <head>
  3. <title>Sheen demo (material property)</title>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  6. <link type="text/css" rel="stylesheet" href="main.css">
  7. <style>
  8. body {
  9. color: #333;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <div id="info">Sheen demo by <a href="https://github.com/DanielSturk">DanielSturk</a></div>
  15. <div id="container"></div>
  16. <script src="js/libs/ammo.js"></script>
  17. <script type="module">
  18. import * as THREE from '../build/three.module.js';
  19. import * as Nodes from './jsm/nodes/Nodes.js';
  20. import Stats from './jsm/libs/stats.module.js';
  21. import { GUI } from './jsm/libs/dat.gui.module.js';
  22. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  23. import { FBXLoader } from './jsm/loaders/FBXLoader.js';
  24. // Graphics variables
  25. var camera, controls, scene, renderer, stats;
  26. var directionalLight;
  27. var mesh, sphere, material, nodeMaterial;
  28. var params = {
  29. nodeMaterial: true,
  30. color: new THREE.Color( 255, 0, 127 ),
  31. sheenBRDF: true,
  32. sheen: new THREE.Color( 10, 10, 10 ), // corresponds to .04 reflectance
  33. roughness: .9,
  34. exposure: 2,
  35. };
  36. // model
  37. new FBXLoader().load( 'models/fbx/cloth.fbx', function ( loadedModel ) {
  38. mesh = loadedModel.children[0];
  39. init();
  40. } );
  41. function init( ) {
  42. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.2, 2000 );
  43. scene = new THREE.Scene();
  44. scene.background = new THREE.Color( 0xbfd1e5 );
  45. mesh.scale.multiplyScalar( .5 );
  46. scene.add( mesh );
  47. //
  48. material = new THREE.MeshPhysicalMaterial();
  49. material.side = THREE.DoubleSide;
  50. material.metalness = 0;
  51. //
  52. nodeMaterial = new Nodes.StandardNodeMaterial();
  53. nodeMaterial.side = THREE.DoubleSide;
  54. nodeMaterial.metalness = new Nodes.FloatNode( 0 );
  55. nodeMaterial.roughness = new Nodes.FloatNode();
  56. nodeMaterial.color = new Nodes.ColorNode( params.color.clone() );
  57. //
  58. sphere = new THREE.Mesh(
  59. new THREE.SphereBufferGeometry( 1, 100, 100 ),
  60. material
  61. );
  62. scene.add(sphere);
  63. camera.position.set( - 12, 7, 4 );
  64. var container = document.getElementById( 'container' );
  65. renderer = new THREE.WebGLRenderer();
  66. renderer.setPixelRatio( window.devicePixelRatio );
  67. renderer.setSize( window.innerWidth, window.innerHeight );
  68. renderer.shadowMap.enabled = true;
  69. container.appendChild( renderer.domElement );
  70. controls = new OrbitControls( camera, renderer.domElement );
  71. controls.target.set( 0, 2, 0 );
  72. controls.update();
  73. directionalLight = new THREE.DirectionalLight( 0xffffff, .5 );
  74. directionalLight.position.set( 0, 10, 0 );
  75. directionalLight.castShadow = true;
  76. directionalLight.add(
  77. new THREE.Mesh(
  78. new THREE.SphereBufferGeometry( .5 ),
  79. new THREE.MeshBasicMaterial( { color: 0xffffff } )
  80. )
  81. );
  82. scene.add( directionalLight );
  83. stats = new Stats();
  84. stats.domElement.style.position = 'absolute';
  85. stats.domElement.style.top = '0px';
  86. container.appendChild( stats.dom );
  87. window.addEventListener( 'resize', onWindowResize, false );
  88. var gui = new GUI();
  89. function onUpdate() {
  90. mesh.material = sphere.material = params.nodeMaterial
  91. ? nodeMaterial
  92. : material;
  93. material.sheen = params.sheenBRDF
  94. ? new THREE.Color()
  95. : null;
  96. material.needsUpdate = true;
  97. nodeMaterial.sheen = params.sheenBRDF
  98. ? new Nodes.ColorNode( material.sheen )
  99. : undefined;
  100. nodeMaterial.needsCompile = true;
  101. }
  102. gui.add( params, 'nodeMaterial' ).onChange( onUpdate );
  103. gui.addColor( params, 'color' );
  104. gui.add( params, 'sheenBRDF' ).onChange( onUpdate );
  105. gui.addColor( params, 'sheen' );
  106. gui.add( params, 'roughness', 0, 1 );
  107. gui.add( params, 'exposure', 0, 3 );
  108. gui.open();
  109. onUpdate();
  110. animate();
  111. }
  112. function onWindowResize() {
  113. camera.aspect = window.innerWidth / window.innerHeight;
  114. camera.updateProjectionMatrix();
  115. renderer.setSize( window.innerWidth, window.innerHeight );
  116. }
  117. function animate() {
  118. requestAnimationFrame( animate );
  119. render();
  120. stats.update();
  121. }
  122. function render() {
  123. //
  124. material.color.copy( params.color ).multiplyScalar( 1 / 255 );
  125. material.roughness = params.roughness;
  126. //
  127. nodeMaterial.color.value.copy( material.color );
  128. nodeMaterial.roughness.value = params.roughness;
  129. //
  130. if ( params.sheenBRDF ) {
  131. material.sheen.copy( params.sheen ).multiplyScalar( 1 / 255 );
  132. }
  133. //
  134. renderer.toneMappingExposure = params.exposure;
  135. renderer.render( scene, camera );
  136. }
  137. </script>
  138. </body>
  139. </html>