webgpu_materialx_noise.html 4.6 KB

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