Trying to do some kind of portal...

Hello!
For a project of mine, I’m trying to create portals that you can shoot through. The solution I’ve found is to use render textures. It works pretty well, but, as you can imagine, the fidelity is not it’s strongest point.
Let me give an example: You’re looking to your side of the portal. Then, an enemy pass near the other side, and you try to shoot him. Something like this should work:

void Shoot(Vector3 origin, Vector3 dir, float damage) {
if(Physics.Raycast(origin, dir, out hit)) {
			point = hit.point;
			if(hit.collider.tag == "Enemy") {
				Health healthScript = hit.collider.gameObject.GetComponent<healthScript>();
				if(healthScript) {
					healthScript.AdjustHP(-damage);
				}
			}
			if(hit.collider.tag == "Portal") {
				Vector3 anotherPortal = myTwin.position;
				Shoot (damage, anotherPortal, (myCamera.position - point).normalized * -1);
			}
		}
}

If the Raycast finds a portal in its way, it calls the shoot method recursively until an enemy or nothing is hit. I went happily to test my code. At first, it looked successful, but two or three minutes later, it seemed totally wrong. And the reason is simple: different perspectives. The problem is not in the shoot algorithm, but in the way render textures are rendered on a plane.
Imagine the following situation: You create a portal at a wall, and another at the opposite side of the wall. It should work as a hole that you can pass through. Unfortunately, it doesn’t work this way. After a lot of tests, I realized that the problem is in the fact that the other camera begins its fustrum in a different spot than yours, so the perspective is distorted. I don’t know if I explained myself well, but an image talks more than words:

If you look with attention, you’ll notice that the same cuboid that appears within the area of the portal can be seen outside the portal, what is not reasonable with the given example. I’ve tried a LOT of different approachs, rotate the camera, weird Vectorial math, but nothing solved my problem. Does anybody has a clue about how to do this properly?

Oddly, this post was automatically put on the “admin moderation” list for no apparent reason. I have enabled it and posted here so it will be placed back into circulation.

I have no idea what the question being asked is.

You first start talking about bullets.

Then view fustrum of the camera for the portal being off.

Then clipping of a cube in a portal.

What’s wrong?

Yeah, maybe it seems confuse. The problem is: I want my portal to work like a hole in a wall, not like a security camera. Is it clear now?

Edit: I really need precision. Precision enough to the portal not being noticeable without some trippy effect around it. And that camera perspective distortion is really annoying in this case. Am I clearer now? If you need me to draw, I’ll draw.

So your question is just:

how do you accomplish a portal?

Actually my question is: How to render the portal properly (I mean, without distortion). I prefer the theoretical solution instead of the full code, though.

Aw, so you’re dealing with the fact that it renders as a flat surface… which means that as you move about the relative view stays looking flat. Causing it to look more like you’re looking at a TV screen in the level then like a portal?

So lets say you have camera V, this is your normal viewing camera. And you have camera P, this is your camera that represents the portal.

When you’re facing the portal entrance, V has a certain view direction, it’s forward. The angle it hits the entrance needs to be refracted on the portal exit. Giving you the leaving vector from the portal exit. It’d be like… you’re basically figuring out at what direction you’d be leaving the portal if you enter at such direction (like a bullet).

You’ll be basing the transformation of said forward vector off the normals of both planes.

Now you just make P face in the appropriate direction at the exit. Now you get that appropriate leave feel.

This same method is how bullets will work.

It’s also how walking through will work.

Simple equation to get the rotation of said vector as a quaternion. This rotation you can just append to things to get the correction directions (append, not set).

v1 := entrance normal
v2 := exit normal
f1 := forward
q = Quat.FromToRot( -v1, v2 )
f2 = q * f1

So your camera P’s rotation would be V.rotation * q
Your bullets direction would be f2
Your rotation will append q to itself.

Ok, you’re getting into it. I’ve already done it, but the REAL problem is that it doesn’t matter how you redirect the camera, it’ll always seem wrong. Because your camera’s field of view begins before the portal’s FOV. This should explain better:
1062437--39502--$Problema clipping.png

your near plane it to far away…

move your camera back as far as the near plane is on the portal

your near planes distance should be set to the value that would allow the near planes size in the frustum be the size of the portal entrance

The camera will pivot around the point that is the portal’s center. Like a camera on a boom that faces its boom’s fulcrum.

I can see another issue coming though…

there’s going to be a issue with the facing direction of the texture rendered.

I’m betting a shader that renders the texture “facing the camera”, like you might do for grass or something should help. The actual geometry stays facing the correct direction, just the uv’s move to face the camera, and clip off the overhanging parts of the rotated UV relative to the geometry.

Now it’s getting tricky. Can you explain it better? I haven’t understand well the previous post too.