Browse Source

Update Terrain binary save

ShiroSmith 5 years ago
parent
commit
3654646d78
2 changed files with 15 additions and 4 deletions
  1. 3 2
      hrt/prefab/terrain/Terrain.hx
  2. 12 2
      hrt/prefab/terrain/Tile.hx

+ 3 - 2
hrt/prefab/terrain/Terrain.hx

@@ -327,12 +327,13 @@ class Terrain extends Object3D {
 
 	public function saveNormals( ctx : Context ) {
 		for( tile in terrain.tiles ) {
-			if( tile.grid == null || tile.grid.normals == null ) continue;
+			if( tile.grid == null || tile.grid.normals == null || tile.grid.tangents == null ) continue;
 			var normals = tile.grid.normals;
 			var tangents = tile.grid.tangents;
 			var fileName = tile.tileX + "_" + tile.tileY + "_" + "n";
 			var stride = 3 * 4 + 3 * 4; // Normal + Tangent
-			var bytes = haxe.io.Bytes.alloc(normals.length * stride);
+			var vertexCount = normals.length;
+			var bytes = haxe.io.Bytes.alloc(vertexCount * stride);
 			for( i in 0 ... normals.length ) {
 				bytes.setFloat(i*stride, normals[i].x);
 				bytes.setFloat(i*stride+4, normals[i].y);

+ 12 - 2
hrt/prefab/terrain/Tile.hx

@@ -68,10 +68,20 @@ class Tile extends h3d.scene.Mesh {
 	}
 
 	public function createBigPrim( bytes : haxe.io.Bytes ) {
-		if( bigPrim != null ) bigPrim.dispose();
+
+		var stride = 3 * 4 + 3 * 4; // Normal + Tangent
+		var vertexCount = (terrain.cellCount + 1) * (terrain.cellCount + 1);
+		var length = vertexCount * stride;
+		if( bytes.length != stride * vertexCount ) {
+			throw "Bytes length doesn't match with the size of tiles";
+			return;
+		}
+
+		if( bigPrim != null )
+			bigPrim.dispose();
+
 		normalTangentBytes = bytes;
 		bigPrim = new h3d.prim.BigPrimitive(9, true);
-		var stride = 3 * 4 + 3 * 4; // Normal + Tangent
 		inline function addVertice(x : Float, y : Float, i : Int) {
 			// Pos
 			bigPrim.addPoint(x, y, getHeight(x / terrain.tileSize, y / terrain.tileSize)); // Use addPoint() instead of addVertexValue() for the bounds