• 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 /
avatar image
Question by Freesmith · Dec 26, 2016 at 10:11 AM · materialtexture2dbakingshader writinguv2

Bake a texture from shader

Hi, I am trying to bake a final result from shader to texture. the only t$$anonymous$$ng a shader does is apply second uv set on texture.

alt text

Here is the shader code :

 Shader "Custom/uv2Tex" {
     Properties {
         _MainTex ("Diffuse", 2D) = "w$$anonymous$$te" { }   
     }
     SubShader {
         Pass {
         CGPROGRAM
         #pragma vertex vert
         #pragma fragment frag
  
         sampler2D _MainTex;
  
        struct a2v {
             float4 vertex : POSITION;
             float2  texcoord : TEXCOORD0;
             float2 texcoord1 : TEXCOORD1;
         };
  
         struct v2f {
             float4 vertex : POSITION;
             float2  texcoord : TEXCOORD0;
             float2 texcoord1 : TEXCOORD1;
         };
  
         v2f vert (a2v v)
         {
             v2f o;
             o.vertex = mul( UNITY_MATRIX_MVP, v.vertex);          
             o.texcoord1 = v.texcoord1;
             return o;
         }
  
         half4 frag (v2f i) : COLOR
         {
             half4 color = tex2D(_MainTex, i.texcoord1);         
             return color;
         }
       
         ENDCG
         }
     }
 }

and here is the script that bakes the texture:

 using UnityEngine;
 using System.Collections;
 using System.IO;
 public class BakeVideo : MonoBehaviour {
     public Texture2D Maintexture;
     public RenderTexture RT;
     public Material bakeMat;
     public MeshRenderer Go;
 
     // Use t$$anonymous$$s for initialization
     IEnumerator Start () {
         
     
         yield return UploadPNG();
 
     }
     
     IEnumerator UploadPNG() 
     {
         yield return new WaitForEndOfFrame();
         bakeMat = Go.sharedMaterial;
         Maintexture = Go.sharedMaterial.GetTexture ("_MainTex") as Texture2D;
 
         Grap$$anonymous$$cs.Blit (Maintexture, RT, bakeMat);
         // Create a texture the size of the screen, RGB24 format
         RenderTexture.active = RT;
         Texture2D tex = new Texture2D(512,512);
 
         // Read screen contents into the texture
         tex.ReadPixels(new Rect(0, 0, 512, 512), 0, 0);
         tex.Apply();
 
         // Encode texture into PNG
         byte[] bytes = tex.EncodeToPNG();
         Object.Destroy(tex);
 
         // For testing purposes, also write to a file in the project folder
         File.WriteAllBytes(Application.dataPath + "/../SavedScreen.png", bytes);
     }
 
 }
 

The problem is that my result is always looking the same as the original texture, as if the Grap$$anonymous$$cs.Blit is not taking into account the uv alteration in shader? Is there a way to solve t$$anonymous$$s? Thanks

baking.jpg (299.6 kB)
Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by SunnyChow · Dec 26, 2016 at 10:23 AM

I t$$anonymous$$nk you are using a mesh with customised second uv data. and i believe Grap$$anonymous$$cs.Blit doesn't have similar uv data.

To ac$$anonymous$$eve what you want, the easiest way it's to set a camera and render it to render texture, or you can use GL.LoadOrtho() and Grap$$anonymous$$cs.DrawMeshNow() to do the similar rendering manually

Comment

People who like this

0 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 Freesmith · Dec 28, 2016 at 06:43 PM 0
Share

yes, i'am using a mesh with customised second uv data; thnx, I will look into GL.LoadOrtho() and Graphics.DrawMeshNow() ;

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Texture Takes Main Color of Material 1 Answer

Configure standard shader/material with Texture2Darray: how? 1 Answer

Convert Texture to Texture2D e.g. renderer.material.mainTexture 3 Answers

Applying offset to a texture via script 1 Answer

Texture breaking along center of mesh 1 Answer


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