Search Unity

  1. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice
  2. We're running great holiday deals on subscriptions, swag and Asset Store packages! Take a peek at this blog for more information!
    Dismiss Notice
  3. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  4. Unity 2017.2 is now released.
    Dismiss Notice
  5. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  6. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  8. Unity 2017.3 beta is now available for download.
    Dismiss Notice

Reflection probes in custom shader?

Discussion in 'Shaders' started by neginfinity, Apr 30, 2015.

  1. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    6,107
    How do I access information from reflection probe (meaning its cubemap) in custom vertex/fragment shader? (NOT a surface shader)

    I don't see any information about that anywhere. There is one example here, but it is surface shader and does not explain how to use reflection probe in custom vertex/fragment shader (I get white cubemap by default)

    I'd expect the engine to automatically pass me data if I declare _Cube variable, but that doesn't seem to happen.
     
  2. colin299

    colin299

    Joined:
    Sep 2, 2013
    Posts:
    122
    could you pass the cubemap from material?
    Code (CSharp):
    1.  Properties {
    2.          _MainTex("Main Map", 2D) = ""{}
    3.       _Cube("Reflection Map", Cube) = "" {}
    4.    }
    Code (CSharp):
    1. uniform samplerCUBE _Cube;
    Code (CSharp):
    1. texCUBE(_Cube, reflectedDir)
     
  3. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    6,107
    No, this doesn't work - I get white cubemap when I do that.
     
  4. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    6,107
    Here is sample shader:

    Code (csharp):
    1.  
    2. Shader "Custom/CubemapDebug" {
    3.     Properties {
    4.         _Cube("Reflection Map", Cube) = "" {}
    5.     }
    6.  
    7. SubShader {
    8.     Tags { "RenderType"="Opaque" }
    9.  
    10.         pass
    11.         {      
    12.         Tags { "LightMode"="Always"}
    13.  
    14.         CGPROGRAM
    15.  
    16.         #pragma target 3.0
    17.  
    18.         #pragma vertex vert
    19.         #pragma fragment frag
    20.         #include "UnityCG.cginc"
    21.  
    22.         sampler2D _DiffuseTexture;
    23.         samplerCUBE _Cube;
    24.  
    25.         struct v2f{
    26.             float4 pos : SV_POSITION;
    27.             float3 coord: TEXCOORD0;
    28.         };
    29.  
    30.         v2f vert(appdata_base v){
    31.             v2f o;
    32.  
    33.             o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    34.             o.coord = v.normal;
    35.  
    36.             return o;
    37.         }
    38.  
    39.         float4 frag(v2f i) : COLOR{
    40.             float4 finalColor = texCUBE(_Cube, i.coord);
    41.             return finalColor;
    42.         }
    43.  
    44.             ENDCG
    45.         }
    46.    }
    47.     FallBack Off
    48. }
    49.  
    50.  
    And here is result:
    [​IMG]

    As you can see, reflection probe is active and and rendered, but cubemap is not being passed into shader.
     
  5. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,401
    Download the builtin shaders sources and then check the source for Default Sources extra/Cubemaps
    There you will see a function UNITY_DECLARE_TEXCUBE() and the helpers are inside UnityShaderVariables.cginc file
     
  6. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    6,107
    UNITY_DECLARE_TEXCUBE is used 4 times total in the whole shader library, and it doesn't actually do anything useful - it declares sampler and cubetexture, and using this macros changes nothing.

    The only helper is SampleCubeReflection, which is irrelevant to the problem.
    The problem is not computing coordinates for cubemap. The problem is that cubemap is not set by unity.

    Once again, here's the test shader:

    Code (csharp):
    1.  
    2. Shader "Custom/CubemapDebug" {
    3.     Properties {
    4.         _Cube("Reflection Map", CUBE) = "" {}
    5.     }
    6.  
    7. SubShader {
    8.     Tags { "RenderType"="Opaque" }
    9.  
    10.         pass
    11.         {      
    12.         Tags { "LightMode"="Always"}
    13.  
    14.         CGPROGRAM
    15.  
    16.         #pragma target 3.0
    17.  
    18.         #pragma vertex vert
    19.         #pragma fragment frag
    20.         #include "UnityCG.cginc"
    21.  
    22.         sampler2D _DiffuseTexture;
    23.         //half4 _Cube_HDR;
    24.         //UNITY_DECLARE_TEXCUBE(unity_SpecCube0);
    25.         //UNITY_DECLARE_TEXCUBE(_Cube);
    26.         samplerCUBE _Cube;
    27.  
    28.         struct v2f{
    29.             float4 pos : SV_POSITION;
    30.             float3 coord: TEXCOORD0;
    31.         };
    32.  
    33.         v2f vert(appdata_base v){
    34.             v2f o;
    35.  
    36.             o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    37.             o.coord = v.normal;
    38.  
    39.             return o;
    40.         }
    41.  
    42.         float4 frag(v2f i) : COLOR{
    43.             float3 coords = normalize(i.coord);
    44.             float4 finalColor = 1.0;
    45.             finalColor = texCUBE(_Cube, coords);
    46.             return finalColor;
    47.         }
    48.  
    49.             ENDCG
    50.         }
    51.    }
    52.     FallBack Off
    53. }
    54.  
    55.  
     
  7. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,401
    Hmm you are right. It seems irrelevant.
    Well, i wondered this myself so what i did was create a default shader in unity and check its source.
    And here is the relevant part, you better check the source for default shader too and you can see whats going on there:

    Code (CSharp):
    1.   // Call GI (lightmaps/SH/reflections) lighting function
    2.   UnityGIInput giInput;
    3.   UNITY_INITIALIZE_OUTPUT(UnityGIInput, giInput);
    4.   giInput.light = gi.light;
    5.   giInput.worldPos = worldPos;
    6.   giInput.worldViewDir = worldViewDir;
    7.   giInput.atten = atten;
    8.   #if defined(LIGHTMAP_ON) || defined(DYNAMICLIGHTMAP_ON)
    9.     giInput.lightmapUV = IN.lmap;
    10.   #else
    11.     giInput.lightmapUV = 0.0;
    12.   #endif
    13.   #if UNITY_SHOULD_SAMPLE_SH
    14.     giInput.ambient = IN.sh;
    15.   #else
    16.     giInput.ambient.rgb = 0.0;
    17.   #endif
    18.   giInput.probeHDR[0] = unity_SpecCube0_HDR;
    19.   giInput.probeHDR[1] = unity_SpecCube1_HDR;
    20.   #if UNITY_SPECCUBE_BLENDING || UNITY_SPECCUBE_BOX_PROJECTION
    21.     giInput.boxMin[0] = unity_SpecCube0_BoxMin; // .w holds lerp value for blending
    22.   #endif
    23.   #if UNITY_SPECCUBE_BOX_PROJECTION
    24.     giInput.boxMax[0] = unity_SpecCube0_BoxMax;
    25.     giInput.probePosition[0] = unity_SpecCube0_ProbePosition;
    26.     giInput.boxMax[1] = unity_SpecCube1_BoxMax;
    27.     giInput.boxMin[1] = unity_SpecCube1_BoxMin;
    28.     giInput.probePosition[1] = unity_SpecCube1_ProbePosition;
    29.   #endif
    30.   LightingStandard_GI(o, giInput, gi);
    31.  
    32.   // realtime lighting: call lighting function
    33.   c += LightingStandard (o, worldViewDir, gi);
    34.   UNITY_APPLY_FOG(IN.fogCoord, c); // apply fog
    35.   UNITY_OPAQUE_ALPHA(c.a);
    36.   return c;
     
    macrod likes this.
  8. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    6,107
    In this code fragment, no cubemaps are being accessed. Cubemaps are read using texCUBE call. Code fragment you quoted has none of them.
    Also from the looks of it, it may or may not be portion that actually computes GI, and not the one that uses it in the scene.

    Anyway. I did see the references to unity_SpecCube0/1 which (by the looks at it), and they can be accessed via "texCUBE(samplerunity_SpecCube0, coords)", BUT they're white and unset when I try to read them in my shader.
     
  9. colin299

    colin299

    Joined:
    Sep 2, 2013
    Posts:
    122
    hello @neginfinity ,
    i can see from your fragment shader code,
    Code (CSharp):
    1. finalColor = texCUBE(_Cube, coords);
    2. //coords variable is normal in your code,
    3. //texCUBE actually wants (cubemap,reflection vector)
    Calculating reflection vector needs "normal" & "view direction vector".

    This is the cubemap code i use, it is very simple.
    Code (CSharp):
    1. Shader "C4Cat/CubeMap" {
    2.    Properties {
    3.          _MainTex("Main Map", 2D) = ""{}
    4.       _Cube("Reflection Map", Cube) = "" {}
    5.       _reflectionAmount("Reflection amount", Range (0.0,1.0)) = 0.3
    6.    }
    7.    SubShader {
    8.       Pass {  
    9.          CGPROGRAM
    10.          #pragma vertex vert
    11.          #pragma fragment frag
    12.          #include "UnityCG.cginc"
    13.          // User-specified uniforms
    14.          uniform samplerCUBE _Cube;
    15.          uniform sampler _MainTex;  
    16.          uniform fixed _reflectionAmount;
    17.          struct vertexInput {
    18.             float4 vertex : POSITION;
    19.             float3 normal : NORMAL;
    20.             float2 uv : TEXCOORD;
    21.          };
    22.          struct vertexOutput {
    23.             float4 pos : SV_POSITION;
    24.             float3 normalDir : TEXCOORD0;
    25.             float3 viewDir : TEXCOORD1;
    26.             float2 uv : TEXCOORD2;
    27.          };
    28.          vertexOutput vert(vertexInput input)
    29.          {
    30.             vertexOutput output;
    31.            
    32.              output.uv = input.uv;
    33.             float4x4 modelMatrix = _Object2World;
    34.             float4x4 modelMatrixInverse = _World2Object;
    35.                // multiplication with unity_Scale.w is unnecessary
    36.                // because we normalize transformed vectors
    37.             output.viewDir = mul(modelMatrix, input.vertex).xyz
    38.                - _WorldSpaceCameraPos;
    39.             output.normalDir = normalize(
    40.                mul(float4(input.normal, 0.0), modelMatrixInverse).xyz);
    41.             output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
    42.             return output;
    43.          }
    44.          float4 frag(vertexOutput input) : COLOR
    45.          {
    46.             float3 reflectedDir =
    47.                reflect(input.viewDir, normalize(input.normalDir));
    48.             return lerp(tex2D(_MainTex,input.uv),texCUBE(_Cube, reflectedDir),_reflectionAmount);
    49.          }
    50.          ENDCG
    51.       }
    52.    }
    53. }
     
  10. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,401
    Well, dont get focused on to see some simple texcube function somewhere. The thing i pasted actually shows how it Works. And those references you saw are actually have to be initialized first. Dig deeper inside UnityGlobalIllumination.cginc file and find out what UnityGIInput does.
     
  11. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    6,107
    Sigh. I'm NOT trying to calculate reflection vector. I KNOW how to calculate reflection vector, thank you.
    I'm trying to read data from cubemap created by reflection probe.
    To read data from cubemap, you need 3 component vector (or 4, if you want to specify lod) and a cubemap.
    The problem is that my cubemap is blank and unity does not seem to pass it automatically AND there is zero information on the subject.

    In your example, is cubemap computed by reflection probe? If not, then that isn't what I'm looking for.

    On my system your shader produces white object with no reflection. If you have to specify cubemap manually for it, then again that's not what I'm looking for.

    Umm, no offense, are you making an educated guess here?

    Unless I'm mistaken, standard shader is somewhat of a special case, just because it can do something, doesn't mean the data for that will be accessible in custom vertex/fragment shader.
     
  12. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,401
    No offense taken, you seem to know your ways around anyways. Soo, good luck :)
     
  13. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    6,107
    Well, there is accessible data regarding reflection problems.

    Code (csharp):
    1.  
    2. UNITY_DECLARE_TEXCUBE(unity_SpecCube0);
    3. UNITY_DECLARE_TEXCUBE(unity_SpecCube1);
    4.  
    5. CBUFFER_START(UnityReflectionProbes)
    6.     float4 unity_SpecCube0_BoxMax;
    7.     float4 unity_SpecCube0_BoxMin;
    8.     float4 unity_SpecCube0_ProbePosition;
    9.     half4  unity_SpecCube0_HDR;
    10.  
    11.     float4 unity_SpecCube1_BoxMax;
    12.     float4 unity_SpecCube1_BoxMin;
    13.     float4 unity_SpecCube1_ProbePosition;
    14.     half4  unity_SpecCube1_HDR;
    15. CBUFFER_END
    16.  
    17.  
    I can read probe position/size/etc.

    However, actual textures (samplerunity_SpecCube0/samplerunity_SpecCube1) are white and apparently are not set.
     
  14. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    6,107
    Got it.

    Code (csharp):
    1.  
    2. Shader "Custom/CubemapDebug" {
    3.     Properties {
    4.         _Cube("Reflection Map", CUBE) = "" {}
    5.     }
    6.  
    7. SubShader {
    8.     Tags { "RenderType"="Opaque" }
    9.  
    10.     pass
    11.     {      
    12.         //Tags { "LightMode"="ForwardAdd"}
    13.  
    14.         CGPROGRAM
    15.  
    16.         #pragma target 3.0
    17.  
    18.         #pragma vertex vert
    19.         #pragma fragment frag
    20.         #include "UnityCG.cginc"
    21.  
    22.         sampler2D _DiffuseTexture;
    23.         //half4 _Cube_HDR;
    24.         //UNITY_DECLARE_TEXCUBE(unity_SpecCube0);
    25.             //UNITY_DECLARE_TEXCUBE(_Cube);
    26.         samplerCUBE _Cube;
    27.  
    28.         struct v2f{
    29.             float4 pos : SV_POSITION;
    30.             float3 coord: TEXCOORD0;
    31.         };
    32.  
    33.             v2f vert(appdata_base v){
    34.                 v2f o;
    35.  
    36.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    37.                 o.coord = v.normal;
    38.        
    39.                 return o;
    40.             }
    41.  
    42.             float4 frag(v2f i) : COLOR{
    43.                 float3 coords = normalize(i.coord);
    44.                 float4 finalColor = 1.0;
    45.                 float4 val = UNITY_SAMPLE_TEXCUBE(unity_SpecCube0, coords);
    46.                 finalColor.xyz = DecodeHDR(val, unity_SpecCube0_HDR);
    47.                 finalColor.w = 1.0;              
    48.                 return finalColor;
    49.             }
    50.  
    51.             ENDCG
    52.         }
    53.     }
    54.     FallBack Off
    55. }
    56.  
    [​IMG]

    Completely undocumented, as usual.

    Question closed.
     
    Lipoly, JonathanPearl, ds44 and 4 others like this.
  15. colin299

    colin299

    Joined:
    Sep 2, 2013
    Posts:
    122
    @neginfinity
    Yes, it is, I bake the cubemap using unity5 reflection probe.
    Are you trying to do something totally different? because I can not understand your problem.
     
  16. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    6,107
    On my machine, your shader produces gray sphere. Which means that unity does not automatically assign environmental cubemap to "_Cube("Reflection Map", Cube)" slot (which is what I would expect it to do).

    [​IMG]

    Which means that, assuming cubemaps are supposed to work out of the box, there's a missing step in configuration somewhere.

    Anyway. If it really works on your machine I'd appreciate if you (or someone else) uploaded working example with one object and cubemap somewhere. That way I'd be able to figure out what's missing.

    In my case it works only if I do this:
    Code (csharp):
    1.  
    2.          float4 frag(vertexOutput input) : COLOR
    3.          {
    4.             float3 reflectedDir =
    5.                reflect(input.viewDir, normalize(input.normalDir));
    6.                float4 c = UNITY_SAMPLE_TEXCUBE(unity_SpecCube0, reflectedDir);
    7.             return lerp(tex2D(_MainTex,input.uv), c,_reflectionAmount);
    8.          }
    9.  
    10.  
     
  17. Flailer

    Flailer

    Joined:
    Apr 1, 2014
    Posts:
    66
    Maybe just to clarify what the guy above is saying.

    The example given here uses the normal of the model, which was done presumably for speed, and just to get the cubemap to show, if you want to have a reflection in the same way as the Unity Standard shader it requires using a reflection vector as input texcoords.
     
  18. NorthernVisionStudio

    NorthernVisionStudio

    Joined:
    Oct 18, 2013
    Posts:
    60
    There's actually another way, which incorporates multiple reflection probes and the different levels of gloss, as seen on Standard Shader.

    I'm using a surface shader... so in Unity5 there is a new method for lighting that you must implement:

    Code (CSharp):
    1. inline void LightingXXXXXX_GI (MarmosetOutput s, UnityGIInput data, inout UnityGI gi) {
    2.     data.boxMax[0] = unity_SpecCube0_BoxMax;
    3.     data.boxMin[0] = unity_SpecCube0_BoxMin;
    4.     data.probePosition[0] = unity_SpecCube0_ProbePosition;
    5.     data.probeHDR[0] = unity_SpecCube0_HDR;
    6.  
    7.     data.boxMax[1] = unity_SpecCube1_BoxMax;
    8.     data.boxMin[1] = unity_SpecCube1_BoxMin;
    9.     data.probePosition[1] = unity_SpecCube1_ProbePosition;
    10.     data.probeHDR[1] = unity_SpecCube1_HDR;
    11.  
    12.     gi = UnityGlobalIllumination (data, 1.0, 0.0, s.Normal, true);
    13.    
    14. }
    XXXX is the name used to specify the lighting model in the .shader file. This would have to be a custom lighting model.

    Then in your forward and deferred lighting functions, you use these declarations:

    Code (CSharp):
    1. inline half4 LightingXXXXXXX_Deferred (Output s, half3 viewDir, UnityGI gi, out half4 outDiffuseOcclusion, out half4 outSpecSmoothness, out half4 outNormal) {
    You can vary the parameters, but I'm not sure exactly how that works. Anyways, in that function (this one, for example), you have access to the processed results for GI and reflection probes.

    GI is under UnityGI.indirect.diffuse
    Reflection is under UnityGI.indirect.specular.

    Sorry I don't have time to be more detailed. I'll be glad to help out if anyone needs a specific concern answered.
     
    laurentlavigne likes this.
  19. Sparrowfc

    Sparrowfc

    Joined:
    Jan 31, 2013
    Posts:
    95
    Bravo, It's exactly what I want. Just don't know why calling texCUBE(unity_SpecCube0, ..) directly won't work
     
  20. FuzzyShan

    FuzzyShan

    Joined:
    Jul 30, 2012
    Posts:
    136
    invalid subscript for when trying to write fragment shader viewDir -_-'...
     
  21. Flailer

    Flailer

    Joined:
    Apr 1, 2014
    Posts:
    66
    Check you've actually got it defined, or that you are using input not i or something like that to reference the v2f struct.
     
  22. HuikuShih

    HuikuShih

    Joined:
    Nov 8, 2012
    Posts:
    11
    calculate your viewDir in vertex shader like this:
    Code (CSharp):
    1. float3 viewDir = WorldSpaceViewDir(v.vertex);
    And USE UNITY_SAMPLE_TEXCUBE_LOD to control the backed cube map LOD
     
  23. MrJimGamer

    MrJimGamer

    Joined:
    Nov 24, 2015
    Posts:
    11
    Hello dudes i need help on using reflection probs on a custom surface shader with custom lighting function.Please HELP,HEEEEELP.
     
  24. NorthernVisionStudio

    NorthernVisionStudio

    Joined:
    Oct 18, 2013
    Posts:
    60
    why dont you show us what you have so far. be happy to help
     
  25. MrJimGamer

    MrJimGamer

    Joined:
    Nov 24, 2015
    Posts:
    11
    I will not post the code because i plan to sell it because i combine: Albedo,Normal1,Normal2,PBR GGX Gloss,PBR GGX Specular,Subsurface Scattering,Occlusion,Alpha,Custom Real Parallax,Emission,Rim,Secondary Albedo and Transparency.So practically you can make anything with this shader from skin material to rock material.(The image is from a resent test of the shader).I just want the code on how to access reflection probs from surface shader,as you can see for now i use a cubemap. Screenshot (19).png
     
  26. NorthernVisionStudio

    NorthernVisionStudio

    Joined:
    Oct 18, 2013
    Posts:
    60
    The way I access it from the surface shader is to render the reflection probe to a cube map, rather than the built-in cubemap. Then you can sample it with the mip levels being the convolution levels.

    The problem is that with Deferred rendering, Unity automatically blends reflection probe cube maps per pixel. This will not happen automatically in the surface shader. Something you will have to solve. Does your shader support Deferred and Forward?

    Lets start there and see what you understand from this.
     
  27. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    6,107
    Good luck.

    However, what, exactly would stop someone from looking up corresponding equations and making alternative, and releasing it for free?

    In any case, I already posted how to access cubemap before:

    Code (csharp):
    1.  
    2. float4 frag(vertexOutput input) : COLOR
    3.          {
    4.             float3 reflectedDir =
    5.                reflect(input.viewDir, normalize(input.normalDir));
    6.                float4 c = UNITY_SAMPLE_TEXCUBE(unity_SpecCube0, reflectedDir);
    7.            return lerp(tex2D(_MainTex,input.uv), c,_reflectionAmount);
    8.          }
    9.  
    That kind of thing used to work back in may. It might not work right now.

    Keep in mind that this relied on undocumented functionality, so if you decide to use that, unity team might accidentally break your shader during update.

    Also, in case of shaders it is worth checking if they work in both GL and D3D mode. Sometimes there are differences in behavior which in rare cases might result in big problems.

    Also, some people like deferred rendering pipeline too.

    Also, IIRC there was at least one Free PBR solution on asset store (LuxRender, wasn't it?), so that one is also worth checking out.

    Also, keep in mind that you'll need to support more than light and ideally brerendered shadowmaps as well.
     
  28. MrJimGamer

    MrJimGamer

    Joined:
    Nov 24, 2015
    Posts:
    11
    Yes it support Deferred and Forward lighting and i have made deferent passes for them in the shader.
     
  29. MrJimGamer

    MrJimGamer

    Joined:
    Nov 24, 2015
    Posts:
    11
    This is fragment shader i need the surface shader code
     
  30. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    6,107
    "Fragment" and "surface" shaders use the same language. Just saying.
     
  31. MrJimGamer

    MrJimGamer

    Joined:
    Nov 24, 2015
    Posts:
    11
    Yes but they use different approaches on the execution i have xp on both.AND you can use cg,hlsl,glsl, for shader writing,it depents on what language do you write it with.
     
  32. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    6,107
    Have you tried to use that snippet?
    Also, have you downloaded and studied standard shader code?
     
  33. MrJimGamer

    MrJimGamer

    Joined:
    Nov 24, 2015
    Posts:
    11
    In an oldest post i saw you posted something about a method called "inline half4 LightingXXXXXXX_Deferred (Output s, half3 viewDir, UnityGI gi, out half4 outDiffuseOcclusion, out half4 outSpecSmoothness, out half4 outNormal)" and that is a lighting function for deferred lighting but you didnt post the code that goes in it for the reflection probs to work do you have any code like this or maybe some pointers ???
     
  34. MrJimGamer

    MrJimGamer

    Joined:
    Nov 24, 2015
    Posts:
    11
    Snippet??? And yes i have studied the standard shader but she takes all her functions from "UnityPBSLighting.cginc" and i studied that too but it had some unity varuables that are only for the engine to use and there is not book out there to explain them to you
     
  35. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    6,107
    Reflection cubemap for the environment is only available through undocumented engine variable.
    In the May it was available through unity_SpecCube0 AND unity_SpecCube1 variables, it also was encoded and needed to be passed through macro/function called something like "DecodeHDR" to get actual colors. IIRC with no reflection probes in scene there was some problem with this. There were also some equally undocumented variables that contained reflection probe center and size.

    There is no book(and probably won't be any) explaining how standard shader works. You need to read the whole things, scan for relevant info and figure out how it works by yoruself, without any book or documentation (there isn't any).

    While unity implies that you can access environmental cubemap by just declaring Cubemap sampler, at the time of writing of the first post it didn't work (meaning sampler cubemap wasn't automatically assigned by the engine), that may or may not have been fixed since then (although I doubt it was fixed).

    You also needed to read cg includes and study THEIR code too. That includes unityPBS.cginc and any cginc file THAT might include.

    IIRC that function was irrelevant.

    Search unity *.cginc files for mentions of unity_SpecCube0 variable and you'll spot few variables that correspond to reflection probe data (center/size), plus you'll find that DecodeHDR (or whatever it was called function). Those variables should be accessible even in surface shader, although I probably wouldn't be implementing custom PBR lighting model in surface shaders to begin with.

    Before investing a lot of your time into writing your own shader, investigate existing solutions on the asset store. There's ShaderForge ( https://www.assetstore.unity3d.com/en/#!/content/14147 ) which includes complete PBR shader chain including subsurface scattering, Free Lux shader ( https://www.assetstore.unity3d.com/en/#!/content/16000 ) and few others.

    ----

    Also, since I'm no longer interested in the original question, I'm unsubscribing from this thread. Good luck with your shader.
     
  36. MrJimGamer

    MrJimGamer

    Joined:
    Nov 24, 2015
    Posts:
    11
    You wrote a lot dude lol,i stydied the shaders and the files but there is some varuables like "UNITY_SOMETHING_SOMETHING()" and this where created from the unity developers for the engine and i dont want the shader to work i want to invest time to make my own so i can learn about this things and learn to program surface shaders or frafment shaders (post effects) i like investing time and efford to do that,but i ask here if someone knows anything already about the topic before doing it myself so i can save some time for real problems like the optimization of my paralax effect.Tnx for the help i guess and good bye
     
  37. Rusfighter

    Rusfighter

    Joined:
    Jan 18, 2014
    Posts:
    60
    Offtopic: so you going to sell the standard shader :p?

    Ontopic: it might be faster to set the cubemap from a C# script? I only don't know if such data is accessible...
    If you look at the mesh renderer component, the reflections probes are filled in there. So might an idea to get data from that component?
     
  38. MrJimGamer

    MrJimGamer

    Joined:
    Nov 24, 2015
    Posts:
    11
    Well i may going to sell my shader ,not the standard one ,i have implemented algorithms like,real parallax ,rim,subsurfase scattering ,vertes animation based on normal,i split sepcular map to ,gloss and metalness for better control and so the models dont seem ugly,and i resently implemented a snow algorithm ,so i think thats all you need to make a good lokking game.