Drawing texture using a different texture
Hi,
I need to get fragment of source texture and draw it in another. Of course SetPixel is a bad idea. For now best thing I did is drawing triangle on new texture using GL, but I cannot do it with source texture. I have tried GL.TexCoord2(...), with setting shaders, texture to material. Without success.
The first question is whether it is a good idea, and second how to draw this triangle using texture. Thank you for any help you can provide.
$$anonymous$$odification of solution, now works perfectly also with shader. I'm using function that is creating mesh from 2 triangles, look there. They have values xy from 0..1 As we can see there is no OpenGL needed.
private void RenderToTexture (Triangle2D outputTriangle, Triangle2D inputTriangle){
// create material for distorted$$anonymous$$ap rendering, notice shader type
$$anonymous$$aterial material = new $$anonymous$$aterial (Shader.Find ("Sprites/Default"));
material.mainTexture = source;
material.SetPass (0);
// get a temporary RenderTexture
RenderTexture renderTexture =
RenderTexture.GetTemporary (textureResolution, textureResolution);
// set the RenderTexture as global target
RenderTexture.active = renderTexture;
// render immediately to the active render texture
GL.Push$$anonymous$$atrix ();
GL.LoadPixel$$anonymous$$atrix (0, 1, 1, 0);
Graphics.Draw$$anonymous$$eshNow (Create$$anonymous$$esh (outputTriangle, inputTriangle),
Vector3.zero, Quaternion.identity);
GL.Pop$$anonymous$$atrix ();
// read the active RenderTexture into a new Texture2D
Texture2D newTexture = new Texture2D (textureResolution, textureResolution);
newTexture.ReadPixels (new Rect (0, 0, textureResolution, textureResolution), 0, 0);
// clean up after the party
RenderTexture.active = null;
RenderTexture.ReleaseTemporary (renderTexture);
// return the goods
newTexture.Apply ();
return newTexture;
}
Answer by JockPL · May 11, 2017 at 08:50 PM
Modified solution that I found. magic line is with
Graphics.DrawTexture(new Rect(0, 0, 0, 0), source)
I have no idea why it is working, but is
static Texture2D RenderGLToTexture(int width, int height, Material material, Texture2D source)
{
// get a temporary RenderTexture //
RenderTexture renderTexture = RenderTexture.GetTemporary(width, height);
// set the RenderTexture as global target (that means GL too)
RenderTexture.active = renderTexture;
// clear GL //
GL.Clear(false, true, Color.black);
// render GL immediately to the active render texture //
RenderGLStuff(width, height, material, source);
// read the active RenderTexture into a new Texture2D //
Texture2D newTexture = new Texture2D(width, height);
newTexture.ReadPixels(new Rect(0, 0, width, height), 0, 0);
// clean up after the party //
RenderTexture.active = null;
RenderTexture.ReleaseTemporary(renderTexture);
// return the goods //
return newTexture;
}
static void RenderGLStuff(int width, int height, Material material, Texture2D source)
{
material.SetPass(0);
GL.PushMatrix();
GL.LoadPixelMatrix(0, width, height, 0);
Graphics.DrawTexture(new Rect(0, 0, 0, 0), source);
GL.LoadOrtho();
GL.Begin(GL.TRIANGLES);
GL.TexCoord2(1, 0); GL.Vertex3(0.5f, 0, 0);
GL.TexCoord2(1, 1); GL.Vertex3(1, 1, 0);
GL.TexCoord2(0, 1); GL.Vertex3(0, 1, 0);
GL.End();
GL.PopMatrix();
}
Great, this what i search! Thanks @JockPL :)
I see, this drawing method Texture2D.ReadPixels() is fully on GPU, right? Its great.
Im not a strong in Unity.
https://docs.unity3d.com/ScriptReference/Texture2D.ReadPixels.html
Your answer
Follow this Question
Related Questions
Material.SetTexture doesn't work if not "_MainTex" 0 Answers
Texure2D is not changing 1 Answer
Resources.Load doesn't work 1 Answer
2D advanced texture blurriness 1 Answer