webgpu_materials_basic.html 4.8 KB

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