Forum

Missing Replace Texture Callback on Success

16 March 2018 21:01
Hi @ll,

in
replace_image(obj, text_name, image, callback)
extern/textures.js, line 214
the callback is only called on error, but not on success.

exports.replace_image = function(obj, text_name, image, callback) {
callback = callback || function() {};
var tex = m_textures.get_texture_by_name(obj, text_name);
if (!tex) {
m_print.error("Couldn't find texture \"" + text_name + "\" in object \"" + obj.name + "\".");
callback(false);
return;
}
if (tex.is_movie) {
m_print.error("Changing video textures is forbidden.");
callback(false);
return;
}
if (!("src" in image)) {
m_print.error("Incorrect image.");
callback(false);
return;
}
var image_path = image.src;
m_textures.change_image(obj, tex, text_name, image, image_path);
}


The function parameter description says:
"Callback to be executed after changing", which suggests a callback also on success.

Is the replace_image function synchronous and expected to be interpreted as successful when passed without being interrupted by the callback? Or is there a callback(true) missing?


Cheers, Daniel
@ Emotional3D
19 March 2018 12:51
There is a bug. It is already fixed. And all will work in the next release.
Here is an updated change_image code snipped code:
import b4w from "blend4web";

var m_data    = b4w.data;
var m_app     = b4w.app;
var m_cfg     = b4w.config;
var m_cont    = b4w.container;
var m_mouse   = b4w.mouse;
var m_tex     = b4w.textures;
var m_scenes  = b4w.scenes;
var m_version = b4w.version;

var DEBUG = (m_version.type() === "DEBUG");

var APP_ASSETS_PATH = m_cfg.get_assets_path("change_image");
var TEX_ASSETS_PATH = APP_ASSETS_PATH + "textures/";

var tex_names = ["1_b.png", "2_b.png", "table_napkin_1.png","table_napkin_1_normal.png",
                 "table_napkin_2.png", "table_napkin_2_normal.png"];

var _textures = [];
var _all_tex_loaded = false;
var _loaded_tex_count = 0;
function texture_load_cb() {
    _loaded_tex_count++;
    if (_loaded_tex_count == tex_names.length)
        _all_tex_loaded = true;
}
for (var i = 0; i < tex_names.length; i++) {
    var tex = new Image();
    tex.src = TEX_ASSETS_PATH + tex_names[i];
    tex.onload = texture_load_cb;
    _textures.push(tex)
}

var _world = null;
var _wait_for_image_loading = false;
var _napkin_flag = false;
var _stand_1 = null;
var _stand_2 = null;

export function init() {
    m_app.init({
        canvas_container_id: "main_canvas_container",
        callback: init_cb,
        physics_enabled: false,
        alpha: true,
        show_fps: true,
        autoresize: true,
        assets_dds_available: !DEBUG,
        assets_min50_available: !DEBUG,
        console_verbose: true
    });
}

function init_cb(canvas_elem, success) {

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

function load() {
    m_data.load(APP_ASSETS_PATH + "change_image.json", load_cb);
}

function load_cb(data_id) {
    m_app.enable_camera_controls(false, false, false, null, true);
    var container = m_cont.get_canvas();
    _world = m_scenes.get_world_by_name("World");
    _stand_1 = m_scenes.get_object_by_name("stand");
    _stand_2 = m_scenes.get_object_by_name("stand_2");
    container.addEventListener("mousedown", main_canvas_clicked_cb, false);
}

function main_canvas_clicked_cb(e) {

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

    var obj = m_scenes.pick_object(x, y);
    if (obj && _all_tex_loaded) {
        switch(m_scenes.get_object_name(obj)) {
        case "Sphere_button_2":
            m_tex.replace_image(_world, "lightmap", _textures[0])
            m_tex.replace_image(_stand_1, "cubemap_slot", _textures[0]);
            break;
        case "Sphere_button_1":
            m_tex.replace_image(_world, "lightmap", _textures[1])
            m_tex.replace_image(_stand_1, "cubemap_slot", _textures[1]);
            break;
        case "table_napkin":
            if (_napkin_flag) {
                m_tex.replace_image(obj, "table_napkin", _textures[2]);
                m_tex.replace_image(obj, "table_napkin_normal", _textures[3]);
            } else {
                m_tex.replace_image(obj, "table_napkin", _textures[4]);
                m_tex.replace_image(obj, "table_napkin_normal", _textures[5]);
            }
            _napkin_flag = !_napkin_flag;
            break;
        default:
            return;
        }
    }
}
init();
Alexander (Blend4Web Team)
twitter
 
Please register or log in to leave a reply.