2
0

webgl_nodes_materialx_noise.html 5.1 KB

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