lin.xianming Posted March 21, 2017 Share Posted March 21, 2017 Hello, I try to create a customized material with some shaders. As far as I know, the normal way is put a .mat file into a folder which is related with a .world file and so on. after call the method: Console::get()->run("world_load"); then the customized material has been loaded into engine. Correspondingly, Console::get()->run("world_quit") can remove the material from engine This mechanism works well. But I intend to give the customized material longer lifetime, as long as the running engine instance, just like those default materials automatically loaded by engine. Regardless how many times worlds are loaded or quitted, the material still stick in engine. So I have to load my .mat file before the first world_load call. I attempt to use some methods like: pMaterials->addWorldLibrary(str); pMaterials->load(str); Unfortunatelly, Both return 0. Thanks Link to comment
ulf.schroeter Posted March 21, 2017 Share Posted March 21, 2017 Unigine core materials are loaded into the engine via script code in function init() in file <sdk-folder>/data/core/scripts/system/system.h. /* */ void init(string name_,string languages_[],string locales_[]) { ..... // load materials engine.materials.load("core/materials/default/unigine_post.mat"); engine.materials.load("core/materials/default/unigine_render.mat"); engine.materials.load("core/materials/default/unigine_decal.mat"); engine.materials.load("core/materials/default/unigine_decal_terrain.mat"); ..... So I would expect that usage of pMaterials->load() should do the trick. Any error messages within Unigine log file indicating some load issue with your material library (e.g. invalid material definition, file not found, etc) ? For testing purposes you can temporarily add your custom material library load operation to the system script code just to check if it is generally loadable. Link to comment
lin.xianming Posted March 22, 2017 Author Share Posted March 22, 2017 Hi Ulf, I DID find a file by the name of <sdk-folder>/data/core/scripts/system/system.h, but I didn't find anything associated with loading materials. The Init() method looks like this: void init(string name_, string languages_[], string locales_[]) { name = name_; languages.copy(languages_); locales.copy(locales_); toggle = APP_KEY_ESC; language = -1; // splash parameters splash_4x3_name = "core/gui/splash.png"; splash_16x9_name = "core/gui/splash.png"; splash_transform = vec4(0.5f, 0.33f, 0.5f, 0.5f); splash_color = engine.gui.parseColor("0c0c0c"); splash_text = "<p align=center><font color=#606060 size=16><xy y=%100 dy=-20/>UNIGINE Corp. (c) 2005-2017</font></p>"; splash_threshold = 16; // export locale #ifdef EXPORT_LOCALE if (locales.size()) { string tokens[0]; if (strsplit(locales[0], ":", tokens) == 2) engine.gui.saveDictionary(tokens[0], tokens[1]); else engine.gui.saveDictionary(locales[0]); } #endif // export extern #ifdef EXPORT_EXTERN File file = new File(); if (file.open("extern_info.h", "wb")) { file.puts(get_extern_info(EXPORT_EXTERN)); file.close(); } delete file; #endif // initialize window user_interface = new UserInterface(engine.getGui(), name); #ifndef HAS_INTERFACE engine.gui.addChild(main_w, GUI_ALIGN_CENTER | GUI_ALIGN_OVERLAP); engine.gui.removeChild(main_w); engine.gui.removeChild(main_w); #endif // set language set_language(engine.config.getInt("system_language", 0)); // initialize tabs foreach (string tab; tab_prefix) { call(tab + "init"); call(tab + "update"); } // update language if (language_cb != NULL) update_language(); Link to comment
ulf.schroeter Posted March 22, 2017 Share Posted March 22, 2017 Ah, sorry, only had access to Unigine 2.0 sources yesterday evening. At least for 2.4 the code for default material library loading now has been move to the engine C++ code in Engine.cpp, so no longer customizable by script modification. Nevertheless also in C++ engine.materials.load(<librarypath>) is used. Therefore I would still try to verify that your own material loads without any problems when included in some world file. Also make sure that the data path where your library is located is actually visible for the engine FileSystem class. Also check the log for any error message Link to comment
lin.xianming Posted March 22, 2017 Author Share Posted March 22, 2017 Anyway, I have already loaded my material file into the engine :) . If the .mat file has any semantic mistake, the console will report it to me. </material> <material name="customized_forward_material" editable="0"> <options transparent="2"/> <state name="ambient">1</state> <shader pass="ambient" object="mesh_static" ambient="1" defines="BASE_AMBIENT" vertex="data/shaders/vertex/ambient.vert" fragment="data/shaders/fragment/ambient.frag"/> <bind object="mesh_dynamic" to="mesh_static"/> <bind object="mesh_skinned" to="mesh_static"/> <texture unit="0" name="albedo" anisotropy="1" shader="fragment" pass="deferred,ambient">unknown.dds</texture> <texture unit="1" name="height" shader="all" pass="deferred,ambient">unknown.dds</texture> <texture unit="2" shader="fragment" type="opacity_depth"/> <texture unit="3" shader="fragment" type="gbuffer_normal"/> </material> After loading the .mat I can inheritMaterial new materials from the "customized_forward_material". VERIFY(pMaterials->load("uni_materials/materials.mat")); VERIFY(pMaterials->inheritMaterial("custom_forward_material", "uni_materials/materials.mat", strMaterial)); Now the problem is: Actually I appointed two shader files in the .mat file: vertex="data/shaders/vertex/ambient.vert" fragment="data/shaders/fragment/ambient.frag"/> But the key point is: either I write any meaningless words to the shader files or type wrong shader file names in .mat, the console dosen't give me any report about it. In another word, I don't know how to get the shaders compilation outcomes. I try to figure out some clues from the sample "ComputShaderImage". I type in some semantic mistakes into the shader file "compute_read_write.comp". I can get error reports only when the AppWorldLogic::update takes effect. class AppWorldLogic : public WorldLogic { ... ... virtual int update() { /* //If I disable the following codes, then no shader file(.comp) compilation outcome persents out. Render *render = Render::get(); // Create resources if invalid create_resources(); if (render->isFirstFrame()) { RenderState *state = RenderState::get(); texture_render->setColorTexture(0, dynamic_texture); texture_render->enable(); state->setBufferMask(0, RenderState::BUFFER_ALL); state->clearBuffer(RenderState::BUFFER_ALL, vec4::ZERO, 0.0f); texture_render->disable(); texture_render->unbindColorTexture(); return 1; } float ifps = Game::get()->getIFps(); int id = compute_material->findParameter("ifps"); compute_material->setParameterSlider(id, ifps); // Set unordered access textures for compute shader texture_render->setUnorderedAccessTexture(0, dynamic_texture, 0); texture_render->enable(TextureRender::USAGE_COMPUTE); // Dispatch 32x32 (1024) group threads (each group has 32x32 (1024) threads, this value defines in compute shader file) int group_threads_x = 1024 / 32; int group_threads_y = 1024 / 32; // If incomplete group exist render->renderComputeMaterial(compute_material, group_threads_x, group_threads_y); texture_render->disable(); texture_render->unbindUnorderedAccessTexture(); */ return 1; } ... ... }; Link to comment
lin.xianming Posted March 23, 2017 Author Share Posted March 23, 2017 Hi I have loaded the customized material into engine with shaders as well :) . I can get different outcomes when I modify shader codes, which testifies It works. Acturally my demand is quite simple: create a rectangle mesh with the customized material, which is supposed to show out what the original texture exactly is. This is the .frag file: #include <core/shaders/common/fragment.h> INIT_TEXTURE(0, TEX_COLOR)// Adds a texture sampler // Input data struct STRUCT(FRAGMENT_IN) INIT_POSITION // Projected position INIT_IN(float4,0) // Texcoord (uv) END MAIN_BEGIN(FRAGMENT_OUT,FRAGMENT_IN) float4 texcoord = IN_DATA(0);// Get the UV coords OUT_COLOR = TEXTURE(TEX_COLOR,texcoord.xy);// Get the texture data MAIN_END For some reasons, the outcome is kind of similar to the original texture, but the hue and saturation are not exactly the same. I guess maybe It is causted by some post effects. So I turn off some render config options, like: pConsole->run("render_filmic 0"); But the differences are still obvious. Link to comment
silent Posted March 24, 2017 Share Posted March 24, 2017 To get the exactly the same texture color you can skip all the post processing effects (render_skip_post_materials 1). Can you check that? It's also better to create a separate thread for different issue to keep forum clean and healthy :) How to submit a good bug report --- FTP server for test scenes and user uploads: ftp://files.unigine.com user: upload password: 6xYkd6vLYWjpW6SN Link to comment
lin.xianming Posted March 26, 2017 Author Share Posted March 26, 2017 Great Idea. I'm sorry about that. Link to comment
Recommended Posts