webgl_test_memory.html 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. var container;
  25. var camera, scene, renderer;
  26. init();
  27. animate();
  28. function init() {
  29. container = document.createElement( 'div' );
  30. document.body.appendChild( container );
  31. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 10000 );
  32. camera.position.z = 200;
  33. scene = new THREE.Scene();
  34. scene.background = new THREE.Color( 0xffffff );
  35. renderer = new THREE.WebGLRenderer();
  36. renderer.setPixelRatio( window.devicePixelRatio );
  37. renderer.setSize( window.innerWidth, window.innerHeight );
  38. container.appendChild( renderer.domElement );
  39. }
  40. function createImage() {
  41. var canvas = document.createElement( 'canvas' );
  42. canvas.width = 256;
  43. canvas.height = 256;
  44. var context = canvas.getContext( '2d' );
  45. context.fillStyle = 'rgb(' + Math.floor( Math.random() * 256 ) + ',' + Math.floor( Math.random() * 256 ) + ',' + Math.floor( Math.random() * 256 ) + ')';
  46. context.fillRect( 0, 0, 256, 256 );
  47. return canvas;
  48. }
  49. //
  50. function animate() {
  51. requestAnimationFrame( animate );
  52. render();
  53. }
  54. function render() {
  55. var geometry = new THREE.SphereBufferGeometry( 50, Math.random() * 64, Math.random() * 32 );
  56. var texture = new THREE.CanvasTexture( createImage() );
  57. var material = new THREE.MeshBasicMaterial( { map: texture, wireframe: true } );
  58. var mesh = new THREE.Mesh( geometry, material );
  59. scene.add( mesh );
  60. renderer.render( scene, camera );
  61. scene.remove( mesh );
  62. // clean up
  63. geometry.dispose();
  64. material.dispose();
  65. texture.dispose();
  66. }
  67. </script>
  68. </body>
  69. </html>