1
\$\begingroup\$

I want to have tiles with different sprites on the back. Initally I thought I will mix two textures together and then apply mixed texture to game object. I thought that should be easy, but google returned discussion about shaders and their superiority.

After reading about shaders I decided to use shaders instead.

Also I am using rather big sprites and I am scaling them down in shader. Kind of want to be sure that tiles look good on big screens also.

Also to be sure that game works on smaller screens - I change camera distance when game does not fit in the screen.

Everything is working nice when tile is "near". But when tile is far away it starts to show artifacts over all surface of tile. It appears that scaling is done not pixel by pixel but in some chunks.

Modified shader code (I commented out mainTex because it does not change anything from "bug" perspective):

Shader "Custom/TwoTextureShader3"

{

    Properties{
        _Color("Color", Color) = (1,1,1,1)
        _MainTex("Main Texture Color (RGB)", 2D) = "white" {}
        _MainTex2("Overlay Texture Color (RGB) Alpha (A)", 2D) = "white" {}
        _Glossiness("Smoothness", Range(0,1)) = 1
        _Metallic("Metallic", Range(0,1)) = 0.2
        _OffsX("Offset X", Range(-2, 2)) = 0
        _OffsY("Offset Y", Range(-2, 2)) = 0
        _Scale("Scale", Range(0, 10)) = 1
    }
        SubShader{
            Tags { "RenderType" = "Transparent"}

            LOD 200
            CGPROGRAM
            // Physically based Standard lighting model, and enable shadows on all light types

            #pragma surface surf Standard alpha fullforwardshadows

            // Use shader model 3.0 target, to get nicer looking lighting
            #pragma target 3.0


            sampler2D _MainTex;
            sampler2D _MainTex2;

            fixed _OffsX;
            fixed _OffsY;
            fixed _Scale;

            struct Input {
                float2 uv_MainTex;
                float2 _MainTex2;
            };



            half _Glossiness;
            half _Metallic;
            fixed4 _Color;

            void surf(Input IN, inout SurfaceOutputStandard o) {

                float4 mainTex = tex2D(_MainTex, IN.uv_MainTex);

                fixed2 offsetUV = fixed2(_OffsX, _OffsY);
                float4 overlayTex = tex2D(_MainTex2, IN.uv_MainTex * _Scale + offsetUV);

                half3 mainTexVisible = mainTex.rgb * (1 - overlayTex.a);
                half3 overlayTexVisible = overlayTex.rgb * (overlayTex.a);

                float3 finalColor = (mainTexVisible + overlayTexVisible) * _Color;

                //o.Albedo = finalColor.rgb;
                o.Albedo = overlayTex.rgb;

                o.Metallic = _Metallic;
                o.Smoothness = _Glossiness;
                o.Alpha = max(mainTex.a,overlayTex.a);
                //o.Alpha = 1;
            }
            ENDCG
        }
            FallBack "Diffuse"
}

Tiles looks good when they are near

Tiles looks good when they are near

Tiles looks awfull when they are far away

Tiles looks awfull when they are far away

Currently I am thinking about using two Meshes for single tile, so that game never hints about what is on the back of the tile even when graphics hardware really want to.

\$\endgroup\$
  • 1
    \$\begingroup\$ Looks like you're sampling outside the area of the texture with Clamp mode, so you're sampling the outermost row/column of the texture, and you don't have enough padding to keep this outermost row/column black/transparent when the image is downsampled. \$\endgroup\$ – DMGregory Nov 12 at 16:14
  • \$\begingroup\$ Wrap mode is clamp indeed. \$\endgroup\$ – Semiirs Nov 12 at 16:22
  • \$\begingroup\$ Adding transparent border around image solved the problem. But It kind of wastes "disk space" and player bandwidth a bit. \$\endgroup\$ – Semiirs Nov 12 at 17:18
  • \$\begingroup\$ @Semiirs Hello, and welcome to GameDev SE! Please provide an answer on how you got around this particular problem and pick the answer as well. It might help others and you'll get free internet points! \$\endgroup\$ – John Hamilton Nov 21 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.