About Store Forum Documentation Contact



Post Reply 
Runtime Material+Import
Author Message
txguy Offline
Banned

Post: #1
Runtime Material+Import
Is it possible to create material at runtime without importing?
I use load function in material.
material.load("texture.jpg");

Then set material pointer
MaterialPtr mptr = &material.

I set material on mesh but it's just yellow.

Looking through the demos but haven't found an example.
(This post was last modified: 01-15-2023 06:07 AM by txguy.)
11-30-2022 09:18 AM
Find all posts by this user Quote this message in a reply
Fex Offline
Gold Supporter

Post: #2
RE: Runtime Material
I saved this image to disk as water_from_path.png in the generated .exe location C:\Esenthel\Projects\_Build_\01 - Rendering:

http://192.210.237.111/water_from_path.png

Modified the tutorial project 12 - Rendering / 01 - Rendering

Code:
/******************************************************************************/
Mesh mesh;
Image imageFromFile;
Image image;
/******************************************************************************/
void InitPre()
{
   INIT();
   Ms.hide();
   Ms.clip(null, 1);
}
/******************************************************************************/
bool Init()
{
   Cam.dist=2;

   if(!imageFromFile.ImportPNG("water_from_path.png")) Exit("problem");
  
   image.mustCreate2D(512, 512, IMAGE_R8G8B8A8_SRGB, 1);
   if(image.lock()) // in order to edit the texture we must first lock it
   {
      
      FREPD(y, image.h()) // iterate through all y's
      FREPD(x, image.w()) // iterate through all x's
      {
         image.color(x, y, imageFromFile.color(x, y) );
      }
      image.unlock(); // unlock
   }
  
   image.save("testImg");
  
   Material material;
   ImagePtr imgPtr;
   imgPtr.require("testImg");
   material.base_0 = imgPtr;
   material.validate();
   material.save("testMtr");

   mesh.parts.New().base.create(Ball(1), VTX_TEX0|VTX_NRM|VTX_TAN);// create Mesh with 1 MeshPart, and create this MeshPart's base from Ball with automatic texture coordinates, normals and tangents
   mesh.material(Materials.get("testMtr")).setRender().setBox();
   return true;
}
/******************************************************************************/
void Shut()
{
}
/******************************************************************************/
bool Update()
{
   if(Kb.bp(KB_2))mesh.material(UID(2123216029, 1141820639, 615850919, 3316401700)).setRender().setBox();
   if(Kb.bp(KB_1))mesh.material(Materials.get("testMtr")).setRender().setBox();
   if(Kb.bp(KB_ESC))return false;
   Cam.transformByMouse(1.5, 10, CAMH_ZOOM|CAMH_ROT);
   return true;
}
/******************************************************************************/
void Render() // Rendering Function
{
   switch(Renderer()) // Rendering Function will be called multiple times for different Rendering Modes
   {
      // the most important is the "prepare" mode in which you should draw all the meshes and add lights
      case RM_PREPARE:
      {
         // check if the mesh is inside the viewing frustum and add it to the drawing list
         if(Frustum(mesh))mesh.draw(MatrixIdentity);

         // add directional light
         LightDir(Vec(0, 0, 1)).add();
      }break;
   }
}
void Draw()
{
   // instead of typical "D.clear(WHITE);" we'll now use advanced rendering:
   Renderer(Render); // render using 'Render' function

   D.text(0, 0.9, S+"Fps: "+Time.fps()); // show number of fps
}
/******************************************************************************/


And it works. You can use the "1" and "2" keys to switch between the textures/materials.

This saves to disk and reloads from disk which may not be ok for you. I didn't look into if it can be done in-memory only at runtime, if it's required you may have to change the engine source.
12-01-2022 03:09 AM
Find all posts by this user Quote this message in a reply
txguy Offline
Banned

Post: #3
RE: Runtime Material
I only get the fps counter. If i render text after the switch in the render function it doesnt appear though nor does the mesh. I appreciate your help because the img is saving on the drive and this could work for not having to drag hundreds of textures into the engine if I can get through next hurdle.

RENDERPREPARE doesn't render anything but text appears if I put text render in default switch.

If I build app in the examples the meshes render but not if i paste the code into new project from start and set material to imported one. Setting is not set correct or something in the examples sets a mode I think.
Got it working. Thank you. Pro engine.

If you have time kind human can you help with the mesh load. This doesnt seem to work although I thought it would.
mesh.load("objpath.obj");
(This post was last modified: 12-01-2022 03:35 PM by txguy.)
12-01-2022 08:43 AM
Find all posts by this user Quote this message in a reply
txguy Offline
Banned

Post: #4
RE: Runtime Material
Sets object mesh.
Object gobject;
MeshPtr meshptr = &mesh;
gobject.mesh(true, meshptr);
(This post was last modified: 01-16-2023 07:48 AM by txguy.)
12-02-2022 08:56 AM
Find all posts by this user Quote this message in a reply
txguy Offline
Banned

