- Home /
count <= std::numeric_limits::max() error when viewing custom trees on terrain
Since the premade trees in Unity weren't what I needed, I modeled my own tree and leaf objects and wrote a script to place them in the terrainData with the position data taken from an image file. There are several problems: The trees appear black, they only appear in the bottom left corner of each terrain object, and I keep getting this bizarre "count <= std::numeric_limits::max() UnityEditor.DockArea:OnGUI()" error. This makes absolutely no sense. All of the public values are either floats, ints, or objects, there are no unsigned shorts accessible from the dock area.
When I manually paint on the trees using the terrain tree painter, I don't get any errors and they appear grey (I have yet to texture them, so this coloration is expected). When I rotate the view around, I get more errors identical to the ones I already have. When manually remove the trees that were generated, I stop getting errors.
The two classes work by taking an image texture, checking if a pixels alpha channel is greater then zero, using its coords to determine its rotation and alpha channel to determine which custom tree prefab to use, and finally storing the information in a custom class 2d array.
Then during the update phase it creates an array of trees and applies it to the TerrainData of each terrain object.
The HeightMapTerrainChunkHandler3x3 class:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
[ExecuteInEditMode]
public class HeightMapTerrainChunkHandler3x3 : MonoBehaviour
{
public struct XY_AndValueArray{
public float[] xArray;
public float[] yArray;
public int[] valueArray;
public XY_AndValueArray(float[] xArray, float[] yArray, int[] valueArray){
this.xArray = xArray;
this.yArray = yArray;
this.valueArray = valueArray;
}
}
public int HeightMapResolution = 513;
XY_AndValueArray[,] treePositions;
void Start ()
{
planes = GameObject.FindGameObjectsWithTag ("DeformPlane");
if (Application.isPlaying) {
Terrain dummyTerrain = GameObject.Find ("TerrainDummy").GetComponent<Terrain>();
for (int i = 0; i < planes.Length; i++) {
HeightMapTerrainChunk3x3 planesChunk = planes [i].GetComponent<HeightMapTerrainChunk3x3> ();
planesChunk.InitTerrain(dummyTerrain, HeightMapResolution);
}
treePositions = initTreePositions(dummyTerrain.terrainData.treePrototypes.Length);
}
}
XY_AndValueArray[,] initTreePositions(int numberOfTreeTypes){
XY_AndValueArray[,] xyValueArray = new XY_AndValueArray[NumberOfChunksAcross, NumberOfChunksAcross];
int textureWidth = treePositionTexture.width;
for (int x = 0; x < NumberOfChunksAcross; x++) {
for (int y = 0; y < NumberOfChunksAcross; y++) {
//Debug.LogFormat("
xyValueArray [x, y] = getTreePositions (x * (textureWidth / NumberOfChunksAcross),
y * (textureWidth / NumberOfChunksAcross), textureWidth / NumberOfChunksAcross, treePositionTexture, numberOfTreeTypes);
}
}
return xyValueArray;
}
void FixedUpdate ()
{
if (Application.isPlaying) {
//checks is there terrain objects need to be updated
for (int i = 0; i < planes.Length; i++) {
HeightMapTerrainChunk3x3 heightMapChunkScript = planes [i].GetComponent<HeightMapTerrainChunk3x3> ();
heightMapChunkScript.PlaceTrees(treePositions[x,z]);
}
}
}
}
The HeightMapTerrainChunk3x3 class:
using UnityEngine;
using System.Collections;
public class HeightMapTerrainChunk3x3 : MonoBehaviour {
public TerrainData terrainObjectData;
public Terrain terrainObject;
public void InitTerrain(Terrain terrain, int heightMapResolution){
terrainObject = GetComponent<Terrain> ();
terrainObjectData = GetComponent<Terrain> ().terrainData;
terrainObjectData.heightmapResolution = heightMapResolution;
terrainObject.terrainData.treePrototypes = terrain.terrainData.treePrototypes;
}
public void PlaceTrees(HeightMapTerrainChunkHandler3x3.XY_AndValueArray array){
TreePrototype[] treePrototypes = GetComponent<Terrain> ().terrainData.treePrototypes;
for (int i = 0; i < array.valueArray.Length; i++) {
TreeInstance tree = new TreeInstance ();
tree.position = new Vector3 (array.xArray [i], 0f, array.yArray [i]);
tree.rotation = Mathf.PerlinNoise (array.yArray [i], array.xArray [i]);
tree.widthScale = 1f;
tree.heightScale = 1f;
tree.color = Color.green;
tree.lightmapColor = Color.magenta;
//Debug.LogFormat("{0}", array.valueArray [i]);
if(array.valueArray [i] >= treePrototypes.Length){
tree.prototypeIndex = array.valueArray [i] - 1;
}else if (array.valueArray[i] < 0) {
tree.prototypeIndex = 0;
}else{
tree.prototypeIndex = array.valueArray [i];
}
terrainObject.AddTreeInstance (tree);
}
terrainObject.Flush ();
}
}
Any ideas on what's going on?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
NullReferenceException: Object reference not set to an instance of an object. (Cameras switching) 1 Answer
Help with basic AI script 1 Answer
Space Shooter: Ending the Game 0 Answers