Updating material.

Nov 29, 2010 at 7:15 PM
Edited Nov 30, 2010 at 7:33 AM

Hello, I have some problem.

In my project i need to update material often. As written here i have implement the IMap interface like this:

 

public class generNewMap : IMap
{
    public  WriteableBitmap _bitmap;

    int IMap.Height { get { return _bitmap.PixelHeight; } }
    int IMap.Width { get { return _bitmap.PixelWidth; } }

    bool IMap.HasPixelChanges { get { return false; } }
    bool IMap.IsDynamic { get { return false; } }
   
    int[] IMap.GetPixelsAs32BppARGB()
    {
        return _bitmap.Pixels;
    }
    
}

 

And then was used this construction:

 

_materialBitmap = getWr();//getting writeableBitmap
_material.DiffuseMap = new generNewMap(_matherialBitmap);

 

In this way everething is fine, except one little thing: each time i creating new instance of generNewMap(...) the program eats about 2MB of Ram. And it's never cleared by GC. So i need to update just WriteableBitmap in my generNewMap instance instead of creating new instance each time. I have done it, but here is new problem: after updating image in genereNewMap instance nothing happend. I mean the image is updated fine ( to test it i added an Image to the screen and update the image source; that's why _bitmap is public). In debugger i've noticed that the GetPixelsAs32BppARGB() was called just one time in the begin and never after it, but after updating the _bitmap value i need to update a texture field in the 3D object (i think so).

How can I do it? Am I on the right way (update just bitmap instead of creating new object)? If I am on the wrong way, how I can solve this memory problem?

Coordinator
Nov 30, 2010 at 7:40 PM

Hi,

there are several issues here; first off - the feature you're looking for is not completely finished in Balder, dynamically being able to update a texture every frame or every so often. Due to an architecture change, this feature sort of died. That being said, the way you're doing it will work "fine", you'll get the expected result, but its far from optimal and something I want to get done.

The second problem is the memory leak, this is also a known problem. The reason for this is that there is a Cache that kicks in inside Balder that is holding references to assets - and a bitmap is an asset, so you'd need to work alongside with the cache and manually evict stuff from it till this whole thing has been fixed. There was a discussion here about this a month or two ago (maybe longer), but I couldn't find it while reading this. Maybe if you go back in the threads you'll find it, in it there was a workaround that was quite nice.

Dec 1, 2010 at 9:11 PM

Are you talking about this and this discussions? 
I don't understand how exactly i have to delete all references?  

Is there example of working code? 

Thank you very much for helping me, i'm very appreciate it.

Coordinator
Dec 7, 2010 at 7:17 PM

Sorry for the slow response here - but thats the ones I was thinking about.

Apr 21, 2011 at 12:46 PM

Hi, adept,

I'm encounter the exactly same problem with E13Mort, which is need to use a WriteableBitmap as DiffuseMap of a Material so that I can update the appearance of the Material dynamically.( exactly, needs to update parts of the Material periodically)

 After created a class that implemented the IMap interface, which also has a WriteableBitmap property, the initial appearance of the Material looks fine. But when I update the Pixels of the WriteableBitmap, the Material didn't change.

The only way to change the appearance of the Material I found is creating a new WriteableBitmap and give it to the DiffuseMap again. But in my scenario, I have to created a 4096*2048 WriteableBitmap and use it as an inside material of a sphere, every new WriteableBitmap of this size could consume more 100M memory additionally, this is unacceptable.

So is there any chance to update only parts of DiffuseMap of a Material dynamically? I really need this feature. Thanks!

Coordinator
Apr 26, 2011 at 6:41 AM

Hi,

I'm sorry to say that it is in fact not working at all this little thing. It used to, but I see that I refactored it all a while back and then it stopped working.
I'll put it up as a bug in Balder - can't say anything about when it will be fixed though. 

Coordinator
Apr 26, 2011 at 6:41 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Apr 27, 2011 at 2:07 AM
adept wrote:

Hi,

I'm sorry to say that it is in fact not working at all this little thing. It used to, but I see that I refactored it all a while back and then it stopped working.
I'll put it up as a bug in Balder - can't say anything about when it will be fixed though. 

Thanks for your reply. However, looking forward to the coming solution.