Tom Perm
Tom Perm

Reputation: 19

How to implement a texture atlas shader?

I would like to know how to use shaders with texture atlases. Does anyone know how I can get only one part of the image to be drawn?

Current View :

image1


Desired View :

image2


Shader Source code :

Shader "Test/WeaponMaker" {                             
    Properties {                                                             
        _WeaponTex ("Weapon Box Background", 2D) = "white" {}        
        _BoxTex ("Weapon", 2D) = "white" {} 
        _WeaponColor("Weapon Color", color) = (0.0, 0.0, 0.0, 0.0)
    }
    SubShader {
        Tags { "RenderType"="Opaque" }                            
        CGPROGRAM
        #pragma surface surf NoLighting
        sampler2D _WeaponTex;
        sampler2D _BoxTex;
        half4 _WeaponColor;
        struct Input {
            float2 uv_WeaponTex;
            float2 uv_BoxTex;
        };    
        fixed4 LightingNoLighting(SurfaceOutput output, fixed3 lightDir, fixed atten){
            fixed4 light = 0;        
            light.rgb = output.Albedo;
            light.a = output.Alpha;
            return light;
        }
        half2 CalculatorBoxUV(half2 uv, half2 weaponPosition, half2 scale){    
            uv = (uv - weaponPosition) * scale;
            return uv;    
        }
        void surf (Input IN, inout SurfaceOutput output) {
            half4 weaponTex = tex2D (_WeaponTex, IN.uv_WeaponTex);
            half4 boxTex = 0;
            half4 finalBoxTex = 0;
            boxTex = tex2D(_BoxTex, CalculatorBoxUV(IN.uv_BoxTex, half2(0.35, 0.35), half2(3, 3)));
            finalBoxTex.rgb += boxTex.rgb;
            finalBoxTex.a += boxTex.r;
            output.Albedo = lerp(weaponTex.rgb, finalBoxTex.rgb * _WeaponColor.rgb, finalBoxTex.a);
        }
        ENDCG
    }
    FallBack "Diffuse"
}

Upvotes: 2

Views: 1339

Answers (0)

Related Questions