webgl_nodes_loader_gltf_iridescence.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - GLTFloader + Iridescence + 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_iridescence" target="_blank" rel="noopener">KHR_materials_iridescence</a> + Nodes<br />
  12. Iridescence Lamp from <a href="https://github.com/KhronosGroup/glTF-Sample-Models/tree/master/2.0/IridescenceLamp" target="_blank" rel="noopener">glTF-Sample-Models</a><br />
  13. <a href="https://hdrihaven.com/hdri/?h=venice_sunset" target="_blank" rel="noopener">Venice Sunset</a> from <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, uv, vec2, checker, float, timerLocal } 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. let renderer, scene, camera, controls;
  35. init().catch( function ( err ) {
  36. console.error( err );
  37. } );
  38. async function init() {
  39. renderer = new THREE.WebGLRenderer( { antialias: true } );
  40. renderer.setPixelRatio( window.devicePixelRatio );
  41. renderer.setSize( window.innerWidth, window.innerHeight );
  42. renderer.useLegacyLights = false;
  43. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  44. renderer.setAnimationLoop( render );
  45. document.body.appendChild( renderer.domElement );
  46. scene = new THREE.Scene();
  47. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.25, 20 );
  48. camera.position.set( 0.35, 0.05, 0.35 );
  49. controls = new OrbitControls( camera, renderer.domElement );
  50. controls.addEventListener( 'change', render );
  51. controls.target.set( 0, 0.2, 0 );
  52. controls.update();
  53. const rgbeLoader = new RGBELoader()
  54. .setPath( 'textures/equirectangular/' );
  55. const gltfLoader = new GLTFLoader().setPath( 'models/gltf/' );
  56. const [ texture, gltf ] = await Promise.all( [
  57. rgbeLoader.loadAsync( 'venice_sunset_1k.hdr' ),
  58. gltfLoader.loadAsync( 'IridescenceLamp.glb' ),
  59. ] );
  60. // nodes
  61. gltf.scene.traverse( mesh => {
  62. const material = mesh.material;
  63. if ( material && material.iridescence > 0 ) {
  64. const iridescenceFactorNode = checker( uv().add( vec2( timerLocal( - .05 ), 0 ) ).mul( 20 ) );
  65. const nodeMaterial = NodeMaterial.fromMaterial( material ); // @TODO: NodeMaterial.fromMaterial can be removed if WebGLNodes will apply it by default (as in WebGPURenderer)
  66. nodeMaterial.iridescenceNode = iridescenceFactorNode;
  67. nodeMaterial.iridescenceIORNode = float( 1.3 );
  68. nodeMaterial.iridescenceThicknessNode = float( 400 );
  69. mesh.material = nodeMaterial;
  70. }
  71. } );
  72. // environment
  73. texture.mapping = THREE.EquirectangularReflectionMapping;
  74. scene.background = texture;
  75. scene.environment = texture;
  76. // model
  77. scene.add( gltf.scene );
  78. window.addEventListener( 'resize', onWindowResize );
  79. }
  80. function onWindowResize() {
  81. camera.aspect = window.innerWidth / window.innerHeight;
  82. camera.updateProjectionMatrix();
  83. renderer.setSize( window.innerWidth, window.innerHeight );
  84. }
  85. function render() {
  86. nodeFrame.update();
  87. renderer.render( scene, camera );
  88. }
  89. </script>
  90. </body>
  91. </html>