ShiroSmith пре 6 година
родитељ
комит
86addbae5c
2 измењених фајлова са 251 додато и 240 уклоњено
  1. 148 140
      h3d/scene/pbr/terrain/Tile.hx
  2. 103 100
      h3d/shader/pbr/Terrain.hx

+ 148 - 140
h3d/scene/pbr/terrain/Tile.hx

@@ -18,7 +18,7 @@ class Tile extends h3d.scene.Mesh {
 	var heightmapPixels : hxd.Pixels.PixelsFloat;
 	var heightmapPixels : hxd.Pixels.PixelsFloat;
 	var shader : h3d.shader.pbr.Terrain;
 	var shader : h3d.shader.pbr.Terrain;
 
 
-	public function new( x : Int, y : Int , ?parent ){
+	public function new( x : Int, y : Int , ?parent ) {
 		super(null, null, parent);
 		super(null, null, parent);
 		this.tileX = x;
 		this.tileX = x;
 		this.tileY = y;
 		this.tileY = y;
@@ -42,31 +42,31 @@ class Tile extends h3d.scene.Mesh {
 		o.surfaceWeightArray.wrap = Clamp;
 		o.surfaceWeightArray.wrap = Clamp;
 		o.surfaceWeightArray.preventAutoDispose();
 		o.surfaceWeightArray.preventAutoDispose();
 		o.surfaceWeightArray.realloc = null;
 		o.surfaceWeightArray.realloc = null;
-		for(i in 0 ... surfaceWeights.length)
-			if(surfaceWeights[i] != null) getTerrain().copyPass.apply(surfaceWeights[i], o.surfaceWeightArray, None, null, i);
+		for( i in 0 ... surfaceWeights.length )
+			if( surfaceWeights[i] != null ) getTerrain().copyPass.apply(surfaceWeights[i], o.surfaceWeightArray, None, null, i);
 
 
 		o.heightmapPixels = heightmapPixels.clone();
 		o.heightmapPixels = heightmapPixels.clone();
 		return o;
 		return o;
 	}
 	}
 
 
-	function set_heightMap(v){
+	function set_heightMap( v ) {
 		shader.heightMap = v;
 		shader.heightMap = v;
 		return heightMap = v;
 		return heightMap = v;
 	}
 	}
 
 
-	inline function getTerrain(){
+	inline function getTerrain() {
 		return Std.instance(parent, Terrain);
 		return Std.instance(parent, Terrain);
 	}
 	}
 
 
-	public function getHeightPixels(){
-		if(needNewPixelCapture || heightmapPixels == null)
+	public function getHeightPixels() {
+		if( needNewPixelCapture || heightmapPixels == null )
 			heightmapPixels = heightMap.capturePixels();
 			heightmapPixels = heightMap.capturePixels();
 		needNewPixelCapture = false;
 		needNewPixelCapture = false;
 		return heightmapPixels;
 		return heightmapPixels;
 	}
 	}
 
 
-	public function refreshMesh(){
-		if(grid == null || grid.width != getTerrain().cellCount || grid.height != getTerrain().cellCount || grid.cellWidth != getTerrain().cellSize || grid.cellHeight != getTerrain().cellSize){
+	public function refreshMesh() {
+		if( grid == null || grid.width != getTerrain().cellCount || grid.height != getTerrain().cellCount || grid.cellWidth != getTerrain().cellSize || grid.cellHeight != getTerrain().cellSize ) {
 			if(grid != null) grid.dispose();
 			if(grid != null) grid.dispose();
 		 	grid = new h3d.prim.Grid( getTerrain().cellCount, getTerrain().cellCount, getTerrain().cellSize, getTerrain().cellSize);
 		 	grid = new h3d.prim.Grid( getTerrain().cellCount, getTerrain().cellCount, getTerrain().cellSize, getTerrain().cellSize);
 			primitive = grid;
 			primitive = grid;
@@ -75,21 +75,21 @@ class Tile extends h3d.scene.Mesh {
 		computeNormals();
 		computeNormals();
 	}
 	}
 
 
-	public function blendEdges(){
+	public function blendEdges() {
 		var adjTileX = getTerrain().getTile(tileX - 1, tileY);
 		var adjTileX = getTerrain().getTile(tileX - 1, tileY);
-		if( adjTileX != null){
+		if( adjTileX != null ) {
 			var flags = new haxe.EnumFlags<Direction>();
 			var flags = new haxe.EnumFlags<Direction>();
         	flags.set(Left);
         	flags.set(Left);
 			adjTileX.computeEdgesHeight(flags);
 			adjTileX.computeEdgesHeight(flags);
 		}
 		}
 		var adjTileY = getTerrain().getTile(tileX, tileY - 1);
 		var adjTileY = getTerrain().getTile(tileX, tileY - 1);
-		if( adjTileY != null){
+		if( adjTileY != null ) {
 			var flags = new haxe.EnumFlags<Direction>();
 			var flags = new haxe.EnumFlags<Direction>();
         	flags.set(Up);
         	flags.set(Up);
 			adjTileY.computeEdgesHeight(flags);
 			adjTileY.computeEdgesHeight(flags);
 		}
 		}
 		var adjTileXY = getTerrain().getTile(tileX - 1, tileY - 1);
 		var adjTileXY = getTerrain().getTile(tileX - 1, tileY - 1);
-		if( adjTileXY != null){
+		if( adjTileXY != null ) {
 			var flags = new haxe.EnumFlags<Direction>();
 			var flags = new haxe.EnumFlags<Direction>();
         	flags.set(UpLeft);
         	flags.set(UpLeft);
 			adjTileXY.computeEdgesHeight(flags);
 			adjTileXY.computeEdgesHeight(flags);
@@ -105,92 +105,92 @@ class Tile extends h3d.scene.Mesh {
 	}
 	}
 
 
 	public function refresh(){
 	public function refresh(){
-		if(heightMap == null || heightMap.width != getTerrain().heightMapResolution + 1){
+		if( heightMap == null || heightMap.width != getTerrain().heightMapResolution + 1 ) {
 			var oldHeightMap = heightMap;
 			var oldHeightMap = heightMap;
 			heightMap = new h3d.mat.Texture(getTerrain().heightMapResolution + 1, getTerrain().heightMapResolution + 1, [Target], RGBA32F );
 			heightMap = new h3d.mat.Texture(getTerrain().heightMapResolution + 1, getTerrain().heightMapResolution + 1, [Target], RGBA32F );
 			heightMap.wrap = Clamp;
 			heightMap.wrap = Clamp;
 			heightMap.filter = Linear;
 			heightMap.filter = Linear;
 			heightMap.preventAutoDispose();
 			heightMap.preventAutoDispose();
 			heightMap.realloc = null;
 			heightMap.realloc = null;
-			if(oldHeightMap != null){
+			if( oldHeightMap != null ) {
 				getTerrain().copyPass.apply(oldHeightMap, heightMap);
 				getTerrain().copyPass.apply(oldHeightMap, heightMap);
 				oldHeightMap.dispose();
 				oldHeightMap.dispose();
 			}
 			}
 			needNewPixelCapture = true;
 			needNewPixelCapture = true;
 		}
 		}
 
 
-		if(surfaceIndexMap == null || surfaceIndexMap.width != getTerrain().weightMapResolution){
+		if( surfaceIndexMap == null || surfaceIndexMap.width != getTerrain().weightMapResolution ) {
 			var oldSurfaceIndexMap = surfaceIndexMap;
 			var oldSurfaceIndexMap = surfaceIndexMap;
 			surfaceIndexMap = new h3d.mat.Texture(getTerrain().weightMapResolution, getTerrain().weightMapResolution, [Target], RGBA);
 			surfaceIndexMap = new h3d.mat.Texture(getTerrain().weightMapResolution, getTerrain().weightMapResolution, [Target], RGBA);
 			surfaceIndexMap.filter = Nearest;
 			surfaceIndexMap.filter = Nearest;
 			surfaceIndexMap.preventAutoDispose();
 			surfaceIndexMap.preventAutoDispose();
 			surfaceIndexMap.realloc = null;
 			surfaceIndexMap.realloc = null;
-			if(oldSurfaceIndexMap != null){
+			if( oldSurfaceIndexMap != null ) {
 				getTerrain().copyPass.apply(oldSurfaceIndexMap, surfaceIndexMap);
 				getTerrain().copyPass.apply(oldSurfaceIndexMap, surfaceIndexMap);
 				oldSurfaceIndexMap.dispose();
 				oldSurfaceIndexMap.dispose();
 			}
 			}
 		}
 		}
 
 
-		if( getTerrain().surfaces.length > 0 && (surfaceWeights.length != getTerrain().surfaces.length || surfaceWeights[0].width != getTerrain().weightMapResolution)){
+		if( getTerrain().surfaces.length > 0 && (surfaceWeights.length != getTerrain().surfaces.length || surfaceWeights[0].width != getTerrain().weightMapResolution) ) {
 			var oldArray = surfaceWeights;
 			var oldArray = surfaceWeights;
 			surfaceWeights = new Array<h3d.mat.Texture>();
 			surfaceWeights = new Array<h3d.mat.Texture>();
-			surfaceWeights = [for (i in 0...getTerrain().surfaces.length) null];
-			for(i in 0 ... surfaceWeights.length){
+			surfaceWeights = [for( i in 0...getTerrain().surfaces.length ) null];
+			for( i in 0 ... surfaceWeights.length ) {
 				surfaceWeights[i] = new h3d.mat.Texture(getTerrain().weightMapResolution, getTerrain().weightMapResolution, [Target], R8);
 				surfaceWeights[i] = new h3d.mat.Texture(getTerrain().weightMapResolution, getTerrain().weightMapResolution, [Target], R8);
 				surfaceWeights[i].wrap = Clamp;
 				surfaceWeights[i].wrap = Clamp;
 				surfaceWeights[i].preventAutoDispose();
 				surfaceWeights[i].preventAutoDispose();
 				surfaceWeights[i].realloc = null;
 				surfaceWeights[i].realloc = null;
-				if(i < oldArray.length && oldArray[i] != null)
+				if( i < oldArray.length && oldArray[i] != null )
 					getTerrain().copyPass.apply(oldArray[i], surfaceWeights[i]);
 					getTerrain().copyPass.apply(oldArray[i], surfaceWeights[i]);
 			}
 			}
 			generateWeightArray();
 			generateWeightArray();
 
 
-			for(i in 0 ... oldArray.length)
+			for( i in 0 ... oldArray.length )
 				if( oldArray[i] != null) oldArray[i].dispose();
 				if( oldArray[i] != null) oldArray[i].dispose();
 		}
 		}
 	}
 	}
 
 
-	public function generateWeightArray(){
-		if(surfaceWeightArray == null || surfaceWeightArray.width != getTerrain().weightMapResolution || surfaceWeightArray.get_layerCount() != surfaceWeights.length){
-			if(surfaceWeightArray != null) surfaceWeightArray.dispose();
+	public function generateWeightArray() {
+		if( surfaceWeightArray == null || surfaceWeightArray.width != getTerrain().weightMapResolution || surfaceWeightArray.get_layerCount() != surfaceWeights.length ) {
+			if( surfaceWeightArray != null ) surfaceWeightArray.dispose();
 			surfaceWeightArray = new h3d.mat.TextureArray(getTerrain().weightMapResolution, getTerrain().weightMapResolution, surfaceWeights.length, [Target], R8);
 			surfaceWeightArray = new h3d.mat.TextureArray(getTerrain().weightMapResolution, getTerrain().weightMapResolution, surfaceWeights.length, [Target], R8);
 			surfaceWeightArray.wrap = Clamp;
 			surfaceWeightArray.wrap = Clamp;
 			surfaceWeightArray.preventAutoDispose();
 			surfaceWeightArray.preventAutoDispose();
 			surfaceWeightArray.realloc = null;
 			surfaceWeightArray.realloc = null;
 		}
 		}
-		for(i in 0 ... surfaceWeights.length)
-			if(surfaceWeights[i] != null) getTerrain().copyPass.apply(surfaceWeights[i], surfaceWeightArray, None, null, i);
+		for( i in 0 ... surfaceWeights.length )
+			if( surfaceWeights[i] != null ) getTerrain().copyPass.apply(surfaceWeights[i], surfaceWeightArray, None, null, i);
 	}
 	}
 
 
 	public function computeEdgesHeight(flag : haxe.EnumFlags<Direction>){
 	public function computeEdgesHeight(flag : haxe.EnumFlags<Direction>){
 
 
-		if(heightMap == null) return;
+		if( heightMap == null ) return;
 		var pixels : hxd.Pixels.PixelsFloat = getHeightPixels();
 		var pixels : hxd.Pixels.PixelsFloat = getHeightPixels();
 
 
-		if(flag.has(Left)){
+		if( flag.has(Left) ) {
 			var adjTileX = getTerrain().getTile(tileX + 1, tileY);
 			var adjTileX = getTerrain().getTile(tileX + 1, tileY);
 			var adjHeightMapX = adjTileX != null ? adjTileX.heightMap : null;
 			var adjHeightMapX = adjTileX != null ? adjTileX.heightMap : null;
-			if(adjHeightMapX != null){
+			if( adjHeightMapX != null ) {
 				var adjpixels : hxd.Pixels.PixelsFloat = adjTileX.getHeightPixels();
 				var adjpixels : hxd.Pixels.PixelsFloat = adjTileX.getHeightPixels();
-				for( i in 0 ... heightMap.height - 1){
+				for( i in 0 ... heightMap.height - 1 ) {
 					pixels.setPixelF(heightMap.width - 1, i, adjpixels.getPixelF(0,i) );
 					pixels.setPixelF(heightMap.width - 1, i, adjpixels.getPixelF(0,i) );
 				}
 				}
 			}
 			}
 		}
 		}
-		if(flag.has(Up)){
+		if( flag.has(Up) ) {
 			var adjTileY = getTerrain().getTile(tileX, tileY + 1);
 			var adjTileY = getTerrain().getTile(tileX, tileY + 1);
 			var adjHeightMapY = adjTileY != null ? adjTileY.heightMap : null;
 			var adjHeightMapY = adjTileY != null ? adjTileY.heightMap : null;
-			if(adjHeightMapY != null){
+			if( adjHeightMapY != null ) {
 				var adjpixels : hxd.Pixels.PixelsFloat = adjTileY.getHeightPixels();
 				var adjpixels : hxd.Pixels.PixelsFloat = adjTileY.getHeightPixels();
-				for( i in 0 ... heightMap.width - 1){
+				for( i in 0 ... heightMap.width - 1) {
 					pixels.setPixelF(i, heightMap.height - 1, adjpixels.getPixelF(i,0) );
 					pixels.setPixelF(i, heightMap.height - 1, adjpixels.getPixelF(i,0) );
 				}
 				}
 			}
 			}
 		}
 		}
-		if(flag.has(UpLeft)){
+		if( flag.has(UpLeft) ) {
 			var adjTileXY = getTerrain().getTile(tileX + 1, tileY + 1);
 			var adjTileXY = getTerrain().getTile(tileX + 1, tileY + 1);
 			var adjHeightMapXY = adjTileXY != null ? adjTileXY.heightMap : null;
 			var adjHeightMapXY = adjTileXY != null ? adjTileXY.heightMap : null;
-			if(adjHeightMapXY != null){
+			if( adjHeightMapXY != null ) {
 				var adjpixels : hxd.Pixels.PixelsFloat = adjTileXY.getHeightPixels();
 				var adjpixels : hxd.Pixels.PixelsFloat = adjTileXY.getHeightPixels();
 				pixels.setPixelF(heightMap.width - 1, heightMap.height - 1, adjpixels.getPixelF(0,0));
 				pixels.setPixelF(heightMap.width - 1, heightMap.height - 1, adjpixels.getPixelF(0,0));
 			}
 			}
@@ -200,8 +200,8 @@ class Tile extends h3d.scene.Mesh {
 		needNewPixelCapture = false;
 		needNewPixelCapture = false;
 	}
 	}
 
 
-	public function computeEdgesNormals(){
-		if(grid.normals == null) return;
+	public function computeEdgesNormals() {
+		if( grid.normals == null ) return;
 		var t0 = new h3d.col.Point(); var t1 = new h3d.col.Point(); var t2 = new h3d.col.Point();
 		var t0 = new h3d.col.Point(); var t1 = new h3d.col.Point(); var t2 = new h3d.col.Point();
 		var triCount = Std.int(grid.triCount() / grid.width);
 		var triCount = Std.int(grid.triCount() / grid.width);
 		var vertexCount = grid.width + 1;
 		var vertexCount = grid.width + 1;
@@ -210,7 +210,7 @@ class Tile extends h3d.scene.Mesh {
 		var istep = triCount * 3 - 6;
 		var istep = triCount * 3 - 6;
 		var i0, i1, i2 : Int = 0;
 		var i0, i1, i2 : Int = 0;
 
 
-		inline function computeVertexPos(tile : Tile){
+		inline function computeVertexPos( tile : Tile ) {
 			t0.load(tile.grid.points[i0]); t1.load(tile.grid.points[i1]); t2.load(tile.grid.points[i2]);
 			t0.load(tile.grid.points[i0]); t1.load(tile.grid.points[i1]); t2.load(tile.grid.points[i2]);
 			t0.z += tile.getHeight(t0.x / getTerrain().tileSize, t0.y / getTerrain().tileSize);
 			t0.z += tile.getHeight(t0.x / getTerrain().tileSize, t0.y / getTerrain().tileSize);
 			t1.z += tile.getHeight(t1.x / getTerrain().tileSize, t1.y / getTerrain().tileSize);
 			t1.z += tile.getHeight(t1.x / getTerrain().tileSize, t1.y / getTerrain().tileSize);
@@ -242,21 +242,21 @@ class Tile extends h3d.scene.Mesh {
 		var adjDownRightTile = getTerrain().getTile(tileX - 1, tileY - 1);
 		var adjDownRightTile = getTerrain().getTile(tileX - 1, tileY - 1);
 		var adjDownRightGrid = adjDownRightTile != null ? adjDownRightTile.grid: null;
 		var adjDownRightGrid = adjDownRightTile != null ? adjDownRightTile.grid: null;
 
 
-		if(adjUpGrid != null && adjUpGrid.normals != null){
+		if( adjUpGrid != null && adjUpGrid.normals != null ) {
 			var pos = 0;
 			var pos = 0;
-			for( i in 0 ... vertexCount)
+			for( i in 0 ... vertexCount )
 				adjUpGrid.normals[i].set(0,0,0);
 				adjUpGrid.normals[i].set(0,0,0);
 			for( i in 0 ... triCount ) {
 			for( i in 0 ... triCount ) {
 				i0 = adjUpGrid.idx[pos++]; i1 = adjUpGrid.idx[pos++]; i2 = adjUpGrid.idx[pos++];
 				i0 = adjUpGrid.idx[pos++]; i1 = adjUpGrid.idx[pos++]; i2 = adjUpGrid.idx[pos++];
 				computeVertexPos(adjUpTile);
 				computeVertexPos(adjUpTile);
 				var n = computeNormal();
 				var n = computeNormal();
-				if(i0 <= adjUpGrid.width){ adjUpGrid.normals[i0].x += n.x; adjUpGrid.normals[i0].y += n.y; adjUpGrid.normals[i0].z += n.z;}
-				if(i1 <= adjUpGrid.width){ adjUpGrid.normals[i1].x += n.x; adjUpGrid.normals[i1].y += n.y; adjUpGrid.normals[i1].z += n.z;}
-				if(i2 <= adjUpGrid.width){ adjUpGrid.normals[i2].x += n.x; adjUpGrid.normals[i2].y += n.y; adjUpGrid.normals[i2].z += n.z;}
+				if( i0 <= adjUpGrid.width ) { adjUpGrid.normals[i0].x += n.x; adjUpGrid.normals[i0].y += n.y; adjUpGrid.normals[i0].z += n.z;}
+				if( i1 <= adjUpGrid.width ) { adjUpGrid.normals[i1].x += n.x; adjUpGrid.normals[i1].y += n.y; adjUpGrid.normals[i1].z += n.z;}
+				if( i2 <= adjUpGrid.width ) { adjUpGrid.normals[i2].x += n.x; adjUpGrid.normals[i2].y += n.y; adjUpGrid.normals[i2].z += n.z;}
 			}
 			}
-			for( i in 0 ... vertexCount)
+			for( i in 0 ... vertexCount )
 				adjUpGrid.normals[i].normalize();
 				adjUpGrid.normals[i].normalize();
-			for( i in 1 ... vertexCount - 1){
+			for( i in 1 ... vertexCount - 1 ) {
 				var n = grid.normals[s + i].add(adjUpGrid.normals[i]);
 				var n = grid.normals[s + i].add(adjUpGrid.normals[i]);
 				n.normalize();
 				n.normalize();
 				grid.normals[s + i].load(n);
 				grid.normals[s + i].load(n);
@@ -264,7 +264,7 @@ class Tile extends h3d.scene.Mesh {
 			}
 			}
 		}
 		}
 
 
-		if(adjDownGrid != null && adjDownGrid.normals != null){
+		if( adjDownGrid != null && adjDownGrid.normals != null ) {
 			var pos = triCount * (adjDownGrid.width - 1) * 3;
 			var pos = triCount * (adjDownGrid.width - 1) * 3;
 			for( i in 0 ... vertexCount)
 			for( i in 0 ... vertexCount)
 				adjDownGrid.normals[s + i].set(0,0,0);
 				adjDownGrid.normals[s + i].set(0,0,0);
@@ -272,13 +272,13 @@ class Tile extends h3d.scene.Mesh {
 				i0 = adjDownGrid.idx[pos++]; i1 = adjDownGrid.idx[pos++]; i2 = adjDownGrid.idx[pos++];
 				i0 = adjDownGrid.idx[pos++]; i1 = adjDownGrid.idx[pos++]; i2 = adjDownGrid.idx[pos++];
 				computeVertexPos(adjDownTile);
 				computeVertexPos(adjDownTile);
 				var n = computeNormal();
 				var n = computeNormal();
-				if(i0 >= (adjDownGrid.width * adjDownGrid.height + adjDownGrid.height)){ adjDownGrid.normals[i0].x += n.x; adjDownGrid.normals[i0].y += n.y; adjDownGrid.normals[i0].z += n.z;}
-				if(i1 >= (adjDownGrid.width * adjDownGrid.height + adjDownGrid.height)){ adjDownGrid.normals[i1].x += n.x; adjDownGrid.normals[i1].y += n.y; adjDownGrid.normals[i1].z += n.z;}
-				if(i2 >= (adjDownGrid.width * adjDownGrid.height + adjDownGrid.height)){ adjDownGrid.normals[i2].x += n.x; adjDownGrid.normals[i2].y += n.y; adjDownGrid.normals[i2].z += n.z;}
+				if( i0 >= (adjDownGrid.width * adjDownGrid.height + adjDownGrid.height) ) { adjDownGrid.normals[i0].x += n.x; adjDownGrid.normals[i0].y += n.y; adjDownGrid.normals[i0].z += n.z;}
+				if( i1 >= (adjDownGrid.width * adjDownGrid.height + adjDownGrid.height) ) { adjDownGrid.normals[i1].x += n.x; adjDownGrid.normals[i1].y += n.y; adjDownGrid.normals[i1].z += n.z;}
+				if( i2 >= (adjDownGrid.width * adjDownGrid.height + adjDownGrid.height) ) { adjDownGrid.normals[i2].x += n.x; adjDownGrid.normals[i2].y += n.y; adjDownGrid.normals[i2].z += n.z;}
 			}
 			}
-			for( i in 1 ... vertexCount - 1)
+			for( i in 1 ... vertexCount - 1 )
 				adjDownGrid.normals[s + i].normalize();
 				adjDownGrid.normals[s + i].normalize();
-			for( i in 1 ... vertexCount - 1){
+			for( i in 1 ... vertexCount - 1 ){
 				var n = grid.normals[i].add(adjDownGrid.normals[s + i]);
 				var n = grid.normals[i].add(adjDownGrid.normals[s + i]);
 				n.normalize();
 				n.normalize();
 				grid.normals[i].load(n);
 				grid.normals[i].load(n);
@@ -286,25 +286,25 @@ class Tile extends h3d.scene.Mesh {
 			}
 			}
 		}
 		}
 
 
-		if(adjLeftGrid != null && adjLeftGrid.normals != null){
+		if( adjLeftGrid != null && adjLeftGrid.normals != null ) {
 			var pos = 0;
 			var pos = 0;
 			var istep = triCount * 3 - 6;
 			var istep = triCount * 3 - 6;
 			var needStep = false;
 			var needStep = false;
-			for( i in 0 ... vertexCount)
+			for( i in 0 ... vertexCount )
 				adjLeftGrid.normals[i * step].set(0,0,0);
 				adjLeftGrid.normals[i * step].set(0,0,0);
 			for( i in 0 ... triCount ) {
 			for( i in 0 ... triCount ) {
 				i0 = adjLeftGrid.idx[pos++]; i1 = adjLeftGrid.idx[pos++]; i2 = adjLeftGrid.idx[pos++];
 				i0 = adjLeftGrid.idx[pos++]; i1 = adjLeftGrid.idx[pos++]; i2 = adjLeftGrid.idx[pos++];
 				computeVertexPos(adjLeftTile);
 				computeVertexPos(adjLeftTile);
 				var n = computeNormal();
 				var n = computeNormal();
-				if(i0 % (adjLeftGrid.width + 1) == 0){ adjLeftGrid.normals[i0].x += n.x; adjLeftGrid.normals[i0].y += n.y; adjLeftGrid.normals[i0].z += n.z;}
-				if(i1 % (adjLeftGrid.width + 1) == 0){ adjLeftGrid.normals[i1].x += n.x; adjLeftGrid.normals[i1].y += n.y; adjLeftGrid.normals[i1].z += n.z;}
-				if(i2 % (adjLeftGrid.width + 1) == 0){ adjLeftGrid.normals[i2].x += n.x; adjLeftGrid.normals[i2].y += n.y; adjLeftGrid.normals[i2].z += n.z;}
-				if(needStep) pos += istep;
+				if( i0 % (adjLeftGrid.width + 1) == 0 ) { adjLeftGrid.normals[i0].x += n.x; adjLeftGrid.normals[i0].y += n.y; adjLeftGrid.normals[i0].z += n.z;}
+				if( i1 % (adjLeftGrid.width + 1) == 0 ) { adjLeftGrid.normals[i1].x += n.x; adjLeftGrid.normals[i1].y += n.y; adjLeftGrid.normals[i1].z += n.z;}
+				if( i2 % (adjLeftGrid.width + 1) == 0 ) { adjLeftGrid.normals[i2].x += n.x; adjLeftGrid.normals[i2].y += n.y; adjLeftGrid.normals[i2].z += n.z;}
+				if( needStep) pos += istep;
 				needStep = !needStep;
 				needStep = !needStep;
 			}
 			}
-			for( i in 0 ... vertexCount)
+			for( i in 0 ... vertexCount )
 				adjLeftGrid.normals[i * step].normalize();
 				adjLeftGrid.normals[i * step].normalize();
-			for( i in 1 ... vertexCount - 1){
+			for( i in 1 ... vertexCount - 1 ){
 				var n = grid.normals[i * step + (step - 1)].add(adjLeftGrid.normals[i * step]);
 				var n = grid.normals[i * step + (step - 1)].add(adjLeftGrid.normals[i * step]);
 				n.normalize();
 				n.normalize();
 				grid.normals[i * step + (step - 1)].load(n);
 				grid.normals[i * step + (step - 1)].load(n);
@@ -312,25 +312,25 @@ class Tile extends h3d.scene.Mesh {
 			}
 			}
 		}
 		}
 
 
-		if(adjRightGrid != null && adjRightGrid.normals != null){
+		if( adjRightGrid != null && adjRightGrid.normals != null ) {
 			var pos = (triCount - 2) * 3;
 			var pos = (triCount - 2) * 3;
 			var istep = (triCount - 2) * 3;
 			var istep = (triCount - 2) * 3;
 			var needStep = false;
 			var needStep = false;
-			for( i in 0 ... vertexCount)
+			for( i in 0 ... vertexCount )
 				adjRightGrid.normals[i * step + (step - 1)].set(0,0,0);
 				adjRightGrid.normals[i * step + (step - 1)].set(0,0,0);
 			for( i in 0 ... triCount ) {
 			for( i in 0 ... triCount ) {
 				i0 = adjRightGrid.idx[pos++]; i1 = adjRightGrid.idx[pos++]; i2 = adjRightGrid.idx[pos++];
 				i0 = adjRightGrid.idx[pos++]; i1 = adjRightGrid.idx[pos++]; i2 = adjRightGrid.idx[pos++];
 				computeVertexPos(adjRightTile);
 				computeVertexPos(adjRightTile);
 				var n = computeNormal();
 				var n = computeNormal();
-				if((i0 + 1) % (adjRightGrid.width + 1) == 0){ adjRightGrid.normals[i0].x += n.x; adjRightGrid.normals[i0].y += n.y; adjRightGrid.normals[i0].z += n.z;}
-				if((i1 + 1) % (adjRightGrid.width + 1) == 0){ adjRightGrid.normals[i1].x += n.x; adjRightGrid.normals[i1].y += n.y; adjRightGrid.normals[i1].z += n.z;}
-				if((i2 + 1) % (adjRightGrid.width + 1) == 0){ adjRightGrid.normals[i2].x += n.x; adjRightGrid.normals[i2].y += n.y; adjRightGrid.normals[i2].z += n.z;}
-				if(needStep) pos += istep;
+				if( (i0 + 1) % (adjRightGrid.width + 1) == 0 ) { adjRightGrid.normals[i0].x += n.x; adjRightGrid.normals[i0].y += n.y; adjRightGrid.normals[i0].z += n.z; }
+				if( (i1 + 1) % (adjRightGrid.width + 1) == 0 ) { adjRightGrid.normals[i1].x += n.x; adjRightGrid.normals[i1].y += n.y; adjRightGrid.normals[i1].z += n.z; }
+				if( (i2 + 1) % (adjRightGrid.width + 1) == 0 ) { adjRightGrid.normals[i2].x += n.x; adjRightGrid.normals[i2].y += n.y; adjRightGrid.normals[i2].z += n.z; }
+				if( needStep) pos += istep;
 				needStep = !needStep;
 				needStep = !needStep;
 			}
 			}
-			for( i in 0 ... vertexCount)
+			for( i in 0 ... vertexCount )
 				adjRightGrid.normals[i * step + (step - 1)].normalize();
 				adjRightGrid.normals[i * step + (step - 1)].normalize();
-			for( i in 1 ... vertexCount - 1){
+			for( i in 1 ... vertexCount - 1 ) {
 				var n = grid.normals[i * step].add(adjRightGrid.normals[i * step + (step - 1)]);
 				var n = grid.normals[i * step].add(adjRightGrid.normals[i * step + (step - 1)]);
 				n.normalize();
 				n.normalize();
 				grid.normals[i * step].load(n);
 				grid.normals[i * step].load(n);
@@ -344,7 +344,7 @@ class Tile extends h3d.scene.Mesh {
 		var upRight = step * grid.height;
 		var upRight = step * grid.height;
 
 
 		var n = new h3d.col.Point();
 		var n = new h3d.col.Point();
-		if(adjUpRightGrid != null && adjUpRightGrid.normals != null){
+		if( adjUpRightGrid != null && adjUpRightGrid.normals != null ) {
 			var pos = (triCount) * 3 - 6;
 			var pos = (triCount) * 3 - 6;
 			i0 = adjUpRightGrid.idx[pos++]; i1 = adjUpRightGrid.idx[pos++]; i2 = adjUpRightGrid.idx[pos++];
 			i0 = adjUpRightGrid.idx[pos++]; i1 = adjUpRightGrid.idx[pos++]; i2 = adjUpRightGrid.idx[pos++];
 			computeVertexPos(adjUpRightTile);
 			computeVertexPos(adjUpRightTile);
@@ -354,34 +354,34 @@ class Tile extends h3d.scene.Mesh {
 			n = n.add(computeNormal());
 			n = n.add(computeNormal());
 			n.normalize();
 			n.normalize();
 		}
 		}
-		if(adjRightGrid != null && adjRightGrid.normals != null) n = n.add(adjRightGrid.normals[topLeft]);
-		if(adjUpGrid != null && adjUpGrid.normals != null) n = n.add(adjUpGrid.normals[downRight]);
+		if( adjRightGrid != null && adjRightGrid.normals != null ) n = n.add(adjRightGrid.normals[topLeft]);
+		if( adjUpGrid != null && adjUpGrid.normals != null ) n = n.add(adjUpGrid.normals[downRight]);
 		n = n.add(grid.normals[upRight]);
 		n = n.add(grid.normals[upRight]);
 		n.normalize();
 		n.normalize();
-		if(adjUpRightGrid != null && adjUpRightGrid.normals != null) adjUpRightGrid.normals[downLeft].load(n);
-		if(adjRightGrid != null && adjRightGrid.normals != null) adjRightGrid.normals[topLeft].load(n);
-		if(adjUpGrid != null && adjUpGrid.normals != null) adjUpGrid.normals[downRight].load(n);
+		if( adjUpRightGrid != null && adjUpRightGrid.normals != null ) adjUpRightGrid.normals[downLeft].load(n);
+		if( adjRightGrid != null && adjRightGrid.normals != null ) adjRightGrid.normals[topLeft].load(n);
+		if( adjUpGrid != null && adjUpGrid.normals != null ) adjUpGrid.normals[downRight].load(n);
 		grid.normals[upRight].load(n);
 		grid.normals[upRight].load(n);
 
 
 		n.set(0,0,0);
 		n.set(0,0,0);
-		if(adjUpLeftGrid != null && adjUpLeftGrid.normals != null){
+		if( adjUpLeftGrid != null && adjUpLeftGrid.normals != null ) {
 			var pos = 0;
 			var pos = 0;
 			i0 = adjUpLeftGrid.idx[pos++]; i1 = adjUpLeftGrid.idx[pos++]; i2 = adjUpLeftGrid.idx[pos++];
 			i0 = adjUpLeftGrid.idx[pos++]; i1 = adjUpLeftGrid.idx[pos++]; i2 = adjUpLeftGrid.idx[pos++];
 			computeVertexPos(adjUpLeftTile);
 			computeVertexPos(adjUpLeftTile);
 			n = computeNormal();
 			n = computeNormal();
 			n.normalize();
 			n.normalize();
 		}
 		}
-		if(adjLeftGrid != null && adjLeftGrid.normals != null) n = n.add(adjLeftGrid.normals[upRight]);
-		if(adjUpGrid != null && adjUpGrid.normals != null) n = n.add(adjUpGrid.normals[downLeft]);
+		if( adjLeftGrid != null && adjLeftGrid.normals != null ) n = n.add(adjLeftGrid.normals[upRight]);
+		if( adjUpGrid != null && adjUpGrid.normals != null ) n = n.add(adjUpGrid.normals[downLeft]);
 		n = n.add(grid.normals[topLeft]);
 		n = n.add(grid.normals[topLeft]);
 		n.normalize();
 		n.normalize();
-		if(adjUpLeftGrid != null && adjUpLeftGrid.normals != null) adjUpLeftGrid.normals[downRight].load(n);
-		if(adjLeftGrid != null && adjLeftGrid.normals != null) adjLeftGrid.normals[upRight].load(n);
-		if(adjUpGrid != null && adjUpGrid.normals != null) adjUpGrid.normals[downLeft].load(n);
+		if( adjUpLeftGrid != null && adjUpLeftGrid.normals != null ) adjUpLeftGrid.normals[downRight].load(n);
+		if( adjLeftGrid != null && adjLeftGrid.normals != null ) adjLeftGrid.normals[upRight].load(n);
+		if( adjUpGrid != null && adjUpGrid.normals != null ) adjUpGrid.normals[downLeft].load(n);
 		grid.normals[topLeft].load(n);
 		grid.normals[topLeft].load(n);
 
 
 		n.set(0,0,0);
 		n.set(0,0,0);
-		if(adjDownLeftGrid != null && adjDownLeftGrid.normals != null){
+		if( adjDownLeftGrid != null && adjDownLeftGrid.normals != null ) {
 			var pos = (triCount) * 3 * (adjDownLeftGrid.height - 1) ;
 			var pos = (triCount) * 3 * (adjDownLeftGrid.height - 1) ;
 			i0 = adjDownLeftGrid.idx[pos++]; i1 = adjDownLeftGrid.idx[pos++]; i2 = adjDownLeftGrid.idx[pos++];
 			i0 = adjDownLeftGrid.idx[pos++]; i1 = adjDownLeftGrid.idx[pos++]; i2 = adjDownLeftGrid.idx[pos++];
 			computeVertexPos(adjDownLeftTile);
 			computeVertexPos(adjDownLeftTile);
@@ -391,48 +391,48 @@ class Tile extends h3d.scene.Mesh {
 			n = n.add(computeNormal());
 			n = n.add(computeNormal());
 			n.normalize();
 			n.normalize();
 		}
 		}
-		if(adjLeftGrid != null && adjLeftGrid.normals != null) n = n.add(adjLeftGrid.normals[downRight]);
-		if(adjDownGrid != null && adjDownGrid.normals != null) n = n.add(adjDownGrid.normals[topLeft]);
+		if( adjLeftGrid != null && adjLeftGrid.normals != null ) n = n.add(adjLeftGrid.normals[downRight]);
+		if( adjDownGrid != null && adjDownGrid.normals != null ) n = n.add(adjDownGrid.normals[topLeft]);
 		n = n.add(grid.normals[downLeft]);
 		n = n.add(grid.normals[downLeft]);
 		n.normalize();
 		n.normalize();
-		if(adjDownLeftGrid != null && adjDownLeftGrid.normals != null) adjDownLeftGrid.normals[upRight].load(n);
-		if(adjLeftGrid != null && adjLeftGrid.normals != null) adjLeftGrid.normals[downRight].load(n);
-		if(adjDownGrid != null && adjDownGrid.normals != null) adjDownGrid.normals[topLeft].load(n);
+		if( adjDownLeftGrid != null && adjDownLeftGrid.normals != null ) adjDownLeftGrid.normals[upRight].load(n);
+		if( adjLeftGrid != null && adjLeftGrid.normals != null ) adjLeftGrid.normals[downRight].load(n);
+		if( adjDownGrid != null && adjDownGrid.normals != null ) adjDownGrid.normals[topLeft].load(n);
 		grid.normals[downLeft].load(n);
 		grid.normals[downLeft].load(n);
 
 
 		n.set(0,0,0);
 		n.set(0,0,0);
-		if(adjDownRightGrid != null && adjDownRightGrid.normals != null){
+		if( adjDownRightGrid != null && adjDownRightGrid.normals != null ) {
 			var pos = triCount * 3 * adjDownRightGrid.width - 3;
 			var pos = triCount * 3 * adjDownRightGrid.width - 3;
 			i0 = adjDownRightGrid.idx[pos++]; i1 = adjDownRightGrid.idx[pos++]; i2 = adjDownRightGrid.idx[pos++];
 			i0 = adjDownRightGrid.idx[pos++]; i1 = adjDownRightGrid.idx[pos++]; i2 = adjDownRightGrid.idx[pos++];
 			computeVertexPos(adjDownRightTile);
 			computeVertexPos(adjDownRightTile);
 			n = computeNormal();
 			n = computeNormal();
 			n.normalize();
 			n.normalize();
 		}
 		}
-		if(adjRightGrid != null && adjRightGrid.normals != null) n = n.add(adjRightGrid.normals[downLeft]);
-		if(adjDownGrid != null && adjDownGrid.normals != null) n = n.add(adjDownGrid.normals[upRight]);
+		if( adjRightGrid != null && adjRightGrid.normals != null ) n = n.add(adjRightGrid.normals[downLeft]);
+		if( adjDownGrid != null && adjDownGrid.normals != null ) n = n.add(adjDownGrid.normals[upRight]);
 		n = n.add(grid.normals[downRight]);
 		n = n.add(grid.normals[downRight]);
 		n.normalize();
 		n.normalize();
-		if(adjDownRightGrid != null && adjDownRightGrid.normals != null) adjDownRightGrid.normals[topLeft].load(n);
-		if(adjRightGrid != null && adjRightGrid.normals != null) adjRightGrid.normals[downLeft].load(n);
-		if(adjDownGrid != null && adjDownGrid.normals != null) adjDownGrid.normals[upRight].load(n);
+		if( adjDownRightGrid != null && adjDownRightGrid.normals != null ) adjDownRightGrid.normals[topLeft].load(n);
+		if( adjRightGrid != null && adjRightGrid.normals != null ) adjRightGrid.normals[downLeft].load(n);
+		if( adjDownGrid != null && adjDownGrid.normals != null ) adjDownGrid.normals[upRight].load(n);
 		grid.normals[downRight].load(n);
 		grid.normals[downRight].load(n);
 
 
-		if(adjUpTile != null) adjUpTile.needAlloc = true;
-		if(adjDownTile != null) adjDownTile.needAlloc = true;
-		if(adjLeftTile != null) adjLeftTile.needAlloc = true;
-		if(adjRightTile != null) adjRightTile.needAlloc = true;
-		if(adjUpLeftTile != null) adjUpLeftTile.needAlloc = true;
-		if(adjDownLeftTile != null) adjDownLeftTile.needAlloc = true;
-		if(adjUpRightTile != null) adjUpRightTile.needAlloc = true;
-		if(adjDownRightTile != null) adjDownRightTile.needAlloc = true;
+		if( adjUpTile != null ) adjUpTile.needAlloc = true;
+		if( adjDownTile != null ) adjDownTile.needAlloc = true;
+		if( adjLeftTile != null ) adjLeftTile.needAlloc = true;
+		if( adjRightTile != null ) adjRightTile.needAlloc = true;
+		if( adjUpLeftTile != null ) adjUpLeftTile.needAlloc = true;
+		if( adjDownLeftTile != null ) adjDownLeftTile.needAlloc = true;
+		if( adjUpRightTile != null ) adjUpRightTile.needAlloc = true;
+		if( adjDownRightTile != null ) adjDownRightTile.needAlloc = true;
 		this.needAlloc = true;
 		this.needAlloc = true;
 	}
 	}
 
 
-	public function computeNormals(){
-		if(grid.normals == null) grid.normals = new Array<h3d.col.Point>();
+	public function computeNormals() {
+		if( grid.normals == null ) grid.normals = new Array<h3d.col.Point>();
 		grid.normals = [
 		grid.normals = [
-		for (i in 0...grid.points.length){
-			if(i < grid.normals.length){
+		for ( i in 0...grid.points.length ) {
+			if( i < grid.normals.length ) {
 				grid.normals[i].set(0,0,0);
 				grid.normals[i].set(0,0,0);
 				grid.normals[i];
 				grid.normals[i];
 			} else
 			} else
@@ -471,9 +471,9 @@ class Tile extends h3d.scene.Mesh {
 
 
 	public function getHeight(u : Float, v : Float, ?fast = false) : Float {
 	public function getHeight(u : Float, v : Float, ?fast = false) : Float {
 		var pixels = getHeightPixels();
 		var pixels = getHeightPixels();
-		if(pixels == null) return 0.0;
-		if(heightMap.filter == Linear && !fast){
-			inline function getPix(u, v){
+		if( pixels == null ) return 0.0;
+		if( heightMap.filter == Linear && !fast ) {
+			inline function getPix(u, v) {
 				return pixels.getPixelF(Std.int(hxd.Math.clamp(u, 0, pixels.width - 1)), Std.int(hxd.Math.clamp(v, 0, pixels.height - 1))).r;
 				return pixels.getPixelF(Std.int(hxd.Math.clamp(u, 0, pixels.width - 1)), Std.int(hxd.Math.clamp(v, 0, pixels.height - 1))).r;
 			}
 			}
 			var px = u * (heightMap.width - 1) + 0.5;
 			var px = u * (heightMap.width - 1) + 0.5;
@@ -499,24 +499,24 @@ class Tile extends h3d.scene.Mesh {
 	}
 	}
 
 
 	public override function dispose() {
 	public override function dispose() {
-		if(heightMap != null) heightMap.dispose();
-		if(surfaceIndexMap != null) surfaceIndexMap.dispose();
-		for(i in 0 ... surfaceWeights.length)
-			if( surfaceWeights[i] != null) surfaceWeights[i].dispose();
+		if( heightMap != null ) heightMap.dispose();
+		if( surfaceIndexMap != null ) surfaceIndexMap.dispose();
+		for( i in 0 ... surfaceWeights.length )
+			if( surfaceWeights[i] != null ) surfaceWeights[i].dispose();
 	}
 	}
 
 
 	var cachedBounds : h3d.col.Bounds;
 	var cachedBounds : h3d.col.Bounds;
 	var cachedHeightBound : Bool = false;
 	var cachedHeightBound : Bool = false;
 	override function emit( ctx:RenderContext ){
 	override function emit( ctx:RenderContext ){
-		if(!isReady()) return;
-		if(cachedBounds == null) {
+		if( !isReady() ) return;
+		if( cachedBounds == null ) {
 			cachedBounds = getBounds();
 			cachedBounds = getBounds();
 			cachedBounds.zMax = 0;
 			cachedBounds.zMax = 0;
 			cachedBounds.zMin = 0;
 			cachedBounds.zMin = 0;
 		}
 		}
-		if(cachedBounds != null && cachedHeightBound == false && heightMap != null){
-			for( u in 0 ... heightMap.width ){
-				for( v in 0 ... heightMap.height ){
+		if( cachedBounds != null && cachedHeightBound == false && heightMap != null ){
+			for( u in 0 ... heightMap.width ) {
+				for( v in 0 ... heightMap.height ) {
 					var h = getHeight(u / heightMap.width, v / heightMap.height, true);
 					var h = getHeight(u / heightMap.width, v / heightMap.height, true);
 					cachedBounds.zMin = cachedBounds.zMin > h ? h : cachedBounds.zMin;
 					cachedBounds.zMin = cachedBounds.zMin > h ? h : cachedBounds.zMin;
 					cachedBounds.zMax = cachedBounds.zMax < h ? h : cachedBounds.zMax;
 					cachedBounds.zMax = cachedBounds.zMax < h ? h : cachedBounds.zMax;
@@ -524,12 +524,12 @@ class Tile extends h3d.scene.Mesh {
 			}
 			}
 			cachedHeightBound = true;
 			cachedHeightBound = true;
 		}
 		}
-		if(ctx.camera.frustum.hasBounds(cachedBounds))
+		if( ctx.camera.frustum.hasBounds(cachedBounds) )
 			super.emit(ctx);
 			super.emit(ctx);
 	}
 	}
 
 
 	override function sync(ctx:RenderContext) {
 	override function sync(ctx:RenderContext) {
-		if(!isReady()) return;
+		if( !isReady() ) return;
 
 
 		shader.SHOW_GRID = getTerrain().showGrid;
 		shader.SHOW_GRID = getTerrain().showGrid;
 		shader.SURFACE_COUNT = getTerrain().surfaces.length;
 		shader.SURFACE_COUNT = getTerrain().surfaces.length;
@@ -540,37 +540,45 @@ class Tile extends h3d.scene.Mesh {
 		shader.primSize = getTerrain().tileSize;
 		shader.primSize = getTerrain().tileSize;
 		shader.cellSize = getTerrain().cellSize;
 		shader.cellSize = getTerrain().cellSize;
 
 
-		shader.albedoTextures = getTerrain().surfaceArray.albedo;
-		shader.normalTextures = getTerrain().surfaceArray.normal;
-		shader.pbrTextures = getTerrain().surfaceArray.pbr;
-		shader.weightTextures = surfaceWeightArray;
-		shader.heightMap = heightMap;
-		shader.surfaceIndexMap = surfaceIndexMap;
-
-		shader.surfaceParams = getTerrain().surfaceArray.params;
-		shader.secondSurfaceParams = getTerrain().surfaceArray.secondParams;
-		shader.tileIndex.set(tileX, tileY); // = new h3d.Vector(tileX, tileY);
-		shader.parallaxAmount = getTerrain().parallaxAmount;
-		shader.minStep = getTerrain().parallaxMinStep;
-		shader.maxStep = getTerrain().parallaxMaxStep;
-		shader.heightBlendStrength = getTerrain().heightBlendStrength;
-		shader.heightBlendSharpness = getTerrain().heightBlendSharpness;
+		if( !shader.CHECKER && !shader.COMPLEXITY ){
+			shader.albedoTextures = getTerrain().surfaceArray.albedo;
+			shader.normalTextures = getTerrain().surfaceArray.normal;
+			shader.pbrTextures = getTerrain().surfaceArray.pbr;
+			shader.weightTextures = surfaceWeightArray;
+			shader.heightMap = heightMap;
+			shader.surfaceIndexMap = surfaceIndexMap;
+
+			shader.surfaceParams = getTerrain().surfaceArray.params;
+			shader.secondSurfaceParams = getTerrain().surfaceArray.secondParams;
+			shader.tileIndex.set(tileX, tileY); // = new h3d.Vector(tileX, tileY);
+			shader.parallaxAmount = getTerrain().parallaxAmount;
+			shader.minStep = getTerrain().parallaxMinStep;
+			shader.maxStep = getTerrain().parallaxMaxStep;
+			shader.heightBlendStrength = getTerrain().heightBlendStrength;
+			shader.heightBlendSharpness = getTerrain().heightBlendSharpness;
+		}
 	}
 	}
 
 
 	function isReady(){
 	function isReady(){
-		if(primitive == null)
-			return false;
-		if( getTerrain().surfaceArray == null || getTerrain().surfaces.length == 0 || surfaceWeights.length != getTerrain().surfaces.length)
+		if( primitive == null )
 			return false;
 			return false;
+
+		if( !getTerrain().showChecker || getTerrain().showComplexity ) {
+			if( getTerrain().surfaceArray == null || getTerrain().surfaces.length == 0 || surfaceWeights.length != getTerrain().surfaces.length )
+				return false;
+
+			for( i in 0 ... surfaceWeights.length )
+				if( surfaceWeights[i] == null )
+					return false;
+		}
+
 		if( heightMap == null )
 		if( heightMap == null )
 			return false;
 			return false;
-		for( i in 0 ... surfaceWeights.length )
-			if( surfaceWeights[i] == null )
-				return false;
+
 		return true;
 		return true;
 	}
 	}
 
 
-	override function getLocalCollider():h3d.col.Collider {
+	override function getLocalCollider() : h3d.col.Collider {
 		return null;
 		return null;
 	}
 	}
 }
 }

+ 103 - 100
h3d/shader/pbr/Terrain.hx

@@ -86,108 +86,111 @@ class Terrain extends hxsl.Shader {
 		}
 		}
 
 
 		function fragment() {
 		function fragment() {
-			// Extract participating surfaces from the pixel
-			var texIndex = surfaceIndexMap.get(calculatedUV).rgb;
-
-			var i1 : Int = int(texIndex.r * 255);
-			var uv1 = getPOMUV(calculatedUV, i1);
-			var surfaceUV1 = getsurfaceUV(i1, uv1);
-			var pbr1 = pbrTextures.get(surfaceUV1).rgba;
-			var albedo1 = albedoTextures.get(surfaceUV1).rgb;
-			var normal1 = normalTextures.get(surfaceUV1).rgba;
-			var h1 = pbr1.a;
-			var aw1 = weightTextures.get(vec3(calculatedUV, i1)).r;
-
-			var i2 : Int = int(texIndex.g * 255);
-			var aw2 = weightTextures.get(vec3(calculatedUV, i2)).r;
-
-			var i3 : Int = int(texIndex.b * 255);
-			var aw3 = weightTextures.get(vec3(calculatedUV, i3)).r;
-
-			// Sum of each surface
-			var albedo = vec3(0);
-			var normal = vec4(0,0,0,0);
-			var pbr = vec4(0);
-			var weightSum = 0.0;
-
-			// Keep the surface with the heightest weight for sharpness
-			var maxAlbedo = vec3(0);
-			var maxPbr = vec4(0);
-			var maxNormal = vec4(0);
-			var curMaxWeight = -1.0;
-
-			// Alpha / Height Blend
-			var b1 = 0.0, b2 = 0.0, b3 = 0.0;
-			b1 = mix(aw1, aw1 * h1, heightBlendStrength);
-			albedo += albedo1 * b1;
-			pbr += pbr1 * b1;
-			normal += normal1 * b1;
-
-			// Find the max
-			var maxW = clamp(ceil(b1 - curMaxWeight), 0, 1);
-			curMaxWeight = mix(curMaxWeight, b1, maxW);
-			maxAlbedo = mix(maxAlbedo, albedo1, maxW);
-			maxPbr = mix(maxPbr, pbr1, maxW);
-			maxNormal = mix(maxNormal, normal1, maxW);
-
-			if(aw2 > 0){
-				var uv2 = getPOMUV(calculatedUV, i2);
-				var surfaceUV2 = getsurfaceUV(i2, uv2);
-				var pbr2 = pbrTextures.get(surfaceUV2).rgba;
-				var albedo2 = albedoTextures.get(surfaceUV2).rgb;
-				var normal2 = normalTextures.get(surfaceUV2).rgba;
-				var h2 = pbr2.a;
-				b2 = mix(aw2, aw2 * h2, heightBlendStrength);
-				albedo += albedo2 * b2;
-				pbr += pbr2 * b2;
-				normal += normal2 * b2;
-				maxW = clamp(ceil(b2 - curMaxWeight), 0, 1);
-				curMaxWeight = mix(curMaxWeight, b2, maxW);
-				maxAlbedo = mix(maxAlbedo, albedo2, maxW);
-				maxPbr = mix(maxPbr, pbr2, maxW);
-				maxNormal = mix(maxNormal, normal2, maxW);
-			}
 
 
-			if(aw3 > 0){
-				var uv3 = getPOMUV(calculatedUV, i3);
-				var surfaceUV3 = getsurfaceUV(i3, uv3);
-				var pbr3 = pbrTextures.get(surfaceUV3).rgba;
-				var albedo3 = albedoTextures.get(surfaceUV3).rgb;
-				var normal3 = normalTextures.get(surfaceUV3).rgba;
-				var h3 = pbr3.a;
-				b3 = mix(aw3, aw3 * h3, heightBlendStrength);
-				albedo += albedo3 * b3;
-				pbr += pbr3 * b3;
-				normal += normal3 * b3;
-				maxW = clamp(ceil(b3 - curMaxWeight), 0,1);
-				curMaxWeight = mix(curMaxWeight, b3, maxW);
-				maxAlbedo = mix(maxAlbedo, albedo3, maxW);
-				maxPbr = mix(maxPbr, pbr3, maxW);
-				maxNormal = mix(maxNormal, normal3, maxW);
+			if( !CHECKER && !COMPLEXITY ) {
+				// Extract participating surfaces from the pixel
+				var texIndex = surfaceIndexMap.get(calculatedUV).rgb;
+
+				var i1 : Int = int(texIndex.r * 255);
+				var uv1 = getPOMUV(calculatedUV, i1);
+				var surfaceUV1 = getsurfaceUV(i1, uv1);
+				var pbr1 = pbrTextures.get(surfaceUV1).rgba;
+				var albedo1 = albedoTextures.get(surfaceUV1).rgb;
+				var normal1 = normalTextures.get(surfaceUV1).rgba;
+				var h1 = pbr1.a;
+				var aw1 = weightTextures.get(vec3(calculatedUV, i1)).r;
+
+				var i2 : Int = int(texIndex.g * 255);
+				var aw2 = weightTextures.get(vec3(calculatedUV, i2)).r;
+
+				var i3 : Int = int(texIndex.b * 255);
+				var aw3 = weightTextures.get(vec3(calculatedUV, i3)).r;
+
+				// Sum of each surface
+				var albedo = vec3(0);
+				var normal = vec4(0,0,0,0);
+				var pbr = vec4(0);
+				var weightSum = 0.0;
+
+				// Keep the surface with the heightest weight for sharpness
+				var maxAlbedo = vec3(0);
+				var maxPbr = vec4(0);
+				var maxNormal = vec4(0);
+				var curMaxWeight = -1.0;
+
+				// Alpha / Height Blend
+				var b1 = 0.0, b2 = 0.0, b3 = 0.0;
+				b1 = mix(aw1, aw1 * h1, heightBlendStrength);
+				albedo += albedo1 * b1;
+				pbr += pbr1 * b1;
+				normal += normal1 * b1;
+
+				// Find the max
+				var maxW = clamp(ceil(b1 - curMaxWeight), 0, 1);
+				curMaxWeight = mix(curMaxWeight, b1, maxW);
+				maxAlbedo = mix(maxAlbedo, albedo1, maxW);
+				maxPbr = mix(maxPbr, pbr1, maxW);
+				maxNormal = mix(maxNormal, normal1, maxW);
+
+				if(aw2 > 0){
+					var uv2 = getPOMUV(calculatedUV, i2);
+					var surfaceUV2 = getsurfaceUV(i2, uv2);
+					var pbr2 = pbrTextures.get(surfaceUV2).rgba;
+					var albedo2 = albedoTextures.get(surfaceUV2).rgb;
+					var normal2 = normalTextures.get(surfaceUV2).rgba;
+					var h2 = pbr2.a;
+					b2 = mix(aw2, aw2 * h2, heightBlendStrength);
+					albedo += albedo2 * b2;
+					pbr += pbr2 * b2;
+					normal += normal2 * b2;
+					maxW = clamp(ceil(b2 - curMaxWeight), 0, 1);
+					curMaxWeight = mix(curMaxWeight, b2, maxW);
+					maxAlbedo = mix(maxAlbedo, albedo2, maxW);
+					maxPbr = mix(maxPbr, pbr2, maxW);
+					maxNormal = mix(maxNormal, normal2, maxW);
+				}
+
+				if(aw3 > 0){
+					var uv3 = getPOMUV(calculatedUV, i3);
+					var surfaceUV3 = getsurfaceUV(i3, uv3);
+					var pbr3 = pbrTextures.get(surfaceUV3).rgba;
+					var albedo3 = albedoTextures.get(surfaceUV3).rgb;
+					var normal3 = normalTextures.get(surfaceUV3).rgba;
+					var h3 = pbr3.a;
+					b3 = mix(aw3, aw3 * h3, heightBlendStrength);
+					albedo += albedo3 * b3;
+					pbr += pbr3 * b3;
+					normal += normal3 * b3;
+					maxW = clamp(ceil(b3 - curMaxWeight), 0,1);
+					curMaxWeight = mix(curMaxWeight, b3, maxW);
+					maxAlbedo = mix(maxAlbedo, albedo3, maxW);
+					maxPbr = mix(maxPbr, pbr3, maxW);
+					maxNormal = mix(maxNormal, normal3, maxW);
+				}
+
+				// Normalisation
+				weightSum = b1 + b2 + b3;
+				albedo /= vec3(weightSum);
+				pbr /= vec4(weightSum);
+				normal /= vec4(weightSum);
+
+				// Sharpness
+				albedo = mix(albedo, maxAlbedo, heightBlendSharpness);
+				pbr = mix(pbr, maxPbr, heightBlendSharpness);
+				normal = mix(normal, maxNormal, heightBlendSharpness);
+
+				// Output
+				normal = vec4(unpackNormal(normal), 0.0);
+				pixelColor = vec4(albedo, 1.0);
+				transformedNormal = normalize(normal.xyz) * TBN;
+				roughnessValue = 1 - pbr.g * pbr.g;
+				metalnessValue = pbr.r;
+				occlusionValue = pbr.b;
+				emissiveValue = 0;
 			}
 			}
 
 
-			// Normalisation
-			weightSum = b1 + b2 + b3;
-			albedo /= vec3(weightSum);
-			pbr /= vec4(weightSum);
-			normal /= vec4(weightSum);
-
-			// Sharpness
-			albedo = mix(albedo, maxAlbedo, heightBlendSharpness);
-			pbr = mix(pbr, maxPbr, heightBlendSharpness);
-			normal = mix(normal, maxNormal, heightBlendSharpness);
-
-			// Output
-			normal = vec4(unpackNormal(normal), 0.0);
-			pixelColor = vec4(albedo, 1.0);
-			transformedNormal = normalize(normal.xyz) * TBN;
-			roughnessValue = 1 - pbr.g * pbr.g;
-			metalnessValue = pbr.r;
-			occlusionValue = pbr.b;
-			emissiveValue = 0;
-
 			// DEBUG
 			// DEBUG
-			if(CHECKER){
+			if( CHECKER ) {
 				var tile = abs(abs(floor(input.position.x)) % 2 - abs(floor(input.position.y)) % 2);
 				var tile = abs(abs(floor(input.position.x)) % 2 - abs(floor(input.position.y)) % 2);
 				pixelColor = vec4(mix(vec3(0.4), vec3(0.1), tile), 1.0);
 				pixelColor = vec4(mix(vec3(0.4), vec3(0.1), tile), 1.0);
 				transformedNormal = vec3(0,0,1) * TBN;
 				transformedNormal = vec3(0,0,1) * TBN;
@@ -196,7 +199,7 @@ class Terrain extends hxsl.Shader {
 				occlusionValue = 1;
 				occlusionValue = 1;
 				emissiveValue = 0;
 				emissiveValue = 0;
 			}
 			}
-			else if(COMPLEXITY){
+			else if( COMPLEXITY ) {
 				var blendCount = 0 + weightTextures.get(vec3(0)).r * 0;
 				var blendCount = 0 + weightTextures.get(vec3(0)).r * 0;
 				for(i in 0 ... SURFACE_COUNT)
 				for(i in 0 ... SURFACE_COUNT)
 					blendCount += ceil(weightTextures.get(vec3(calculatedUV, i)).r);
 					blendCount += ceil(weightTextures.get(vec3(calculatedUV, i)).r);
@@ -207,7 +210,7 @@ class Terrain extends hxsl.Shader {
 				metalnessValue = 0;
 				metalnessValue = 0;
 				occlusionValue = 1;
 				occlusionValue = 1;
 			}
 			}
-			if(SHOW_GRID){
+			if( SHOW_GRID ) {
 				var gridColor = vec4(1,0,0,1);
 				var gridColor = vec4(1,0,0,1);
 				var tileEdgeColor = vec4(1,1,0,1);
 				var tileEdgeColor = vec4(1,1,0,1);
 				var grid : Vec2 = ((input.position.xy.mod(cellSize) / cellSize ) - 0.5) * 2.0;
 				var grid : Vec2 = ((input.position.xy.mod(cellSize) / cellSize ) - 0.5) * 2.0;