BabylonJS / Editor

Community managed visual editor for Babylon.js
http://editor.babylonjs.com/
813 stars 232 forks source link

Missing textures while importing draco #148

Closed zuffik closed 3 years ago

zuffik commented 4 years ago

Hello guys, I have following problem: I am using my cloned repo of editor, in file src/editor/scene/scene-importer.ts I added a method that is being called on menu item click. It is supposed to load compressed draco file (made from obj) and load textures from mtl file. Both are hosted on local server. The point is that the draco file is being loaded without problem but when I try to load texture from mtl this does not affect material at all.

Thanks in advance (Please don't judge the code itself, it deserves refactor I am not capable of doing right now)

public static async ImportDraco (editor: Editor): Promise<void> {
        $('#draco').click();

        const files: File[] = await new Promise<File[]>(res => {
            $('#draco').on('change', (e: ChangeEvent<HTMLInputElement>) => {
                $('#draco').off('change');
                const files: File[] = [];
                for (let i = 0; i < (e.target.files || []).length; i++) {
                    files.push(e.target.files[i]);
                }
                res(files);
            });
        });
        if (files.length == 0) {
            return;
        }
        const fd = new FormData();
        fd.append('files', files[0]);
        const baseURL = 'http://localhost:5000/';
        BABYLON.DracoCompression.Configuration = {
            decoder: {
                wasmUrl: "https://cdn.jsdelivr.net/gh/google/draco/javascript/draco_decoder.js",
                wasmBinaryUrl: "https://cdn.jsdelivr.net/gh/google/draco/javascript/draco_decoder.wasm",
                fallbackUrl: "https://cdn.jsdelivr.net/gh/google/draco/javascript/draco_wasm_wrapper.js",
            }
        };
        BABYLON.Tools.LoadFile('http://localhost:5000/tree.drc', async function(data: ArrayBuffer) {
            const dracoCompression = new BABYLON.DracoCompression();

            const attributes = {
                // [BABYLON.VertexBuffer.UVKind]: 'uv',
                // [BABYLON.VertexBuffer.NormalKind]: 'normal',
                // [BABYLON.VertexBuffer.ColorKind]: 'color',
                [BABYLON.VertexBuffer.PositionKind]: 'position'
            };

            const scene = editor.core.scene as unknown as BABYLON.Scene;
            const vertexData = await dracoCompression.decodeMeshAsync(data, attributes as any);

            const mesh = new BABYLON.Mesh("dracoMesh", scene);
            const geometry = new BABYLON.Geometry("dracoGeometry", scene, vertexData);
            geometry.applyToMesh(mesh);
            console.log(mesh);
            editor.graph.add({
                data: mesh,
                id: 'tree',
                text: 'tree'
            }, editor.graph.root);

            const mtl = new BABYLON.MTLFileLoader();
            const mtlData = await axios.get('/strazky_tree_bright.mtl', {baseURL});
            mtl.parseMTL(scene, mtlData.data, baseURL);
            mtl.materials[0].backFaceCulling = false;
            mesh.material = mtl.materials[0];
            console.log(mesh.getBoundingInfo());

            scene.addMesh(mesh as any);

        }, undefined, undefined, true);
    }
Snímka obrazovky 2019-10-15 o 21 01 52 Snímka obrazovky 2019-10-15 o 21 01 25
julien-moreau commented 4 years ago

Hey! I don't know anything about draco for now so I'm getting some informations about and have a look asap! :)

julien-moreau commented 4 years ago

Are you sure that the MTLFileLoader has only one material in the .materials array? Can you share me the material's properties here please?

zuffik commented 4 years ago

I am sure in this particular case:

console.log(mtl.materials.length, mtl.materials, mtl.materials[0]);

output:

Snímka obrazovky 2019-10-15 o 22 22 14

Full object is here (too large unfortunately :/ )

https://www.dropbox.com/s/aou29hv3b562hmi/material.json?dl=0

julien-moreau commented 4 years ago

Still weird, I’m back and I’m having a look Can you share me the draco files and the link to your fork? Thanks

zuffik commented 4 years ago

Hi, here is my repository: https://gitlab.com/fricka/overhe4d and here are files: https://www.dropbox.com/s/6j1tw0io4699dsn/babylon-draco.zip?dl=0

there are 3 files:

julien-moreau commented 4 years ago

Can you share also the textures please?

zuffik commented 4 years ago

Sure, sorry I forgot: https://www.dropbox.com/s/19j77mv4etrsk9t/babylon-textures.zip?dl=0

julien-moreau commented 4 years ago

Are you sure that they are not applied? Here I can that they are applied but are scaled a lot: image

julien-moreau commented 4 years ago

The obj works, maybe the problem comes from the draco file or the draco reader. You should verify your UV coordinates to be sure they are same

zuffik commented 4 years ago

I can achieve this by directly assigning texture to decompressed file (not through mtl file). Otherwise it's just black. Okay I will check if the file and code is correct and let you know.

zuffik commented 4 years ago

Hi, I was working around with this and the code keeps running into this error:

GL ERROR :GL_INVALID_OPERATION : glDrawElements: attempt to access out of range vertices in attribute 0

Do you know what can be cause of this situation? I am not familiar with webgl unfortunately.

julien-moreau commented 4 years ago

Unfortunately not if I don't have the resources to reproduce. Anyway, I think that the geometries are wrong. I mean that position buffer (or normal or whatever) looks smaller than the indices values

Any news ?

julien-moreau commented 4 years ago

@zuffik any news ? :)

zuffik commented 4 years ago

Hi, I will dive into this today and tomorrow so I will let you know. Thank you so far

zuffik commented 4 years ago

Okay so I checked the materials and everything should be fine (as I think). I ran into following error: when I click View->Materials view and click the material that just got loaded, there is error in console: [Error] Not allowed to load local resource: file:///http://localhost:5000//textura_strazky_Update.png. But as I understand, there is a way to load resources externaly (or is it allowed only from imgur and dropbox?)

julien-moreau commented 4 years ago

The error comes from the editor manages only files and not URLs What are you trying to do exactly with the editoor?

zuffik commented 4 years ago

As I said: just clicked on material (loaded from mtl file) in "Materials view" and this came up. Maybe it's just some side effect but I was wondering if there may be some causality between this error and missing textures.

julien-moreau commented 3 years ago

Done, Editor v4 now uses local file system for files :)