Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create TextureModel from Memory Pointer #1403

Closed
KrabatTilt opened this issue Sep 21, 2020 · 9 comments
Closed

Create TextureModel from Memory Pointer #1403

KrabatTilt opened this issue Sep 21, 2020 · 9 comments
Labels
Feature Request Request New Feature SharpDX

Comments

@KrabatTilt
Copy link

KrabatTilt commented Sep 21, 2020

Usually i use following code to load and display images from file:

MemoryStream ms = await LoadFileAsMemoryStreamAsync("test.jpg");
// ms contains jpg encoded image
((PhongMaterialCore) Model.Material).DiffuseMap = new TextureModel(ms);

But now I have a situation where I get the image data uncompressed. I have an IntPtr to memory location, width and heigt in pixel, the PixelFormat and the stride. Is it possible to create a TextureModel from that data?

I found following example on how to do it in SharpDX from a WriteableBitmapSource. But I cannot find a way to get from Texture2D to HelixToolkit TextureModel.

@KrabatTilt KrabatTilt changed the title Create TextureModel from Bitmap Pointer Create TextureModel from Memory Pointer Sep 21, 2020
@holance
Copy link
Member

holance commented Sep 26, 2020

Currently you can convert your raw data into color4 array and create the texture model from the color4 array.

@holance holance added Feature Request Request New Feature SharpDX labels Sep 26, 2020
@lovefri
Copy link

lovefri commented Apr 20, 2022

It seems like this is the answer I am looking for but I cant get it to work.

I am trying to use a bitmap to create an image on a mesh. My first aproach was to save the bitmap as a png file and pass the filename to the TextureModel wich worked fine but when i try to do it with the same bitmap in memory it does not work.

How do I Get the albedomap TextureModel form a bitmap in memory?

A draft.

        SharpDX.Color4[] c;
        using (var stream = new MemoryStream())
        {
            bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
            a = stream.ToArray();
            c = getColor4FromStream(a);

            var mat = new HelixToolkit.Wpf.SharpDX.Model.PBRMaterialCore()
            {

These commented lines work if I use them
//AlbedoColor = new SharpDX.Color4(1, 1, 1, 1), // Set to something other than black
//AlbedoMap = new TextureModel(imageFile),
//AlbedoMapFilePath = Path.GetFileName(imageFile),

                AlbedoColor = new SharpDX.Color4(1, 1, 1, 1), // Set to something other than black
                AlbedoMap = new TextureModel(c),
                EnableTessellation = false
            };



            meshNode.Material = mat;
            meshNode.RenderWireframe = false;
        }





    private SharpDX.Color4[] getColor4FromStream(byte[] a)
    {
        List<SharpDX.Color4> result = new List<SharpDX.Color4>();
        foreach (var color in a)
        {
            result.Add(new SharpDX.Color4(a[0], a[1], a[2], a[3]));
        }
        return result.ToArray();
    }

@holance
Copy link
Member

holance commented Apr 20, 2022

You are compressing your data to a PNG, which is not the raw color data anymore.
Are you trying to use bitmap format? Or your data is raw color data?
If it is bitmap, you can just load the bitmap to a memory stream and pass it to the TextureModel.

@lovefri
Copy link

lovefri commented Apr 21, 2022

I have a bitmap and want to use it directly. My first attempt was to pass the stream but the MeshNode ends up blank/invisible.

This was my first attempt:

@lovefri
Copy link

lovefri commented Apr 21, 2022

        using (var stream = new MemoryStream())
        {
            bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);


            var mat = new HelixToolkit.Wpf.SharpDX.Model.PBRMaterialCore()
            {

                //AlbedoColor = new SharpDX.Color4(1, 1, 1, 1), // Set to something other than black
                //AlbedoMap = new TextureModel(imageFile),
                //AlbedoMapFilePath = Path.GetFileName(imageFile),

                AlbedoColor = new SharpDX.Color4(1, 1, 1, 1),
                AlbedoMap = new TextureModel(stream),
                EnableTessellation = false
            };



            meshNode.Material = mat;
            meshNode.RenderWireframe = false;
        }


        meshNode.Geometry = mesh;

        return meshNode;

@holance
Copy link
Member

holance commented Apr 21, 2022

using (var stream = new MemoryStream())
The stream is disposed before helix toolkit can use it.

@lovefri
Copy link

lovefri commented Apr 22, 2022

ah. I thought that the bitmap was stored in the meshnode.Material eaven after the stream closed. The plan waas to return a meshNode and in another function add that to : ViewportViewModel.GroupModel.AddNode(meshNode);

Are you saying that the stream needs to be open until the ViewPort gets the mesh?

@holance
Copy link
Member

holance commented Apr 22, 2022

Yes, it needs to be wait until it gets converted to a texture map.

There is a setting to allow helix toolkit to automatically dispose the stream once done with it.

You just need to use new TextureModel(stream, true).

@lovefri
Copy link

lovefri commented Apr 22, 2022

Tackar :)

@holance holance closed this as completed Apr 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Request Request New Feature SharpDX
Projects
None yet
Development

No branches or pull requests

3 participants