Advice or tips on reading pixel color?

I want to read a bitmap image for placing some assets at runtime…because blender just cannot handle what I am trying to do with it :)…

anyway…anyone have any knowledge they could possibly pass on…I mean I have been googling™ this forever(like 3-4 min :slight_smile: ) and could not find any reference to reading pixels…I don’t really care about the method used…raycast or cycling through the pixels etc…I just want to be able to read the color and possibly alpha value…

any help would be appreciated.

I may need to look into another lib…but I would prefer not.

You have to have the image in some form that blender python can read it. This means it should be one of the classes from the bge.texture module (eg the ImageFFMPEG if reading the image from disk or an ImageRender if reading from an in-game camera). Once you have that, you can convert it to pixels by dumping the data into a buffer with the refresh(bytearray_buffer). Or if you have it bound to a texture you can use the to_list() function (still has to be an ImageFFMPEG from disk though). Either way you’ll end up with a 1D array of RGBARGBARGAB… Then it’s up to you to encapsulate how you want to look it up (by UV, raycast, etc).

Be aware the conversion is not fast at all. It’s fine for images from disk because you can convert them into pixels once, but for in-game cameras you can only manage about 256x256 pixels at 60FPS if you want to have other things in your game. (CPU/GPU bottleneck).

For an example using ImageRender to get pixels from in-game camera, see my most recent BGMC game (Solar Sailing Simulator). The comment lies, it doesn’t return an array, it simply fills the array. It’s exactly the same for ImageFFMPEG.

ok so I could use render.texture or MAscatter_map for example?..so long as it is accessible inside BGE? …I will look at your example…it is fine if it is a 1d array…I am only going to read it once at startup and convert it to a 2D array as objects are placed(with a preset z value) they will do a single raycast and drop in or self endObject()…invalid placement for example based on what the rayhit returns…I would have never considered looking at ‘imageFFMPEG’…

looks like I have some homework to do. Thanks.

The image source has to come from disk or from an in-game camera (or in-game mirror, or a deck-link device). I don’t think you can use ones from materials/textures/images that were loaded automatically by blender.
Hmm, there’s another demo where I sample a texture and use it to deform terrain. Here.

thanks for the help, in my tests I was not able to improve performance the way I thought I could…it was worth an afternoon of testing though…

I will mark this as solved.

Thanks again.

If you only need to read it once at the game start, then where is the issue? Just hide it behind a load screen if it takes a while. Maybe make sure you’re in module mode, and don’t re-extract it each frame?

I am not doing it every frame, I run it once during load and cycle through the image in the vicinity of the player…after that I load the rest every 5(I played with this a bit though 2-5 frames) frames until they are all loaded…but that gives a stutter during the first few seconds of gameplay…hardly noticeable…but it’s not the load time(initialization time), it’s the number of elements I am trying to manage…it is a bottleneck of both BGE and my computer…I thought by adding the assets at runtime it would improve performance…and it did, but not enough…trying to manage about 2k trees of various types…is just intensive…I assume it is the BGE…but I’m sure my computer plays a role as well. AMD Phenom 2 & gtx 650 Ti…a bit old. I did this all in a test scene…and TBH I went ahead and deleted it…I just do not think this will work out as I imagined.

…in any case I need to find a solution…or scale it all down…which I do not want to do…I may need to hard code it for every piece of terrain…would be a PITA, but might be worthwhile.

Trees… Would making empty LOD models help in your case ? So that the load system takes care of removing faces for you ?

(Like past a certain distance you setup the trees to use some mesh where you’d have deleted every vertex ?)

Edit: I also know that then the physics can become the problem, with too many objects, so there’s that.

I have used a ‘noVertObject’ as an lod in the past…and I am trying this again now…my problem is when I am rendering 2.5k trees…well not at the same time…but in the scene…it takes the frame rate down a lot…I am still doing some investigation and working on a new method(as in code ‘method’) of trying to solve this…it’s nothing fancy, but it’s the simple things that often work very well for me.

maybe something like this could come in handy, its a lod test i made to see what performance i could get from dynamically adding and removing objects from the scene based on distance, the result where pretty good.

it uses KDTree’s and Set’s to reduce the number of objects to check every frame.

(there is around 10.000 trees in the test level.)

file size: 58mb
http://15b.dk/blendfiles/lod-dist-spawn-test2.blend

WASD to move

  • / + to change lod distance

well I did get something working, but I’m not experienced with using tree structures…I will take a look at it…mine works, but it is still not very fast…ofc I have not implemented the batching yet…I literally just got it up and running and need to refine it.