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 /
avatar image
0
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 thing a shader does is apply second uv set on texture.

alt text

Here is the shader code :

 Shader "Custom/uv2Tex" {
     Properties {
         _MainTex ("Diffuse", 2D) = "white" { }   
     }
     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 this for initialization
     IEnumerator Start () {
         
     
         yield return UploadPNG();
 
     }
     
     IEnumerator UploadPNG() 
     {
         yield return new WaitForEndOfFrame();
         bakeMat = Go.sharedMaterial;
         Maintexture = Go.sharedMaterial.GetTexture ("_MainTex") as Texture2D;
 
         Graphics.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 Graphics.Blit is not taking into account the uv alteration in shader? Is there a way to solve this? Thanks

baking.jpg (299.6 kB)
Comment
Add comment
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
0

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

I think you are using a mesh with customised second uv data. and i believe Graphics.Blit doesn't have similar uv data.

To achieve 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 Graphics.DrawMeshNow() to do the similar rendering manually

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 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.Draw$$anonymous$$eshNow() ;

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

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

Applying offset to a texture via script 1 Answer

Enable Shader Forge Displacement 1 Answer

Texture breaking along center of mesh 1 Answer

Changing two different objects renderer colour 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