Sigma88Mods

normal maps

Feb 7th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. void Method()
  2. {
  3. // Loop through the pixels
  4. for (int y = 0; y < IMGwidth; y++)
  5. {
  6. for (int x = 0; x < IMGheight; x++)
  7. {
  8. // Longitude
  9. double lon = ((i + x) * 360d / IMGwidth) % 360d - 180d;
  10.  
  11. // Latitude
  12. double lat = ((j + y) * 360d / IMGwidth) % 180d - 90d;
  13. if (lat < -90) lat += 180;
  14.  
  15. // Create a VertexBuildData
  16. PQS.VertexBuildData data = new PQS.VertexBuildData
  17. {
  18. directionFromCenter = body.GetRelSurfaceNVector(lat, lon).normalized,
  19. vertHeight = pqs.radius
  20. };
  21.  
  22. // Build from the Mods
  23. modOnVertexBuildHeight(data);
  24. // Adjust the height
  25. double height = (data.vertHeight - pqs.radiusMin) / pqs.radiusDelta;
  26.  
  27. if (data.vertHeight < pqs.radius)
  28. height = (pqs.radius - pqs.radiusMin) / pqs.radiusDelta;
  29.  
  30. if (height < 0)
  31. height = 0;
  32. else if (height > 1)
  33. height = 1;
  34.  
  35. // Find First and last Latitude pixels
  36. normalMapValues[(y * tile) + x ] = height * pqs.radiusDelta;
  37. }
  38. }
  39.  
  40. // Apply the maps
  41. if (exportNormalMap)
  42. normalMap = CreateNormalMap(normalMapValues, pqs);
  43. }
  44.  
  45.  
  46. Texture2D CreateNormalMap(double[] normalMapValues, PQS pqs)
  47. {
  48. double dS = pqs.radius * 2 * Math.PI / width;
  49.  
  50. Texture2D texture = new Texture2D(IMGwidth, IMGheight, TextureFormat.ARGB32, true);
  51. for (int y = 0; y < texture.height; y++)
  52. {
  53. for (var x = 0; x < texture.width; x++)
  54. {
  55. // force slope = 0 at the poles
  56. if (y == 0 || y == IMGheight - 1)
  57. {
  58. texture.SetPixel(x, y, new Color(0.5f, 0.5f, 0.5f, 0.5f));
  59. }
  60. // otherwise calculate it from the terrain
  61. else
  62. {
  63. int xN = x - 1;
  64. int xP = x + 1;
  65.  
  66. int yN = y - 1;
  67. int yP = y + 1;
  68.  
  69. // shift all by one since `normalMapValues` has an extra frame of 1 pixel
  70. double dX = normalMapValues[(y * tile) + xP] - normalMapValues[(y * tile) + xN];
  71. double dY = normalMapValues[(yP * tile) + x] - normalMapValues[(yN * tile) + x];
  72.  
  73. double slopeX = (1 + dX / Math.Pow(dX * dX + dS * dS, 0.5) * normalStrength) / 2;
  74. double slopeY = (1 - dY / Math.Pow(dY * dY + dS * dS, 0.5) * normalStrength) / 2;
  75.  
  76. texture.SetPixel(x, y, new Color((float)slopeY, (float)slopeY, (float)slopeY, (float)slopeX));
  77. }
  78. }
  79. }
  80.  
  81. texture.Apply();
  82. return texture;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment