0

I have an fbx model which I loading in engine, but when I try to apply texture programmatically on this model it doesn't work, but it works on primitive object like cube.

This is what I do

 auto fileLoaderComplete = sceneManager->assets()->loader()->complete()->connect([&](file::Loader::Ptr loader)

{


            auto chair = sceneManager->assets()->symbol(FBX_MODEL_FILENAME);


            //chair->addComponent(Transform::create());

            auto chairMaterial = material::BasicMaterial::create();

            chairMaterial->diffuseMap(sceneManager->assets()->texture(TEXTURE_FILENAME));


            chair->addComponent(Surface::create(

                 geometry::QuadGeometry::create(sceneManager->assets()->context()),                                                                                                                      
                 chairMaterial,                                                                                                                      
                 sceneManager->assets()->effect("effect/Basic.effect")

            ));



            chair->component<Transform>()->matrix(chair->component<Transform>()->matrix() * math::scale(math::vec3(0.005f)));

            // Add the symbol to the scene

            root->addChild(chair);

            }); 

But no any texture applied on the model. How to make it work properly?

Arsenius
  • 4,972
  • 4
  • 26
  • 39

2 Answers2

0

I don't know what you are really trying to achieve, but this code add a quad to the root node of your chair model with a texture on it. If the triangle culling is enabled and that your camera doesn't face this quad, it's normal that you see no change.

What you need to do is to retrieve the node that contains the surface that you want to texture and update its material.

It should looks like something like this:

auto fileLoaderComplete = sceneManager->assets()->loader()->complete()->connect([&](file::Loader::Ptr loader)
{
    auto chair = sceneManager->assets()->symbol(FBX_MODEL_FILENAME);
    auto texture = sceneManager->assets()->texture(TEXTURE_FILENAME);

    auto surfaceNodeSet = scene::NodeSet::create(chair)
        ->descendants(true)
        ->where([](scene::Node::Ptr n)
    {
        return n->hasComponent<Surface>();
    });

    for (auto surfaceNode : surfaceNodeSet->nodes())
    {
        auto surface = surfaceNode->component<Surface>();
        surface->material()->data()->set("diffuseMap", texture->sampler());
    }

    chair->component<Transform>()->matrix(chair->component<Transform>()->matrix() * math::scale(math::vec3(0.005f)));

    // Add the symbol to the scene
    root->addChild(chair);
});
Noxalus
  • 65
  • 1
  • 11
  • Instead scene::NodeSet::create(model) should be scene::NodeSet::create(chair) I believe? However this code still doesn't work take a look result http://postimg.org/image/h8ep1b0lj/ – Arsenius Mar 09 '16 at 03:18
  • You are right, you must give "chair" variable to create your NodeSet, I've edited the code. But this should work, make sure that your texture is properly loaded and that your NodeSet contains some nodes. – Noxalus Mar 09 '16 at 09:41
  • This is full code https://github.com/atonamy/FbxImport/blob/master/src/main.cpp and this is model and texture https://github.com/atonamy/FbxImport/tree/master/asset/models I tried with cube and sphere and texture loaded and works properly. But when I try chair model doesn't work. Please try yourself. – Arsenius Mar 09 '16 at 23:43
  • NodeSet contains node and I checked surface->geometry is also there. I tried diffuseColor and it works, but why texture still doesn't? – Arsenius Mar 10 '16 at 00:04
  • Correct link to texture and model https://github.com/atonamy/FbxImport/tree/master/asset/model – Arsenius Mar 10 '16 at 00:34
0

Thanks for this help https://stackoverflow.com/a/35874003/3278271

I figured out why in this case texturing still doesn't work even if you use surfaceNode->component()->material(). The reason is that my fbx model doesn't contain UV mapping.

So now the question is how to define UV programmatically?

In Unity you can do something like this http://forum.unity3d.com/threads/programmatically-generate-uv-map.26095/

But how to do it in Minko?

Community
  • 1
  • 1
Arsenius
  • 4,972
  • 4
  • 26
  • 39