I am using cardboard v1.4.1, and wanting to be able to toggle in and out of the cardboard vr. When transitioning out from cardboard the camera is stretched (I assume one of the eyes is stretched across the screen). I am using the following code that was provided in the example.
private void StopXR()
{
Debug.Log("Stopping XR...");
XRGeneralSettings.Instance.Manager.StopSubsystems();
Debug.Log("XR stopped.");
Debug.Log("Deinitializing XR...");
XRGeneralSettings.Instance.Manager.DeinitializeLoader();
Debug.Log("XR deinitialized.");
}
What would be the best way to approach this?
Try this:
void StopXR()
{
var xrManager = XRGeneralSettings.Instance.Manager;
if (!xrManager.isInitializationComplete)
return; // Safety check
xrManager.StopSubsystems();
xrManager.DeinitializeLoader();
}
And if you are experiencig a distorted or stretched screen after exiting VR:
camera.ResetAspect();
camera.fieldOfView = defaultFov;
camera.ResetProjectionMatrix();
camera.ResetWorldToCameraMatrix();
Related
I have been trying to experiment with moving UI Elements using the position of the mouse, Trying to make it behave like a cursor. I was able to move the Element when the Canvas Render Mode was set to "Screen Space". I was able to clamp it within the Canvas as well.
But I want it to work when the Canvas Render Mode is "World Space" as well. If I use the same code that I used for the Screen Space, the Element leaves its boundaries and gets messed up as the angles are being varied.
I really need help. Any clue how to do it?
You can view how the scene looks in this image below. : https://img.techpowerup.org/200624/screen.png
If Raycasting is the solution, can someone please help me out and provide a snippet of code or something.
What you are looking for is probably RectTransformUtility.ScreenPointToWorldPointInRectangle
Transform a screen space point to a position in world space that is on
the plane of the given RectTransform.
The cam parameter should be the camera associated with the screen
point. For a RectTransform in a Canvas set to Screen Space - Overlay
mode, the cam parameter should be null.
When ScreenPointToWorldPointInRectangle is used from within an event
handler that provides a PointerEventData object, the correct camera
can be obtained by using PointerEventData.enterEventData (for hover
functionality) or PointerEventData.pressEventCamera (for click
functionality). This will automatically use the correct camera (or
null) for the given event.
You didn't post your code but you could probably use it in a GraphicRaycaster.Raycast. Something like
GraphicRaycaster m_Raycaster;
PointerEventData m_PointerEventData;
EventSystem m_EventSystem;
Canvas canvas;
void Start()
{
//Fetch the Raycaster from the GameObject (the Canvas)
m_Raycaster = GetComponent<GraphicRaycaster>();
//Fetch the Event System from the Scene
m_EventSystem = GetComponent<EventSystem>();
canvas = GetComponnet<Canvas>();
//Set up the new Pointer Event
m_PointerEventData = new PointerEventData(m_EventSystem);
}
void Update()
{
//Check if the left Mouse button is clicked
if (Input.GetKey(KeyCode.Mouse0))
{
//Set the Pointer Event Position to that of the mouse position
m_PointerEventData.position = Input.mousePosition;
//Create a list of Raycast Results
List<RaycastResult> results = new List<RaycastResult>();
//Raycast using the Graphics Raycaster and mouse click position
m_Raycaster.Raycast(m_PointerEventData, results);
//For every result returned, output the name of the GameObject on the Canvas hit by the Ray
foreach (RaycastResult result in results)
{
Debug.Log("Hit " + result.gameObject.name);
// until here it was the API example for GraphicRaycaster.Raycast
// Now get the first hit object
var rect = result.gameObject.GetComponent<RectTransform>();
if(rect)
{
// check which camera to get (main or null for ScreenSpace Overlay)
var camera = canvas.renderMode == RenderMode.ScreenSpaceOverlay ? null : Camera.main;
if(RectTransformUtility.ScreenPointToWorldPointInRectangle(rect, result.screenPosition, camera, out var worldPosition))
{
cursor3D.transform.position = worldPosition;
cursor3D.transform.rotation = result.gameObject.transform.rotation;
break;
}
}
}
}
}
Especially note also
Attach this script to your Canvas GameObject.
Also attach a GraphicsRaycaster component to your canvas by clicking the Add Component button in the Inspector window.
Also make sure you have an EventSystem in your hierarchy.
I have four buttons at the bottom, notes would then fall from the top of the screen, I am not sure how to code the touchscreen part for mobile, I have it working for pc format, but I am not sure what I need to change for it to work.
I have 2 scripts, one for the buttons and then one for every note falling down.
This is what I have at the moment:
For the buttons:
void Update()
{
if(Input.GetKeyDown(keyToPress))
{
theSR.sprite = pressedImage;
}
if(Input.GetKeyUp(keyToPress))
{
theSR.sprite = defaultImage;
}
}
For the Notes:
void Update()
{
if(Input.GetKeyDown(keyToPress))
{
if(canBePressed)
{
gameObject.SetActive(false);
//GameManager.instance.NoteHit();
//Determine if player hits the note normal, good, perfect
if (Mathf.Abs(transform.position.y) > 0.25)
{
GameManager.instance.NormalHit();
Instantiate(hitEffect, transform.position, hitEffect.transform.rotation);
}
else if (Mathf.Abs(transform.position.y) > 0.05f)
{
GameManager.instance.GoodHit();
Instantiate(goodEffect, transform.position, goodEffect.transform.rotation);
}
else
{
GameManager.instance.PerfectHit();
Instantiate(perfectEffect, transform.position, perfectEffect.transform.rotation);
}
}
}
}
This works perfectly if it was PC only but I just don't know what to change for it to buttons to react exactly the same way in mobile as it would on PC
Well the answer depends on how you intend to get the user input on mobile. There are 2 ways to do this if I think of off my head.
The first would be to get input touch screen position and check if the position on the x axis is in one of 4 compartments for the nots. You can get that with this
The second, you can actually use the UI buttons from Unity and they work on all platforms. So you can test on PC and the upload to your phone to have it working the same way.
Hope any of this helps you!
I am using kinect v2 c# to detect hand gesture. All my algo used is working right. The problem is that I want kinect to display only hand detected but not all of body and to give all points of the hand in a black background?
This is code that gets the points of contouring hand.
private void HandsController_HandsDetected(object sender, HandCollection e) {
// Display the results!
if (e.HandLeft != null)
{
point = e.HandLeft.ContourDepth;
}
}
for example you can write it like this
// //left hand in front of left Shoulder
if (body.Joints[JointType.HandLeft].Position.Z < body.Joints[JointType.ElbowLeft].Position.Z && body.Joints[JointType.HandRight].Position.Y < body.Joints[JointType.SpineBase].Position.Y)
{
//Action here
}
You can see a sample of how other libraries uses this code here
I also have a tutorial on how to implements swipe gesture using Vitruvius Library do check them out! :D
I would like your help please on a WebCamera problem. I've used a library available from Nuget; WebEye.Controls.Wpf.WebCameraControl (version 1.0.0). The URL is https://www.nuget.org/packages/WebEye.Controls.Wpf.WebCameraControl/
The article and instructions are available at: http://www.codeproject.com/Articles/330177/Yet-another-Web-Camera-control
Due to project constraints, I developed a WPF application for a Linx tablet (Windows 10), rather than as a Universal Windows Application. I used the WebEye library to connect to the webcam on the tablet and take pictures with it. It works fine when I hold the tablet in landscape, but not when I hold the tablet in portrait mode. In portrait mode, the CameraPreview/VideoWindow automatically rotates -90 degrees.
I tried resolve the problem to no avail.
Rotate the control surrounding the video window via the Webcamera RenderTransform or LayoutTransform property – The control rotates but the video image does not rotate correctly.
Tried to rotate the VideoWindow inside the WebCamera Content property - I got the source code from the GitHub and set the VideoWindow available for access. Recompled the library and used it to rotate the VideoWindow via its RenderTransform property. https://github.com/jacobbo/WebEye/tree/master/WebCameraControl
No matter what I do, the camera preview is always -90 degrees.
The library is simple and it does not have many properties to manipulate the video window.
The webcontrol is in XAML.
<wpf:WebCameraControl x:Name="webCameraControl"
MouseDoubleClick="webCameraControl_MouseDoubleClick"
StylusButtonUp="webCameraControl_StylusButtonUp"
MouseUp="webCameraControl_MouseUp"
TouchUp="webCameraControl_TouchUp"
GotMouseCapture="webCameraControl_GotMouseCapture"
/>
This is how I initialised the WebCamera. When the UserControl is loaded, it will then automatically connect to the webcam on the tablet. See startViewing() function.
private WebCameraId _cameraID = null;
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
startViewing();
}
private void startViewing()
{
List<WebCameraId> cams = (List<WebCameraId>)webCameraControl.GetVideoCaptureDevices();
if (cams.Count > 0)
{
_cameraID = (WebCameraId)cams[0];
webCameraControl.StartCapture(_cameraID);
}
}
I tried to force the control to rotate it correctly when the app detects a change in the Display screen. See DisplaySettingsChanged event.
public ucWebCam()
{
InitializeComponent();
Microsoft.Win32.SystemEvents.DisplaySettingsChanged += SystemEvents_DisplaySettingsChanged;
}
private void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e)
{
try
{
double angle = 0;
if (SystemParameters.PrimaryScreenWidth > SystemParameters.PrimaryScreenHeight)
{
angle = 0;
}
else
{
angle = -90;
}
webCameraControl.StopCapture();
adjustWebcamAngle(angle);
webCameraControl.StartCapture(_cameraID);
}
catch (Exception ex)
{
}
}
private void adjustWebcamAngle(double angle)
{
try
{
// IGNORE portrait boolean flag
bool portrait = false;
if (angle == 90 || angle == 180)
{
portrait = true;
}
// TRIED TO SET THE ANGLE OF THE CONTROL TO NO AVAIL
RotateTransform rotTransform = new RotateTransform(angle);
//rotTransform.Angle = angle;
ScaleTransform scaleTransform = new ScaleTransform();
//scaleTransform.ScaleX = (portrait) ? 0 : 1;
//scaleTransform.ScaleY = (portrait) ? 0 : 1;
TransformGroup tGroup = new TransformGroup();
//tGroup.Children.Add(scaleTransform);
tGroup.Children.Add(rotTransform);
// ROTATE CAMERA!
webCameraControl.RenderTransform = tGroup;
} catch (Exception ex)
{
}
}
So far I only rotated the WebCam control, not the video image.
I looked through the comments in Alexander's article with no joy at: http://www.codeproject.com/Articles/330177/Yet-another-Web-Camera-control
How can I rotate the Camera preview correctly? Can you please advise where I'm going wrong?
I have attached two images to illustrate my problem.
In case you are not a stranger to C#, I would recommend you use the EMGUCV nuget and create your own preview application. It`s not that hard, and you could use MVVM to bind the frames to your View, making it better than using code behind your control.
PS: This is not an answer, but it is a solution to your problem. I cannot not post comments yet, as I do not have 50 points :(
Have a nice day.
I am creating a camera app using MediaCapture. I am trying to adapt to screen rotation and thus I have created the following method:
private void Current_SizeChanged(object sender, Windows.UI.Core.WindowSizeChangedEventArgs e)
{
width = Window.Current.Bounds.Width;
height = Window.Current.Bounds.Height;
//captureManager.SetPreviewRotation(VideoRotation.Clockwise90Degrees);
//if (ApplicationView.GetForCurrentView().Orientation.ToString() == "Portrait") capturePreview.Stretch = Stretch.Fill;
capturePreview.Width = width;
capturePreview.Height = height;
imagePreview.Width = width;
imagePreview.Height = height;
//if (ApplicationView.GetForCurrentView().Orientation.ToString() == "Portrait") capturePreview.Stretch = Stretch.Fill;
//else capturePreview.Stretch = Stretch.Uniform;
}
When I open my app the camera fills the whole page but when I flip the camera the capturePreview only takes half of the page, It doesn't matter if I start the app in portrait or horizontal mode, the other mode won't work but the first mode will also if flipped in to.
Another question is about the actual capturePreview layout, I found that if I keep the screen at horizontal layout the camera works great but if the device is in portrait mode I cant fill the whole page without stretching the photo, is there a way to keep only one element on the screen in a certain rotation (capturePreview) I tried rotating it with a rotation tranform but that also affects the actual place of the element.
Thank you
In WP you get the CurrentViewState through the Window.Sizechanged Event.
See this MSDN article (first result from google)