Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Method()
- {
- // Loop through the pixels
- for (int y = 0; y < IMGwidth; y++)
- {
- for (int x = 0; x < IMGheight; x++)
- {
- // Longitude
- double lon = ((i + x) * 360d / IMGwidth) % 360d - 180d;
- // Latitude
- double lat = ((j + y) * 360d / IMGwidth) % 180d - 90d;
- if (lat < -90) lat += 180;
- // Create a VertexBuildData
- PQS.VertexBuildData data = new PQS.VertexBuildData
- {
- directionFromCenter = body.GetRelSurfaceNVector(lat, lon).normalized,
- vertHeight = pqs.radius
- };
- // Build from the Mods
- modOnVertexBuildHeight(data);
- // Adjust the height
- double height = (data.vertHeight - pqs.radiusMin) / pqs.radiusDelta;
- if (data.vertHeight < pqs.radius)
- height = (pqs.radius - pqs.radiusMin) / pqs.radiusDelta;
- if (height < 0)
- height = 0;
- else if (height > 1)
- height = 1;
- // Find First and last Latitude pixels
- normalMapValues[(y * tile) + x ] = height * pqs.radiusDelta;
- }
- }
- // Apply the maps
- if (exportNormalMap)
- normalMap = CreateNormalMap(normalMapValues, pqs);
- }
- Texture2D CreateNormalMap(double[] normalMapValues, PQS pqs)
- {
- double dS = pqs.radius * 2 * Math.PI / width;
- Texture2D texture = new Texture2D(IMGwidth, IMGheight, TextureFormat.ARGB32, true);
- for (int y = 0; y < texture.height; y++)
- {
- for (var x = 0; x < texture.width; x++)
- {
- // force slope = 0 at the poles
- if (y == 0 || y == IMGheight - 1)
- {
- texture.SetPixel(x, y, new Color(0.5f, 0.5f, 0.5f, 0.5f));
- }
- // otherwise calculate it from the terrain
- else
- {
- int xN = x - 1;
- int xP = x + 1;
- int yN = y - 1;
- int yP = y + 1;
- // shift all by one since `normalMapValues` has an extra frame of 1 pixel
- double dX = normalMapValues[(y * tile) + xP] - normalMapValues[(y * tile) + xN];
- double dY = normalMapValues[(yP * tile) + x] - normalMapValues[(yN * tile) + x];
- double slopeX = (1 + dX / Math.Pow(dX * dX + dS * dS, 0.5) * normalStrength) / 2;
- double slopeY = (1 - dY / Math.Pow(dY * dY + dS * dS, 0.5) * normalStrength) / 2;
- texture.SetPixel(x, y, new Color((float)slopeY, (float)slopeY, (float)slopeY, (float)slopeX));
- }
- }
- }
- texture.Apply();
- return texture;
- }
Advertisement
Add Comment
Please, Sign In to add comment