webgl_nodes_loader_gltf_transmission.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - GLTFloader + transmission + 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. </head>
  9. <body>
  10. <div id="info">
  11. <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_transmission" target="_blank" rel="noopener">KHR_materials_transmission</a> + Nodes<br />
  12. Iridescent Dish With Olives by <a href="https://github.com/echadwick-wayfair" target="_blank" rel="noopener">Eric Chadwick</a><br />
  13. <a href="https://hdrihaven.com/hdri/?h=royal_esplanade" target="_blank" rel="noopener">Royal Esplanade</a> by <a href="https://hdrihaven.com/" target="_blank" rel="noopener">HDRI Haven</a>
  14. </div>
  15. <!-- Import maps polyfill -->
  16. <!-- Remove this when import maps will be widely supported -->
  17. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  18. <script type="importmap">
  19. {
  20. "imports": {
  21. "three": "../build/three.module.js",
  22. "three/addons/": "./jsm/",
  23. "three/nodes": "./jsm/nodes/Nodes.js"
  24. }
  25. }
  26. </script>
  27. <script type="module">
  28. import * as THREE from 'three';
  29. import { NodeMaterial, float, texture } from 'three/nodes';
  30. import { nodeFrame } from 'three/addons/renderers/webgl/nodes/WebGLNodes.js';
  31. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  32. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  33. import { RGBELoader } from 'three/addons/loaders/RGBELoader.js';
  34. import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
  35. let camera, scene, renderer, controls, clock, mixer;
  36. init();
  37. animate();
  38. function init() {
  39. clock = new THREE.Clock();
  40. const container = document.createElement( 'div' );
  41. document.body.appendChild( container );
  42. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 20 );
  43. camera.position.set( 0, 0.4, 0.7 );
  44. scene = new THREE.Scene();
  45. new RGBELoader()
  46. .setPath( 'textures/equirectangular/' )
  47. .load( 'royal_esplanade_1k.hdr', function ( envMap ) {
  48. envMap.mapping = THREE.EquirectangularReflectionMapping;
  49. scene.background = envMap;
  50. scene.environment = envMap;
  51. // model
  52. new GLTFLoader()
  53. .setPath( 'models/gltf/' )
  54. .setDRACOLoader( new DRACOLoader().setDecoderPath( 'jsm/libs/draco/gltf/' ) )
  55. .load( 'IridescentDishWithOlives.glb', function ( gltf ) {
  56. // nodes
  57. const glassMesh = gltf.scene.getObjectByName( 'glassCover' );
  58. const material = glassMesh.material;
  59. if ( material && material.transmission > 0 ) {
  60. const nodeMaterial = NodeMaterial.fromMaterial( material ); // @TODO: NodeMaterial.fromMaterial can be removed if WebGLNodes will apply it by default (as in WebGPURenderer)
  61. nodeMaterial.transmissionNode = float( 1 );
  62. nodeMaterial.iorNode = float( 1.5 );
  63. nodeMaterial.thicknessNode = texture( material.thicknessMap ).g.mul( 0.1 );
  64. //nodeMaterial.attenuationDistanceNode;
  65. //nodeMaterial.attenuationColorNode;
  66. // ignore traditional maps
  67. nodeMaterial.transmissionMap = null;
  68. nodeMaterial.thicknessMap = null;
  69. glassMesh.material = nodeMaterial;
  70. }
  71. mixer = new THREE.AnimationMixer( gltf.scene );
  72. mixer.clipAction( gltf.animations[ 0 ] ).play();
  73. scene.add( gltf.scene );
  74. } );
  75. } );
  76. renderer = new THREE.WebGLRenderer( { antialias: true } );
  77. renderer.setPixelRatio( window.devicePixelRatio );
  78. renderer.setSize( window.innerWidth, window.innerHeight );
  79. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  80. renderer.toneMappingExposure = 1;
  81. container.appendChild( renderer.domElement );
  82. controls = new OrbitControls( camera, renderer.domElement );
  83. controls.enableDamping = true;
  84. controls.minDistance = 0.5;
  85. controls.maxDistance = 1;
  86. controls.target.set( 0, 0.1, 0 );
  87. controls.update();
  88. window.addEventListener( 'resize', onWindowResize );
  89. }
  90. function onWindowResize() {
  91. camera.aspect = window.innerWidth / window.innerHeight;
  92. camera.updateProjectionMatrix();
  93. renderer.setSize( window.innerWidth, window.innerHeight );
  94. }
  95. //
  96. function animate() {
  97. requestAnimationFrame( animate );
  98. if ( mixer ) mixer.update( clock.getDelta() );
  99. controls.update(); // required if damping enabled
  100. render();
  101. }
  102. function render() {
  103. nodeFrame.update();
  104. renderer.render( scene, camera );
  105. }
  106. </script>
  107. </body>
  108. </html>