webgl_nodes_loader_gltf_transmission.html 4.6 KB

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