Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to dispose a model ENTIRELY? #3

Open
shyakocat opened this issue Oct 20, 2019 · 0 comments
Open

How to dispose a model ENTIRELY? #3

shyakocat opened this issue Oct 20, 2019 · 0 comments

Comments

@shyakocat
Copy link

Excuse me,

I write a HTML and put a button under every mmd model,when user click it,it create a canvas to show the mmd model.

I simply create camera,light,scene,renderer & MMDLoader(as demo do),and when user close it,I simply set them to null.

let camera, scene, renderer, mesh, cav;
let loader;

function init(canvas, url) {

    camera = new THREE.PerspectiveCamera(45, 1, 1, 2000);
    camera.position.set(0, 10, 35);
    camera.up.set(0, 0, 1);

    scene = new THREE.Scene();

    var ambient = new THREE.AmbientLight(0x666666);
    scene.add(ambient);

    var directionalLight = new THREE.DirectionalLight(0x887766);
    directionalLight.position.set(-1, 1, 1).normalize();
    scene.add(directionalLight);

    renderer = new THREE.WebGLRenderer({antialias: true, canvas: canvas});
    renderer.setPixelRatio(window.devicePixelRatio);
    renderer.setSize(512, 512);
    renderer.setClearColor(new THREE.Color(0x000000));

    loader = new THREE.MMDLoader();
    loader.loadModel(url, (obj) => {
        mesh = obj;
        scene.add(mesh);
    }, null, null, path.dirname(url) + '/', path.extname(url).substring(1));
}

function animate() {
    if (!cav) return;
    if (!document.body.contains(cav)) {
        scene.remove(mesh);
        mesh.dispose();
        renderer.dispose();
        cav = camera = scene = renderer = mesh = loader = null;
        return;
    }
    scene.rotation.y = new Date().getTime() * 0.001;
    renderer.render(scene, camera);
    requestAnimationFrame(animate);
}

But,it lead to memory leak.Every view of a model add 400M~1G memory(depend on model size),deleteing the canvas doesn't decrease the memory.

So,how to unload a model properly?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant