Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by plangiewicz · Jul 22, 2018 at 12:44 PM · shadershadersshader programmingtexture atlas

Blending texture array in shader without textures between.

Hello. I have texture2d array that has for example just colours (red, green, blue), and when i use it i want to blend just texture 1 and 3 but shader cannot do that because interpolation in UVs? I use it by SetUVS and just set texture index in 3rd uv value, then in shader get it.

 Shader "Custom/Tex2DArray" {
     Properties {
         _Color ("Color", Color) = (1,1,1,1)
         _MainTex ("Albedo (RGB)", 2DArray) = "white" {}
         _MainTex2 ("Albedo (RGB)", 2D) = "white" {}
         _Glossiness ("Smoothness", Range(0,1)) = 0.5
         _Metallic ("Metallic", Range(0,1)) = 0.0
         _SliceRange("Slices", Range(1,24)) = 1
         _UVScale("uv scale", Float) = 1.0
     }
     SubShader {
         Tags { "RenderType"="Opaque" }
         LOD 200
         Pass{
             CGPROGRAM
 
             #pragma vertex vert
             #pragma fragment frag
             #pragma multi_compile_fog
             #pragma target 3.5
 
             #include "UnityCG.cginc"
 
             struct appdata{
                 float4 vertex : POSITION;
                 float3 uv : TEXCOORD2;
             };
 
             struct v2f
             {
                 float3 uv : TEXCOORD2;
                 UNITY_FOG_COORDS(1)
                 float4 vertex : SV_POSITION;
             };
         
             UNITY_DECLARE_TEX2DARRAY (_MainTex);
             sampler2D _MainTex2;
             float4 _MainTex2_ST;
             half _Glossiness;
             half _Metallic;
             fixed4 _Color;
             float _SliceRange;
             float _UVScale;
 
             v2f vert (appdata v)
             {
                 v2f o;
                 o.vertex = UnityObjectToClipPos(v.vertex);
                 o.uv = v.uv;
                 return o;
             }
 
             fixed4 frag(v2f i) : SV_Target
             {
                 float f = floor(i.uv.z);
                 float c = ceil(i.uv.z);
                 float blendValue = i.uv.z - f;
                 fixed4 tex1 = UNITY_SAMPLE_TEX2DARRAY(_MainTex, float3(i.uv.xy, f));
                 fixed4 tex2 = UNITY_SAMPLE_TEX2DARRAY(_MainTex, float3(i.uv.xy, c));
                 fixed4 color = lerp (tex1, tex2, blendValue);
                 return color;
             }
 
             ENDCG
         }
     }
     FallBack "Diffuse"
 }

and it blend fine but i don't want textures between... like in image:

alt text

unity-shader.png (145.7 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 Hybridizer · Jan 26, 2019 at 08:44 PM

This problem was a headache for me for a long time, but I eventually found a (maybe not perfect but good enough) solution:

Use a fixed4 (I like vertex colors) to create a stepped array lookup value. Basically: float lookup = float(i.color.r 8 + i.color.g 4 + i.color.b 2 + i.color.a);* This allows up to 16 textures in an array, and works very well with Jason Booth's Vertex Painter asset. There are small things here and there that might prove annoying, but so far this is the simplest way to effectively achieve this result. Note that to achieve an edge fading effect like yours you'll need to run a second identical TexArray setup, using another fixed4 such as TexCoord1, and lerp between the two resulting fixed4s like in your given shader.

Hopefully either this is still relevant to your needs, or that someone else with the same issue finds it helpful.

Comment
Add comment · 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

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

144 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 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

Unlit Transparent With Color Support 1 Answer

create a texture with shader code? ( without using a texture ) 1 Answer

How to add Emission to my custom shader? 2 Answers

The Best Way To Make Stylised Grass in Unity? 0 Answers

Apply an affine transformation 2 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