Free Support Forum - aspose.com

Obj to u3d conversion, missing texture

Hello,
I am trying to convert .obj files with texture (png format) to u3d files. But when I read the obj file with
scene.Open()
and save it as u3d, the texture png is missing at the output. If I save the scene as fbx or any other format, I can see the texture, but when I try to save the same scene to u3d, the .png texture is missing. Other meshes with solid color can generate but, only the mesh with a png texture is missing its texture.
Is that a known bug, or am I missing something?

@gurhalil

Would you please share your sample file in .zip format along with sample code snippet that you are using. We will test the scenario in our environment and address it accordingly.

I am using one of your sample codes, this code is creating a cube with 6 faces and texture. For fbx, and obj it creates the texture. But when I try to save it as u3d, no texture

private static void RubikCube()
{
Bitmap[] bitmaps = CreateRubikBitmaps();
Scene scene = new Scene();
//create a box and convert it to mesh, so we can manually specify the material per face
var box = (new Box()).ToMesh();

        //create a material mapping, the box mesh generated from Box primitive class contains 6 polygons, then we can reference the material of polygon(specified by MappingMode.Polygon) by index(ReferenceMode.Index)
        var materials = (VertexElementMaterial)box.CreateElement(VertexElementType.Material, MappingMode.Polygon, ReferenceMode.Index);
        //and each polygon uses different materials, the indices of these materials are specified below
        materials.SetIndices(new int[] { 0, 1, 2, 3, 4, 5 });
        //create the node and materials(referenced above)
        var boxNode = scene.RootNode.CreateChildNode(box);
        var tex = new Texture();
        
        for (int i = 0; i < bitmaps.Length; i++)
        {
            //create material with texture
            var material = new LambertMaterial();
            tex = new Texture();
            using (var ms = new MemoryStream())
            {
                bitmaps[i].Save(ms, ImageFormat.Png);
                var bytes = ms.ToArray();
                //Save it to Texture.Content as embedded texture, thus the scene with textures can be exported into a single FBX file.
                tex.Content = bytes;
                
             //   tex.EnableMipMap = true;
                tex.Name = string.Format("cube_{0}.png", i);
                //Give it a name and save it to disk so it can be opened with .obj file 
                tex.FileName = string.Format("cube_{0}.png", i);
                tex.AlphaSource = AlphaSource.PixelAlpha;
                File.WriteAllBytes(tex.FileName, bytes);
                //Dispose the bitmap since we're no longer need it.
                bitmaps[i].Dispose();
            }
            //the texture is used as diffuse
            material.SetTexture("DiffuseColor", tex);
            material.SetTexture(Material.MapNormal, tex);
            material.Name = string.Format("cube_{0}.png", i);
           
            //  material.AmbientColor = new Vector3(1,0,0);
            //  material.DiffuseColor = new Vector3(0.5, 0.5, 0.5);
            //  material.EmissiveColor = new Vector3(0, 1, 1);

            //attach it to the node where contains the box mesh
            boxNode.Materials.Add((Material)material);
        }
        //save it to file

        //3D viewer of Windows 10 does not support multiple materials, you'll see same textures in each face, but the tools from Autodesk does
        scene.Save("test.fbx", FileFormat.FBX7500ASCII);
        PdfSaveOptions opt = new PdfSaveOptions();
        opt.LightingScheme = PdfLightingScheme.CAD;
        opt.RenderMode = PdfRenderMode.Solid;

        string dataDir = ".\\";
        // Initialize an object
        U3DSaveOptions saveU3DOptions = new U3DSaveOptions();
        // Export normal data.
        saveU3DOptions.ExportNormals = true;
        // Export the texture coordinates.
        saveU3DOptions.ExportTextureCoordinates = true;
        // Export the vertex diffuse color.
        saveU3DOptions.ExportVertexDiffuse = true;
        // Export vertex specular color
        saveU3DOptions.ExportVertexSpecular = true;
        // Flip the coordinate system.
        saveU3DOptions.FlipCoordinateSystem = true;
        // Configure the look up paths to allow importer to find external dependencies.
        saveU3DOptions.LookupPaths = new List<string>(new string[] { dataDir });
        // Compress the mesh data
        saveU3DOptions.MeshCompression = true;
       
       // scene.RootNode.ChildNodes[0].Material.
       
        // Save in the PDF format
        scene.Save("output_out.u3d", saveU3DOptions);
        //NOTE: Multiple materials of mesh in Aspose.3D's OBJ Exporter is not supported yet.
        //But we can split the mesh with multiple materials into different meshes by using PolygonModifier.SplitMesh
        PolygonModifier.SplitMesh(scene, SplitMeshPolicy.CloneData);
        //following code will also generate a material library file(test.mtl) which uses the textures exported in above code.
        scene.Save("test.obj", FileFormat.WavefrontOBJ);

    }

    private static Bitmap[] CreateRubikBitmaps()
    {
        Brush[] colors = { Brushes.White, Brushes.Red, Brushes.Blue, Brushes.Yellow, Brushes.Orange, Brushes.Green };
        Bitmap[] bitmaps = new Bitmap[6];
        //initialize the cell colors
        int[] cells = new int[6 * 9];
        for (int i = 0; i < cells.Length; i++)
        {
            cells[i] = i / 9;
        }
        //shuffle the cells
        Random random = new Random();
        Array.Sort(cells, (a, b) => random.Next(-1, 2));
        //paint each face

        // size of each face is 256px
        const int size = 256;
        // size of cell is 80x80 
        const int cellSize = 80;
        // calculate padding size between each cell
        const int paddingSize = (size - cellSize * 3) / 4;
        int cellId = 0;
        for (int i = 0; i < 6; i++)
        {
            bitmaps[i] = new Bitmap(size, size, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            using (Graphics g = Graphics.FromImage(bitmaps[i]))
            {
                g.Clear(Color.Black);
                for (int j = 0; j < 9; j++)
                {
                    //calculate the cell's position
                    int row = j / 3;
                    int column = j % 3;
                    int y = row * (cellSize + paddingSize) + paddingSize;
                    int x = column * (cellSize + paddingSize) + paddingSize;
                    Brush cellBrush = colors[cells[cellId++]];
                    //paint cell
                    g.FillRectangle(cellBrush, x, y, cellSize, cellSize);
                }
            }
        }
        return bitmaps;
    }

@gurhalil

We have logged an issue as THREEDNET-795 in our issue tracking system for the sake of further investigation. We will look into its details and keep you posted with the status of its correction. Please be patient and spare us some time.

We are sorry for the inconvenience.

@gurhalil

The earlier logged issue has been fixed in Aspose.3D for .NET 20.12.1 hotfix.

Aspose.3D-20.12.1.zip (5.3 MB)

The issues you have found earlier (filed as THREEDNET-795) have been fixed in Aspose.3D for .NET 21.1.