Kaynağa Gözat

Merge branch 'master' of https://github.com/HeapsIO/hide

trethaller 5 yıl önce
ebeveyn
işleme
638728a632

+ 1 - 0
hide/Ide.hx

@@ -806,6 +806,7 @@ class Ide {
 		}
 		menu.find(".project .open").click(function(_) {
 			chooseDirectory(function(dir) {
+				if( dir == null ) return;
 				if( StringTools.endsWith(dir,"/res") || StringTools.endsWith(dir,"\\res") )
 					dir = dir.substr(0,-4);
 				setProject(dir);

+ 4 - 4
hide/comp/SceneEditor.hx

@@ -1,5 +1,6 @@
 package hide.comp;
 
+import h3d.col.Sphere;
 import hrt.prefab.terrain.Terrain;
 import h3d.scene.Mesh;
 import h3d.col.FPoint;
@@ -587,7 +588,7 @@ class SceneEditor {
 		var meshes = context.shared.getObjects(elt, h3d.scene.Mesh);
 		var invRootMat = local3d.getAbsPos().clone();
 		invRootMat.invert();
-		var bounds = new h3d.col.Bounds();
+		var bounds = new h3d.col.Sphere();
 		for(mesh in meshes) {
 			if(mesh.ignoreCollide)
 				continue;
@@ -602,9 +603,8 @@ class SceneEditor {
 
 			var localMat = mesh.getAbsPos().clone();
 			localMat.multiply(localMat, invRootMat);
-			var lb = mesh.primitive.getBounds().clone();
-			lb.transform(localMat);
-			bounds.add(lb);
+			var lb = mesh.primitive.getBounds().toSphere();
+			bounds.r = hxd.Math.max(bounds.r, lb.r + localMat.getPosition().length());
 		}
 		var meshCollider = new h3d.col.Collider.GroupCollider([for(m in meshes) {
 			var c : h3d.col.Collider = try m.getGlobalCollider() catch(e: Dynamic) null;

+ 22 - 9
hrt/prefab/l3d/MeshGenerator.hx

@@ -97,6 +97,18 @@ class MeshPart {
 	}
 }
 
+class MeshGeneratorRoot extends h3d.scene.Object {
+
+	public function new( ?parent : h3d.scene.Object ) {
+		super(parent);
+	}
+
+	override function syncRec( ctx ) {
+		if( posChanged ) 
+			super.syncRec(ctx);
+	}
+}
+
 class MeshGenerator extends Object3D {
 
 	public var root : MeshPart;
@@ -130,7 +142,7 @@ class MeshGenerator extends Object3D {
 		ctx.local3d = new h3d.scene.Object(ctx.local3d);
 		ctx.local3d.name = name;
 
-		var rootObject = new h3d.scene.Object(ctx.local3d);
+		var rootObject = new MeshGeneratorRoot(ctx.local3d);
 		rootObject.name = "rootObject";
 
 		if( root == null ) {
@@ -157,11 +169,17 @@ class MeshGenerator extends Object3D {
 		createEmptyMeshPart(ctx, root);
 		#end
 
-		createMeshPart(ctx, root, ctx.local3d.getObjectByName("rootObject"));
+		var rootObject = ctx.local3d.getObjectByName("rootObject");
+		createMeshPart(ctx, root, rootObject);
 
 		#if editor
-		for(m in ctx.local3d.findAll(o -> Std.downcast(o, h3d.scene.Mesh)))
-			m.cullingCollider = new h3d.col.ObjectCollider(m, m.primitive.getBounds().toSphere());
+		// Only one CullingCollider for performance
+		var rootPos = rootObject.getAbsPos().getPosition();
+		var mainCullingCollider = new h3d.col.Sphere(rootPos.x, rootPos.y, rootPos.z, 0.0);
+		for( m in ctx.local3d.findAll(o -> Std.downcast(o, h3d.scene.Mesh)) ) {
+			m.cullingCollider = mainCullingCollider;
+			mainCullingCollider.r = hxd.Math.max(mainCullingCollider.r, m.primitive.getBounds().toSphere().r + m.getAbsPos().getPosition().sub(rootPos).length());
+		}
 		#end
 	}
 
@@ -242,10 +260,6 @@ class MeshGenerator extends Object3D {
 		return null;
 	}
 
-	function getMaxDepth( hmd : hxd.fmt.hmd.Library ) {
-
-	}
-
 	#if editor
 
 	function generate( ctx : EditContext, mp : MeshPart, maxDepth : Int, curDepth : Int) {
@@ -258,7 +272,6 @@ class MeshGenerator extends Object3D {
 			generate(ctx, cmp, maxDepth, curDepth);
 	}
 
-
 	function createEmptyMeshPart( ctx : Context, mp : MeshPart ) {
 		var sl = getSocketListFromHMD(getHMD(ctx, mp.meshPath));
 		if( mp.childParts.length < sl.length ) {

+ 8 - 3
hrt/prefab/l3d/Polygon.hx

@@ -89,6 +89,13 @@ class Polygon extends Object3D {
 		if(primitive != null)
 			return primitive;
 
+		primitive = createPrimitive(shape);
+		primitive.incref();
+		cache.set(shape, primitive);
+		return primitive;
+	}
+
+	public static function createPrimitive( shape : Shape ) {
 		var uvs : Array<Point> = null;
 		var points : Array<Point> = null;
 		var indices : Array<Int> = null;
@@ -141,13 +148,11 @@ class Polygon extends Object3D {
 		var idx = new hxd.IndexBuffer();
 		for(i in indices)
 			idx.push(i);
-		primitive = new h3d.prim.Polygon(verts, idx);
+		var primitive = new h3d.prim.Polygon(verts, idx);
 		primitive.normals = [for(p in points) new h3d.col.Point(0, 0, 1.)];
 		primitive.tangents = [for(p in points) new h3d.col.Point(0., 1., 0.)];
 		primitive.uvs = [for(uv in uvs) new h3d.prim.UV(uv.x, uv.y)];
 		primitive.colors = [for(p in points) new h3d.col.Point(1,1,1)];
-		primitive.incref();
-		cache.set(shape, primitive);
 		return primitive;
 	}
 

+ 0 - 3
hrt/prefab/terrain/Surface.hx

@@ -48,13 +48,10 @@ class SurfaceArray {
 		normal = new h3d.mat.TextureArray(res, res, count, [Target], RGBA);
 		pbr = new h3d.mat.TextureArray(res, res, count, [Target], RGBA);
 		albedo.wrap = Repeat;
-		albedo.realloc = null;
 		albedo.preventAutoDispose();
 		normal.wrap = Repeat;
-		normal.realloc = null;
 		normal.preventAutoDispose();
 		pbr.wrap = Repeat;
-		pbr.realloc = null;
 		pbr.preventAutoDispose();
 	}
 

+ 19 - 0
hrt/prefab/terrain/TerrainMesh.hx

@@ -75,6 +75,10 @@ class TerrainMesh extends h3d.scene.Object {
 		return o;
 	}
 
+	public function onContextlost() {
+		generateSurfaceArray();
+	}
+
 	public function getHeight( x : Float, y : Float ) : Float {
 		var z = 0.0;
 		var t = getTileAtWorldPos(x, y);
@@ -126,6 +130,21 @@ class TerrainMesh extends h3d.scene.Object {
 			if( surfaces[i].normal != null ) copyPass.apply(surfaces[i].normal, surfaceArray.normal, null, null, i);
 			if( surfaces[i].pbr != null ) copyPass.apply(surfaces[i].pbr, surfaceArray.pbr, null, null, i);
 		}
+
+		// OnContextLost support
+		surfaceArray.albedo.realloc = function() {
+			for( i in 0 ... surfaceArray.surfaceCount )
+				copyPass.apply(surfaces[i].albedo, surfaceArray.albedo, null, null, i);
+		}
+		surfaceArray.normal.realloc = function() {
+			for( i in 0 ... surfaceArray.surfaceCount )
+				copyPass.apply(surfaces[i].normal, surfaceArray.normal, null, null, i);
+		}
+		surfaceArray.pbr.realloc = function() {
+			for( i in 0 ... surfaceArray.surfaceCount )
+				copyPass.apply(surfaces[i].pbr, surfaceArray.pbr, null, null, i);
+		}
+
 		updateSurfaceParams();
 		refreshAllTex();
 	}

+ 43 - 24
hrt/prefab/terrain/Tile.hx

@@ -21,6 +21,7 @@ class Tile extends h3d.scene.Mesh {
 	// set by prefab loader for CPU access ingame
 	public var packedWeightMapPixel : hxd.Pixels;
 	public var indexMapPixels : hxd.Pixels;
+	public var normalBytes : haxe.io.Bytes;
 
 	var heightmapPixels : hxd.Pixels.PixelsFloat;
 	var shader : hrt.shader.Terrain;
@@ -54,25 +55,6 @@ class Tile extends h3d.scene.Mesh {
 			bigPrim.dispose();
 	}
 
-	public override function clone( ?o : h3d.scene.Object ) : h3d.scene.Object {
-		var o = new Tile(tileX, tileY, cast parent);
-		o.heightMap = heightMap.clone();
-		o.surfaceIndexMap = surfaceIndexMap.clone();
-
-		for( i in 0...surfaceWeights.length )
-			o.surfaceWeights.push(surfaceWeights[i].clone());
-
-		o.surfaceWeightArray = new h3d.mat.TextureArray(terrain.weightMapResolution, terrain.weightMapResolution, surfaceWeights.length, [Target], R8);
-		o.surfaceWeightArray.wrap = Clamp;
-		o.surfaceWeightArray.preventAutoDispose();
-		o.surfaceWeightArray.realloc = null;
-		for( i in 0 ... surfaceWeights.length )
-			if( surfaceWeights[i] != null ) terrain.copyPass.apply(surfaceWeights[i], o.surfaceWeightArray, None, null, i);
-
-		o.heightmapPixels = heightmapPixels.clone();
-		return o;
-	}
-
 	function set_heightMap( v ) {
 		shader.heightMap = v;
 		return heightMap = v;
@@ -87,6 +69,7 @@ class Tile extends h3d.scene.Mesh {
 
 	public function createBigPrim( normals : haxe.io.Bytes ) {
 		if( bigPrim != null ) bigPrim.dispose();
+		normalBytes = normals;
 		bigPrim = new h3d.prim.BigPrimitive(6);
 		var n = new h3d.Vector(0,0,0);
 		inline function addVertice(x : Float, y : Float, i : Int) {
@@ -169,7 +152,11 @@ class Tile extends h3d.scene.Mesh {
 			heightMap.wrap = Clamp;
 			heightMap.filter = Linear;
 			heightMap.preventAutoDispose();
-			heightMap.realloc = null;
+
+			heightMap.realloc = function() {
+				heightMap.uploadPixels(heightmapPixels);
+			}
+
 			if( oldHeightMap != null ) {
 				terrain.copyPass.apply(oldHeightMap, heightMap);
 				oldHeightMap.dispose();
@@ -185,14 +172,18 @@ class Tile extends h3d.scene.Mesh {
 			surfaceIndexMap.setName("terrainSurfaceIndexMap");
 			surfaceIndexMap.filter = Nearest;
 			surfaceIndexMap.preventAutoDispose();
-			surfaceIndexMap.realloc = null;
+
+			surfaceIndexMap.realloc = function() {
+				surfaceIndexMap.uploadPixels(indexMapPixels);
+			}
+
 			if( oldSurfaceIndexMap != null ) {
 				terrain.copyPass.apply(oldSurfaceIndexMap, surfaceIndexMap);
 				oldSurfaceIndexMap.dispose();
 			}
 		}
 	}
-	
+
 	function refreshSurfaceWeightArray() {
 		if( terrain.surfaceArray.surfaceCount > 0 && (surfaceWeights.length != terrain.surfaceArray.surfaceCount || surfaceWeights[0].width != terrain.weightMapResolution) ) {
 				var oldArray = surfaceWeights;
@@ -203,7 +194,6 @@ class Tile extends h3d.scene.Mesh {
 					surfaceWeights[i].setName("terrainSurfaceWeight"+i);
 					surfaceWeights[i].wrap = Clamp;
 					surfaceWeights[i].preventAutoDispose();
-					surfaceWeights[i].realloc = null;
 					if( i < oldArray.length && oldArray[i] != null )
 						terrain.copyPass.apply(oldArray[i], surfaceWeights[i]);
 				}
@@ -234,7 +224,22 @@ class Tile extends h3d.scene.Mesh {
 			surfaceWeightArray.setName("terrainSurfaceWeightArray");
 			surfaceWeightArray.wrap = Clamp;
 			surfaceWeightArray.preventAutoDispose();
-			surfaceWeightArray.realloc = null;
+
+			// OnContextLost support : Restore the textureArray with the pixels from the packedWeight texture
+			surfaceWeightArray.realloc = function() {
+				var engine = h3d.Engine.getCurrent();
+				var unpackWeight = new h3d.pass.ScreenFx(new UnpackWeight());
+				var tmpPackedWeightTexture = new h3d.mat.Texture(terrain.weightMapResolution, terrain.weightMapResolution, [Target]);
+				tmpPackedWeightTexture.uploadPixels(packedWeightMapPixel);
+				for( i in 0 ... surfaceWeightArray.layerCount ) {
+					engine.pushTarget(surfaceWeightArray, i);
+					unpackWeight.shader.indexMap = surfaceIndexMap;
+					unpackWeight.shader.packedWeightTexture = tmpPackedWeightTexture;
+					unpackWeight.shader.index = i;
+					unpackWeight.render();
+					engine.popTarget();
+				}
+			}
 		}
 		for( i in 0 ... surfaceWeights.length )
 			if( surfaceWeights[i] != null ) terrain.copyPass.apply(surfaceWeights[i], surfaceWeightArray, None, null, i);
@@ -639,6 +644,20 @@ class Tile extends h3d.scene.Mesh {
 		shader.heightBlendStrength = terrain.heightBlendStrength;
 		shader.blendSharpness = terrain.blendSharpness;
 
+		// OnContextLost support : re-create the bigPrim
+		var needRealloc = false;
+		if( bigPrim != null ) {
+			for( b in @:privateAccess bigPrim.buffers ) {
+				if( b.isDisposed() ) {
+					needRealloc = true;
+					break;
+				}
+			}
+			if( needRealloc ) {
+				createBigPrim(normalBytes);
+				cachedBounds = null;
+			}
+		}
 	}
 
 	function isReadyForDraw() {