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. Dismiss Notice

Screen coordinates on Windows Phone (shader programming)

Discussion in 'Windows' started by PixelSquad, Dec 16, 2015.

  1. PixelSquad

    PixelSquad

    Joined:
    Sep 4, 2014
    Posts:
    114
    Hi guys

    I'm doing some shader programming, and I came across something odd. It looks like in the editor, Android, and iOS, the result of the following:

    o.pos = mul(UNITY_MATRIX_MVP, v.vertex);

    is a position in screen space where the positive Y axis points at the top of the screen, and the positive X axis is the right side of the screen. This adjusts itself with the device rotation.

    On WP8 (and perhaps also WSA running on ARM), it looks like the positive Y axis is always the bottom of the device's screen, with it standing upright, and the positive X axis is always the right side of the screen, no matter how the device is rotated.

    Is there a way of making it behave like on the other platforms (or at least a way of knowing in which direction the axis are pointing?)

    Many thanks
     
  2. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    EDIT: Two parts to the question!
    • Y axis is flipped
    • Axis doesn't rotate with orientation of the device.
    Y Axis is flipped:
    It's due to the difference in OpenGL and DirectX. Thus it's also true on Xbox, and I'm guessing also on the Universal Windows Platform.

    More info:
    http://docs.unity3d.com/Manual/SL-PlatformDifferences.html

    Rotate axis with device orientation:
    Seems like this should work the same as other platforms, so if it's different maybe this is considered a Unity bug. You might be able to feed this into your shader as an int (enum) or something.
    http://docs.unity3d.com/ScriptReference/Screen-orientation.html
     
    Last edited: Dec 17, 2015
    PixelSquad likes this.
  3. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    You could always use UNITY_UV_STARTS_AT_TOP, although that's more for texture coordinates. Still... that should be a good indicator if you're using D3D (aka. every windows platform) or not.

    On a sidenote... a better option would be checking if SHADER_API_D3D9, SHADER_API_D3D11 or one of the other windows-specific APIs are being used, and if that's so, flip your Y coordinate round so it's correct.

    I can give an example if you need it. :) Out of curiosity, how come you're needing to detect device orientation for this?
     
    PixelSquad likes this.
  4. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,465
    If you're targeting Windows Phone 8.0 (not 8.1), the OS doesn't automatically rotate the screen if you rotate the phone, so we have to do the rotation ourselves. Therefore, the rotation is done by the projection matrix, as needed. This isn't the case when targeting WSA/WP8.1. It was a weird limitation that was lifted in WP8.1.
     
    PixelSquad likes this.
  5. PixelSquad

    PixelSquad

    Joined:
    Sep 4, 2014
    Posts:
    114
    Many thanks for the answers guys.

    @FuzzyQuills

    I'm trying to implement simple screen-space clipping, that's all.
     
  6. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Huh, not sure what you would need that for, but fair enough. :)
     
  7. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    If doing a full screen shader, it sounds like when the user goes from portrait mode to landscape mode on Windows Phone 8, nothing changes axis wise. This means the shader doesn't rotate when the user rotates their phone.

    Normally if the player flips their phone around (holds it sideways or upside-down), Unity or the OS detects this and will flip everything around to match. Eg: if the user goes from portrait to landscape mode, I don't normally need to mess with Unity cameras. They will adjust on their own. (Of course, UI or gameplay might need to be adjusted, but this is about graphics!)

    On Windows Phone 8, the shader coordinates aren't getting flipped around so it sounds like PixelSquad has to flip it himself.