Forum

Code NEARLY working, but not quite

20 July 2017 21:28
Hi all, I'm trying to have this code do a couple of specific things… It's close but no cigar.

I want the JS callback to function on the cube (works fine)
I want the 'pick object' script to return the object name and material name (works fine)
Then I want my external menu to delete the 'selected object'.

I'm currently experiencing 2 issues. My 'pick object' script seems to be trying to select the background as well, i.e. NOT just the object.

Also, the 'unload function' is unloading the entire scene, not the 'selected object'… I'm having lots of problems even though I've spent hours trying, to figure out how a selected object is referenced by any code at all.

Here's my full doc (there are a few bits in there from the cartoon_scene which I'm experimenting with (just ignore).

"use strict"

// register the application module
b4w.register("my_CUBE_main", function(exports, require) {

// import modules used by the app
var m_app       = require("app");
var m_cfg       = require("config");
var m_data      = require("data");
var m_preloader = require("preloader");
var m_ver       = require("version");
var m_logn      = require("logic_nodes");
var m_obj       = require("objects");
var m_mat       = require("material");
var m_scenes    = require("scenes");
var m_trans     = require("transform");
var m_mouse     = require("mouse");    
var m_anim      = require("animation");
var m_cam       = require("camera");
var m_cont      = require("container");
var m_cons      = require("constraints");
var m_ctl       = require("controls");
var m_main      = require("main");
    
    

var _disable_interaction = false;
var _selected_obj = null;
    
    
    
    
// detect application mode
var DEBUG = (m_ver.type() == "DEBUG");

// automatically detect assets path
var APP_ASSETS_PATH = m_cfg.get_assets_path("my_CUBE");

/**
 * export the method to initialize the app (called at the bottom of this file)
 */
exports.init = function() {
    m_app.init({
        canvas_container_id: "main_canvas_container",
        callback: init_cb,
        show_fps: DEBUG,
        console_verbose: DEBUG,
        autoresize: true
    });
}

/**
 * callback executed when the app is initialized 
 */
function init_cb(canvas_elem, success) {

    if (!success) {
        console.log("b4w init failure");
        return;
    }

    
    
    
    
    m_preloader.create_preloader();
    
canvas_elem.addEventListener("mousedown", main_canvas_down);
    
    ////////
//canvas_elem.addEventListener("mouseup", main_canvas_up);
    ///////
    

    // ignore right-click on the canvas element
    canvas_elem.oncontextmenu = function(e) {
        e.preventDefault();
        e.stopPropagation();
        return false;
    };

    
    m_logn.append_custom_callback("action", action_cb);
    
    
    load();
}

/**
 * load the scene data
 */
function load() {
    m_data.load(APP_ASSETS_PATH + "my_CUBE.json", load_cb, preloader_cb);
}

/**
 * update the app's preloader
 */
function preloader_cb(percentage) {
    m_preloader.update_preloader(percentage);
}

/**
 * callback executed when the scene data is loaded
 */
function load_cb(data_id, success) {

    if (!success) {
        console.log("b4w load failure");
        return;
    }

m_app.enable_camera_controls();

///code goes here too///

    
 init_controls();
 
    
    
  }  

    


    
    
    
    
    
    
    
    
    
    

    function action_cb(in_params, out_params) {

console.log("The menu was swapped"); 

       document.getElementById("cssmenu").style.display='none';
       document.getElementById("cssmenu2").style.display='block';
  
    }
    
    
    

      
    
    
    
    
  function main_canvas_down(e) {
   

      
      if (e.preventDefault)
        e.preventDefault();

    var x = m_mouse.get_coords_x(e);
    var y = m_mouse.get_coords_y(e);

    var obj = m_scenes.pick_object(x, y);

    // handling outline effect
    if (_selected_obj != obj) {
        if (_selected_obj)
            m_scenes.clear_outline_anim(_selected_obj);
        if (obj)
            console.log(m_scenes.get_object_name(obj));
	console.log(m_mat.get_materials_names(obj));
            m_scenes.apply_outline_anim(obj, 1, 1, 0);

        _selected_obj = obj;
 
   }    
      
  } 
    
    
 
//function main_canvas_up(e) {
//    _drag_mode = false;
    // enable camera controls after releasing the object
 //   if (!_enable_camera_controls) {
 //       m_app.enable_camera_controls();
//        _enable_camera_controls = true;
 //   }
//}
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
function init_controls() {

    //init_buttons();
      
   document.getElementById("action1").addEventListener("click", function(e) {
       console.log("The menu is working");
        if (_selected_obj) {
            var id = m_scenes.get_object_data_id(_selected_obj);
            m_data.unload(id);
            _selected_obj = null;
        }
    });           
              
   
    
    
    
    
    
    
    
    
    
    
              
              
}  
 
    

});

// import the app module and start the app by calling the init method
b4w.require("my_CUBE_main").init();


Any pointers would be gratefully appreciated.

Kind regards

Charlie
21 July 2017 04:07
the 'unload function' is unloading the entire scene
In the Cartoon Interior project, each object is exported from Blender as a separate JSON file. It then gets loaded and unloaded. Are you trying to unload an object that is part of the main scene? If you want to unload an object all by its self, you will need to first load it all by its self. If you unload an object that is part of the main scene, you will unload the whole scene. Basically you are loading and unloading the JSON file.
23 July 2017 21:46
Gotcha. Thanks
 
Please register or log in to leave a reply.