Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by lesmasamuray · May 05, 2017 at 12:50 AM · convertheightmapjava to c#

Heightmap From Texture - Script Converter

Hello everyone!

Soo, i was trying to convert this script, that was in JavaScript to C#

So far, i "accomplished" that, but even if the script wasn't reporting any compile erros, it won't work!

And by the way, this script is supposed to take a Texture2D and apply into the terrain HeightMap.

Thanks :)

link: Original Script

My "Converted" Script:

 static void ApplyHeightmap(int index)
     {
 
         var heightmap = heightMap[index];
 
         var terrain = Terrain.activeTerrain.terrainData;
         var w = heightmap.width;
         var h = heightmap.height;
         var w2 = terrain.heightmapWidth;
         var heightmapData = terrain.GetHeights(0, 0, w2, w2);
         var mapColors = heightmap.GetPixels();
         var map = new Color[w2 * w2];
 
         if (w2 != w || h != w)
         {
             // Resize using nearest-neighbor scaling if texture has no filtering
             if (heightmap.filterMode == FilterMode.Point)
             {
                 var dx = w / w2;
                 var dy = h / w2;
                 for (int y = 0; y < w2; y++)
                 {
                     var thisY = dy * y * w;
                     var yw = y * w2;
                     for (int x = 0; x < w2; x++)
                     {
                         map[yw + x] = mapColors[thisY + dx * x];
                     }
                 }
             }
             // Otherwise resize using bilinear filtering
             else
             {
                 var ratioX = 1.0f / w2 / (w - 1);
                 var ratioY = 1.0f / w2 / (h - 1);
                 for (int y = 0; y < w2; y++)
                 {
                     var yy = Mathf.Floor(y * ratioY);
                     var y1 = yy * w;
                     var y2 = (yy + 1) * w;
                     var yw = y * w2;
                     for (int x = 0; x < w2; x++)
                     {
                         var xx = Mathf.Floor(x * ratioX);
 
                         int temp_integer = (int)y1;
                         temp_integer += (int)xx;
                         var bl = mapColors[temp_integer];
                         temp_integer = (int)y1;
                         temp_integer += ((int)xx) + 1;
                         var br = mapColors[temp_integer];
                         temp_integer = (int)y2;
                         temp_integer += ((int)xx);
                         var tl = mapColors[temp_integer];
                         temp_integer = (int)y2;
                         temp_integer += ((int)xx) + 1;
                         var tr = mapColors[temp_integer];
 
                         var xLerp = x * ratioX - xx;
                         map[yw + x] = Color.Lerp(Color.Lerp(bl, br, xLerp), Color.Lerp(tl, tr, xLerp), y * ratioY - yy);
                     }
                 }
             }
         }
         else
         {
             // Use original if no resize is needed
             map = mapColors;
         }
 
         // Assign texture data to heightmap
 
         for (int y = 0; y < w2; y++)
         {
             for (int x = 0; x < w2; x++)
             {
                 heightmapData[y, x] = map[y * w2 + x].grayscale;
             }
         }
         terrain.SetHeights(0, 0, heightmapData);
     }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by Oriles · Oct 19, 2017 at 12:06 AM

After some fiddling I finally found the cause... It was some journey, let me tell you... After debugging I found that it was applying same grayscale (line 77) through out the whole terrain. So obviously there was some problem with rounding.


The Java is very forgiving in this it seems. I've replaced whole code to strict type (var => int, float, etc....), that gave me errors about not compatible types, which needed to be resolved. After that I've replaced all your integer shenanigans with FloorToInt methods, which led to fixing the FilterMode.Point path, but not bilinear filtering. But then... then I could't believe my eyes. Do you know why there is this whole problem?

This:

 var ratioX = 1.0f / w2 / (w - 1);
 var ratioY = 1.0f / w2 / (h - 1);

I did't go deep into this, but in Java the

 w2 and w - 1 have priority. In C# not.

if you do this:

 float ratioX = 1.0f / ((float) w2 / (w - 1));
 float ratioY = 1.0f / ((float) w2 / (h - 1));

