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?)
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:
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.
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?
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.
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.