webgl_materials_physical_sheen.html 4.6 KB

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