- Home /
Image processing: reseting image problems
Hey guys :3 I'm currently working on a project at school, trying to do some image processing in Unity (Please don't ask me why, it's a bad idea :P) So we ran into a problem where we can't reset the picture on which we draw the other image <.<'
Here's the OnTriggerStay function:
function OnTriggerStay(myCollider : Collider){
//Here's where we check if the player is close, in order not to waste ressources on Image Processing when the player is not around
if (myCollider.gameObject.tag == "Player"){
timeCount++;
//this is used for optimizing when to do the Image processing
if(timeCount%myTime == 0){
//first we loop through the amount of spiders we have and assign a new random value to the spider, this number is used as a reference for where to draw the spider
for(var k = 0; k < spiderReferenceArray.Length; k++){
spiderReferenceArray[k] += parseInt(Random.RandomRange(1.0f,5.0f)) + 256*parseInt(Random.RandomRange(1.0f,5.0f));
}
//This is where the image should be reset back to the background picture. the background is right, but what happens is that it for some reason doesn't assign the values from backgroundPixels into printPixels
for(var txt = 0; txt < printPixels.Length; txt++){
printPixels[txt].r = backgroundPixels[txt].r;
printPixels[txt].g = backgroundPixels[txt].g;
printPixels[txt].b = backgroundPixels[txt].b;
}
//here we draw the images of the spiders, first loop for every spider we have, second for every pixel in the spider image
for(var j = 0; j < spiderReferenceArray.Length; j++){
for(var i = 0; i < spiderPixels.Length; i++){
if(i%32 == 0){
lineChange++;
}
if(spiderPixels[i].r + spiderPixels[i].g + spiderPixels[i].b < 2.4f){
myPixel = i%32;
printPixels[spiderReferenceArray[j] + myPixel + 256*lineChange].r = spiderPixels[i].r;
printPixels[spiderReferenceArray[j] + myPixel + 256*lineChange].g = spiderPixels[i].g;
printPixels[spiderReferenceArray[j] + myPixel + 256*lineChange].b = spiderPixels[i].b;
}
}
lineChange = 0;
}
//assigns the values of the printPixels to the myTexture, which is the texture being displayed in unity
myTexture.SetPixels(printPixels);
myTexture.Apply();
this.renderer.material.mainTexture = myTexture;
}
}
}
People would be more willing to help if you gave a few essential informations, ins$$anonymous$$d of dumping your undocumented code here and expect everyone to divine the meaning of it.
Please expand your question explaining:
what exactly do you mean by "can't reset" - is there an error, or does it simply not revert your image? does the reset work partially, or not at all? what happens ins$$anonymous$$d? what are the visible side effects?
denote the sections in your code where you're attempting the "reset"
when/under what conditions is the image supposed to reset?
is OnTriggerStay() really the place where you want to do this?
Glancing at your code I notice you never initialize/reset the spiderReferenceArray, you keep adding values to it. Is this the intended behaviour?
Also, to help you debug your code by yourself, use print() or Debug.Log() commands at certain positions (e.g., before/after important for() loops), to check whether they are executed at all (because your if-conditions might be ill-formed), or whether the arrays are what you expect them to be (because array.Length changes unintentionally etc.).
Thanks for the tip :3 $$anonymous$$ost of the things you suggest I've already tried ^^ I'm not new to program$$anonymous$$g, I just assumed the code was so simple that people could wrap their head around it quickly :3
It is still unclear what the effects are. Do you ever see the correct/background image, or only the first time? Where/how do you initialize printPixels, are you sure it has the correct length? Does the texture have a width and height matching that array? Have you tried assigning myTexture.SetPixels(backgroundPixels) directly, just to check whether you see the background at all and the problem maybe lies somewhere else?
There is no method "Random.RandomRange". Are you certain your code is executed (and/or compiled) at all? (EDIT: actually, the method does exist, but it is obsolete, so it shouldn't be used)
The correct name is Random.Range.
Also note, ins$$anonymous$$d of parseInt(Random.Range(1.0f,5.0f)), you can simply use Random.Range(1,6), with both arguments being integers. You'll then need to increase your max by 1.
(converted this to a comment, since it is unrelated to the solution)
Thanks for the response again. The effect I'm trying to create is the image of the spider crawling across the wall, the error I get is that on the wall, it makes a trail of spiders, because it keeps placing the new spider ontop of the previous spider, ins$$anonymous$$d of cleaning the image.
So I have a background Image, which is the source of my initialization of both the myTexture, backgroundPixels and printPixels So they are all the same size and it works perfectly fine, except the fact that it creates that trail of spiders <.<
Answer by Wolfram · Nov 13, 2012 at 12:42 PM
In conclusion from the discussion above, your problem is that both printPixels and backgroundPixels refer to the same array. Make sure one is a copy of the other, not a reference:
Reference:
printPixels=backgroundPixels;
Copy:
printPixels=new Color[backgroundPixels.Length]; // will not copy the *contents*, only provide the memory
If it's still unclear, we'll need to see the code where you actually create/assign the array to both printPixels and backgroundPixels.
Thanks for the answer, I tried it out aaand my background disappeared XD haha But I'll probably just have to look around my code a bit and I might find the right solution :3 I see your point though ^^ Thanks for the help :D The rest I should be able to handle ^^