webgl_nodes_materialx_noise.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - materialx 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> webgl - MaterialX - Noise
  12. </div>
  13. <!-- Import maps polyfill -->
  14. <!-- Remove this when import maps will be widely supported -->
  15. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  16. <script type="importmap">
  17. {
  18. "imports": {
  19. "three": "../build/three.module.js",
  20. "three-nodes/": "./jsm/nodes/"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import { MeshPhysicalNodeMaterial, add, mul, normalWorld, saturate, timerLocal } from 'three-nodes/Nodes.js';
  27. import {
  28. mx_perlin_noise_float,
  29. mx_cell_noise_float,
  30. mx_worley_noise_float,
  31. mx_fractal_noise_float
  32. } from 'three-nodes/materialx/functions/lib/mx_noise.js';
  33. import { nodeFrame } from './jsm/renderers/webgl/nodes/WebGLNodes.js';
  34. import Stats from './jsm/libs/stats.module.js';
  35. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  36. import { HDRCubeTextureLoader } from './jsm/loaders/HDRCubeTextureLoader.js';
  37. let container, stats;
  38. let camera, scene, renderer;
  39. let particleLight;
  40. let group;
  41. init();
  42. animate();
  43. function init() {
  44. container = document.createElement( 'div' );
  45. document.body.appendChild( container );
  46. camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 1, 10000 );
  47. camera.position.z = 1000;
  48. scene = new THREE.Scene();
  49. group = new THREE.Group();
  50. scene.add( group );
  51. new HDRCubeTextureLoader()
  52. .setPath( 'textures/cube/pisaHDR/' )
  53. .load( [ 'px.hdr', 'nx.hdr', 'py.hdr', 'ny.hdr', 'pz.hdr', 'nz.hdr' ],
  54. function ( hdrTexture ) {
  55. const geometry = new THREE.SphereGeometry( 80, 64, 32 );
  56. const offsetNode = timerLocal();
  57. const customUV = add( mul( normalWorld, 10 ), offsetNode );
  58. // left top
  59. let material = new MeshPhysicalNodeMaterial();
  60. material.colorNode = mx_perlin_noise_float( customUV );
  61. let mesh = new THREE.Mesh( geometry, material );
  62. mesh.position.x = - 100;
  63. mesh.position.y = 100;
  64. group.add( mesh );
  65. // right top
  66. material = new MeshPhysicalNodeMaterial();
  67. material.colorNode = mx_cell_noise_float( customUV );
  68. mesh = new THREE.Mesh( geometry, material );
  69. mesh.position.x = 100;
  70. mesh.position.y = 100;
  71. group.add( mesh );
  72. // left bottom
  73. material = new MeshPhysicalNodeMaterial();
  74. material.colorNode = mx_worley_noise_float( customUV, 1, 1 );
  75. mesh = new THREE.Mesh( geometry, material );
  76. mesh.position.x = - 100;
  77. mesh.position.y = - 100;
  78. group.add( mesh );
  79. // right bottom
  80. material = new MeshPhysicalNodeMaterial();
  81. material.colorNode = saturate( mul( add( mx_fractal_noise_float( mul( customUV, .2 ), 7, 2, .7 ), 1 ), .5 ) );
  82. mesh = new THREE.Mesh( geometry, material );
  83. mesh.position.x = 100;
  84. mesh.position.y = - 100;
  85. group.add( mesh );
  86. //
  87. scene.background = hdrTexture;
  88. scene.environment = hdrTexture;
  89. }
  90. );
  91. // LIGHTS
  92. particleLight = new THREE.Mesh(
  93. new THREE.SphereGeometry( 4, 8, 8 ),
  94. new THREE.MeshBasicMaterial( { color: 0xffffff } )
  95. );
  96. scene.add( particleLight );
  97. particleLight.add( new THREE.PointLight( 0xffffff, 1 ) );
  98. renderer = new THREE.WebGLRenderer();
  99. renderer.setPixelRatio( window.devicePixelRatio );
  100. renderer.setSize( window.innerWidth, window.innerHeight );
  101. container.appendChild( renderer.domElement );
  102. //
  103. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  104. renderer.toneMappingExposure = 1.25;
  105. //
  106. renderer.outputEncoding = THREE.sRGBEncoding;
  107. //
  108. stats = new Stats();
  109. container.appendChild( stats.dom );
  110. // EVENTS
  111. new OrbitControls( camera, renderer.domElement );
  112. window.addEventListener( 'resize', onWindowResize );
  113. }
  114. //
  115. function onWindowResize() {
  116. const width = window.innerWidth;
  117. const height = window.innerHeight;
  118. camera.aspect = width / height;
  119. camera.updateProjectionMatrix();
  120. renderer.setSize( width, height );
  121. }
  122. //
  123. function animate() {
  124. requestAnimationFrame( animate );
  125. nodeFrame.update();
  126. render();
  127. stats.update();
  128. }
  129. function render() {
  130. const timer = Date.now() * 0.00025;
  131. particleLight.position.x = Math.sin( timer * 7 ) * 300;
  132. particleLight.position.y = Math.cos( timer * 5 ) * 400;
  133. particleLight.position.z = Math.cos( timer * 3 ) * 300;
  134. for ( let i = 0; i < group.children.length; i ++ ) {
  135. const child = group.children[ i ];
  136. child.rotation.y += 0.005;
  137. }
  138. renderer.render( scene, camera );
  139. }
  140. </script>
  141. </body>
  142. </html>