Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by seowen1 · Aug 16, 2019 at 08:50 PM · scripting problem

RenderTexture from camera viewpoint using a plane as a "window"?

Hi all,

I'm creating a Kinect simulation that needs to adjust what's on screen based on the user's head position/rotation. I've got the Kinect part worked out, and a camera is moving and rotating correctly. The problem is, I want the screen to show a fixed position only changing the perspective based on the camera position.

Basically, I want the user to be looking into a box, with an opening exactly the size of the monitor resolution. When the user moves their head to look around, the perspective of the screen image changes to match, but the view does not ever show the outside of the box, only the inside. This will create an effect that the monitor is the box opening and will seem as though the monitor has depth.

My idea is to use the existing camera that is tracking head movement/rotation correctly as a RenderTexture source, along with a plane that is the exact size of the window resolution. The plane would serve as a "window" that the camera is looking through and that window view would replace the screen draw buffer.

Unfortunately, I'm only familiar with the basics of RenderTextures and thus I cannot figure out how to implement something like this. Any ideas?

![diagram][1] [1]: /storage/temp/144722-2019-08-16-15-35-08.png

2019-08-16-15-35-08.png (157.3 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by seowen1 · Aug 20, 2019 at 09:11 PM

Solved!

Took this code and applied it to my setup. Works perfectly.

 void Update() {
         cameraController.SetCameraGroupRotation(targetWindow.rotation);
 
         if (dynamicWidth) {
             float newWidth = targetWindow.localScale.y * cameraController.GetAspectRatio();
             targetWindow.localScale = new Vector3(newWidth, targetWindow.localScale.y, targetWindow.localScale.z);
         }
     }
 
     void LateUpdate() {
 
         Vector3 pa, pb, pc, pd;
         pa = targetWindow.TransformPoint(new Vector3(-.5f, -.5f, 0)); //Bottom-Left
         pb = targetWindow.TransformPoint(new Vector3( .5f, -.5f, 0)); //Bottom-Right
         pc = targetWindow.TransformPoint(new Vector3(-.5f,  .5f, 0)); //Top-Left
         pd = targetWindow.TransformPoint(new Vector3( .5f,  .5f, 0)); //Top-Right
 
         Vector3 pe = cameraController.GetCameraGroup().position; // eye position
 
         Vector3 vr = (pb - pa).normalized; // right axis of screen
         Vector3 vu = (pc - pa).normalized; // up axis of screen
         Vector3 vn = Vector3.Cross(vr, vu).normalized; // normal vector of screen
 
         Vector3 va = pa - pe; // from pe to pa
         Vector3 vb = pb - pe; // from pe to pb
         Vector3 vc = pc - pe; // from pe to pc
         Vector3 vd = pd - pe; // from pe to pd
 
         float n = cameraController.GetCameraGroup().InverseTransformPoint(targetWindow.position).z; // distance to the near clip plane (screen)
         float f = cameraController.GetFarClipPlane(); // distance of far clipping plane
         float d = Vector3.Dot(va, vn); // distance from eye to screen
         float l = Vector3.Dot(vr, va) * n / d; // distance to left screen edge from the 'center'
         float r = Vector3.Dot(vr, vb) * n / d; // distance to right screen edge from 'center'
         float b = Vector3.Dot(vu, va) * n / d; // distance to bottom screen edge from 'center'
         float t = Vector3.Dot(vu, vc) * n / d; // distance to top screen edge from 'center'
 
         Matrix4x4 p = new Matrix4x4(); // Projection matrix
         p[0, 0] = 2.0f * n / (r - l);
         p[0, 2] = (r + l) / (r - l);
         p[1, 1] = 2.0f * n / (t - b);
         p[1, 2] = (t + b) / (t - b);
         p[2, 2] = (f + n) / (n - f);
         p[2, 3] = 2.0f * f * n / (n - f);
         p[3, 2] = -1.0f;
 
         cameraController.SetProjectionMatrix(p); // Assign matrix to camera
 
         if (drawFrustrumToWindow) { //Draw lines from the camera to the corners f the screen
             Debug.DrawRay(pe, va, Color.blue);
             Debug.DrawRay(pe, vb, Color.blue);
             Debug.DrawRay(pe, vc, Color.blue);
             Debug.DrawRay(pe, vd, Color.blue);
         }
 
     }

cameraController is my own custom class that manages multiple cameras (for post effects, etc).

targetWindow is a generic quad object that serves as the physical monitor. Place the quad where you want your "window" view.

I have the cameraController position being updated by the Kinect in a different class, but you don't need that to make this work.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

189 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Left side panel in monodevelop 1 Answer

Why don't I get access to current context with wglGetCurrentContext (OpenGL) 1 Answer

How to prevent an object from instantiating inside another object? 1 Answer

Mesh triangles and vertices, searching for adjacent triangle 1 Answer

Multiple coroutines starting from Update() freeze/crash Unity 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges