Optimize perlin code

My question is if you have any way to make this delay less than 0.1 or make it un-noticable?
So for example simplify some of the code to make it run a little faster :slight_smile: Is there for example a way to multithread it and do the math on another thread then just apply it on the main thread? I think that the SetHeights and SetDetailLayer is taking alot of time to perform, but that is just a theory.

EDIT: That wasn’t it, it was the loops that took 0.07 each, which gives that small delay. My problem is I can’t make the two loops into one, because I can’t get steepness if the terrain height hasn’t been set.

I have this code which moves 3 tiles in 0.4 seconds, which gives this small lag everytime I walk onto a new tile:

	var Chunk : Terrain;
	var tmp = GameObject.Find("Chunk "+fromnx+","+fromnz);
    var ocupied = GameObject.Find("Chunk "+nx+","+nz);
    if (ocupied != null) {
    	Destroy(tmp);
    	return;
    }
	if (tmp == null) {
	    var td = new TerrainData();
	    td.heightmapResolution = 256;
	    td.alphamapResolution = td.heightmapResolution;
	    td.SetDetailResolution(td.heightmapResolution, 16);
	    
	    td.size = new Vector3(terrainSize, terrainHeight, terrainSize);
	    
	    td.splatPrototypes = splatPrototypes;
	    td.treePrototypes = treeProtoTypes;
	    td.detailPrototypes = detailProtoTypes;
	    tmp = Terrain.CreateTerrainGameObject(td);
	}
	Chunk = tmp.GetComponent(Terrain);
	if (PlayerPrefs.HasKey("GrassDistance")) {
		Chunk.detailObjectDistance = PlayerPrefs.GetInt("GrassDistance");
	}
	var hw : float = Chunk.terrainData.heightmapWidth;
	var ChunkPosX : float = terrainSize * nx;
    var ChunkPosZ : float = terrainSize * nz;
    
	Chunk.transform.position = Vector3(ChunkPosX, 0, ChunkPosZ);
    Chunk.name = "Chunk "+nx+","+nz;
    Chunk.tag = "Chunk";
	
	var startPosX = (hw-1)*(nx-1);
	var startPosZ = (hw-1)*(nz-1);
	
	if (Chunk.transform.Find("Water") == null) {
		var Water = GameObject.Instantiate(WaterPrefab, Vector3(0, WaterLevel, 0), Quaternion.identity);
		Water.name = "Water";
		Water.transform.parent = Chunk.transform;
	}
	
	var a = Chunk.terrainData.alphamapWidth;
	var heightmap = new float[hw,hw];
	var ratio = terrainSize/hw;
	
	for(var x = 0; x < hw; x++) {
		for(var z = 0; z < hw; z++) {
			var worldPosX : float = (x+startPosX)*ratio;
			var worldPosZ : float = (z+startPosZ)*ratio;
			
			var mountains : float = Mathf.Max(0.0f, perlin.FractalNoise2D(worldPosX+250, worldPosZ+250, 5, Frq, 0.8f));
			var plain : float = perlin.FractalNoise2D(worldPosX-250, worldPosZ-250, 1, Frq, 0.5f) + 0.175f;
			
			heightmap[z,x] = plain+mountains;
		}
	}
	
	Chunk.terrainData.SetHeights(0,0,heightmap);
	var map = new float[a, a, Chunk.terrainData.alphamapLayers];
	var detailMap = new int[a,a];
	var aratio = (terrainSize/a);
	var aunit = 1.0f/(a-1);
	for (var ax = 0; ax < a; ax++) {
		for (var az = 0; az < a; az++) {
			var worldPosX1 : float = (ax+startPosX)*aratio;
			var worldPosZ1 : float = (az+startPosZ)*aratio;
			
		    var Biome = GetBiome(worldPosX1,worldPosZ1);
            var normX : float = ax*aunit;
            var normZ : float = az*aunit;
                
            // Get the steepness value at the normalized coordinate.
            var angle = Chunk.terrainData.GetSteepness(normX,normZ);
            var height = Chunk.terrainData.GetInterpolatedHeight(normX,normZ);
            
            if (height < WaterLevel + 2 || Biome == "Desert") {
            	map[az,ax,2] = 1;
            } else if (height > 120 || Biome == "Snow") {
            	map[az,ax,3] = 1;
            } else if (angle > 25) {
            	map[az,ax,1] = 1;
            } else if (Biome == "Plain") {
				detailMap[az,ax] = 2;
            	map[az,ax,0] = 1;
            } else if (Biome == "Forest") {
            	detailMap[az,ax] = 1;
            	map[az,ax,0] = 1;
            }
		}
	}
	Chunk.terrainData.SetDetailLayer(0,0,0,detailMap);
    Chunk.terrainData.SetAlphamaps(0,0,map);

Have you tried .05?