webgpu_materialx_noise.html 5.0 KB

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