Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by JockPL · May 11, 2017 at 07:52 PM · texturetexture2dgl

Drawing texture using a different texture

idea 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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JockPL · Aug 03, 2017 at 06:19 PM 0
Share

$$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;
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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();
     }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AntonioModer · Jun 01, 2017 at 07:27 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

119 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Unity WebCamTexture looks squashed in RawImage 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges