webgl_test_memory2.html 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - webgl memory test II</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 II
  21. </div>
  22. <script type="x-shader/x-fragment" id="fragmentShader">
  23. void main() {
  24. if ( mod ( gl_FragCoord.x, 4.0001 ) < 1.0 || mod ( gl_FragCoord.y, 4.0001 ) < 1.0 )
  25. gl_FragColor = vec4( XXX, 1.0 );
  26. else
  27. gl_FragColor = vec4( 1.0 );
  28. }
  29. </script>
  30. <script type="x-shader/x-vertex" id="vertexShader">
  31. void main() {
  32. vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
  33. gl_Position = projectionMatrix * mvPosition;
  34. }
  35. </script>
  36. <script type="module">
  37. import * as THREE from '../build/three.module.js';
  38. var N = 100;
  39. var container;
  40. var camera, scene, renderer;
  41. var geometry, meshes = [];
  42. var fragmentShader, vertexShader;
  43. init();
  44. setInterval( render, 1000 / 60 );
  45. function init() {
  46. container = document.createElement( 'div' );
  47. document.body.appendChild( container );
  48. vertexShader = document.getElementById( "vertexShader" ).textContent;
  49. fragmentShader = document.getElementById( "fragmentShader" ).textContent;
  50. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 10000 );
  51. camera.position.z = 2000;
  52. scene = new THREE.Scene();
  53. scene.background = new THREE.Color( 0xffffff );
  54. geometry = new THREE.SphereBufferGeometry( 15, 64, 32 );
  55. for ( var i = 0; i < N; i ++ ) {
  56. var material = new THREE.ShaderMaterial( { vertexShader: vertexShader, fragmentShader: generateFragmentShader() } );
  57. var mesh = new THREE.Mesh( geometry, material );
  58. mesh.position.x = ( 0.5 - Math.random() ) * 1000;
  59. mesh.position.y = ( 0.5 - Math.random() ) * 1000;
  60. mesh.position.z = ( 0.5 - Math.random() ) * 1000;
  61. scene.add( mesh );
  62. meshes.push( mesh );
  63. }
  64. renderer = new THREE.WebGLRenderer();
  65. renderer.setPixelRatio( window.devicePixelRatio );
  66. renderer.setSize( window.innerWidth, window.innerHeight );
  67. container.appendChild( renderer.domElement );
  68. }
  69. //
  70. function generateFragmentShader() {
  71. return fragmentShader.replace( "XXX", Math.random() + "," + Math.random() + "," + Math.random() );
  72. }
  73. function render() {
  74. for ( var i = 0; i < N; i ++ ) {
  75. var mesh = meshes[ i ];
  76. mesh.material = new THREE.ShaderMaterial( { vertexShader: vertexShader, fragmentShader: generateFragmentShader() } );
  77. }
  78. renderer.render( scene, camera );
  79. console.log( "before", renderer.info.programs.length );
  80. for ( var i = 0; i < N; i ++ ) {
  81. var mesh = meshes[ i ];
  82. mesh.material.dispose();
  83. }
  84. console.log( "after", renderer.info.programs.length );
  85. }
  86. </script>
  87. </body>
  88. </html>