ソースを参照

Update Terrain

ShiroSmith 5 年 前
コミット
14fe85cf61
1 ファイル変更40 行追加31 行削除
  1. 40 31
      hrt/prefab/terrain/Terrain.hx

+ 40 - 31
hrt/prefab/terrain/Terrain.hx

@@ -25,14 +25,14 @@ class Terrain extends Object3D {
 	public var heightMapResolution : Int = 64;
 	public var heightMapResolution : Int = 64;
 	public var weightMapResolution : Int = 128;
 	public var weightMapResolution : Int = 128;
 	// Parallax Param
 	// Parallax Param
-	var parallaxAmount = 0.0;
-	var parallaxMinStep : Int = 1;
-	var parallaxMaxStep : Int = 16;
+	public var parallaxAmount = 0.0;
+	public var parallaxMinStep : Int = 1;
+	public var parallaxMaxStep : Int = 16;
 	// Blend Param
 	// Blend Param
-	var heightBlendStrength : Float = 0.0;
-	var blendSharpness : Float = 0.0;
+	public var heightBlendStrength : Float = 0.0;
+	public var blendSharpness : Float = 0.0;
 	// Shadows Param
 	// Shadows Param
-	var castShadows = false;
+	public var castShadows = false;
 	// Data for binary save/load
 	// Data for binary save/load
 	var surfaceCount = 0;
 	var surfaceCount = 0;
 	var surfaceSize = 0;
 	var surfaceSize = 0;
@@ -87,32 +87,41 @@ class Terrain extends Object3D {
 		obj.heightBlendStrength = heightBlendStrength;
 		obj.heightBlendStrength = heightBlendStrength;
 		obj.blendSharpness = blendSharpness;
 		obj.blendSharpness = blendSharpness;
 		obj.castShadows = castShadows;
 		obj.castShadows = castShadows;
-		var surfacesProps : Array<SurfaceProps> = [];
-		for(surface in terrain.surfaces){
-			var surfaceProps : SurfaceProps =
-			{
-				albedo : surface.albedo.name,
-				normal : surface.normal.name,
-				pbr : surface.pbr.name,
-				tilling : surface.tilling,
-				angle : surface.angle,
-				offsetX : surface.offset.x,
-				offsetY : surface.offset.y,
-				minHeight : surface.minHeight,
-				maxHeight : surface.maxHeight,
-			};
-			surfacesProps.push(surfaceProps);
+		if( terrain != null && terrain.surfaces != null ) {
+
+			obj.surfaceCount = terrain.surfaces.length == 0 ? 0 : terrain.surfaceArray.surfaceCount;
+			obj.surfaceSize = terrain.surfaces.length == 0 ? 0 : terrain.surfaceArray.albedo.width;
+
+			var surfacesProps : Array<SurfaceProps> = [];
+			for(surface in terrain.surfaces){
+				var surfaceProps : SurfaceProps =
+				{
+					albedo : surface.albedo.name,
+					normal : surface.normal.name,
+					pbr : surface.pbr.name,
+					tilling : surface.tilling,
+					angle : surface.angle,
+					offsetX : surface.offset.x,
+					offsetY : surface.offset.y,
+					minHeight : surface.minHeight,
+					maxHeight : surface.maxHeight,
+				};
+				surfacesProps.push(surfaceProps);
+			}
+			obj.surfaces = surfacesProps;
+		}
+		else {
+			// When cloning 
+			obj.surfaces = tmpSurfacesProps;
+			obj.surfaceCount = tmpSurfacesProps.length;
 		}
 		}
-		obj.surfaces = surfacesProps;
-		obj.surfaceCount = terrain.surfaces.length == 0 ? 0 : terrain.surfaceArray.surfaceCount;
-		obj.surfaceSize = terrain.surfaces.length == 0 ? 0 : terrain.surfaceArray.albedo.width;
 
 
 		#if editor
 		#if editor
 		obj.autoCreateTile = autoCreateTile;
 		obj.autoCreateTile = autoCreateTile;
-	 	obj.showChecker = terrain.showChecker;
+		if( terrain != null ) obj.showChecker = terrain.showChecker;
 		if( modified ) {
 		if( modified ) {
 			modified = false;
 			modified = false;
-			if( editor != null && terrain.surfaces.length > 0 ) editor.saveTextures();
+			if( editor != null && terrain != null && terrain.surfaces != null && terrain.surfaces.length > 0 ) editor.saveTextures();
 		}
 		}
 		#end
 		#end
 
 
@@ -436,7 +445,7 @@ class Terrain extends Object3D {
 		ctx = ctx.clone(this);
 		ctx = ctx.clone(this);
 
 
 
 
-		#if editor
+		/*#if editor
 		// Attach the terrain to the scene directly, avoid slow refresh
 		// Attach the terrain to the scene directly, avoid slow refresh
 		if( cachedInstance != null ) {
 		if( cachedInstance != null ) {
 			ctx.local3d = terrain;
 			ctx.local3d = terrain;
@@ -448,9 +457,9 @@ class Terrain extends Object3D {
 			terrain = new TerrainMesh(ctx.local3d.getScene());
 			terrain = new TerrainMesh(ctx.local3d.getScene());
 			cachedInstance = terrain;
 			cachedInstance = terrain;
 		}
 		}
-		#else
+		#else*/
 		terrain = new TerrainMesh(ctx.local3d);
 		terrain = new TerrainMesh(ctx.local3d);
-		#end
+		//#end
 
 
 		terrain.cellCount = getCellCount();
 		terrain.cellCount = getCellCount();
 		terrain.cellSize = getCellSize();
 		terrain.cellSize = getCellSize();
@@ -500,13 +509,13 @@ class Terrain extends Object3D {
 		#end
 		#end
 	}
 	}
 
 
-	function getCellCount() {
+	public function getCellCount() {
 		var resolution = Math.max(0.1, cellSize);
 		var resolution = Math.max(0.1, cellSize);
 		var cellCount = Math.ceil(Math.min(1000, tileSize / resolution));
 		var cellCount = Math.ceil(Math.min(1000, tileSize / resolution));
 		return cellCount;
 		return cellCount;
 	}
 	}
 
 
-	function getCellSize() {
+	public function getCellSize() {
 		var cellCount = getCellCount();
 		var cellCount = getCellCount();
 		var finalCellSize = tileSize / cellCount;
 		var finalCellSize = tileSize / cellCount;
 		return finalCellSize;
 		return finalCellSize;