webgl_nodes_loader_gltf_iridescence.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. <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, uv, vec2, checker, float, timerLocal } 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. let renderer, scene, camera, controls;
  32. init().catch( function ( err ) {
  33. console.error( err );
  34. } );
  35. async function init() {
  36. renderer = new THREE.WebGLRenderer( { antialias: true } );
  37. renderer.setPixelRatio( window.devicePixelRatio );
  38. renderer.setSize( window.innerWidth, window.innerHeight );
  39. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  40. renderer.setAnimationLoop( render );
  41. document.body.appendChild( renderer.domElement );
  42. scene = new THREE.Scene();
  43. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.25, 20 );
  44. camera.position.set( 0.35, 0.05, 0.35 );
  45. controls = new OrbitControls( camera, renderer.domElement );
  46. controls.target.set( 0, 0.2, 0 );
  47. controls.update();
  48. const rgbeLoader = new RGBELoader()
  49. .setPath( 'textures/equirectangular/' );
  50. const gltfLoader = new GLTFLoader().setPath( 'models/gltf/' );
  51. const [ texture, gltf ] = await Promise.all( [
  52. rgbeLoader.loadAsync( 'venice_sunset_1k.hdr' ),
  53. gltfLoader.loadAsync( 'IridescenceLamp.glb' ),
  54. ] );
  55. // nodes
  56. gltf.scene.traverse( mesh => {
  57. const material = mesh.material;
  58. if ( material && material.iridescence > 0 ) {
  59. const iridescenceFactorNode = checker( uv().add( vec2( timerLocal( - .05 ), 0 ) ).mul( 20 ) );
  60. const nodeMaterial = NodeMaterial.fromMaterial( material ); // @TODO: NodeMaterial.fromMaterial can be removed if WebGLNodes will apply it by default (as in WebGPURenderer)
  61. nodeMaterial.iridescenceNode = iridescenceFactorNode;
  62. nodeMaterial.iridescenceIORNode = float( 1.3 );
  63. nodeMaterial.iridescenceThicknessNode = float( 400 );
  64. mesh.material = nodeMaterial;
  65. }
  66. } );
  67. // environment
  68. texture.mapping = THREE.EquirectangularReflectionMapping;
  69. scene.background = texture;
  70. scene.environment = texture;
  71. // model
  72. scene.add( gltf.scene );
  73. window.addEventListener( 'resize', onWindowResize );
  74. }
  75. function onWindowResize() {
  76. camera.aspect = window.innerWidth / window.innerHeight;
  77. camera.updateProjectionMatrix();
  78. renderer.setSize( window.innerWidth, window.innerHeight );
  79. }
  80. function render() {
  81. nodeFrame.update();
  82. renderer.render( scene, camera );
  83. }
  84. </script>
  85. </body>
  86. </html>