Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by cod · Jul 24, 2012 at 08:41 PM · javascriptterrainarraytexture2deditorutility

What's wrong with this script?

 #pragma strict
 @MenuItem ("Terrain/Set Detail Layers")

 static function SetDetailLayers () {
 var splatmap : Texture2D = Selection.activeObject as Texture2D;
 if (splatmap == null) { 
 EditorUtility.DisplayDialog("No texture selected", "Please select a texture", "Cancel"); 
 return; 
 }
 
 var wi = splatmap.width;
 if (splatmap.height != wi) {
 EditorUtility.DisplayDialog("Wrong size", "Splatmap width and height must be the same", "Cancel"); 
 return;
 }
 
 if (Mathf.ClosestPowerOfTwo(wi) != wi) {
 EditorUtility.DisplayDialog("Wrong size", "Splatmap width and height must be a power of two", "Cancel"); 
 return; 
 }

 var terrain = Terrain.activeTerrain.terrainData;
 var w : int = terrain.detailWidth;
 var h : int = terrain.detailHeight;
 var i : int = 0;
 var splatmapData : int[,] = terrain.GetDetailLayer(0,0,w,h,0);
 
 var mapColors : Color[] = splatmap.GetPixels();
 var grayScales : float[] = new float[mapColors.length];

 for(i=0;i<mapColors.length;i++){
 grayScales[i] = mapColors[i].grayscale;
 }

 Debug.Log(grayScales);

 //var splatDetail : int[,] = grayScales;
 //Debug.Log(splatDetail);
 
 var x : int = 0;
 var y : int = 0;
 
 for (y = 0; y &lt; w; y++) {
 for (x = 0; x &lt; w; x++) {
 splatmapData[x,y] = grayScales[y];

 }
 }
 
 
 terrain.SetDetailLayer(0, 0, 0, splatmapData);

  }
Comment
Add comment · Show 1
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 DaveA · Jul 24, 2012 at 08:42 PM 4
Share

Edit your post, select the code, hit the 010/101 button to format it, and indicate line 30, what error is it giving?

4 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Mizuho · Jul 25, 2012 at 09:51 AM

I don't see how it's a problem with nulls. The obvious problem is that he's trying to set a int[,] multi-dimensional array using a Color[] array. I'm sorry, whoever wrote this code, but int[,] != Color[]. Simple as that.

This is your culprit:

 var mapColors : Color[] = splatmap.GetPixels();
 var splatDetail : int[,] = mapColors;

I'm not sure how exactly you intended to convert that over (I assume it was int[width,height]?), but just dropping in mapColors isn't going to work. You'll need to cycle through all the Colors and put them into that splatDetail yourself.

On another note, Debug.Log(splatDetail); isn't going to give you much other than null or the System.Type for multi-dimensional int arrays.

Comment
Add comment · Show 8 · 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 Bovine · Jul 25, 2012 at 09:58 AM 0
Share

his non-strict version of the code he had posted was complaining of a null reference exception

avatar image cod · Jul 25, 2012 at 09:58 AM 0
Share

the point is that I can convert Color to int[,,], but if I want to use a grayscale texture to paint detail layers, how can I do that? I tried with splatmap.GetPixels().grayscale, but this doesn't work :(

avatar image Mizuho · Jul 25, 2012 at 10:03 AM 0
Share

@Bovine: I see. The edited code makes it reaaaaaally easy to spot the one I pointed out. Truly, #pragma strict is your friend.

@cod: What did you use that with? splatmap.SetPixels(splatmap.GetPixels().grayscale)?

avatar image Bovine · Jul 25, 2012 at 10:04 AM 0
Share

You need to get a specific pixel and get the grayscale value of that. Unless JS has convenience syntax that allows you to call a property on all contents of an array, that code isn't going to work.

This however should do:

float greyScale = splatmap.GetPixel(x, z).grayscale;

For each pixel, but it might be more efficient to get the color[] array back and walk it as I suggested below.

avatar image Bovine · Jul 25, 2012 at 10:07 AM 0
Share

pragma strict

This is recommended in the Unity docs. I don't have this problem in C# :oD

I believe strict is also faster:

http://docs.unity3d.com/Documentation/ScriptReference/index.Performance_Optimization.html

Show more comments
avatar image
1

Answer by Bovine · Jul 24, 2012 at 09:12 PM

Some variable on line 30 of your code is null.

Possibly splatmapData or terrain, but it's hard to say.

I suggest you add a breakpoint and then walk over the code to see what the null variable is - or count the lines in the script :o)

UPDATE:

Since you've changed the code in your example above to a strict version and the error has changed, it seems clear that you are trying to set a multi-dimensional array from a single dimensional array.

I have moved my comment up and deleted the comment:

Your Color array has one dimension and your splatDetail has two - Unity cannot convert one to the other. What happened to the grayscale property?

You'll have to walk your one dimensional Color array manually:

splatmapData[x,y] = mapColors[x + y * w].grayscale;

Perhaps? or Similar depending the order of rows and columns returned by GetPixels(). Presuming this is the right width?

