Răsfoiți Sursa

- Update Terrain
- Update Prefab Data savepath

ShiroSmith 7 ani în urmă
părinte
comite
b3e07fc0e3

+ 22 - 0
hide/prefab/ContextShared.hx

@@ -66,5 +66,27 @@ class ContextShared extends hxd.prefab.ContextShared {
 		sys.io.File.saveBytes(file, bytes);
 	}
 
+	override function savePrefabDat(file : String, ext:String, p : hide.prefab.Prefab, bytes : haxe.io.Bytes ){
+		var path = new haxe.io.Path(currentPath);
+		path.ext = "dat";
+		var datDir = path.toString();
+		var instanceDir = datDir + "/" + p.name;
+		if(!sys.FileSystem.isDirectory( hide.Ide.inst.getPath(datDir)))
+			sys.FileSystem.createDirectory( hide.Ide.inst.getPath(datDir));
+		if(!sys.FileSystem.isDirectory( hide.Ide.inst.getPath(instanceDir)))
+			sys.FileSystem.createDirectory( hide.Ide.inst.getPath(instanceDir));
+
+		var path = new haxe.io.Path("");
+		path.dir = instanceDir;
+		path.file = file;
+		path.ext = ext;
+
+		var file = hide.Ide.inst.getPath(path.toString());
+		if( bytes == null )
+			try sys.FileSystem.deleteFile(file) catch( e : Dynamic ) {};
+		else
+			sys.io.File.saveBytes(file, bytes);
+	}
+
 	#end
 }

+ 2 - 1
hide/prefab/terrain/Brush.hx

@@ -79,7 +79,8 @@ class BrushPreview {
 			tiles.push(tilePreview);
 		}
 		tilePreview.used = true;
-		tilePreview.heightMap = terrain.getTile(x,y).heightMap;
+		var t = terrain.getTile(x,y);
+		tilePreview.heightMap = t == null ? null : t.heightMap;
 		tilePreview.shader.heightMapSize = tilePreview.heightMap.width;
 		var pos = new h3d.Vector(x * terrain.tileSize, y * terrain.tileSize);
 		tilePreview.setPosition(pos.x, pos.y, pos.z + 0.1 * terrain.scaleZ * offsetDir);

+ 29 - 88
hide/prefab/terrain/Terrain.hx

@@ -11,12 +11,6 @@ typedef SurfaceProps = {
 	offsetY : Float
 };
 
