Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Need Help Creating a Transparent Hexagon Prism

Discussion in 'General Graphics' started by Phanixis, Feb 10, 2016.

  1. Phanixis

    Phanixis

    Joined:
    Oct 19, 2015
    Posts:
    27
    I have a 3d model of a hexagon that I have applied a hexagon border texture to (the texture is just a hexagon outline in .jpg format). Currently it appears as a white hexagon with a black border along the edges when viewed from above. So far so good. Now I want to make everything about the hexagon accept the border/texture transparent, so the player can only see the outline of the shape and not the interior. How do I go about doing this? I have set the material to unlit/transparent, but that doesn't seem to make the hexagon prism the material is applied to transparent. I would greatly appreciate any assistance that can be provided in this regard.

    Thanks for the help.
     
  2. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Make a texture with transparency and set it to transparent.
     
  3. Phanixis

    Phanixis

    Joined:
    Oct 19, 2015
    Posts:
    27
    The transparency is a property of the texture, not the material? How would I set the texture, or in this case some of the texture, to transparent? All I did to create the texture was create a rather thick outline of a hexagon in .png format.
     
  4. smd863

    smd863

    Joined:
    Jan 26, 2014
    Posts:
    292
    Create an alpha channel in an image editing program, and that will control the transparency. In Gimp, for example, you go to Layer->Add Alpha Channel. I don't have it open right now, but it is something like that and very easy to do.

    If you only need a black outline, you can save memory by importing a grayscale texture as "Alpha 8" and selecting "Alpha from Grayscale" and "Alpha is Transparency". You will want to switch the outline to white (fully opaque), and the center to black (full transparent). Instead of using the full 32-bits (or compressing the texture) you will get a perfectly clean 8-bit alpha.

    Then use this shader:
    Code (csharp):
    1.  
    2. // Unlit alpha-blended shader.
    3. // - no lighting
    4. // - no lightmap support
    5. // - no per-material color
    6.  
    7. Shader "Unlit/AlphaOnly" {
    8. Properties {
    9.     _MainTex ("Trans (A)", 2D) = "white" {}
    10.     _Color ("Color", Color) = (0,0,0,0)
    11. }
    12.  
    13. SubShader {
    14.     Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    15.     LOD 100
    16.  
    17.     ZWrite Off
    18.     Blend SrcAlpha OneMinusSrcAlpha
    19.  
    20.     Pass {
    21.         CGPROGRAM
    22.             #pragma vertex vert
    23.             #pragma fragment frag
    24.             #pragma multi_compile_fog
    25.        
    26.             #include "UnityCG.cginc"
    27.  
    28.             struct appdata_t {
    29.                 float4 vertex : POSITION;
    30.                 float2 texcoord : TEXCOORD0;
    31.             };
    32.  
    33.             struct v2f {
    34.                 float4 vertex : SV_POSITION;
    35.                 half2 texcoord : TEXCOORD0;
    36.                 UNITY_FOG_COORDS(1)
    37.             };
    38.  
    39.             sampler2D _MainTex;
    40.             float4 _MainTex_ST;
    41.             fixed4 _Color;
    42.        
    43.             v2f vert (appdata_t v)
    44.             {
    45.                 v2f o;
    46.                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    47.                 o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
    48.                 UNITY_TRANSFER_FOG(o,o.vertex);
    49.                 return o;
    50.             }
    51.        
    52.             fixed4 frag (v2f i) : SV_Target
    53.             {
    54.                 fixed4 col = tex2D(_MainTex, i.texcoord);
    55.                 UNITY_APPLY_FOG(i.fogCoord, col);
    56.                 return fixed4(_Color.rgb, col.a); // Output color, and the alpha channel of the texture
    57.             }
    58.         ENDCG
    59.     }
    60. }
    61.  
    62. }
    63.  
    This is just the regular Alpha-Unlit shader except it outputs only a solid color, and only uses the texture's alpha channel for transparency. If you want to texture it, you can use a regular 32-bit texture with the regular Alpha-Unlit shader.
     
  5. Phanixis

    Phanixis

    Joined:
    Oct 19, 2015
    Posts:
    27
    CaptainScience this is exactly the sort of thing I was looking for. So the first step is to stop creating my textures in MS Paint and get a program like Gimp and add some alpha channels.

    Ok, I have added the alpha channel in Gimp and imported the new texture to unity, and now the tiles work perfectly. I didn't even need your script, which is a good thing because that script was generating over a hundred error messages.

    Thanks for the help.
     
    Last edited: Feb 13, 2016
  6. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Yes, MS paint is useless for anything to do with alpha channels. ;)
    And that was a shader file, not a script... :D that would be why it threw a million errors.