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. Join us on March 30, 2023, between 5 am & 1 pm EST, in the Performance Profiling Dev Blitz Day 2023 - Q&A forum and Discord where you can connect with our teams behind the Memory and CPU Profilers.
    Dismiss Notice

Problem creating custom shader

Discussion in 'Project Tiny' started by Sarkahn, Nov 4, 2020.

  1. Sarkahn

    Sarkahn

    Joined:
    Jan 9, 2013
    Posts:
    440
    Hello, I'm trying to get a custom shader working with 0.31 release. I tried making a simple one from this example just to get started. When I try to build my scene I get this error:
    Unity_ig5yS4GS8U.png

    This is the shader
    Code (CSharp):
    1. Shader "MyMeshTest/SimpleShader"
    2. {
    3.     Properties
    4.     {
    5.         [MainTexture] _BaseMap("Albedo", 2D) = "white" {}
    6.         [MainColor] _BaseColor("Color", Color) = (1,1,1,1)
    7.     }
    8.  
    9.     SubShader
    10.     {
    11.         Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" }
    12.         LOD 100
    13.  
    14.         Pass
    15.         {
    16.             Name "Unlit"
    17.             HLSLPROGRAM
    18.  
    19.             #pragma vertex vert
    20.             #pragma fragment frag
    21.  
    22.             #pragma multi_compile_instancing
    23.  
    24.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"          
    25.  
    26.             struct Attributes
    27.             {
    28.                 float4 positionOS   : POSITION;
    29.                 float2 uv : TEXCOORD0;
    30.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    31.             };
    32.  
    33.             struct Varyings
    34.             {
    35.                 float4 vertex  : SV_POSITION;
    36.                 float2 uv : TEXCOORD0;
    37.  
    38.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    39.                 UNITY_VERTEX_OUTPUT_STEREO
    40.             };
    41.  
    42.             TEXTURE2D(_BaseMap);
    43.             SAMPLER(sampler_BaseMap);
    44.  
    45.             CBUFFER_START(UnityPerMaterial)
    46.                 half4 _BaseColor;
    47.                 float4 _BaseMap_ST;
    48.             CBUFFER_END
    49.  
    50.             Varyings vert(Attributes IN)
    51.             {
    52.                 Varyings OUT = (Varyings)0;
    53.  
    54.                 UNITY_SETUP_INSTANCE_ID(IN);
    55.                 UNITY_TRANSFER_INSTANCE_ID(IN, OUT);
    56.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
    57.  
    58.                 VertexPositionInputs vertInput = GetVertexPositionInputs(IN.positionOS.xyz);
    59.                 OUT.vertex = vertInput.positionCS;
    60.                 OUT.uv = TRANSFORM_TEX(IN.uv, _BaseMap);
    61.                
    62.                 return OUT;
    63.             }
    64.        
    65.             half4 frag(Varyings IN) : SV_Target
    66.             {
    67.                 UNITY_SETUP_INSTANCE_ID(IN);
    68.                 UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(IN);
    69.  
    70.                 return _BaseColor;
    71.             }
    72.             ENDHLSL
    73.         }
    74.     }
    75. }

    I tested my shader in a normal URP project and it seems to work. Any idea what I'm doing wrong?
     
  2. Sarkahn

    Sarkahn

    Joined:
    Jan 9, 2013
    Posts:
    440
    I'd love to start messing with Tiny but I can't seem to get this to work. Is there an example of a simple custom shader in Tiny that I can build off of?
     
  3. v_vuk

    v_vuk

    Unity Technologies

    Joined:
    Jul 11, 2017
    Posts:
    35
    Hi Sarkahn! Custom shaders are not yet supported. Stay tuned for support in a later release.
     
  4. Sarkahn

    Sarkahn

    Joined:
    Jan 9, 2013
    Posts:
    440
    Ahh so the "initial support" for shaders from the .31 release post and getting started guide just means the work is starting I guess? Looking forward to when it's in!
     
  5. djsell

    djsell

    Joined:
    Aug 29, 2013
    Posts:
    77
    Then someone should fix the release notes that say otherwise.

    https://forum.unity.com/threads/project-tiny-0-31-preview-is-available.998752/
     
    cultureulterior likes this.
  6. ElliotB

    ElliotB

    Joined:
    Aug 11, 2013
    Posts:
    121
    Hi Sarkahn,

    I was also excited by the release notes! I haven't had a chance to look at this myself so I can't offer much, but did you see there are some shaders in the folder
    packages.unity.com\com.unity.tiny@0.31.0-preview.23\Unity.Tiny.Rendering.Native\shadersrc~

    They might be a good starting point?
     
  7. ElliotB

    ElliotB

    Joined:
    Aug 11, 2013
    Posts:
    121
    If I use a material based on this rather crude shader, it works in a windows il2cpp build. It does not work for the wasm build, looks like the shader is replaced (material applied to the river in the images below):

    Code (csharp):
    1. Shader "Unlit/TinyExample"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.     }
    7.         SubShader
    8.         {
    9.             Tags { "RenderType" = "Opaque" }
    10.             LOD 100
    11.  
    12.             Pass
    13.             {
    14.                 CGPROGRAM
    15.                 #pragma vertex vert
    16.                 #pragma fragment frag
    17.  
    18.                 #include "UnityCG.cginc"
    19.  
    20.             struct appdata
    21.             {
    22.                 float4 pos : POSITION;
    23.                 float3 normal : NORMAL;
    24.                 float2 uv : TEXCOORD0;
    25.             };
    26.  
    27.             struct v2f
    28.             {
    29.                 float2 uv : TEXCOORD0;
    30.                 float4 pos : SV_POSITION;
    31.                 float3 normal : NORMAL;
    32.             };
    33.  
    34.             CBUFFER_START(UnityPerMaterial)
    35.             sampler2D _MainTex;
    36.             float4 _MainTex_ST;
    37.             CBUFFER_END
    38.  
    39.             v2f vert (appdata v)
    40.             {
    41.                 v2f o;
    42.                 o.pos = UnityObjectToClipPos(v.pos);
    43.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    44.                 o.normal = UnityObjectToClipPos(v.normal);
    45.                 return o;
    46.             }
    47.  
    48.             float4 frag (v2f i) : SV_Target
    49.             {
    50.                 float4 col = tex2D(_MainTex, i.uv);
    51.                 float shading = i.normal.y;
    52.                 return col * shading * float4(i.normal.xyz,1.0);
    53.             }
    54.             ENDCG
    55.         }
    56.     }
    57. }
    58.  
    customshaders.png
     
    Sarkahn likes this.
  8. Sarkahn

    Sarkahn

    Joined:
    Jan 9, 2013
    Posts:
    440
    Yeah I had a similar experience doing a normal C# build. I could get the hlsl to work but it doesn't seem to be able to pass the material properties into the shader, which I think is in line with the documentation right now. Looking forward to when the support is fleshed out some more!
     
    NotaNaN likes this.
  9. ElliotB

    ElliotB

    Joined:
    Aug 11, 2013
    Posts:
    121
    What's the status of custom shaders in 0.32? I see this is still listed in the notes for 0.31, and then there wasn't mention of it in the 0.32 notes. Is there a planned eta for custom shaders in tiny, (and on a related note if there is an eta for being able to set material properties like in hybrid renderer?)

    Thanks for the new tiny, looking forward to working with it someday :)