[Original picture]
[The picture taken in unity]
I wanna take a screenshot with camera in unity. i can get a screenshot with camera but it looks weird like second picture. how can i solve this problem?
public void ScreenShot(int imgName)
{
RenderTexture activeRenderTexture = RenderTexture.active; RenderTexture.active = targetCam.targetTexture;
targetCam.Render();
Texture2D image = new Texture2D(targetCam.targetTexture.width, targetCam.targetTexture.height);
_texture = image;
image.ReadPixels(new Rect(0, 0, targetCam.targetTexture.width, targetCam.targetTexture.height), 0, 0);
image.Apply();
RenderTexture.active = activeRenderTexture;
byte[] bytes = image.EncodeToPNG();
}
This is the code used in unity
Related
I have some problems understanding the proper usage of Graphics.Blit() in Unity
I have a Shader (volumeShaderSecondPass), for debug reasons it simply returns float4(1.0,1.0,1.0,1.0) right now.
If I use it in a material created in the Editor, I correctly see just a white square if it is applied to a cube:
If I create a material in code and try to use Graphics.Blit() to display it, my whole screen turns white.
void OnRenderImage(RenderTexture src, RenderTexture dst)
{
volumeCam.targetTexture = volumeFirstPassTexture;
volumeCam.backgroundColor = new Color(1.0f, 0f, 0.5f, 0.5f);
volumeCam.clearFlags = CameraClearFlags.Color;
volumeCam.cullingMask = 1 << LayerMask.NameToLayer("Volume");
volumeCam.RenderWithShader(volumeShaderFirstPass, null);
volumeMat = new Material(volumeShaderSecondPass);
volumeMat.hideFlags = HideFlags.DontSave;
volumeMat.SetTexture("_Texture3D", data);
Graphics.Blit(volumeFirstPassTexture, dst, volumeMat);
}
I have been trying to change the format from a camera that give a texture in Alpha8 to RGBA and have been unsuccessful so far.
This is the code I've tried:
public static class TextureHelperClass
{
public static Texture2D ChangeFormat(this Texture2D oldTexture, TextureFormat newFormat)
{
//Create new empty Texture
Texture2D newTex = new Texture2D(2, 2, newFormat, false);
//Copy old texture pixels into new one
newTex.SetPixels(oldTexture.GetPixels());
//Apply
newTex.Apply();
return newTex;
}
}
And I'm calling the code like this:
Texture imgTexture = Aplpha8Texture.ChangeFormat(TextureFormat.RGBA32);
But the image gets corrupted and isn't visible.
Does anyone know how to change this Alpha8 to RGBA so I can process it like any other image in OpenCV?
A friend provided me with the answer:
Color[] cs =oldTexture.GetPixels();
for(int i = 0; i < cs.Length; i++){//we want to set the r g b values to a
cs[i].r = cs[i].a;
cs[i].g = cs[i].a;
cs[i].b = cs[i].a;
cs[i].a = 1.0f;
}
//set the pixels in the new texture
newTex.SetPixels(cs);
//Apply
newTex.Apply();
This will take alot of resources but it will work for sure.
If you know a better way to make this change please add an answer to this thread.
I am trying to load a texture(and create a sprite from it eventually) from disk but sprite renders as low resolution image.
What I am doing:
-> Download the image from url. Once the image is downloaded, I save the texture as png to disk so that next time it doesn't requires a download.
WWW www = new WWW(url);
yield return www;
if (www.isDone)
{
if (string.IsNullOrEmpty(www.error))
{
Sprite img = Sprite.Create(www.texture, new Rect(0, 0, www.texture.width, www.texture.height), new Vector2(0, 0));
reward.RewardSprite = img;
byte[] bytes = www.texture.EncodeToPNG();
FileManager.SaveRewardImage(reward.rewardId, bytes);
}
else
{
Debug.Log(www.error);
}
}
-> Load from disk
string path = string.Format("Cache\\Venue\\{0}", nameWithoutExtension);
return Resources.Load<Texture2D>(path);
The first time when the texture loads from url, its resolution seems fine(because its the original one). When it loads from cache, it attenuates to a lower one.
Can someone tell me what am I missing, or even if there is way around it?
Thanks in advance.
You can overload your texture in your sprite creation in the same way that you do with the rectangle and specify in TextureFormat the format you need:
Sprite img = Sprite.Create(
new Texture2D (www.texture.width, www.texture.height, TextureFormat format, bool mipmap),
new Rect(0, 0, www.texture.width, www.texture.height), new Vector2(0, 0));
Surprisingly in Unity, for years the only way to simply scale an actual PNG is to use the very awesome library http://wiki.unity3d.com/index.php/TextureScale
Example below
How do you scale a PNG using Unity5 functions? There must be a way now with new UI and so on.
So, scaling actual pixels (such as in Color[]) or literally a PNG file, perhaps downloaded from the net.
(BTW if you're new to Unity, the Resize call is unrelated. It merely changes the size of an array.)
public WebCamTexture wct;
public void UseFamousLibraryToScale()
{
// take the photo. scale down to 256
// also crop to a central-square
WebCamTexture wct;
int oldW = wct.width; // NOTE example code assumes wider than high
int oldH = wct.height;
Texture2D photo = new Texture2D(oldW, oldH,
TextureFormat.ARGB32, false);
//consider WaitForEndOfFrame() before GetPixels
photo.SetPixels( 0,0,oldW,oldH, wct.GetPixels() );
photo.Apply();
int newH = 256;
int newW = Mathf.FloorToInt(
((float)newH/(float)oldH) * oldW );
// use a famous Unity library to scale
TextureScale.Bilinear(photo, newW,newH);
// crop to central square 256.256
int startAcross = (newW - 256)/2;
Color[] pix = photo.GetPixels(startAcross,0, 256,256);
photo = new Texture2D(256,256, TextureFormat.ARGB32, false);
photo.SetPixels(pix);
photo.Apply();
demoImage.texture = photo;
// consider WriteAllBytes(
// Application.persistentDataPath+"p.png",
// photo.EncodeToPNG()); etc
}
Just BTW it occurs to me I'm probably only talking about scaling down here (as you often have to do to post an image, create something on the fly or whatever.) I guess, there would not often be a need to scale up in size an image; it's pointless quality-wise.
If you're okay with stretch-scaling, actually there's simpler way by using a temporary RenderTexture and Graphics.Blit. If you need it to be Texture2D, swapping RenderTexture.active temporarily and read its pixels to Texture2D should do the trick. For example:
public Texture2D ScaleTexture(Texture src, int width, int height){
RenderTexture rt = RenderTexture.GetTemporary(width, height);
Graphics.Blit(src, rt);
RenderTexture currentActiveRT = RenderTexture.active;
RenderTexture.active = rt;
Texture2D tex = new Texture2D(rt.width,rt.height);
tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0);
tex.Apply();
RenderTexture.ReleaseTemporary(rt);
RenderTexture.active = currentActiveRT;
return tex;
}
I add a cube to unity scene. I want to set this cub's texture by using an image.
I use below code to load image and set texture :
Texture2D text2D = new Texture2D(Screen.width, Screen.height,TextureFormat.RGB24 , false);
text2D.SetPixels(((Texture2D)Resources.Load("image")).GetPixels());
MeshRenderer renderer = cube.GetComponent<MeshRenderer>();
renderer.material.mainTexture = text2D;
I see only a gray cube not the image on the scene.
You can shorten this quite a bit with only:
renderer.material.mainTexture = Resources.Load<Texture2D>("image");
Note that if the image is not found then you get null.
To see changes on the Texture2D, use text2d.Apply();
This is even more easy to do.
Try
public GameObject _cube;
void Start()
{
Renderer rend = _cube.GetComponent<Renderer> ();
rend.material.mainTexture = Resources.Load ("image") as Texture;
}
LoadImage method can also be used to do the job. But here, you have to pass in the image as .bytes format.
Example:
public TextAsset image;
void Start()
{
var texture = new Texture2D(100, 100, TextureFormat.ARGB32, false);
texture.LoadImage(image.bytes);
GetComponent<Renderer>().material.mainTexture = texture;
texture.Apply();
}