3

I have been losing my sanity on this for a while now. Just disregard the part which is commented out (Not the problem). I am trying to do a 360 degree longitude and 180 degree longitude sweep and then raycast. But apparently the direction vector (Dir in this case) is stuck at (0, 0, -1). I checked the x y and z values and they are working fine. I have no idea now what is wrong. Please answer before I kill myself. sigh

    {
        Vector3 Dir = new Vector3(0.0f, 0.0f, 0.0f);
        Output = new Texture2D(2160, 1080);
        for (int i = 0; i < 1080; i++)
        {
            float theta = (Mathf.Deg2Rad * i * 1.0f) / 6.0f;
            for (int j = 0; j < 2160; j++)
            {
                float phi = (Mathf.Deg2Rad * j) / 6.0f;
                float x = Mathf.Sin(theta) * Mathf.Cos(phi);
                float y = Mathf.Sin(theta) * Mathf.Sin(phi);
                float z = Mathf.Cos(theta);
                Dir = new Vector3(x, y, z);
                Debug.Log(x + " " + y + " " + z);
                /* RaycastHit raycastHit;
                if (Physics.Raycast(transform.position, Dir, out raycastHit))
                {
                    Renderer rend = raycastHit.transform.GetComponent<MeshRenderer>();
                    Texture2D tex = rend.material.mainTexture as Texture2D;
                    Vector2 pCoord = raycastHit.textureCoord;
                    pCoord.x *= tex.width;
                    pCoord.y *= tex.height;
                    Color color = tex.GetPixel((int)(pCoord.x * rend.material.mainTextureScale.x), (int)(pCoord.y * rend.material.mainTextureScale.y));
                    Output.SetPixel(j, i, color);
                    Debug.Log("Ray hit an object! @  theta " + i.ToString() + " phi " + j.ToString() + " Color: " + color.ToString() + "Hit Coordinate x: " + pCoord.x + " y: " + pCoord.y);
                }
                */
            }
        }
        //System.IO.File.WriteAllBytes("D:\\FYProject\\WebcamSnaps\\" + naming + ".png", Output.EncodeToPNG());
    }
  • What do you mean by "stuck at `(0,0,-1)`? Is that the value of your Vector3 after calling `Dir = new Vector3(x, y, z);`? Your code seems fine, but as an optimization I would reassign x, y and z on the Vector3 instead of creating a new one each loop iteration. – Antoine Thiry Mar 18 '21 at 08:02
  • By stuck on (0,0,-1) I mean I am getting nothing other than (0,0,-1) in any iteration. The value of Dir stays the same. It is the same as you said. I tried reassigning just now before I am writing this but am still getting the same results. – Khizar Shakoor Mar 18 '21 at 08:09
  • I took your code and run it and the `(0,0,-1)` stuck you mention does not happen. The values change accordingly for the direction in the loop. Other question is if the behaviour is the desired one, but the values change in the loop and the `Dir` vector accordingly so I would check the logs in case there might be something else that you are logging to the console – rustyBucketBay Mar 18 '21 at 08:37
  • Ok so, I did a bit of testing, I think your math is not correct somehow (not the best at math). But here's what I found: your x,y and z value are 0,0,1 while `theta` is equal to 0. On your frst 2160 iterations theta will be 0, only after it will go above and you will have different values on x,y and z – Antoine Thiry Mar 18 '21 at 08:40
  • Only the Dir vector is stuck at 0,0,1 and the theta should be 0 for the first iteration. A theta of zero means maximum elevation. The issue that I am facing is that the dir vector does not change value on any iteration. Its stuck on the first calculated value. About the math, it is from text book of coordinate systems so I dont think it would be wrong. At this point Im sure it's just messing with me. – Khizar Shakoor Mar 18 '21 at 11:44
  • Here is the maths that I used https://physicscatalyst.com/graduation/spherical-coordinates-system/ – Khizar Shakoor Mar 18 '21 at 11:53

1 Answers1

1

In one of your comments, you stated "I am getting nothing other than (0,0,-1) in any iteration."

Your code has an external loop with an iteration factor of 1080. You inner loop has an iteration factor of 2160. In total, your combined loops produce 2,332,800 output values. I do not believe you verified the output of over two million debug statements. I think you checked the last few dozen values, saw the same output over and over again, and assumed that pattern continued all the way up the chain. That was your first mistake.

Your second mistake was wasting several hours assuming a formatting feature was a bug.

Vector3.ToString() rounds its output to one digit after the decimal. If you print the components individually, they'll show what you'd expect.

You have my sympathies.

VPellen
  • 1,071
  • 5
  • 10