- Home /
Projecting a plane onto a sphere
So, I have a Sphere (right in the attached image), a camera (in the center of the sphere) and a Quad in between (the Ubuntu Logo). What I want to achieve is making some kind of projection from the cam to the sphere so I can get the Ubuntu logo printed in the sphere. The goal is getting the pixels of the texture and getting the equirectangular projection of the logo (like the quad in the left).
My first thought was raycasting every pixel in the logo and painting it in the sphere, but doing it with c# is too slow and not as good as I thought.
After this i thought using a blending shader so the logo is printed on the sphere. But I don't think there's a way to write the texture from that.
Does anybody have any better idea? or anybody knows a way to get the information from the zbuffer to get the pixel that is printed on top of the ones in my sphere texture?
Thanks so much :)
At first, I would try Projector. http://docs.unity3d.com/$$anonymous$$anual/class-Projector.html If that doesn't work, you could use two spheres (1st with default image, 2nd with ubuntu logo) Event If the previous solutions don't work you can always write a custom shader for that purpose.
Yes, that's not my problem, I know how to show the sphere with the logo. what I want to achieve is writing that information in the sphere texture (in the .jpg) so i get the projection of the logo in the 2D texture as a .jpg/.png.
I'm starting to think I'll have to go with opencv+python. I was trying to avoid that :P
Answer by varumora · Mar 15, 2016 at 08:49 PM
OK, I finally solved it thanks to this:
http://mathinsight.org/spherical_coordinates http://mathworld.wolfram.com/CylindricalEquidistantProjection.html
If someone wants to know how, I just followed my first instinct. Iterating for every frame of the sphere's texture, getting its position in real world and raycasting to the camera. If the raycast passes through the logo I get the color of the raycasthit and paint my sphere texture.
Nice result :')
Hi, varumora! Glad that You solved this problem. Can You share the code?
Answer by devorenge · Feb 29, 2020 at 06:10 AM
I know this is a really old question, But I would like to share a small snippet of code that replicates the askers solution, because I stumbled upon this post today and had to painstakingly implement this rough concept. This code is pretty slow and isn't perfect, but it works. It loops through the entire texture of the sphere and draws anything to it that is in the path from the zero vector to the radius of the sphere.
for (int x = 0; x < sphereTex.width; ++x) {
for (int y = 0; y < sphereTex.height; ++y) {
float phi = y * Mathf.PI / sphereTex.height;
float theta = x * (1 / Mathf.Cos(l1)) * Mathf.PI / (sphereTex.width / 2);
//Debug.DrawLine(new Vector3(r * Mathf.Sin(phi) * Mathf.Cos(theta), r * Mathf.Cos(phi), r * Mathf.Sin(phi) * Mathf.Sin(theta)),
// new Vector3(0, 0, 0), Color.magenta, 1f);
RaycastHit rch = new RaycastHit();
bool hit = Physics.Raycast(new Vector3(0, 0, 0), new Vector3(r * Mathf.Sin(phi) * Mathf.Cos(theta), r * Mathf.Cos(phi), r * Mathf.Sin(phi) * Mathf.Sin(theta)), out rch, r - 3);
if (hit && rch.distance > 3) {
Debug.Log("hit at: " + x + " " + y);
Debug.Log("distance: " + rch.distance);
sphereTex.SetPixel(-x, -y, exampleTex.GetPixel((int)(-rch.textureCoord.x * exampleTex.width), (int)(rch.textureCoord.y * exampleTex.height)));
}
}
}