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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Which shader can be used with cube texture shape?

Discussion in 'Shaders' started by BilalGuvenc, Jul 13, 2020.

  1. BilalGuvenc

    BilalGuvenc

    Joined:
    May 5, 2020
    Posts:
    16
    Skybox/Cubemap shader can be used with cube texture shape which is visible from inside of the box correctly. In other words camera should be inside of the box for this shader.

    Is there a shader, which can be used with cube texture shape which is visible from outside of the box correctly?
    In other words camera should be outside of the box for this shader.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,256
    Are you asking if there's a shader included with Unity?
    No.

    Can you write a shader that uses a cube map texture?
    Yes.

    The only difference between a cubemap texture and a 2D texture is a 2D texture uses a UV with two components which is a position, and a cubemap uses a UVW with three components which is a direction. Most of the time cubemaps are using a world space view direction (skybox) or reflected world space view direction (reflections), but you can just use a box's local vertex position and it'll work.
     
    BilalGuvenc likes this.
  3. BilalGuvenc

    BilalGuvenc

    Joined:
    May 5, 2020
    Posts:
    16
    Thank you for detailed answer, I tried to write custom shader today.
    However, texture does not stay stable on the cube, if I rotate the cube or camera.
    How can I use box's local vertex position?

    Code (CSharp):
    1. Shader "Custom/NewSurfaceShader"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Color", Color) = (1,1,1,1)
    6.         [NoScaleOffset] _MainTex("Cubemap", Cube) = "" {}
    7.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    8.         _Metallic ("Metallic", Range(0,1)) = 0.0
    9.     }
    10.     SubShader
    11.     {
    12.         Tags { "RenderType"="Opaque" }
    13.         LOD 200
    14.  
    15.         CGPROGRAM
    16.         // Physically based Standard lighting model, and enable shadows on all light types
    17.         #pragma surface surf Standard fullforwardshadows
    18.  
    19.         // Use shader model 3.0 target, to get nicer looking lighting
    20.         #pragma target 3.0
    21.  
    22.         samplerCUBE _MainTex;
    23.  
    24.         struct Input
    25.         {
    26.             float3 worldRefl;
    27.         };
    28.  
    29.         half _Glossiness;
    30.         half _Metallic;
    31.         fixed4 _Color;
    32.  
    33.         // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
    34.         // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
    35.         // #pragma instancing_options assumeuniformscaling
    36.         UNITY_INSTANCING_BUFFER_START(Props)
    37.             // put more per-instance properties here
    38.         UNITY_INSTANCING_BUFFER_END(Props)
    39.  
    40.         void surf (Input IN, inout SurfaceOutputStandard o)
    41.         {
    42.             // Albedo comes from a texture tinted by color
    43.             fixed4 c = texCUBE (_MainTex, IN.worldRefl) * _Color;
    44.             o.Albedo = c.rgb;
    45.             // Metallic and smoothness come from slider variables
    46.             o.Metallic = _Metallic;
    47.             o.Smoothness = _Glossiness;
    48.             o.Alpha = c.a;
    49.         }
    50.         ENDCG
    51.     }
    52.     FallBack "Diffuse"
    53. }
    54.  
     
    Last edited: Jul 14, 2020
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,256
    You'll need a custom vertex function to get the object space vertex position and pass it from the vertex shader stage to the fragment shader stage where the surf function runs.

    See the section titled "Custom data computed per-vertex":
    https://docs.unity3d.com/Manual/SL-SurfaceShaderExamples.html

    But instead of
    abs(v.normal)
    you want
    v.vertex.xyz
    .
     
    BilalGuvenc likes this.
  5. BilalGuvenc

    BilalGuvenc

    Joined:
    May 5, 2020
    Posts:
    16
    It works. Thank you, bgolus, very much for detailed answers.