webgl_nodes_materialx_noise.html 4.9 KB

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