2
0

webgl_materials_physical_sheen.html 4.5 KB

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