Post: #5
RE: Runtime Material
Before import point part index and material Memptrs.
MemPtr<XMaterial> mats;
MemPtr<int> partindex;
MemPtr<XAnimation>animations;
Skeleton* skeleton;


Mems<XMaterial> xmaterialmems;
Mems<Int>partindexmems;
Mems<XAnimation>animmems;

mats.point(xmaterialmems);
partindex.point(partindexmems);
animations.point(animmems);

Import(path, &mesh, skeleton, animations, mats, partindex);
(This post was last modified: 01-16-2023 11:10 AM by txguy.)
12-06-2022 04:57 AM
Find all posts by this user Quote this message in a reply
txguy Offline
Banned

Post: #6
RE: Runtime Material
This gets mesh in editor correctly and works with my fbx and wavefront test models.
Edit.EditorInterface EI;
Str message;
if(!EI.connect(message))Exit(message);



UID meshid = EI.newElm(Edit.ELM_OBJ, meshname);
CMemPtr <UID> meshidptr;
meshidptr.point(meshid);

EI.setMesh(meshid, mesh);

EE.Edit.IDParam<Str> test;
test.id = meshid;
test.value = path;
CMemPtr<EE.Edit.IDParam<Str>> testmemptr;
testmemptr.point(test);
\
EI.setElmSrcFile(testmemptr);
EI.reloadElms(meshidptr, true);



This adds a meterial from colormap string.
UID matid = EI.newElm(Edit.ELM_MTRL, mats[0].name);
Edit.Material editmaterial;

FileParams fp;
fp = mats[0].color_map;

Mems<FileParams>fpars;
fpars.add(fp);

editmaterial.color_map = fpars;
EI.setMaterial(matid, editmaterial , true, true);

Seems like the mesh material would be set but is not and still requires setting for proper render from what I understand.


How do i set the object pointer correctly from the mesh that is added?

ObjectPtr meshobjectptr = meshid; //set ObjectPtr to mesh UID
if(!meshobjectptr->mesh())Exit("Object has no mesh");

No error but no render.
Works when the pointer is set from dragging with the mouse though.
Dragging the object with mouse the object gets added in cache at runtime.
Nice productivity start.
(This post was last modified: 01-16-2023 11:20 AM by txguy.)
12-06-2022 12:32 PM
Find all posts by this user Quote this message in a reply
txguy Offline
Banned

Post: #7
RE: Runtime Material+Import
ImportWrapper for asset application folder. It just uses the filename and path.
The extension testing is ugly but works. If there is an easier way I would appreciate an answer.
If you use 3ds you will need to add it. I dont have models from that software.


Using is simple.
ImportWrapper.Instance->Start();
ImportWrapper.Instance->Stop();

Happy not dragging.





bool ThreadFuncA(Thread &thread)
{



Str appAssetPath = SkipEnd(App.exe(), App.name()) + "Assets/";


UID start; //Zero
ImportWrapper.Instance()->GetAssets(appAssetPath, start);


return false; // stop the thread after importing assets
}



class ImportWrapper
{
Edit.EditorInterface m_EI;
Memc<Edit.Elm> m_elms; // list of project elements
Memc<Edit.Project> Projects; // list of projects
static ImportWrapper* m_instance = NULL;


// Get assets from path
void GetAssets(C Str& path, UID& parentid);


public:

ImportWrapper();
~ImportWrapper();

static ImportWrapper* Instance();

void Start();
void Stop();



bool SearchProjectElementList(Str name);
bool SearchProjectBySource(Str source);

bool ConnectEditor();


Image LoadImage(Str path);

bool LoadModel(Str path);


UID ImportEditorMaterial(Str path, Str materialName);

UID ImportEditorMesh(Str path, Str meshName, UID& parentid);
};

ImportWrapper::ImportWrapper()
{


}


ImportWrapper::~ImportWrapper()
{





}

ImportWrapper * ImportWrapper::Instance()
{

if(m_instance == NULL)
m_instance = new ImportWrapper();


return m_instance;
}



void ImportWrapper::Start()
{

bool conresult = ConnectEditor();


if(conresult)
{

Thread thread_a(ThreadFuncA);

thread_a.wait();

thread_a.del();



}
}

void ImportWrapper::Stop()
{

m_EI.disconnect();
delete m_instance;
m_instance = null;

}


bool ImportWrapper::ConnectEditor()
{

Str message;

if(!m_EI.connect(message))
Exit(message);



return true;
}



