webgpu_materialx_noise.html 4.7 KB

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