It does NOT work :D it is still not enough. You have to do this:

 float ratioX = (1.0f / ((float) w2 / (w - 1)));
 float ratioY = (1.0f / ((float) w2 / (h - 1)));

Well without further ado, here is the final script in C#:

 using System.IO;
 using UnityEditor;
 using UnityEngine;

 public static class HeightmapFromTexture
 {
 [MenuItem("Terrain/Heightmap From Texture")]
 static void ApplyHeightmap()
 {
     //string heightmapPath = EditorUtility.OpenFilePanel("Texture", GetFolderPath(SpecialFolder.Desktop), ".png");

     Texture2D heightmap = Selection.activeObject as Texture2D;
     if (heightmap == null)
     {
         EditorUtility.DisplayDialog("No texture selected", "Please select a texture.", "Cancel");
         return;
     }

     var terrain = Terrain.activeTerrain.terrainData;
     int w = heightmap.width;
     int h = heightmap.height;
     int w2 = terrain.heightmapWidth;
     float[,] heightmapData = terrain.GetHeights(0, 0, w2, w2);
     Color[] mapColors = heightmap.GetPixels();
     Color[] map = new Color[w2 * w2];

     if (w2 != w || h != w)
     {
         // Resize using nearest-neighbor scaling if texture has no filtering
         if (heightmap.filterMode == FilterMode.Point)
         {
             float dx = (float) w / (float) w2;
             float dy = (float) h / (float) w2;
             for (int y = 0; y < w2; y++)
             {
                 if (y % 20 == 0)
                 {
                     EditorUtility.DisplayProgressBar("Resize", "Calculating texture", Mathf.InverseLerp(0.0f, w2, y));
                 }

                 int thisY = Mathf.FloorToInt(dy * y) * w;
                 int yw = y * w2;
                 for (int x = 0; x < w2; x++)
                 {
                     map[yw + x] = mapColors[Mathf.FloorToInt(thisY + dx * x)];
                 }
             }
         }
         // Otherwise resize using bilinear filtering
         else
         {
             float ratioX = (1.0f / ((float) w2 / (w - 1)));
             float ratioY = (1.0f / ((float) w2 / (h - 1)));
             for (int y = 0; y < w2; y++)
             {
                 if (y % 20 == 0)
                 {
                     EditorUtility.DisplayProgressBar("Resize", "Calculating texture", Mathf.InverseLerp(0.0f, w2, y));
                 }

                 int yy = Mathf.FloorToInt(y * ratioY);
                 int y1 = yy * w;
                 int y2 = (yy + 1) * w;
                 int yw = y * w2;

                 for (int x = 0; x < w2; x++)
                 {
                     int xx = Mathf.FloorToInt(x * ratioX);

                     Color bl = mapColors[y1 + xx];
                     Color br = mapColors[y1 + xx + 1];
                     Color tl = mapColors[y2 + xx];
                     Color tr = mapColors[y2 + xx + 1];

                     float xLerp = x * ratioX - xx;
                     map[yw + x] = Color.Lerp(Color.Lerp(bl, br, xLerp), Color.Lerp(tl, tr, xLerp), y * ratioY - (float)yy);
                 }
             }
         }
         EditorUtility.ClearProgressBar();
     }
     else
     {
         // Use original if no resize is needed
         map = mapColors;
     }

     // Assign texture data to heightmap

     for (int y = 0; y < w2; y++)
     {
         for (int x = 0; x < w2; x++)
         {
             heightmapData[y, x] = map[y * w2 + x].grayscale;
         }
     }

     terrain.SetHeights(0, 0, heightmapData);
 }
 }

Happy coding @lesmasamuray

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by jwankaro032 · Jan 27, 2019 at 02:31 PM

Love u bro <3

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

103 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Convert Java to C# 2 Answers

Can someone help me please? 0 Answers

change my code from java to c# 1 Answer

JavaScript to C# 1 Answer

Convert Java to C# 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges