All files Scene.js

41.74% Statements 48/115
17.65% Branches 6/34
58.33% Functions 7/12
42.11% Lines 48/114

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308              5x   5x   5x   5x   5x         5x 5x   5x                         5x           5x                   5x   5x                   5x   5x       5x 5x 5x   5x                     5x     5x     5x     5x 5x     5x 5x 5x 5x 5x 5x 5x 5x 5x             5x 5x 5x 5x 5x             1x             1x             1x             4x 4x                                                                                                                                   2x 3x   3x 3x                                                                                                                                                                               1x 1x    
/*
   Copyright 2020 Jorge Gascon Perez <jorge.gascon.perez@gmail.com>
*/
 
 
class Scene {
 
    selected_meshes_ids = new Set();
 
    canvas = null;
 
    engine = null;
 
    scene = null;
 
    camera = null;
 
 
 
    constructor(canvas_id) {
        this.clear();
        this.canvas = document.getElementById(canvas_id);
 
        Iif (this.canvas) {
 
            this.engine = new BABYLON.Engine(this.canvas,
                                             true,
                                             {
                                                preserveDrawingBuffer: true,
                                                stencil: true
                                             });
 
        } else { //if (!this.canvas)
 
            //Engine that does not require WebGL, used for testing and server-side purposes.
            //More information at: https://doc.babylonjs.com/features/nullengine
            this.engine = new BABYLON.NullEngine();
 
        } //if (this.canvas)
 
        //Assertive programming: If something critical fails, the program is killed:
        //"Dead programs tell no lies" (from the Book "The pragmatic Programmer").
        Iif (!this.engine) {
            let error_text = "ERROR Creating Babylon engine";
            if (typeof module === 'undefined') {
                console.error(error_text);
                console.trace();
                alert(error_text + "\n(more details in 'Developer Tools --> Console')");
            } //if (typeof module === 'undefined') {
            throw error_text;
        } //if (!this.engine)
 
        this.scene = new BABYLON.Scene(this.engine);
 
        Iif (!this.scene) {
            let error_text = "ERROR Creating Babylon scene";
            if (typeof module === 'undefined') {
                console.error(error_text);
                console.trace();
                alert(error_text + "\n(more details in 'Developer Tools --> Console')");
            } //if (typeof module === 'undefined')
            throw error_text;
        } //if (!this.scene)
 
        this.scene.clearColor = BABYLON.Color3.Black();
 
        this.camera = new BABYLON.ArcRotateCamera("Camera",
                                                  Math.PI / 2, 1.6, 5,
                                                  new BABYLON.Vector3(0, 2, 0),
                                                  this.scene);
        this.camera.allowUpsideDown = false;
        this.camera.wheelPrecision = 150;
        this.camera.minZ = 0.01;
 
        Iif (this.canvas) {
            this.camera.attachControl(this.canvas, true);
        } //if (this.canvas)
 
        //TODO: Setting the lights and the Skybox, this code should be in another place.
 
        //~ var hdrTexture = new BABYLON.CubeTexture("images/Studio_Softbox_2Umbrellas_cube_specular.env",
                                                 //~ this.scene);
        //~ hdrTexture.gammaSpace = false;
        //~ this.scene.environmentTexture = hdrTexture;
 
        var light = new BABYLON.HemisphericLight("light1",
                                                 new BABYLON.Vector3(2, 2, 5),
                                                 this.scene);
        light.intensity = 3;
 
        // Environment Texture
        var hdrTexture = BABYLON.CubeTexture.CreateFromPrefilteredData("images/environment.dds",
                                                                       this.scene);
 
        this.scene.imageProcessingConfiguration.exposure = 0.6;
        this.scene.imageProcessingConfiguration.contrast = 1.6;
 
        // Skybox
        var hdrSkybox = BABYLON.Mesh.CreateBox("hdrSkyBox", 1000.0, this.scene);
        var hdrSkyboxMaterial = new BABYLON.PBRMaterial("skyBox", this.scene);
        hdrSkyboxMaterial.backFaceCulling = false;
        hdrSkyboxMaterial.reflectionTexture = hdrTexture.clone();
        hdrSkyboxMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
        hdrSkyboxMaterial.microSurface = 1.0;
        hdrSkyboxMaterial.disableLighting = true;
        hdrSkybox.material = hdrSkyboxMaterial;
        hdrSkybox.infiniteDistance = true;
    } //constructor(canvas_id)
 
 
 
 
    clear() {
        this.selected_meshes_ids.clear();
        this.canvas = null;
        this.engine = null;
        this.scene = null;
        this.camera = null;
    } //clear()
 
 
 
 
    connect_render_loop(a_function) {
        this.engine.runRenderLoop(a_function);
    } //connect_render_loop(a_function)
 
 
 
 
    render() {
        this.scene.render();
    } //render()
 
 
 
 
    resize() {
        this.engine.resize();
    } //resize()
 
 
 
 
    load_model(filename) {
        let my_this = this;
        BABYLON.SceneLoader.ImportMesh("",
                                       "",
                                       filename,
                                       this.scene,
                                       function (meshes) {
                                            for (var i=0; i<meshes.length; i++) {
                                                let mesh = meshes[i];
 
                                                if (mesh.id == '__root__') {
                                                    continue;
                                                } //if (mesh.id == '__root__')
 
                                                my_this.set_mesh_as_selectable(mesh);
                                            } //for (var i=0; i<meshes.length; i++)
                                       } //function (meshes)
                                      ); //BABYLON.SceneLoader.ImportMesh(
    } //load_model(filename)
 
 
 
 
    set_mesh_as_selectable(mesh) {
        if (!mesh) {
            return;
        } //if (!mesh)
 
        let mesh_id = mesh.id;
 
        LOG(mesh_id);
 
        mesh.actionManager = new BABYLON.ActionManager(this.scene);
 
        let my_this = this;
 
        mesh.actionManager.registerAction(
                new BABYLON.ExecuteCodeAction(
                    BABYLON.ActionManager.OnPickTrigger, //trigger
                    function (event) {
                        let mesh = event.meshUnderPointer;
                        my_this.selected_meshes_ids.add(mesh.id);
                        LOG('Selected ['+mesh.id+']');
                        mesh.showBoundingBox = true;
                    } //function (event)
                ) //new BABYLON.ExecuteCodeAction(
        ).then(
                new BABYLON.ExecuteCodeAction(
                    BABYLON.ActionManager.OnPickTrigger, //trigger
                    function (event) {
                        let mesh = event.meshUnderPointer;
                        my_this.selected_meshes_ids.delete(mesh.id);
                        LOG('DeSelected ['+mesh.id+']');
                        mesh.showBoundingBox = false;
                    } //function (event)
                ) //new BABYLON.ExecuteCodeAction(
        ); //mesh.actionManager.registerAction(
    } //set_mesh_as_selectable(mesh)
 
 
 
 
    set_texture_to_selected_meshes(texture_url) {
        let mesh;
        let old_mat;
        let new_mat;
        let new_mat_id;
 
        for (let mesh_id of this.selected_meshes_ids) {
            mesh = this.scene.getMeshByID(mesh_id);
 
            Eif (!mesh) {
                continue;
            } //if (!mesh)
 
            LOG('Setting to ['+mesh_id+'] texture ['+texture_url+']');
 
            //TODO: use (or generate) a "material_id" more elegant than with texture_url:
            new_mat_id = texture_url;
 
            new_mat = this.scene.getMaterialByID(new_mat_id);
 
            if (!new_mat) {
                LOG("Creating new material ["+new_mat_id+"]");
 
                if (mesh.material) {
                    //A fast way to create a material with the most similar properties
                    //to the existing one.
                    new_mat = mesh.material.clone(new_mat_id);
 
                } else { //if (!mesh.material)
 
                    //Creating a sort-of matte material.
                    new_mat = new BABYLON.PBRMaterial(new_mat_id, this.scene);
                    new_mat.albedoColor = BABYLON.Color3.White();
                    //new_mat.reflectionTexture = this.scene.getMaterialByID("skyBox").reflectionTexture.clone();
                    new_mat.microSurface = 0.96;
                    new_mat.albedoColor = BABYLON.Color3.White();
                    new_mat.reflectivityColor = new BABYLON.Color3(0.0, 0.0, 0.0);
                } //if (mesh.material)
 
                //No need to call the next function (the new material is added to the scene automatically).
                //this.scene.addMaterial(new_mat);
 
                //Another way to create a new material.
                //~ new_mat = new BABYLON.StandardMaterial(new_mat_id, this.scene);
 
                //Looking for if this texture is already loaded in the scene.
                let new_texture = null;
                this.scene.textures.forEach(
                    function(texture) {
                        if (texture.url == texture_url) {
                            new_texture = texture;
                            return;
                        } //if (texture.url == texture_url)
                    } //function(texture)
                ); //this.scene.textures.forEach
 
                if (!new_texture) {
                    LOG("Creating new texture ["+texture_url+"]");
                    new_texture = new BABYLON.Texture(texture_url, this.scene);
//OJO: Put this line, otherwise the texture will be incorrect.
                    new_texture.vScale = -1;
                    //new_texture.invertY = true;
                } //if (!new_texture)
 
                new_mat.albedoTexture = new_texture;
            } //if (!new_mat)
 
//BUG?: The newly asigned material/texture do not use the model's UV texture coords, Is it a bug of Babylon?
 
            old_mat = mesh.material;
 
            if (old_mat && old_mat.id != new_mat.id) {
                mesh.material = null;
                old_mat.unbind();
 
                //Unused materials are deleted
                if (old_mat.getBindedMeshes().length == 0) {
                    LOG("Deleting old material ["+old_mat.id+"]");
                    old_mat.dispose(true, true, true); //(forceDisposeEffect, forceDisposeTextures, notBoundToMesh)
 
//FIXME: Are orphan textures actually deleted?
 
                } //if (old_mat.getBindedMeshes.length == 0)
            } //if (old_mat && old_mat.id != new_mat.id)
 
            LOG("Materials in scene ["+this.scene.materials.length+"]");
            LOG("Textures in scene ["+this.scene.textures.length+"]");
            mesh.material = new_mat;
 
        } //for (let mesh_id of this.selected_meshes_ids)
 
    } //set_texture_to_selected_meshes(texture_url)
 
} //class Scene
 
 
 
 
Eif (typeof module !== 'undefined') {
    module.exports = Scene;
} //if (typeof module !== 'undefined')