why the sphere texture map can not actually match

2019-06-12 04:38发布

i have a sphere in my 3d project, and i have an earth texture, i use the algorithm from wiki to calculate the texture coordinate. the code in my effect file look like this:

float pi = 3.14159265359f;
output.uvCoords.x = 0.5 + atan2(input.normal.z, input.normal.x) / (2 * pi);
output.uvCoords.y = 0.5f - asin(input.normal.y) / pi;

the result is the picture below:

  1. look from left( there is a line, this is my question) enter image description here

  2. look from front

enter image description here

3.look from right

enter image description here

2条回答
forever°为你锁心
2楼-- · 2019-06-12 04:46

finally i figure it out by myself, The problem lies in the fact, that i'am calculating the texture coordinates in the vertex shader. The problem is that one vertex is on the far right of the texture, while the other 2 vertices of a triangle are on the far left of the texture, which results in almost the whole texture being visible on such a triangle. so there is a line of jumbled texture coords. the solution is i should send the normal to pixel shader and calculate the texture coord in the pixel shader

查看更多
聊天终结者
3楼-- · 2019-06-12 04:53

Not pretend to be a complete answer at all, but there are some ideas:

  • Try 6.28 instead 6.18, because 3.14 * 2 = 6.28. It is always a good idea to create variables or macro instead of plain numbers, to prevent such sad mistakes in future
  • Try to use more precise value of Pi (numbers to the right of the decimal point)
  • Try to normalize normal vector before calculations
  • Even better calculate texcoords on CPU once and for all, instead of calculating on each shader invocation. You can use any asset library for this purpose or just quickly move your HLSL to main code.

    #define PI          3.14159265359f
    #define PImul2      6.28318530718f  // pi*2
    #define PIdiv2      1.57079632679f  // pi/2
    #define PImul3div2  2.09439510239   // 3*pi/2
    #define PIrev       0.31830988618f  // 1/pi
    ...
    

Hope it helps.

查看更多
登录 后发表回答