why the sphere texture map can not actually match

落爺英雄遲暮 提交于 2019-12-10 12:09:22

问题


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)

  2. look from front

3.look from right


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/19702285/why-the-sphere-texture-map-can-not-actually-match

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!