webgl_loader_nodes.html 4.7 KB

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