webgpu_materialx_noise.html 5.0 KB

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