- Home /
Convert texture coordinates into a polar version?
Hi there!
So basically, as the title says, I'm trying to convert the texture coordinates into a polar/spherical texture. However I dont manage to get it to work (tried many things out there like:
https://stackoverflow.com/questions/58910317/is-it-accurate-to-convert-latitude-and-longitude-into-3d-coordinate-on-sphere https://stackoverflow.com/questions/25932175/glsl-es-mapping-texture-from-rectangular-to-polar-coordinates-with-repeating and many more things)
I know this is achievable since LibNoise has its own version implemented (I also tried to adjust it but doesnt work either). Let me show you an example texture: https://ibb.co/KVFHKJj
You can see the effect Im trying to get in the poles, where the texture gets distorted the closer you are to the poles. What I'd like is to get that same effect when setting/creating a new texture from script, but I cant manage to get it no matter how hard I try....
To further understand, I must explain a little more: I have a texture created from script (that is mapped in a sphere/planet) so when the user clicks on it, a new city is created (that basically, change the pixels colors around the click position so it looks like a city):
https://ibb.co/T0d2YWk
The problem is if you click near the poles, the pixels are weirdly place, like this: https://ibb.co/GRgyFqk
And this is the code so far, if anyone can help or know anyway to accomplish this I will highly appreciate it. Thanks! //Simply get a the clicked point and draw a ray + create a temporal texture
var ray = Camera.main.ScreenPointToRay(Input.mousePosition); Renderer renderer = hit.transform.GetComponent(); MeshCollider meshCollider = hit.collider as MeshCollider; Material thisMat = renderer.material; Texture2D heightMap = renderer.material.GetTexture("_HeightMap") as Texture2D; Texture2D tempTex = new Texture2D(nightTex.width, nightTex.height); Graphics.CopyTexture(thisMat.GetTexture("_Lights") as Texture2D, tempTex);
int thisCitySeed = UnityEngine.Random.Range(int.MinValue, int.MaxValue);
UnityEngine.Random.InitState(thisCitySeed);
Vector2 pixelCenter = hit.textureCoord;
pixelCenter.x *= nightTex.width;
pixelCenter.y *= nightTex.height;
int ringsPerCity = UnityEngine.Random.Range(10, 150);
//This FOR loop is basically for the city shape and such stuff
//Also, I suppose here is where I would calculate the polar coordinate of each calculate pixel so I can move it to the proper place, but i dont know how
for (int i = 0; i < ringsPerCity; i++)
{
float cityRadius = 0f + (i / 2f);
float lightsAngle = UnityEngine.Random.Range(1f, 10f);
int maxLoop = lightsAngle >= 6f ? 100 : 80;
int loop = i == 0 ? maxLoop - 100 : maxLoop / i;
for (int j = 0; j < loop; j++)
{
var offset = new Vector2(Mathf.Sin(lightsAngle), Mathf.Cos(lightsAngle)) * cityRadius;
tempTex.SetPixel((int)pixelCenter.x + (int)offset.x, (int)pixelCenter.y + (int)offset.y, Color.white);
lightsAngle++;
}
}
tempTex.Apply();
If is needed any more information please ask! And please note that this is not an end version of the script, its only for testing purposes, once I get it working I will change it so the code is clearer!
**EDIT: Please, this is effect I am looking for: https://graphicdesign.stackexchange.com/questions/111391/how-can-i-distort-an-image-to-wrap-around-a-sphere-with-no-pinching**
Your answer
Follow this Question
Related Questions
.tex to assetbundle 0 Answers
Unity 5 upside down texture on 1 side of cube 1 Answer
Shader Forge can't work in Unity2017 0 Answers
Fps drops after entering house 0 Answers
Assigning UV Map to model at runtime 0 Answers