-5

How can I add a Texture to following shader given by Oculus API for fading :

Shader "Oculus/Unlit Transparent Color" {
Properties {
    _Color ("Main Color", Color) = (1,1,1,1)
    _MainTex ("Texture", 2D) = "white" {} //  I added this property to apply Texture. Where can I use it?
}

SubShader {
    Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    LOD 100
    Fog {Mode Off}
    Cull Off
    ZWrite Off
    Blend SrcAlpha OneMinusSrcAlpha
    Color [_Color]

    Pass {}
}
}
1
  • 12
    About all the downvoters If you are sure that This question does not show research effort;it is unclear and not useful. then you should justify that by adding a proper comment. If you are down voting the question because of meta effect and you can't downvote the answer because you don't want to lose the reputation then such herd behavior is not acceptable.
    – akash
    Aug 20, 2016 at 7:33

2 Answers 2

30

You can use the property you have added in the Pass {} which should contain at least a vertex and a fragment shader (or a surface shader).

In the pass you define a sampler2D with the same name as you property, so you can use it in your shader functions.

There are some examples shaders in unitys documentation: https://docs.unity3d.com/Manual/ShaderTut2.html
https://docs.unity3d.com/Manual/SL-SurfaceShaderExamples.html

Edit
There is also an example for shaders that are structured like yours:
https://docs.unity3d.com/Manual/ShaderTut1.html
According to this, you can add something like

SetTexture [_MainTex] {
    // some properties
}

So in a similar fashion like setting the Color. (Sorry I haven't used shaders structured like that yet, only vertex/fragment shaders, so you will have to try if it works and/or read up the examples provided by unity or wait for another answer of someone with more expertise :) )

1

As xyLe_ suggested in his post, In Pass{}block I added following code and it worked:

  SetTexture [_MainTex] {
            constantColor [_Color]
            Combine texture * primary DOUBLE, texture * constant
        }
2
  • 22
    You should have marked his answer as correct. If it was lacking anything you only needed to tell him, or add it in an edit.
    – user1228
    Aug 18, 2016 at 19:31
  • 1
    This answer has been discussed on meta: meta.stackoverflow.com/questions/332723/…
    – user000001
    Aug 19, 2016 at 5:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.