Search Unity

Question convert godot shader into unity shader

Discussion in 'Shaders' started by demozbox, Jul 28, 2021.

  1. demozbox

    demozbox

    Joined:
    Nov 26, 2014
    Posts:
    83
    I need to convert godot shader into unity shader.
    The shader outlines 2dTexture and blooms the outline.
    Can anyone help me?
    Code (CSharp):
    1. shader_type canvas_item;
    2. uniform vec3 Scale;
    3. uniform float Amount;
    4. uniform float Offset;
    5. uniform vec4 Color : hint_color;
    6. // BlurCustom
    7. vec4 blurWithAmountFunc(sampler2D _tex_b1r_cst, vec2 _uv_b1r_cst, float _l0d_b1r_cst, int _amnt_b1r_cst, float _0ffst_b1r_cst) {
    8.   vec4 _c0l_b1r_cst = vec4(0, 0, 0, 0);
    9.   _amnt_b1r_cst = int(max(min(float(_amnt_b1r_cst), 20.0), 0.0)); // have to do this int() float() S*** because of gles2
    10.                                   // max _amnt_b1r_cst is 20 for not to kill PC
    11.   for(int x = -_amnt_b1r_cst; x <= _amnt_b1r_cst; x++) {
    12.     for(int y = -_amnt_b1r_cst; y <= _amnt_b1r_cst; y++) {
    13.       vec2 _c00rd_b1r_cst = _uv_b1r_cst + vec2(float(x), float(y)) * _0ffst_b1r_cst;
    14.       if (_l0d_b1r_cst < 0.0){
    15.         _c0l_b1r_cst += texture(_tex_b1r_cst, _c00rd_b1r_cst);
    16.       }else{
    17.         _c0l_b1r_cst += textureLod(_tex_b1r_cst, _c00rd_b1r_cst, _l0d_b1r_cst);
    18.       }
    19.     }
    20.   }
    21.   int _nmb_ne1ghb0urs_b1r_cst = (_amnt_b1r_cst * 2 + 1) * (_amnt_b1r_cst * 2 + 1);
    22.   _c0l_b1r_cst /= float(_nmb_ne1ghb0urs_b1r_cst);
    23.   return _c0l_b1r_cst;
    24. }
    25. void vertex() {
    26. // Output:0
    27. }
    28. void fragment() {
    29. // Input:17
    30. // Texture:25
    31.   vec3 n_out25p0;
    32.   float n_out25p1;
    33.   {
    34.     vec4 TEXTURE_tex_read = texture(TEXTURE, UV.xy);
    35.     n_out25p0 = TEXTURE_tex_read.rgb;
    36.     n_out25p1 = TEXTURE_tex_read.a;
    37.   }
    38. // Input:18
    39.   vec3 n_out18p0 = vec3(UV, 0.0);
    40. // VectorUniform:27
    41.   vec3 n_out27p0 = Scale;
    42. // ScaleUV:26
    43.   vec3 n_in26p2 = vec3(0.50000, 0.50000, 0.00000);
    44.   vec3 n_out26p0;
    45.   {
    46.     n_out26p0.xy = (n_out18p0.xy - n_in26p2.xy) * n_out27p0.xy + n_in26p2.xy;
    47.   }
    48. // ScalarUniform:13
    49.   float n_out13p0 = Amount;
    50. // ScalarUniform:21
    51.   float n_out21p0 = Offset;
    52. // BlurCustom:19
    53.   float n_in19p2 = -1.00000;
    54.   vec3 n_out19p0;
    55.   float n_out19p1;
    56.   {
    57.     vec4 n_out19p0n_out19p1 = blurWithAmountFunc(TEXTURE, n_out26p0.xy, n_in19p2, int(n_out13p0), n_out21p0);
    58.     n_out19p0 = n_out19p0n_out19p1.rgb;
    59.     n_out19p1 = n_out19p0n_out19p1.a;
    60.   }
    61. // ColorUniform:23
    62.   vec3 n_out23p0 = Color.rgb;
    63.   float n_out23p1 = Color.a;
    64. // ColorOp:22
    65.   vec3 n_out22p0;
    66.   {
    67.     float base = vec3(n_out19p1).x;
    68.     float blend = n_out23p0.x;
    69.     if (base < 0.5) {
    70.       n_out22p0.x = 2.0 * base * blend;
    71.     } else {
    72.       n_out22p0.x = 1.0 - 2.0 * (1.0 - blend) * (1.0 - base);
    73.     }
    74.   }
    75.   {
    76.     float base = vec3(n_out19p1).y;
    77.     float blend = n_out23p0.y;
    78.     if (base < 0.5) {
    79.       n_out22p0.y = 2.0 * base * blend;
    80.     } else {
    81.       n_out22p0.y = 1.0 - 2.0 * (1.0 - blend) * (1.0 - base);
    82.     }
    83.   }
    84.   {
    85.     float base = vec3(n_out19p1).z;
    86.     float blend = n_out23p0.z;
    87.     if (base < 0.5) {
    88.       n_out22p0.z = 2.0 * base * blend;
    89.     } else {
    90.       n_out22p0.z = 1.0 - 2.0 * (1.0 - blend) * (1.0 - base);
    91.     }
    92.   }
    93. // ColorOp:24
    94.   vec3 n_out24p0 = min(n_out25p0, n_out22p0);
    95. // Output:0
    96.   COLOR.rgb = n_out24p0;
    97.   COLOR.a = dot(n_out22p0, vec3(0.333333, 0.333333, 0.333333));
    98. }
    99. void light() {
    100. // Output:0
    101. }
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    Godot uses a GLSL like shader language. You can look at some beginner Unity shader guides for how to setup a basic unlit shader, and then look for some GLSL to HLSL (the shader language Unity uses) guides to do the rest.

    If you make an attempt at converting the shader here and have problems, I'll be happy to try to help you figure out what's wrong.
     
    lilacsky824 likes this.
  3. demozbox

    demozbox

    Joined:
    Nov 26, 2014
    Posts:
    83
    I already looked for some beginers unity shaders and it doesnt help. First I need to make planty basic hlsl shaders to anderstand what to do with particular one.
    And on top of this - one more shader on glsl - also not just one beginer tutoreal.

    the task is not that easy if you have blank knowledge about shaders. S***ty advice.
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    Which is why I recommended you start doing some beginner tutorials to learning how to write Unity shaders.

    This forum isn't a place to ask people to do your work for you, it's a place to find assistance when you don't understand something. Translating a Godot shader to Unity isn't something asked very often; in fact this is literally the first time I've ever seen someone ask the question. I tried to answer the two main issues you'll run into with this task, but ultimately it'll come down to you learning how to do it yourself, and barring that, hiring someone else.

    At the most basic level, to convert from GLSL to HLSL is mostly search and replace some terms from
    vec
    to
    float
    , as well as a few other functions that have slightly different names. Here's the second link returned from Google when searching for "glsl to hlsl":
    https://docs.microsoft.com/en-us/windows/uwp/gaming/glsl-to-hlsl-reference

    But that translated code will be useless unless you can copy it into an otherwise already working Unity shader. The basic unlit shader is probably a good starting point, and there are a ton of basic Unity shader tutorials out there that can get you most of the way through understanding what most the parts of an unlit shader do.

    The first Google search result on that search term is this page on converting ShaderToy to Unity, which isn't directly applicable, but likely covers some similar problems that you'll have.
    https://alastaira.wordpress.com/2015/08/07/unity-shadertoys-a-k-a-converting-glsl-shaders-to-cghlsl/

    As for the high level specifics of how to translate those elements unique to Godot to Unity ... no idea. I've never written a shader for Godot, and I suspect few others if any on this forum have either. And as you don't seem to be familiar with Godot's shaders yourself, I wonder if you can't ask the person who original wrote that shader for assistance.
     
    lilacsky824 and Invertex like this.
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    If you want help understanding what specific parts of the above shader code does (as it is mostly glsl) I can try to answer those questions. But again a beginner shader tutorial can answer those questions just as well.
     
    lilacsky824 likes this.