webgl_nodes_loader_gltf_sheen.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - GLTFloader + Sheen + Nodes</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. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. body {
  10. background: #bbbbbb;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <div id="info">
  16. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - GLTFLoader + <a href="https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_sheen" target="_blank" rel="noopener">KHR_materials_sheen</a> + Nodes<br />
  17. Sheen Chair from <a href="https://github.com/KhronosGroup/glTF-Sample-Models/tree/master/2.0/SheenChair" target="_blank" rel="noopener">glTF-Sample-Models</a>
  18. </div>
  19. <!-- Import maps polyfill -->
  20. <!-- Remove this when import maps will be widely supported -->
  21. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  22. <script type="importmap">
  23. {
  24. "imports": {
  25. "three": "../build/three.module.js",
  26. "three/addons/": "./jsm/",
  27. "three/nodes": "./jsm/nodes/Nodes.js"
  28. }
  29. }
  30. </script>
  31. <script type="module">
  32. import * as THREE from 'three';
  33. import { NodeMaterial, uv, mix, color, checker } from 'three/nodes';
  34. import { nodeFrame } from 'three/addons/renderers/webgl/nodes/WebGLNodes.js';
  35. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  36. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  37. import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
  38. let camera, scene, renderer, controls;
  39. init();
  40. animate();
  41. function init() {
  42. const container = document.createElement( 'div' );
  43. document.body.appendChild( container );
  44. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 20 );
  45. camera.position.set( - 0.75, 0.7, 1.25 );
  46. scene = new THREE.Scene();
  47. // model
  48. new GLTFLoader()
  49. .setPath( 'models/gltf/' )
  50. .load( 'SheenChair.glb', function ( gltf ) {
  51. scene.add( gltf.scene );
  52. const object = gltf.scene.getObjectByName( 'SheenChair_fabric' );
  53. // Convert to NodeMaterial
  54. const material = NodeMaterial.fromMaterial( object.material ); // @TODO: NodeMaterial.fromMaterial can be removed if WebGLNodes will apply it by default (as in WebGPURenderer)
  55. const checkerNode = checker( uv().mul( 5 ) );
  56. material.sheenNode = mix( color( 0x00ffff ), color( 0xffff00 ), checkerNode );
  57. material.sheenRoughnessNode = checkerNode;
  58. object.material = material;
  59. } );
  60. renderer = new THREE.WebGLRenderer( { antialias: true } );
  61. renderer.setPixelRatio( window.devicePixelRatio );
  62. renderer.setSize( window.innerWidth, window.innerHeight );
  63. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  64. renderer.toneMappingExposure = 1;
  65. container.appendChild( renderer.domElement );
  66. const environment = new RoomEnvironment();
  67. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  68. scene.background = new THREE.Color( 0xbbbbbb );
  69. scene.environment = pmremGenerator.fromScene( environment ).texture;
  70. controls = new OrbitControls( camera, renderer.domElement );
  71. controls.enableDamping = true;
  72. controls.minDistance = 1;
  73. controls.maxDistance = 10;
  74. controls.target.set( 0, 0.35, 0 );
  75. controls.update();
  76. window.addEventListener( 'resize', onWindowResize );
  77. }
  78. function onWindowResize() {
  79. camera.aspect = window.innerWidth / window.innerHeight;
  80. camera.updateProjectionMatrix();
  81. renderer.setSize( window.innerWidth, window.innerHeight );
  82. }
  83. //
  84. function animate() {
  85. requestAnimationFrame( animate );
  86. nodeFrame.update();
  87. controls.update(); // required if damping enabled
  88. render();
  89. }
  90. function render() {
  91. renderer.render( scene, camera );
  92. }
  93. </script>
  94. </body>
  95. </html>