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 Edahsrevlis · Oct 18, 2016 at 05:14 AM · shaderoffsetspritesheetanimated

SetTextureOffset Shader code

I'm new to Shader code. I'm using a ChromaKey shader to remove the black color from this John Travolta sprite sheet. And I'm using an Animate Texture Sheet script to cycle through the sprite sheet positions.

The Animate Texture Sheet script works fine with unity's built in "Particles/Additive" Shader, but it does not with this ChromaKey shader. SetTextureOffset doesn't affect it, which leads me to believe the shader is missing something that would allow offset of the UVs.

The end goal is to animate real green screen footage to place 2D hologram audience members into our 3D spectator sport game.

Inspector Settings - Shader

Threshhold: 0.0039, Slope: 0, Color: (5,4,5, 255) Texture: <http://imgur.com/a/OgUvQ>

Inspector Settings - Script

Columns: 8, Rows: 8, FPS: 15

Does anyone see the problem? Here is the code:

Shader

 Shader"Custom/ChromaKey" {
 
     Properties{
         _MainTex("Base (RGB)", 2D) = "white" {}
         _thresh("Threshold", Range(0, 16)) = 0.65
         _slope("Slope", Range(0, 1)) = 0.63
         _keyingColor("KeyColour", Color) = (1,1,1,1)
     }
 
         SubShader{
         Tags{ "Queue" = "Transparent""IgnoreProjector" = "True""RenderType" = "Transparent" }
         LOD 100
 
         Lighting Off
         ZWrite Off
         AlphaTest Off
         Blend SrcAlpha OneMinusSrcAlpha
 
         Pass{
         CGPROGRAM
         #pragma vertex vert_img
         #pragma fragment frag
         #pragma fragmentoption ARB_precision_hint_fastest
 
         sampler2D _MainTex;
     float3 _keyingColor;
     float _thresh; //0.8
     float _slope; //0.2
 
 #include "UnityCG.cginc"
 
     
 
 
     float4 frag(v2f_img i) : COLOR{
         float3 input_color = tex2D(_MainTex,i.uv).rgb;
     float d = abs(length(abs(_keyingColor.rgb - input_color.rgb)));
     float edge0 = _thresh*(1 - _slope);
     float alpha = smoothstep(edge0,_thresh,d);
     return float4(input_color,alpha);
 
 
     }
 
         ENDCG
     }
     }
 
         FallBack"Unlit/Texture"
 }
 
 

Texture Sheet Script

 using UnityEngine;
 using System.Collections;
 
 public class AnimateTextureSheet : MonoBehaviour
 {
     public int columns = 2;
     public int rows = 2;
     public float framesPerSecond = 10f;
 
     //the current frame to display
     private int index = 0;
     private Vector2 spriteSize;
 
     private Renderer renderer;
 
     void Start()
     {
 
         renderer = GetComponent<Renderer>();
         StartCoroutine(updateTiling());
 
         
 
         //set the tile size of the texture (in UV units), based on the rows and columns
         spriteSize = new Vector2(1f / columns, 1f / rows);
         renderer.materials[0].SetTextureScale("_MainTex", spriteSize);
     }
 
     private IEnumerator updateTiling()
     {
         renderer.materials[0].SetTextureOffset("_MainTex", Vector2.zero);
         while (true)
         {
             //move to the next index
 
             float xOffset = (columns -1) - (index % columns);
             float yOffset = ((index / columns) % rows);
            
 
 
             Vector2 offset = new Vector2(xOffset * spriteSize.x, //x index
                                           (yOffset * spriteSize.y));          //y index
 
             renderer.materials[0].SetTextureOffset("_MainTex", offset);
             
             index++;
             yield return new WaitForSeconds(1f/framesPerSecond);
             
         }
 
     }
 }


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
1
Best Answer

Answer by Namey5 · Oct 18, 2016 at 06:22 AM

You're absolutely right about the shader not supporting texture offsets. Whilst it is a default in surface shaders, you have to do it manually in vertex/fragment shaders, however the UnityCG include has a macro for this specific purpose.

  Shader"Custom/ChromaKey" {
  
      Properties{
          _MainTex("Base (RGB)", 2D) = "white" {}
          _thresh("Threshold", Range(0, 16)) = 0.65
          _slope("Slope", Range(0, 1)) = 0.63
          _keyingColor("KeyColour", Color) = (1,1,1,1)
      }
  
          SubShader{
          Tags{ "Queue" = "Transparent""IgnoreProjector" = "True""RenderType" = "Transparent" }
          LOD 100
  
          Lighting Off
          ZWrite Off
          AlphaTest Off
          Blend SrcAlpha OneMinusSrcAlpha
  
          Pass{
          CGPROGRAM
          #pragma vertex vert_img
          #pragma fragment frag
          #pragma fragmentoption ARB_precision_hint_fastest
  
          sampler2D _MainTex;
          float4 _MainTex_ST;
      float3 _keyingColor;
      float _thresh; //0.8
      float _slope; //0.2
  
  #include "UnityCG.cginc"
  
      
  
  
      float4 frag(v2f_img i) : COLOR{
          float2 uv = TRANSFORM_TEX (i.uv, _MainTex);
          float3 input_color = tex2D(_MainTex, uv).rgb;
      float d = abs(length(abs(_keyingColor.rgb - input_color.rgb)));
      float edge0 = _thresh*(1 - _slope);
      float alpha = smoothstep(edge0,_thresh,d);
      return float4(input_color,alpha);
  
  
      }
  
          ENDCG
      }
      }
  
          FallBack"Unlit/Texture"
  }

That should work.

Comment
Add comment · Show 2 · 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 Edahsrevlis · Oct 18, 2016 at 08:58 PM 0
Share

So I just add float4 _$$anonymous$$ainTex_ST; ... float2 uv = TRANSFOR$$anonymous$$TEX (i.uv, $$anonymous$$ainTex);

It didn't work when I just added those but when I copy/pasted it worked. What else did you change?

avatar image Namey5 Edahsrevlis · Oct 19, 2016 at 04:45 AM 0
Share

$$anonymous$$ake sure that in your texture declaration you are using that variable rather than the input.

 //Use:
 float3 input_color = tex2D(_$$anonymous$$ainTex, uv).rgb;
 
 //Ins$$anonymous$$d of:
 float3 input_color = tex2D(_$$anonymous$$ainTex, i.uv).rgb;

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

Animated Texture based on Movement 0 Answers

How do I add multiple spritesheet/emission maps to Master Node in the Shader Editor? 0 Answers

How to get Terrain to respect shader offset 0 Answers

Scrolling Texture scrolls both maps. Only want single map to scroll? 0 Answers

Vertex Offset Shader deforming verts but not the rendered mesh 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