webgpu_materials_basic.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - material - basic</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. <script type="importmap">
  11. {
  12. "imports": {
  13. "three": "../build/three.webgpu.js",
  14. "three/tsl": "../build/three.webgpu.js",
  15. "three/addons/": "./jsm/"
  16. }
  17. }
  18. </script>
  19. <script type="module">
  20. import * as THREE from 'three';
  21. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  22. let camera, scene, renderer;
  23. const spheres = [];
  24. let mouseX = 0;
  25. let mouseY = 0;
  26. let windowHalfX = window.innerWidth / 2;
  27. let windowHalfY = window.innerHeight / 2;
  28. const params = {
  29. color: '#ffffff',
  30. mapping: THREE.CubeReflectionMapping,
  31. refractionRatio: 0.98,
  32. transparent: false,
  33. opacity: 1
  34. };
  35. const mappings = { ReflectionMapping: THREE.CubeReflectionMapping, RefractionMapping: THREE.CubeRefractionMapping };
  36. document.addEventListener( 'mousemove', onDocumentMouseMove );
  37. init();
  38. function init() {
  39. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.01, 100 );
  40. camera.position.z = 3;
  41. const path = './textures/cube/pisa/';
  42. const format = '.png';
  43. const urls = [
  44. path + 'px' + format, path + 'nx' + format,
  45. path + 'py' + format, path + 'ny' + format,
  46. path + 'pz' + format, path + 'nz' + format
  47. ];
  48. const textureCube = new THREE.CubeTextureLoader().load( urls );
  49. scene = new THREE.Scene();
  50. scene.background = textureCube;
  51. const geometry = new THREE.SphereGeometry( 0.1, 32, 16 );
  52. const material = new THREE.MeshBasicMaterial( { color: 0xffffff, envMap: textureCube } );
  53. for ( let i = 0; i < 500; i ++ ) {
  54. const mesh = new THREE.Mesh( geometry, material );
  55. mesh.position.x = Math.random() * 10 - 5;
  56. mesh.position.y = Math.random() * 10 - 5;
  57. mesh.position.z = Math.random() * 10 - 5;
  58. mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 3 + 1;
  59. scene.add( mesh );
  60. spheres.push( mesh );
  61. }
  62. //
  63. renderer = new THREE.WebGPURenderer();
  64. renderer.setPixelRatio( window.devicePixelRatio );
  65. renderer.setSize( window.innerWidth, window.innerHeight );
  66. renderer.setAnimationLoop( animate );
  67. document.body.appendChild( renderer.domElement );
  68. //
  69. const gui = new GUI( { width: 300 } );
  70. gui.addColor( params, 'color' ).onChange( ( value ) => material.color.set( value ) );
  71. gui.add( params, 'mapping', mappings ).onChange( ( value ) => {
  72. textureCube.mapping = value;
  73. material.needsUpdate = true;
  74. } );
  75. gui.add( params, 'refractionRatio' ).min( 0.0 ).max( 1.0 ).step( 0.01 ).onChange( ( value ) => material.refractionRatio = value );
  76. gui.add( params, 'transparent' ).onChange( ( value ) => {
  77. material.transparent = value;
  78. material.needsUpdate = true;
  79. } );
  80. gui.add( params, 'opacity' ).min( 0.0 ).max( 1.0 ).step( 0.01 ).onChange( ( value ) => material.opacity = value );
  81. gui.open();
  82. //
  83. window.addEventListener( 'resize', onWindowResize );
  84. }
  85. function onWindowResize() {
  86. windowHalfX = window.innerWidth / 2;
  87. windowHalfY = window.innerHeight / 2;
  88. camera.aspect = window.innerWidth / window.innerHeight;
  89. camera.updateProjectionMatrix();
  90. renderer.setSize( window.innerWidth, window.innerHeight );
  91. }
  92. function onDocumentMouseMove( event ) {
  93. mouseX = ( event.clientX - windowHalfX ) / 100;
  94. mouseY = ( event.clientY - windowHalfY ) / 100;
  95. }
  96. //
  97. function animate() {
  98. const timer = 0.0001 * Date.now();
  99. camera.position.x += ( mouseX - camera.position.x ) * .05;
  100. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  101. camera.lookAt( scene.position );
  102. for ( let i = 0, il = spheres.length; i < il; i ++ ) {
  103. const sphere = spheres[ i ];
  104. sphere.position.x = 5 * Math.cos( timer + i );
  105. sphere.position.y = 5 * Math.sin( timer + i * 1.1 );
  106. }
  107. renderer.render( scene, camera );
  108. }
  109. </script>
  110. </body>
  111. </html>