webgpu_materialx_noise.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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, time, 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 customUV = normalWorld.mul( 10 ).add( time );
  47. // left top
  48. let material = new THREE.MeshPhysicalNodeMaterial();
  49. material.colorNode = mx_noise_vec3( customUV );
  50. let mesh = new THREE.Mesh( geometry, material );
  51. mesh.position.x = - 10;
  52. mesh.position.y = 10;
  53. group.add( mesh );
  54. // right top
  55. material = new THREE.MeshPhysicalNodeMaterial();
  56. material.colorNode = mx_cell_noise_float( customUV );
  57. mesh = new THREE.Mesh( geometry, material );
  58. mesh.position.x = 10;
  59. mesh.position.y = 10;
  60. group.add( mesh );
  61. // left bottom
  62. material = new THREE.MeshPhysicalNodeMaterial();
  63. material.colorNode = mx_worley_noise_vec3( customUV );
  64. mesh = new THREE.Mesh( geometry, material );
  65. mesh.position.x = - 10;
  66. mesh.position.y = - 10;
  67. group.add( mesh );
  68. // right bottom
  69. material = new THREE.MeshPhysicalNodeMaterial();
  70. material.colorNode = mx_fractal_noise_vec3( customUV.mul( .2 ) );
  71. mesh = new THREE.Mesh( geometry, material );
  72. mesh.position.x = 10;
  73. mesh.position.y = - 10;
  74. group.add( mesh );
  75. //
  76. scene.background = hdrTexture;
  77. scene.environment = hdrTexture;
  78. }
  79. );
  80. // LIGHTS
  81. particleLight = new THREE.Mesh(
  82. new THREE.SphereGeometry( 0.4, 8, 8 ),
  83. new THREE.MeshBasicMaterial( { color: 0xffffff } )
  84. );
  85. scene.add( particleLight );
  86. particleLight.add( new THREE.PointLight( 0xffffff, 1000 ) );
  87. renderer = new THREE.WebGPURenderer( { antialias: true } );
  88. renderer.setAnimationLoop( animate );
  89. renderer.setPixelRatio( window.devicePixelRatio );
  90. renderer.setSize( window.innerWidth, window.innerHeight );
  91. container.appendChild( renderer.domElement );
  92. //
  93. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  94. renderer.toneMappingExposure = 1.25;
  95. //
  96. //
  97. stats = new Stats();
  98. container.appendChild( stats.dom );
  99. // EVENTS
  100. new OrbitControls( camera, renderer.domElement );
  101. window.addEventListener( 'resize', onWindowResize );
  102. }
  103. //
  104. function onWindowResize() {
  105. const width = window.innerWidth;
  106. const height = window.innerHeight;
  107. camera.aspect = width / height;
  108. camera.updateProjectionMatrix();
  109. renderer.setSize( width, height );
  110. }
  111. //
  112. function animate() {
  113. render();
  114. stats.update();
  115. }
  116. function render() {
  117. const timer = Date.now() * 0.00025;
  118. particleLight.position.x = Math.sin( timer * 7 ) * 30;
  119. particleLight.position.y = Math.cos( timer * 5 ) * 40;
  120. particleLight.position.z = Math.cos( timer * 3 ) * 30;
  121. for ( let i = 0; i < group.children.length; i ++ ) {
  122. const child = group.children[ i ];
  123. child.rotation.y += 0.005;
  124. }
  125. renderer.render( scene, camera );
  126. }
  127. </script>
  128. </body>
  129. </html>
粤ICP备19079148号