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. Dismiss Notice

Project texture onto plane

Discussion in 'Shaders' started by Epictickle, Mar 18, 2014.

  1. Epictickle

    Epictickle

    Joined:
    Aug 12, 2012
    Posts:
    431
    So, I have a projector in my scene, and I want it to project a texture straight down, kind of like an AOE Spell effect.. How would I do this? I've tried all the shaders in the Projectors package, and am now at a loss on how to do this.. I imported a texture that I want, and I ticked the "Alpha is Transparent" tickmark so that (i think) all the white space in the texture will be transparent.. But it doesn't do anything.. My texture is still one big white square with the texture centered on it... I don't want this..

    Here is my texture:
    $WoW___Casting_Circle_by_clampfan101.jpg



    Here is the effect I'm trying to achieve:
    $2789-projection+texture.png



    And here is my shader (note i did not make this shader, and I am HORRIBLE at shader cration):
    Code (csharp):
    1.  
    2. Shader "Projector/Projector MultiplyCG"
    3.  
    4. {
    5.  
    6.     Properties
    7.  
    8.     {
    9.  
    10.         _Color ("Main Colour", Color) = (1,1,1,0)
    11.  
    12.         _ShadowTex ("Cookie", 2D) = "gray" { TexGen ObjectLinear }
    13.  
    14.     }
    15.  
    16.  
    17.  
    18.     Subshader
    19.  
    20.     {
    21.  
    22.         Tags { "RenderType"="Transparent"  "Queue"="Transparent+100"}
    23.  
    24.         Pass
    25.  
    26.         {
    27.  
    28.             ZWrite Off
    29.  
    30.             Offset -1, -1
    31.  
    32.  
    33.  
    34.             Fog { Mode Off }
    35.  
    36.            
    37.  
    38.             AlphaTest Less 1
    39.  
    40.             ColorMask RGB
    41.  
    42.             Blend One SrcAlpha
    43.  
    44. CGPROGRAM
    45.  
    46. #pragma vertex vert
    47.  
    48. #pragma fragment frag
    49.  
    50. #pragma fragmentoption ARB_fog_exp2
    51.  
    52. #pragma fragmentoption ARB_precision_hint_fastest
    53.  
    54. #include "UnityCG.cginc"
    55.  
    56.  
    57.  
    58. struct v2f
    59.  
    60. {
    61.  
    62.     float4 pos : SV_POSITION;
    63.  
    64.     float4 uv_Main     : TEXCOORD0;
    65.  
    66. };
    67.  
    68.  
    69.  
    70. sampler2D _ShadowTex;
    71.  
    72. float4 _Color;
    73.  
    74. float4x4 _Projector;
    75.  
    76.  
    77.  
    78. v2f vert(appdata_tan v)
    79.  
    80. {
    81.  
    82.     v2f o;
    83.  
    84.     o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
    85.  
    86.     o.uv_Main = mul (_Projector, v.vertex);
    87.  
    88.     return o;
    89.  
    90. }
    91.  
    92.  
    93.  
    94. half4 frag (v2f i) : COLOR
    95.  
    96. {
    97.  
    98.     half4 tex =tex2Dproj(_ShadowTex, UNITY_PROJ_COORD(i.uv_Main)) * _Color;
    99.  
    100.     tex.a = 1-tex.a;
    101.  
    102.     return tex;
    103.  
    104. }
    105.  
    106. ENDCG
    107.  
    108.         }
    109.  
    110.     }
    111.  
    112. }
    113.  
     
    Last edited: Mar 18, 2014
  2. drudiverse

    drudiverse

    Joined:
    May 16, 2013
    Posts:
    218
    There is a simple unity projection function for shaping spotlights and light outputs. It is simple and isnt really made for stained glass and detail afaik.

    The effect that you wish to achieve is very simple though!

    add the texture on top of the other texture for the game...

    other options are:

    Make your shader a simple half transparent object, with the green channel resulting in alpha = 1, and anywhere with blue/red makes alpha = 0. So you have a plane with your transparent shape texture... how to project it onto the ground? just read the mesh information where you want to project the texture, copy the 10-20 triangles that coincide with the place the new texture should be, make a little object with those triangles, apply spell texture to it, and make it float a tiny bit above the floor. it will look exactly like your pic. time for making the mesh on the pc is about 100fps. Takes an afternoon to program it though if you are familiar with mesh. if you want the object to move around the floor, you would have scan the triangles and create a triangle overlay mesh every frame, it's possible, and its useful so could make an asset store version.


    Actually a simpler and faster way to do it, is make a projection plane with say 100x100 triangles using create_plane, and where you want to place it flat on the game, use a circle to find all the vertices on the terrain and modify your 100x100 plane to be roughly same as those triangles heights as much as possible but always abit above, it should look very convincing and no need to write any triangle indices, only change height of vertices. It's abit of a task, perhaps search asset store for projection.
     
    Last edited: Mar 24, 2014
  3. TIV-Games

    TIV-Games

    Joined:
    Jun 13, 2015
    Posts:
    12
    Could you please explain this in more depth or point somewhere where I could learn all that in every detail since i'm not familiar at all with mesh programming in c#?
    because i'm trying to achieve a very similar effect but with bullet holes and I would like to adjust the points (vertices or whatver its called) to sort of wrap around the corners and smooth surfaces. thanks
     
  4. Phantom_X

    Phantom_X

    Joined:
    Jul 11, 2013
    Posts:
    305
    Just use unity projector with Projector/Light shader. your texture will need to have a black background instead of the white one you have now.

    Set your texture wrap mode to clamp.
     
  5. Zicandar

    Zicandar

    Joined:
    Feb 10, 2014
    Posts:
    388
    First of all white is not the same as transparency.
    You'll need an actual transparent texture to start with, then if you want to place it on non-flat geometry, I would suggest a decal system.
    (From when I tested Unity's projectors in 4.x, they were HORRENDOUS for performance!)
     
  6. TIV-Games

    TIV-Games

    Joined:
    Jun 13, 2015
    Posts:
    12
    Thanks for replying! What I was interested in is the last paragraph YanVerde wrote where as I understood he were talking about getting individual vertexes on the plane and adjusting their position in order to change the shape of the plane to perfectly suit the surface it is on eg. corner of a wall. I though of using this sort of technique on the plane for the bullet holes to wrap around objects that aren't flat or around corners.
     
  7. Zicandar

    Zicandar

    Joined:
    Feb 10, 2014
    Posts:
    388
    https://www.assetstore.unity3d.com/en/#!/content/22867

    And lots of other assets for this exist already on the asset store. I'd suggest finding something simple/cheap and seeing if that is enough for you.
    That or Deferred decals, although using them will have other limitations. (They do work great for unlit / pure emissive decals).
     
  8. TIV-Games

    TIV-Games

    Joined:
    Jun 13, 2015
    Posts:
    12
    I'm aware of the assets on asset store however my aim is to program it all myself.
     
  9. Zicandar

    Zicandar

    Joined:
    Feb 10, 2014
    Posts:
    388
    http://forum.unity3d.com/threads/deferred-decals.337591/#post-2562905

    I put up some code for deferred decals there, as you only seem to want emissive, (at least for this type of effect), it should be modifiable to handle forward mode also. (You need to get depth info, but on non-mobiles that can be gotten from the hardware buffer, not sure about mobiles tho).
    Biggest benefit of this solution is that you can easily apply them to any surface, no matter how complex. And if your decals are not fixed in place but constantly moving, probably a lot better for the CPU as you don't need to duplicate all geometry constantly.
    Sure, it wouldn't be coding it from scratch yourself, but then again, whats the point? Or you can use it as a basis and create your own "deferred" decal solution :)
     
    TIV-Games likes this.
  10. TIV-Games

    TIV-Games

    Joined:
    Jun 13, 2015
    Posts:
    12
    Thanks will look at it