2
0

webgl_nodes_materialx_noise.html 4.7 KB

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