http://docs.unity3d.com/Documentation/ScriptReference/Texture2D.GetPixels.html

Comment
Add comment · Show 7 · 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 cod · Jul 24, 2012 at 09:38 PM 0
Share

well, splatDetail is null, but I don't really undertand why...

avatar image Bovine · Jul 24, 2012 at 09:45 PM 0
Share

if splatDetail is null, then mapColors.grayscale must also be null.

avatar image cod · Jul 24, 2012 at 09:46 PM 0
Share

I didn't vote, but maybe because is unclear

avatar image Bovine · Jul 24, 2012 at 09:47 PM 0
Share

I know you didn't - I can't be more clear than that. The why is largely for you to understand by exa$$anonymous$$ing the code in the debugger. I was just trying to get you started, oh well.

avatar image cod · Jul 24, 2012 at 10:02 PM 0
Share

ok, so I found the problem is in the "mapColors.grayscale;" but I don't know why...

Show more comments
avatar image
1

Answer by Seth-Bergman · Jul 25, 2012 at 10:11 AM

have you tried separating the two? Once mapColors is filled, cycle through it and grab the grayscale values..

 var mapColors : Color[] = splatmap.GetPixels();
 var grayScales : float[] = new float[mapColors.length];
 
 for(var i=0;i<mapColors.length;i++){
 grayScales[i] = mapColors[i].grayscale;
 }

then you would walk through it:

    var counter = 0;
 
    for(var j=0;j<splatmap.height;j++){
    for(var k=0;k<splatmap.width;k++){
    splatMapData[k,j] = (grayscales[counter] * 10) as int;
    counter++;
    }
 
    }

or something similar..

EDIT:

if you're using this to call SetDetailLayer, though, then you need an int array, so instead maybe:

   for(var j=0;j<splatmap.height;j++){
        for(var k=0;k<splatmap.width;k++){
        splatMapData[k,j] = parseInt(grayscales[counter] * 10);
        counter++;
        }    
        }
Comment
Add comment · Show 13 · 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 cod · Jul 25, 2012 at 10:23 AM 0
Share

doesn't work, because in that manner u just pick out floats from the color array, while if I set directly grayscale to the Color[] it says me that grayscale is not a component of Color[]

avatar image Seth-Bergman · Jul 25, 2012 at 10:26 AM 0
Share

oh my mistake, it would need to be a separate array, I'll fix it... ok fixed, not clear whether this is actually your current issue, but this creates an array of the grayscales from the color array

avatar image Seth-Bergman · Jul 25, 2012 at 10:32 AM 0
Share

as suggested before, you would then need to walk through it to re-allot it to a 2d array of course

edited for that too..

avatar image cod · Jul 25, 2012 at 10:47 AM 0
Share

edit : it works but I can't see details on my terrain :(

avatar image Seth-Bergman · Jul 25, 2012 at 12:15 PM 1
Share

what is it you are trying to change? Either way, looks like SetDetail$$anonymous$$aps takes an int[,].. which tells the number of details at that coordinate. is that what you want?

Show more comments
avatar image
0

Answer by cod · Jul 25, 2012 at 01:43 PM

Here's the complete script

  #pragma strict
 @MenuItem ("Terrain/Set Detail Layers")

 static function SetDetailLayers () {
 var splatmap : Texture2D = Selection.activeObject as Texture2D;
 if (splatmap == null) { 
 EditorUtility.DisplayDialog(&quot;No texture selected&quot;, &quot;Please select a texture&quot;, &quot;Cancel&quot;); 
 return; 
 }
 
 var wi = splatmap.width;
 if (splatmap.height != wi) {
 EditorUtility.DisplayDialog(&quot;Wrong size&quot;, &quot;Splatmap width and height must be the same&quot;, &quot;Cancel&quot;); 
 return;
 }
 
 if (Mathf.ClosestPowerOfTwo(wi) != wi) {
 EditorUtility.DisplayDialog(&quot;Wrong size&quot;, &quot;Splatmap width and height must be a power of two&quot;, &quot;Cancel&quot;); 
 return; 
 }

 var terrain = Terrain.activeTerrain.terrainData;
 var w : int = terrain.detailWidth;
 var h : int = terrain.detailHeight;
 var i : int = 0;
 var splatmapData : int[,] = terrain.GetDetailLayer(0,0,w,h,0);
 
 var mapColors : Color[] = splatmap.GetPixels();
 var grayScales : float[] = new float[mapColors.length];

 for(i=0;i<mapColors.length;i++){
 grayScales[i] = mapColors[i].grayscale;
 }
 
 var j : int = 0;
 var k : int = 0;
 
 var counter : int = 0;
 
   for(j=0; j<splatmap.height;j++){

for(k=0; k

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

8 People are following this question.

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

Related Questions

How to smooth out the values? 1 Answer

Texture 2D array to GUI.Window rows iOS 0 Answers

loading a folder of textures to an array 0 Answers

PackTextures Horizontally? (1 Row) or Alternative Method 1 Answer

Binding Key to first item in array 0 Answers


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