Search Unity

Hololens custom diffuse lighting not working?

Discussion in 'VR' started by JScotty, Oct 17, 2016.

  1. JScotty

    JScotty

    Joined:
    Feb 3, 2014
    Posts:
    3
    Heya ,

    I am working with Hololens, and try to make an transparent object. I created an basic unlit shader with transparency supported.

    The only problem is that the Hololens does not support diffuse lighting?

    my code:

    Shader "Custom/Transparency"
    {
    Properties
    {
    _Color ("Color", Color) = (1,1,1,1)
    _MainTex ("Texture", 2D) = "white" {}
    _Transparency ("Transition Alpha", Range(0.0,1.0)) = 1
    _MaxDiffuse("MaxDiffuse", Range(0.0,1.0)) = 0.5
    }
    SubShader
    {
    Tags { "RenderType"="Transparent+100" }
    LOD 100
    Blend SrcAlpha OneMinusSrcAlpha
    ZWrite Off

    Pass
    {
    CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag

    #include "UnityCG.cginc"

    struct appdata
    {
    float4 pos : POSITION;
    float3 normal : NORMAL;
    float2 texcoord : TEXCOORD0;
    };

    struct v2f
    {
    float4 pos : SV_POSITION;
    float3 normal : NORMAL;
    float2 texcoord : TEXCOORD0;
    float4 posWorld : TEXCOORD1;
    };

    sampler2D _MainTex;
    float4 _MainTex_ST;
    float4 _Color;

    float _MaxDiffuse;
    float4 _LightColor0;

    float _Transparency;

    v2f vert (appdata v) {
    v2f o;
    o.pos = mul(UNITY_MATRIX_MVP, v.pos);
    o.normal = v.normal;
    o.texcoord = v.texcoord;
    return o;
    }

    fixed4 frag (v2f i) : SV_Target {
    float4 alpha = float4(1,1,1,_Transparency);
    fixed4 tex = tex2D(_MainTex, i.texcoord) * _Color * alpha;

    float3 normalDirection = normalize(i.normal);
    float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
    float3 diffuse = _LightColor0.rgb * max(_MaxDiffuse, dot(lightDirection, normalDirection));


    return tex * fixed4(diffuse,1.0);
    }
    ENDCG
    }
    }
    }
     
  2. JScotty

    JScotty

    Joined:
    Feb 3, 2014
    Posts:
    3
    I fixed my problem.
    I used unity's standard - Transparant-Diffuse (Alpha-Diffuse.shader) and added my own transparency property.

    Here my new code in case you got the same problem:

    Shader "Custom/Transparency"
    {
    Properties{
    _Color("Main Color", Color) = (1,1,1,1)
    _MainTex("Base (RGB) Trans (A)", 2D) = "white" {}
    _Transparency ("Transparency", Range(0.0,1.0)) = 1.0
    }

    SubShader{
    Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
    LOD 200

    CGPROGRAM
    #pragma surface surf Lambert alpha:fade

    sampler2D _MainTex;
    fixed4 _Color;
    float _Transparency;

    struct Input {
    float2 uv_MainTex;
    };

    void surf(Input IN, inout SurfaceOutput o) {
    fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    o.Albedo = c.rgb;
    o.Alpha = _Transparency;
    }
    ENDCG
    }

    Fallback "Legacy Shaders/Transparent/VertexLit"
    }