- Home /
WebCam background optimisation
Hi everyone, i am creating an app where there is a background with camera feed, it's supposed to be gray like in the screenshot below.
Now the problem is that with current code, on Android app runs with 3FPS when 1280x720 on SGS4, not good (and I don't want to have resolution of 320x240). What I want is somewhat good quality as well as at least 30FPS.
Right now I am using CameraWebTexture, which is saved in Color[] array, then I process this Array to make image gray and brighten it. Then I apply this array to Texture2D which is further applied on RawImage texture and material.mainTexture.
What I need is ways to optimize the code or alternatives which would let accomplish this task. This should be possible , as for example SnapChat has the same effect on login screen. Willing for your help :)
WebCamTexture webCamTexture;
Color32[] modColors;
Texture2D modTexture;
void Start()
{
SetupTexture();
}
void Update()
{
ModifyTexture();
ApplyTexture();
}
void SetupTexture()
{
webCamTexture = new WebCamTexture(1280,720,30);
webCamTexture.Play();
modTexture = new Texture2D(webCamTexture.width,webCamTexture.height);
RawImage rawImage = GetComponent<RawImage>();
rawImage.texture = modTexture;
rawImage.material.mainTexture = modTexture;
}
void ModifyTexture()
{
modColors = webCamTexture.GetPixels32();
for (int i = 0; i < modColors.Length; i++)
{
byte modColor = (byte)((modColors[i].r + modColors[i].g + modColors[i].b) / 3);
modColor = (byte)(192 + (modColor / 4));
modColors[i] = new Color32(modColor, modColor, modColor, 255);
}
}
void ApplyTexture()
{
modTexture.SetPixels32(modColors);
modTexture.Apply();
}
I am not exactly sure, but the high strain might be because of the way you use the arrays, I am not sure if I completely understood this but the way you are calculating the image should have an massive amount of processing each second.
True, but even without any image processing I get only 8-9FPS
Also I am not sure if it is right to process and apply texture every Update() frame, maybe I need to check for camera feed and only then do all that stuff... as well as if it was really good idea to use RawImage GameObject.