webgl_loader_nodes.html 4.7 KB

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