I'm a Unity dev trying out Godot, and for practice, I'm porting the game me and my team did for a recent game jam to Godot. A core feature of this game is to cut a hole in the cover around the player. I did this with a sprite mask in Unity, but I can't seem to find a good way to do it in Godot 4.
Below is a screenshot of the desired effect from the Unity version.

  • Toxe replied to this.
  • Ratamacue9112 Make a ColorRect node and assign this shader to it:

    shader_type canvas_item;
    
    uniform vec2 holeCenter;
    uniform float holeRadius;
    
    varying vec2 p;
    
    void vertex(){
    	p = (MODEL_MATRIX * vec4(VERTEX, 0.0, 1.0) ).xy;
    }
    
    void fragment(){
    	COLOR.a = step(holeRadius, length(p - holeCenter));
    }

    Set center/radius uniform values from script as needed by calling set_shader_parameter() on ColorRect's material

    For fun, here's also a soft edge version. Change the penumbra parameter to adjust the soft zone:

    shader_type canvas_item;
    
    uniform vec2 holeCenter;
    uniform float holeRadius;
    uniform float penumbra = 20.0;
    
    varying vec2 p;
    
    void vertex(){
    	p = (MODEL_MATRIX * vec4(VERTEX, 0.0, 1.0) ).xy;
    }
    
    void fragment(){
    	COLOR.a = smoothstep(holeRadius - penumbra/2.0, holeRadius + penumbra/2.0, length(p - holeCenter));
    }

    Ratamacue9112 cut a hole in the cover around the player

    What do you mean by that? I am not really sure what we are supposed to see in that image.

    There is a black box covering the screen, and a circle sprite, which is a sprite mask. The circle is a child of the player so they move together.

    • Toxe replied to this.

      Ratamacue9112 So basically a big black box with a hole, right? Does that hole stay the same size or does it vary?

        6 days later

        I did manage to find this shader: https://randommomentania.com/2019/07/godot-color-mask-tutorial/
        However I kept on getting weird effects and couldn't find what I was looking for. I've never tried to use shaders or much special graphics things before, so I might try to look more into this.

        To make myself clearer, for people who have experience with Unity, I would set the cover's Sprite Renderer's mask mode to Visible Outside Mask. A more accurate term might be a cutout but I'm not sure.

        • xyz replied to this.

          Ratamacue9112 Make a ColorRect node and assign this shader to it:

          shader_type canvas_item;
          
          uniform vec2 holeCenter;
          uniform float holeRadius;
          
          varying vec2 p;
          
          void vertex(){
          	p = (MODEL_MATRIX * vec4(VERTEX, 0.0, 1.0) ).xy;
          }
          
          void fragment(){
          	COLOR.a = step(holeRadius, length(p - holeCenter));
          }

          Set center/radius uniform values from script as needed by calling set_shader_parameter() on ColorRect's material

          For fun, here's also a soft edge version. Change the penumbra parameter to adjust the soft zone:

          shader_type canvas_item;
          
          uniform vec2 holeCenter;
          uniform float holeRadius;
          uniform float penumbra = 20.0;
          
          varying vec2 p;
          
          void vertex(){
          	p = (MODEL_MATRIX * vec4(VERTEX, 0.0, 1.0) ).xy;
          }
          
          void fragment(){
          	COLOR.a = smoothstep(holeRadius - penumbra/2.0, holeRadius + penumbra/2.0, length(p - holeCenter));
          }