2
0

webgl_test_memory.html 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - webgl memory test I</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. <style>
  9. body {
  10. background-color: #fff;
  11. color: #000;
  12. }
  13. a {
  14. color: #08f;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="info">
  20. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - memory test I
  21. </div>
  22. <script type="module">
  23. import * as THREE from '../build/three.module.js';
  24. let camera, scene, renderer;
  25. init();
  26. animate();
  27. function init() {
  28. const container = document.createElement( 'div' );
  29. document.body.appendChild( container );
  30. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 10000 );
  31. camera.position.z = 200;
  32. scene = new THREE.Scene();
  33. scene.background = new THREE.Color( 0xffffff );
  34. renderer = new THREE.WebGLRenderer();
  35. renderer.setPixelRatio( window.devicePixelRatio );
  36. renderer.setSize( window.innerWidth, window.innerHeight );
  37. container.appendChild( renderer.domElement );
  38. }
  39. function createImage() {
  40. const canvas = document.createElement( 'canvas' );
  41. canvas.width = 256;
  42. canvas.height = 256;
  43. const context = canvas.getContext( '2d' );
  44. context.fillStyle = 'rgb(' + Math.floor( Math.random() * 256 ) + ',' + Math.floor( Math.random() * 256 ) + ',' + Math.floor( Math.random() * 256 ) + ')';
  45. context.fillRect( 0, 0, 256, 256 );
  46. return canvas;
  47. }
  48. //
  49. function animate() {
  50. requestAnimationFrame( animate );
  51. render();
  52. }
  53. function render() {
  54. const geometry = new THREE.SphereGeometry( 50, Math.random() * 64, Math.random() * 32 );
  55. const texture = new THREE.CanvasTexture( createImage() );
  56. const material = new THREE.MeshBasicMaterial( { map: texture, wireframe: true } );
  57. const mesh = new THREE.Mesh( geometry, material );
  58. scene.add( mesh );
  59. renderer.render( scene, camera );
  60. scene.remove( mesh );
  61. // clean up
  62. geometry.dispose();
  63. material.dispose();
  64. texture.dispose();
  65. }
  66. </script>
  67. </body>
  68. </html>