Minko: how to apply material(texture) on 3d loaded model

163 views Asked by At

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?

2

There are 2 answers

5
Noxalus On

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);
});
0
Arsenius On

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?