123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js - webgl memory test II</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- <style>
- body {
- background-color: #fff;
- color: #000;
- }
- a {
- color: #08f;
- }
- </style>
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - memory test II
- </div>
- <script type="x-shader/x-fragment" id="fragmentShader">
- void main() {
- if ( mod ( gl_FragCoord.x, 4.0001 ) < 1.0 || mod ( gl_FragCoord.y, 4.0001 ) < 1.0 )
- gl_FragColor = vec4( XXX, 1.0 );
- else
- gl_FragColor = vec4( 1.0 );
- }
- </script>
- <script type="x-shader/x-vertex" id="vertexShader">
- void main() {
- vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
- gl_Position = projectionMatrix * mvPosition;
- }
- </script>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- var N = 100;
- var container;
- var camera, scene, renderer;
- var geometry, meshes = [];
- var fragmentShader, vertexShader;
- init();
- setInterval( render, 1000 / 60 );
- function init() {
- container = document.createElement( 'div' );
- document.body.appendChild( container );
- vertexShader = document.getElementById( "vertexShader" ).textContent;
- fragmentShader = document.getElementById( "fragmentShader" ).textContent;
- camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 10000 );
- camera.position.z = 2000;
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0xffffff );
- geometry = new THREE.SphereBufferGeometry( 15, 64, 32 );
- for ( var i = 0; i < N; i ++ ) {
- var material = new THREE.ShaderMaterial( { vertexShader: vertexShader, fragmentShader: generateFragmentShader() } );
- var mesh = new THREE.Mesh( geometry, material );
- mesh.position.x = ( 0.5 - Math.random() ) * 1000;
- mesh.position.y = ( 0.5 - Math.random() ) * 1000;
- mesh.position.z = ( 0.5 - Math.random() ) * 1000;
- scene.add( mesh );
- meshes.push( mesh );
- }
- renderer = new THREE.WebGLRenderer();
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- container.appendChild( renderer.domElement );
- }
- //
- function generateFragmentShader() {
- return fragmentShader.replace( "XXX", Math.random() + "," + Math.random() + "," + Math.random() );
- }
- function render() {
- for ( var i = 0; i < N; i ++ ) {
- var mesh = meshes[ i ];
- mesh.material = new THREE.ShaderMaterial( { vertexShader: vertexShader, fragmentShader: generateFragmentShader() } );
- }
- renderer.render( scene, camera );
- console.log( "before", renderer.info.programs.length );
- for ( var i = 0; i < N; i ++ ) {
- var mesh = meshes[ i ];
- mesh.material.dispose();
- }
- console.log( "after", renderer.info.programs.length );
- }
- </script>
- </body>
- </html>
|