// gets assets recursively
void ImportWrapper::GetAssets(C Str& path, UID& parentid)
{
UID dirid;



for(FileFind ff(path); ff()wink // get files in path
{

if(ff.type == FSTD_DIR)
{
if(!SearchProjectElementList(ff.name))
{


dirid = m_EI.newElm(Edit.ELM_FOLDER, ff.name);


}



//directory recurse
GetAssets(ff.pathName()+"/", dirid);


}

else if(ff.type == FSTD_FILE)
{

// not very clean but works
Str searchtest = path + ff.name;

debugstring = path+ff.name;
//ff.


if(!SearchProjectBySource(searchtest))
{



Str extension = ".fbx";
Str skip = SkipEnd(ff.name, extension);

if(skip.length() < ff.name.length())
ImportEditorMesh(path+ff.name, ff.name(), parentid);


extension = ".obj";
skip = SkipEnd(ff.name, extension);

if(skip.length() < ff.name.length())
ImportEditorMesh(path+ff.name, ff.name, parentid);



extension = ".dae";
skip = SkipEnd(ff.name, extension);


if(skip.length() < ff.name.length())
ImportEditorMesh(path+ff.name, ff.name, parentid);




}

}



}


}







Image ImportWrapper::LoadImage(Str path)
{
Image loadimagefromfile;
Image loadimage;
Str extension = ".png";
Str skip = SkipEnd(path, extension);


if(skip.length() < path.length())
{
//debugstring = "Load Png";

// load png
if(!loadimagefromfile.ImportPNG(path))
Exit("Error Importer - Load png");




}

extension = ".jpg";
skip = SkipEnd(path, extension);

if(skip.length() < path.length())
{

// load jpg
if(!loadimagefromfile.ImportJPG(path))
Exit("Error Importer - Load jpg");




}



extension = ".tga";
skip = SkipEnd(path, extension);

if(skip.length() < path.length())
{

// load tga
if(!loadimagefromfile.ImportTGA(path))
Exit("Error Importer - Load tga");


}


extension = ".tif";
skip = SkipEnd(path, extension);

if(skip.length() < path.length())
{

// load tif
if(!loadimagefromfile.ImportTIF(path))
Exit("Error Importer - Load tif");



}



extension = ".bmp";
skip = SkipEnd(path, extension);

if(skip.length() < path.length())
{

// load bmp
if(!loadimagefromfile.ImportBMP(path))
Exit("Error Importer - Load bmp");


}




loadimage.mustCreate2D(512, 512, IMAGE_R8G8B8A8_SRGB, 1);
if(loadimage.lock()) // in order to edit the texture we must first lock it
{
      
      FREPD(y, loadimage.h()) // iterate through all y's
      FREPD(x, loadimage.w()) // iterate through all x's
      {
loadimage.color(x, y, loadimagefromfile.color(x, y) );
      }
      loadimage.unlock(); // unlock
}



return loadimage;
}







bool ImportWrapper::LoadModel(Str path)
{






Str extension = ".obj";
Str skip = SkipEnd(path, extension);


//bool result = ImportOBJ(path, &mesh, mats, partindex);




if(skip.length() < path.length())
{
//debugstring = "Load Wavefront Model";

return ImportOBJ(path, &mesh, mats, partindex);


}



extension = ".fbx";
skip = SkipEnd(path, extension);


if(skip.length() < path.length())
{

return Import(path, &mesh, skeleton, animations, mats, partindex);

}


extension = ".dae";
skip = SkipEnd(path, extension);


if(skip.length() < path.length())
{

return Import(path, &mesh, skeleton, animations, mats, partindex);

}




return false;
}









bool ImportWrapper::SearchProjectElementList(Str name)
{

m_EI.getElms(m_elms); // get a list of project elements

FREPA(m_elms)
{

if(m_elms[i].full_name == name)
{

return true;


}

}

return false;

}




bool ImportWrapper::SearchProjectBySource(Str source)
{

m_EI.getElms(m_elms); // get a list of project elements

FREPA(m_elms)
{

if(m_elms[i].src_file == source)
{

return true;


}

}

return false;

}


UID ImportWrapper::ImportEditorMaterial(Str path, Str materialName)
{
UID matid = m_EI.newElm(Edit.ELM_MTRL, materialName);
Edit.Material editmaterial;

FileParams fp;
fp = path;


Mems<FileParams>fpars;
fpars.add(fp);

editmaterial.color_map = fpars;

//Fix-add normal and other params if found
m_EI.setMaterial(matid, editmaterial , true, true);

return matid;

}



UID ImportWrapper::ImportEditorMesh(Str path, Str meshname, UID& parentid)
{
//debugstring = path;

UID meshid = m_EI.newElm(Edit.ELM_OBJ, meshname, parentid);
Mesh importmesh;


CMemPtr <UID> meshidptr;
meshidptr.point(meshid);

m_EI.setMesh(meshid, importmesh);

EE.Edit.IDParam<Str> test;
test.id = meshid;
test.value = path;

CMemPtr<EE.Edit.IDParam<Str>> testmemptr;
testmemptr.point(test);


m_EI.setElmSrcFile(testmemptr);
m_EI.reloadElms(meshidptr, true);

return meshid;

}
(This post was last modified: 01-18-2023 11:24 PM by txguy.)
01-18-2023 03:52 AM
Find all posts by this user Quote this message in a reply
Post Reply