webgl_loader_nodes.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - node material</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="container"></div>
  11. <div id="info">
  12. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - Node-Based Material<br />
  13. Serialized using <a href="webgl_materials_nodes.html">webgl_materials_nodes.html</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. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three';
  27. import { GUI } from './jsm/libs/lil-gui.module.min.js';
  28. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  29. import { NodeMaterialLoader } from './jsm/loaders/NodeMaterialLoader.js';
  30. import { TeapotGeometry } from './jsm/geometries/TeapotGeometry.js';
  31. import { NodeFrame } from './jsm/nodes/core/NodeFrame.js';
  32. import { NodeMaterial } from './jsm/nodes/materials/NodeMaterial.js';
  33. const container = document.getElementById( 'container' );
  34. let renderer, scene, camera;
  35. const clock = new THREE.Clock(), fov = 50;
  36. const frame = new NodeFrame();
  37. let teapot, mesh, cloud;
  38. let controls;
  39. let gui;
  40. const param = { load: 'caustic' };
  41. window.addEventListener( 'load', init );
  42. function init() {
  43. renderer = new THREE.WebGLRenderer( { antialias: true } );
  44. renderer.setPixelRatio( window.devicePixelRatio );
  45. renderer.setSize( window.innerWidth, window.innerHeight );
  46. container.appendChild( renderer.domElement );
  47. scene = new THREE.Scene();
  48. camera = new THREE.PerspectiveCamera( fov, window.innerWidth / window.innerHeight, 1, 1000 );
  49. camera.position.x = 50;
  50. camera.position.z = - 50;
  51. camera.position.y = 30;
  52. cloud = new THREE.TextureLoader().load( 'textures/lava/cloud.png' );
  53. cloud.wrapS = cloud.wrapT = THREE.RepeatWrapping;
  54. controls = new OrbitControls( camera, renderer.domElement );
  55. controls.minDistance = 50;
  56. controls.maxDistance = 200;
  57. scene.add( new THREE.AmbientLight( 0x464646 ) );
  58. const light1 = new THREE.DirectionalLight( 0xffddcc, 1 );
  59. light1.position.set( 1, 0.75, 0.5 );
  60. scene.add( light1 );
  61. const light2 = new THREE.DirectionalLight( 0xccccff, 1 );
  62. light2.position.set( - 1, 0.75, - 0.5 );
  63. scene.add( light2 );
  64. teapot = new TeapotGeometry( 15, 18 );
  65. mesh = new THREE.Mesh( teapot );
  66. scene.add( mesh );
  67. window.addEventListener( 'resize', onWindowResize );
  68. updateMaterial();
  69. onWindowResize();
  70. animate();
  71. }
  72. function clearGui() {
  73. if ( gui ) gui.destroy();
  74. gui = new GUI();
  75. gui.add( param, 'load', {
  76. 'caustic': 'caustic',
  77. 'displace': 'displace',
  78. 'wave': 'wave',
  79. 'xray': 'xray'
  80. } ).onFinishChange( function () {
  81. updateMaterial();
  82. } );
  83. gui.open();
  84. }
  85. function addGui( name, value, callback, isColor, min, max ) {
  86. let node;
  87. param[ name ] = value;
  88. if ( isColor ) {
  89. node = gui.addColor( param, name ).onChange( function () {
  90. callback( param[ name ] );
  91. } );
  92. } else if ( typeof value == 'object' ) {
  93. node = gui.add( param, name, value ).onChange( function () {
  94. callback( param[ name ] );
  95. } );
  96. } else {
  97. node = gui.add( param, name, min, max ).onChange( function () {
  98. callback( param[ name ] );
  99. } );
  100. }
  101. return node;
  102. }
  103. function updateMaterial() {
  104. if ( mesh.material ) mesh.material.dispose();
  105. clearGui();
  106. const url = 'nodes/' + param.load + '.json';
  107. const library = {
  108. 'cloud': cloud
  109. };
  110. const loader = new NodeMaterialLoader( undefined, library ).load( url, function () {
  111. const time = loader.getObjectByName( 'time' );
  112. if ( time ) {
  113. // enable time scale
  114. time.timeScale = true;
  115. // gui
  116. addGui( 'timeScale', time.scale, function ( val ) {
  117. time.scale = val;
  118. }, false, - 2, 2 );
  119. }
  120. // set material
  121. mesh.material = loader.material;
  122. } );
  123. }
  124. function onWindowResize() {
  125. const width = window.innerWidth, height = window.innerHeight;
  126. camera.aspect = width / height;
  127. camera.updateProjectionMatrix();
  128. renderer.setSize( width, height );
  129. }
  130. function animate() {
  131. const delta = clock.getDelta();
  132. // update material animation and/or gpu calcs (pre-renderer)
  133. if ( mesh.material instanceof NodeMaterial ) frame.update( delta ).updateNode( mesh.material );
  134. renderer.render( scene, camera );
  135. requestAnimationFrame( animate );
  136. }
  137. </script>
  138. </body>
  139. </html>