-enum RenderMode {
-	PBR;
-	ShaderComplexity;
-	Checker;
-}
-
 class Terrain extends Object3D {
 
 	public var tileSize = 10.0;
@@ -36,7 +30,6 @@ class Terrain extends Object3D {
 
 	#if editor
 	var editor : hide.prefab.terrain.TerrainEditor;
-	public var renderMode : RenderMode = PBR;
 	#end
 
 	override function load( obj : Dynamic ) {
@@ -84,22 +77,15 @@ class Terrain extends Object3D {
 		return obj;
 	}
 
-	function getTerrainDir(ctx : Context){
-		if(ctx.shared.currentPath == null) return null;
-		return ctx.shared.currentPath.split(".l3d")[0] + "_terrain";
-	}
-
 	public function saveHeightTextures(ctx : Context){
-		var dir = getTerrainDir(ctx);
 		for(tile in terrain.tiles){
 			var pixels = tile.heightMap.capturePixels();
 			var name = tile.tileX + "_" + tile.tileY + "_" + "h";
-			ctx.shared.saveTexture(name, pixels.bytes, dir, "heightMap");
+			ctx.shared.savePrefabDat(name, "heightMap", this, pixels.bytes);
 		}
 	}
 
 	public function saveWeightTextures(ctx : Context){
-		var dir = getTerrainDir(ctx);
 		var packedWeightsTex = new h3d.mat.Texture(terrain.weightMapResolution, terrain.weightMapResolution, [Target], RGBA);
 		for(tile in terrain.tiles){
 			h3d.Engine.getCurrent().pushTarget(packedWeightsTex);
@@ -111,69 +97,33 @@ class Terrain extends Object3D {
 			var pixels = packedWeightsTex.capturePixels();
 			var bytes = pixels.toPNG();
 			var name = tile.tileX + "_" + tile.tileY + "_" + "w";
-			ctx.shared.saveTexture(name, bytes, dir, "png");
+			ctx.shared.savePrefabDat(name, "png", this, bytes);
 
 			var pixels = tile.surfaceIndexMap.capturePixels();
 			var bytes = pixels.toPNG();
 			var name = tile.tileX + "_" + tile.tileY + "_" + "i";
-			ctx.shared.saveTexture(name, bytes, dir, "png");
-		}
-	}
-
-	function loadHeightTextures(ctx : Context){
-		var dir = getTerrainDir(ctx);
-		if(!sys.FileSystem.isDirectory(hide.Ide.inst.getPath(dir))) sys.FileSystem.createDirectory(hide.Ide.inst.getPath(dir));
-		var files = sys.FileSystem.readDirectory(hide.Ide.inst.getPath(dir));
-		for(file in files){
-			var texName = file.split(".heightMap")[0];
-			var coords = texName.split("_");
-			if(coords[2] != "h") continue;
-			var x = Std.parseInt(coords[0]);
-			var y = Std.parseInt(coords[1]);
-			var bytes = ctx.shared.loadBytes(dir + "/" + file);
-			if(bytes == null) continue;
-			var pixels : hxd.Pixels.PixelsFloat = new hxd.Pixels(heightMapResolution + 1, heightMapResolution + 1, bytes, RGBA32F);
-			var tile = terrain.createTile(x, y);
-			tile.heightMap.uploadPixels(pixels);
-			tile.refreshMesh();
+			ctx.shared.savePrefabDat(name, "png", this, bytes);
 		}
 	}
 
-	function loadWeightTextures(ctx : Context){
-		var dir = ctx.shared.currentPath.split(".l3d")[0] + "_terrain";
-		if(!sys.FileSystem.isDirectory(hide.Ide.inst.getPath(dir))) sys.FileSystem.createDirectory(hide.Ide.inst.getPath(dir));
-		var files = sys.FileSystem.readDirectory(hide.Ide.inst.getPath(dir));
-		for(file in files){
-			var texName = file.split(".png")[0];
-			var coords = texName.split("_");
-			if(coords[2] != "i") continue;
-			var x = Std.parseInt(coords[0]);
-			var y = Std.parseInt(coords[1]);
-			var tile = terrain.createTile(x, y);
-			var tex = ctx.loadTexture(dir + "/" + file);
-			function wait() {
-				if( tex.flags.has(Loading) ) haxe.Timer.delay(wait, 1);
-				else {
-					tile.surfaceIndexMap = tex.clone();
-					tile.surfaceIndexMap.filter = Nearest;
-					tile.surfaceIndexMap.flags.set(Target);
-					tex.dispose();
-				}
-			}
-			wait();
-		}
-
-		for(file in files){
-			var texName = file.split(".png")[0];
-			var coords = texName.split("_");
-			if(coords[2] != "w") continue;
+	function loadTerrain(ctx : Context){
+		var resDir = ctx.shared.loadDir(this);
+		if(resDir == null) return;
+		for(res in resDir){
+			var file = res.name.split(".")[0];
+			var coords = file.split("_");
 			var x = Std.parseInt(coords[0]);
 			var y = Std.parseInt(coords[1]);
+			var type = coords[2];
 			var tile = terrain.createTile(x, y);
-			var tex = ctx.loadTexture(dir + "/" + file);
-			function wait() {
-				if( tex.flags.has(Loading) || tile.surfaceIndexMap == null) haxe.Timer.delay(wait, 1);
-				else {
+			switch(type){
+				case "h":
+					var bytes = res.entry.getBytes();
+					var pixels : hxd.Pixels.PixelsFloat = new hxd.Pixels(heightMapResolution + 1, heightMapResolution + 1, bytes, RGBA32F);
+					tile.heightMap.uploadPixels(pixels);
+					tile.refreshMesh();
+				case "w":
+					var tex = res.toTexture();
 					for(i in 0 ... tile.surfaceWeights.length){
 						h3d.Engine.getCurrent().pushTarget(tile.surfaceWeights[i]);
 						unpackWeight.shader.indexMap = tile.surfaceIndexMap;
@@ -183,9 +133,13 @@ class Terrain extends Object3D {
 					}
 					tile.generateWeightArray();
 					tex.dispose();
-				}
+				case"i":
+					var tex = res.toTexture();
+					tile.surfaceIndexMap = tex.clone();
+					tile.surfaceIndexMap.filter = Nearest;
+					tile.surfaceIndexMap.flags.set(Target);
+					tex.dispose();
 			}
-			wait();
 		}
 	}
 
@@ -211,9 +165,9 @@ class Terrain extends Object3D {
 
 		for(surfaceProps in tmpSurfacesProps){
 			var surface = terrain.addEmptySurface();
-			var albedo = ctx.shared.loadTexture(hide.Ide.inst.getPath(surfaceProps.albedo));
-			var normal = ctx.shared.loadTexture(hide.Ide.inst.getPath(surfaceProps.normal));
-			var pbr = ctx.shared.loadTexture(hide.Ide.inst.getPath(surfaceProps.pbr));
+			var albedo = ctx.shared.loadTexture(surfaceProps.albedo);
+			var normal = ctx.shared.loadTexture(surfaceProps.normal);
+			var pbr = ctx.shared.loadTexture(surfaceProps.pbr);
 			function wait() {
 				if( albedo.flags.has(Loading) || normal.flags.has(Loading)|| pbr.flags.has(Loading))
 					haxe.Timer.delay(wait, 1);
@@ -242,8 +196,7 @@ class Terrain extends Object3D {
 				}
 			if(ready){
 				terrain.generateSurfaceArray();
-				loadWeightTextures(ctx);
-				loadHeightTextures(ctx);
+				loadTerrain(ctx);
 				for(tile in terrain.tiles)
 					tile.blendEdges();
 			}
@@ -266,21 +219,9 @@ class Terrain extends Object3D {
 		terrain.heightBlendSharpness = heightBlendSharpness;
 
 		if(editor != null) editor.update(propName);
-		if(propName == "renderMode") updateRender();
 		#end
 	}
 
-	function updateRender(){
-		for(tile in terrain.tiles) tile.material.removePass(tile.material.getPass("overlay"));
-		terrain.showChecker = false;
-		terrain.showComplexity = false;
-		switch(renderMode){
-			case PBR:
-			case ShaderComplexity: terrain.showComplexity = true;
-			case Checker: terrain.showChecker = true;
-		}
-	}
-
 	function getCellCount(){
 		var resolution = Math.max(0.1, cellSize);
 		var cellCount = Math.ceil(Math.min(1000, tileSize / resolution));
@@ -314,7 +255,7 @@ class Terrain extends Object3D {
 				<dt>Show Grid</dt><dd><input type="checkbox" field="terrain.showGrid"/></dd>
 				<dt>Visible</dt><dd><input type="checkbox" field="visible"/></dd>
 				<dt>Mode</dt>
-				<dd><select field="renderMode">
+				<dd><select field="editor.renderMode">
 					<option value="PBR">PBR</option>
 					<option value="ShaderComplexity">Shader Complexity</option>
 					<option value="Checker">Checker</option>

+ 35 - 15
hide/prefab/terrain/TerrainEditor.hx

@@ -1,7 +1,15 @@
+
 package hide.prefab.terrain;
+#if editor
 using Lambda;
 import hxd.Key as K;
 
+enum RenderMode {
+	PBR;
+	ShaderComplexity;
+	Checker;
+}
+
 class TerrainRevertData {
 	public var surfaceIndex : Int;
 	public var surface : h3d.scene.pbr.terrain.Surface;
@@ -52,6 +60,7 @@ class TerrainEditor {
 	var uvTexRes = 0.5;
 	var customScene : h3d.scene.Scene;
 	var customRenderer : hide.prefab.terrain.CustomRenderer;
+	var renderMode : RenderMode = PBR;
 
 	public function new(terrainPrefab, undo : hide.ui.UndoHistory){
 		this.terrainPrefab = terrainPrefab;
@@ -85,6 +94,7 @@ class TerrainEditor {
 		|| propName == "editor.currentSurface.angle")
 			terrainPrefab.terrain.updateSurfaceParams();
 		autoCreateTile = terrainPrefab.autoCreateTile;
+		if(propName == "terrain.renderMode") updateRender();
 	}
 
 	public function refresh(){
@@ -93,6 +103,17 @@ class TerrainEditor {
 		weightStrokeBufferArray.refresh(terrainPrefab.weightMapResolution);
 	}
 
+	function updateRender(){
+		for(tile in terrainPrefab.terrain.tiles) tile.material.removePass(tile.material.getPass("overlay"));
+		terrainPrefab.terrain.showChecker = false;
+		terrainPrefab.terrain.showComplexity = false;
+		switch(renderMode){
+			case PBR:
+			case ShaderComplexity: terrainPrefab.terrain.showComplexity = true;
+			case Checker: terrainPrefab.terrain.showChecker = true;
+		}
+	}
+
 	function renderTerrainUV(ctx : Context){
 		var engine = h3d.Engine.getCurrent();
 		var mainScene = @:privateAccess ctx.local3d.getScene();
@@ -346,7 +367,6 @@ class TerrainEditor {
 		fetchPos.y = hxd.Math.clamp(fetchPos.y, 0, uvTexPixels.height - 1);
 		var pixel = uvTexPixels.getPixelF( Std.int(fetchPos.x), Std.int(fetchPos.y));
 		var tiles = terrainPrefab.terrain.getVisibleTiles(@:privateAccess ctx.local3d.getScene().camera);
-		trace(pixel.z);
 		for(i in 0 ... tiles.length)
 			if( hxd.Math.ceil(pixel.z) == i)
 				brushWorldPos = tiles[i].localToGlobal(new h3d.Vector(pixel.x * terrainPrefab.tileSize, pixel.y * terrainPrefab.tileSize, 0));
@@ -735,19 +755,17 @@ class TerrainEditor {
 			</div>');
 
 			props.find(".save").click(function(_) {
-			var dir = ctx.rootContext.shared.currentPath.split(".l3d")[0] + "_terrain";
-			var dirPath = hide.Ide.inst.getPath(dir);
-			var files = sys.FileSystem.readDirectory(dirPath);
-			for(file in files){
-				var name = file.split(".heightMap")[0];
-				name = name.split(".png")[0];
-				var coords = name.split("_");
-				if(coords[2] != "h" && coords[2] != "i" && coords[2] != "w") continue;
-				sys.FileSystem.deleteFile(dirPath + "/" + file);
-			}
-			terrainPrefab.saveWeightTextures(ctx.rootContext);
-			terrainPrefab.saveHeightTextures(ctx.rootContext);
-		});
+				var datPath = new haxe.io.Path(ctx.rootContext.shared.currentPath);
+				datPath.ext = "dat";
+				var fullPath = hide.Ide.inst.getPath(datPath.toString() + "/" + terrainPrefab.name);
+				if( sys.FileSystem.isDirectory(fullPath)){
+					var files = sys.FileSystem.readDirectory(fullPath);
+					for(file in files)
+						sys.FileSystem.deleteFile(fullPath + "/" + file);
+				}
+				terrainPrefab.saveWeightTextures(ctx.rootContext);
+				terrainPrefab.saveHeightTextures(ctx.rootContext);
+			});
 
 		var brushes : Array<Dynamic> = ctx.scene.config.get("terrain.brushes");
 		var brushesContainer = props.find(".terrain-brushes");
@@ -836,4 +854,6 @@ class TerrainEditor {
 		}
 		tmpTexPath = null;
 	}
-}
+}
+
+#end