Search Unity

Projector with surface shader?

Discussion in 'Shaders' started by Fredrum, Jul 12, 2014.

  1. Fredrum

    Fredrum

    Joined:
    Jan 12, 2013
    Posts:
    14
    Does anyone know of any examples of projecting textures in a Surface shader?
    (Unity Projectors stylee)

    All examples I am finding is from frag/vert shader examples and they are too different for me to figure out how to convert.

    Cheers
    Fred
     
  2. Fredrum

    Fredrum

    Joined:
    Jan 12, 2013
    Posts:
    14
    I figured it out. The key was to realise that I could and how to combine surface and vertex shader functions.
    I found that explained here, http://docs.unity3d.com/Manual/SL-SurfaceShaderExamples.html under 'Custom data computed per-vertex'.

    This is my current and working shader for those who are interested,


    Shader "Custom/fTestProj" {
    Properties {
    _Color ("MainColor", Color) = (1,1,1,1)
    _MainTex ("Base (RGB)", 2D) = "white" {}
    _AlphaTex ("Alpha (RGB)", 2D) = "white" {}
    }
    SubShader {
    Tags {"Queue"="Transparent" "IgnoreProjector"="False" "RenderType"="Transparent"}
    LOD 100
    Blend SrcAlpha OneMinusSrcAlpha

    CGPROGRAM
    #pragma surface surf Lambert vertex:fProjUVs
    #include "UnityCG.cginc"

    float4 _Color;
    sampler2D _MainTex;
    sampler2D _AlphaTex;
    uniform float4x4 _Projector;

    struct Input {
    float2 uv_MainTex;
    float4 posProj : TEXCOORD0; // position in projector space
    };

    void fProjUVs (inout appdata_full v, out Input o) {
    UNITY_INITIALIZE_OUTPUT(Input,o);
    o.posProj = mul(_Projector, v.vertex);
    }

    void surf (Input IN, inout SurfaceOutput o) {

    float4 projCol = tex2D(_MainTex , float2(IN.posProj) / IN.posProj.w) * _Color;
    float projAlpha = tex2D(_AlphaTex , float2(IN.posProj) / IN.posProj.w);

    o.Emission = projCol.rgb;
    o.Alpha = projAlpha.r;
    }
    ENDCG
    }
    }
    ------------------------------------------------------------------------------------------------------------------------------

    Sorry about the formatting. the code thing made it look weird too.

    Cheers
    Fred
     
    Last edited: Jul 13, 2014
    FM-Productions likes this.
  3. oleprinds

    oleprinds

    Joined:
    Mar 29, 2013
    Posts:
    4
    Thanks Fred for sharing.. In Unity3D v5 and up there seems to be compile issues..
    I solved it by adding .xy to the two lines that convert the float4 to a float2..


    Code (CSharp):
    1. float4 projCol = tex2D(_MainTex , float2(IN.posProj.xy) / IN.posProj.w) * _Color;
    2. float projAlpha = tex2D(_AlphaTex , float2(IN.posProj.xy) / IN.posProj.w);
     
    FM-Productions likes this.