Search Unity

Question Multidraw shader - to simulate screen wrapping

Discussion in 'Shaders' started by PlayCreatively, Oct 12, 2020.

  1. PlayCreatively

    PlayCreatively

    Joined:
    Jan 25, 2016
    Posts:
    94
    I'm completely new to shaders and I want to find an easy way of simulating screen wrapping (object looping from one end of the screen to the other end). This is for a 2D game using sprites.
    I used to use multiple cameras to do this but I feel it's an overkill and thought the same thing can be done more easily using a shader. Is there a way to write a shader that draws the subject twice but with an offset position, pretty much like chromatic aberration minus the color change?
    Any pointers are appreciated :)
     
  2. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,549
    You could with a geometry shader, but that's also an overkill solution.

    You could instead just have the sprite be fixed to the camera, and as the camera moves you scroll the UV.x value in the shader. As long as your texture import is set to "Repeat", it will continue to wrap your sprite over and over.
     
  3. PlayCreatively

    PlayCreatively

    Joined:
    Jan 25, 2016
    Posts:
    94
    You can't create a space between each repeat can you? Otherwise the result would just be a screen filled with a tiled, 0 spaced out player sprite.

    Screenshot 2020-10-13 004622.png
     
  4. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,549
    You can with some clever math.


    And here is a shadergraph example:


    Note: In this setup, your sprite should be set to Clamp instead of Repeat.
     
    Last edited: Oct 13, 2020
    PlayCreatively likes this.