2
0
Эх сурвалжийг харах

Merge branch 'master' of github.com:HeapsIO/heaps

ncannasse 6 жил өмнө
parent
commit
70fffb4319

+ 1 - 1
h3d/anim/Animation.hx

@@ -35,7 +35,7 @@ class Animation implements hxd.impl.Serializable {
 	@:s public var pause : Bool;
 	@:s public var loop : Bool;
 
-	var events : Array<Array<String>>;
+	public var events(default, null) : Array<Array<String>>;
 
 	var isInstance : Bool;
 	var objects : Array<AnimatedObject>;

+ 27 - 18
h3d/pass/PointShadowMap.hx

@@ -115,10 +115,15 @@ class PointShadowMap extends Shadows {
 			switch( mode ) {
 			case None:
 				return passes;
-			case Dynamic:	// nothing
-			case Mixed:		// nothing
+			case Dynamic:
+				// nothing
+			case Mixed:
+				if( staticTexture == null || staticTexture.isDisposed() )
+					staticTexture = createDefaultShadowMap();
 			case Static:
-				if( staticTexture == null || staticTexture.isDisposed() ) staticTexture = createDefaultShadowMap();
+				if( staticTexture == null || staticTexture.isDisposed() )
+					staticTexture = createDefaultShadowMap();
+				updateCamera();
 				syncShader(staticTexture);
 				return passes;
 			}
@@ -133,27 +138,30 @@ class PointShadowMap extends Shadows {
 		}
 		texture.depthBuffer = depth;
 
+		var validBakedTexture = (staticTexture != null && staticTexture.width == texture.width);
 		var merge : h3d.mat.Texture = null;
-		if( mode == Mixed && !ctx.computingStatic )
+		if( mode == Mixed && !ctx.computingStatic && validBakedTexture)
 			merge = ctx.textures.allocTarget("pointShadowMap", size, size, false, format, [Target, Cube]);
 
 		for(i in 0 ... 6){
-
 			var pointLight = cast(light, h3d.scene.pbr.PointLight);
 
-			if( mode != Mixed || ctx.computingStatic ) {
-				var absPos = light.getAbsPos();
-				lightCamera.setCubeMap(i, new h3d.Vector(absPos.tx, absPos.ty, absPos.tz));
-				lightCamera.zFar = pointLight.range;
-				lightCamera.update();
-			}
+			var absPos = light.getAbsPos();
+			lightCamera.setCubeMap(i, new h3d.Vector(absPos.tx, absPos.ty, absPos.tz));
+			lightCamera.zFar = pointLight.range;
+			lightCamera.update();
 
 			ctx.engine.pushTarget(texture, i);
 			ctx.engine.clear(0xFFFFFF, 1);
 			passes = super.draw(passes);
 			ctx.engine.popTarget();
+		}
 
-			if( mode == Mixed && !ctx.computingStatic ) {
+		if( blur.radius > 0 )
+			blur.apply(ctx, texture);
+
+		if( mode == Mixed && !ctx.computingStatic && merge != null ) {
+			for(i in 0 ... 6){
 				mergePass.shader.texA = texture;
 				mergePass.shader.texB = staticTexture;
 				mergePass.shader.mat = cubeDir[i];
@@ -161,18 +169,19 @@ class PointShadowMap extends Shadows {
 				mergePass.render();
 				ctx.engine.popTarget();
 			}
-		}
-
-		if( blur.radius > 0 && (mode != Mixed || !ctx.computingStatic) )
-				blur.apply(ctx, texture);
-
-		if( mode == Mixed && !ctx.computingStatic )
 			texture = merge;
+		}
 
 		syncShader(texture);
 		return passes;
 	}
 
+	function updateCamera(){
+		var absPos = light.getAbsPos();
+		lightCamera.pos.set(absPos.tx, absPos.ty, absPos.tz);
+		lightCamera.update();
+	}
+
 	override function computeStatic( passes : h3d.pass.Object ) {
 		if( mode != Static && mode != Mixed )
 			return;

+ 13 - 14
h3d/pass/SpotShadowMap.hx

@@ -96,17 +96,19 @@ class SpotShadowMap extends Shadows {
 				return passes;
 			case Dynamic:
 				// nothing
-			case Static, Mixed:
+			case Mixed:
 				if( staticTexture == null || staticTexture.isDisposed() )
 					staticTexture = h3d.mat.Texture.fromColor(0xFFFFFF);
-				if( mode == Static ) {
-					updateCamera();
-					syncShader(staticTexture);
-					return passes;
-				}
+			case Static:
+				if( staticTexture == null || staticTexture.isDisposed() )
+					staticTexture = h3d.mat.Texture.fromColor(0xFFFFFF);
+				updateCamera();
+				syncShader(staticTexture);
+				return passes;
 			}
 
 		passes = filterPasses(passes);
+		updateCamera();
 
 		var texture = ctx.textures.allocTarget("shadowMap", size, size, false, format);
 		if( customDepth && (depth == null || depth.width != size || depth.height != size || depth.isDisposed()) ) {
@@ -115,17 +117,17 @@ class SpotShadowMap extends Shadows {
 		}
 		texture.depthBuffer = depth;
 
-		if( mode != Mixed || ctx.computingStatic ) {
-			updateCamera();
-		}
-
 		ctx.engine.pushTarget(texture);
 		ctx.engine.clear(0xFFFFFF, 1);
 		passes = super.draw(passes);
 		if( border != null ) border.render();
 		ctx.engine.popTarget();
 
-		if( mode == Mixed && !ctx.computingStatic ) {
+		if( blur.radius > 0 )
+			blur.apply(ctx, texture);
+
+		var validBakedTexture = (staticTexture != null && staticTexture.width == texture.width);
+		if( mode == Mixed && !ctx.computingStatic && validBakedTexture ) {
 			var merge = ctx.textures.allocTarget("shadowMap", size, size, false, format);
 			mergePass.shader.texA = texture;
 			mergePass.shader.texB = staticTexture;
@@ -135,9 +137,6 @@ class SpotShadowMap extends Shadows {
 			texture = merge;
 		}
 
-		if( blur.radius > 0 && (mode != Mixed || !ctx.computingStatic) )
-			blur.apply(ctx, texture);
-
 		syncShader(texture);
 		return passes;
 	}

+ 10 - 0
h3d/prim/ModelCache.hx

@@ -26,6 +26,16 @@ class ModelCache {
 		if( lib == null ) {
 			lib = res.toHmd();
 			models.set(path, lib);
+
+			function loadHideData( path : String ) : hxd.fmt.hmd.Library.HideData {
+				var parts = path.split("/");
+				parts.pop();
+				var propsPath = parts.join("/") + "/model.props";
+				if(!hxd.res.Loader.currentInstance.exists(propsPath)) return null;
+				var props = hxd.res.Loader.currentInstance.load(propsPath).toText();
+				return haxe.Json.parse(props);
+			}
+			lib.hideData = loadHideData(path);
 		}
 		return lib;
 	}

+ 3 - 25
h3d/scene/pbr/terrain/Surface.hx

@@ -1,9 +1,9 @@
 package h3d.scene.pbr.terrain;
 
 class Surface {
-	public var albedo (default, set) : h3d.mat.Texture;
-	public var normal (default, set) : h3d.mat.Texture;
-	public var pbr (default, set) : h3d.mat.Texture;
+	public var albedo : h3d.mat.Texture;
+	public var normal : h3d.mat.Texture;
+	public var pbr : h3d.mat.Texture;
 	public var tilling = 1.0;
 	public var offset : h3d.Vector;
 	public var angle = 0.0;
@@ -20,28 +20,6 @@ class Surface {
 		if(normal != null) normal.dispose();
 		if(pbr != null) pbr.dispose();
 	}
-
-	function set_albedo(t : h3d.mat.Texture){
-		return albedo = swap(albedo, t);
-	}
-
-	function set_normal(t : h3d.mat.Texture){
-		return normal = swap(normal, t);
-	}
-
-	function set_pbr(t : h3d.mat.Texture){
-		return pbr = swap(pbr, t);
-	}
-
-	function swap(a : h3d.mat.Texture, b : h3d.mat.Texture) : h3d.mat.Texture {
-		if(a != null) a.dispose();
-		if(b != null){
-			var r = b.clone();
-			r.wrap = Repeat;
-			return r;
-		}
-		return null;
-	}
 }
 
 class SurfaceArray {

+ 16 - 9
h3d/scene/pbr/terrain/Tile.hx

@@ -18,7 +18,7 @@ class Tile extends h3d.scene.Mesh {
 	var heightmapPixels : hxd.Pixels.PixelsFloat;
 	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);
 		this.tileX = x;
 		this.tileY = y;
@@ -27,7 +27,6 @@ class Tile extends h3d.scene.Mesh {
 		material.mainPass.culling = None;
 		this.x = x * getTerrain().tileSize;
 		this.y = y * getTerrain().tileSize;
-		refreshMesh();
 		name = "tile_" + x + "_" + y;
 	}
 
@@ -435,9 +434,9 @@ class Tile extends h3d.scene.Mesh {
 				t2.z += getHeight(t2.x / getTerrain().tileSize, t2.y / getTerrain().tileSize);
 			}
 			var n1 = t1.sub(t0);
-			n1.normalize();
+			n1.normalizeFast();
 			var n2 = t2.sub(t0);
-			n2.normalize();
+			n2.normalizeFast();
 			var n = n1.cross(n2);
 			grid.normals[i0].x += n.x; grid.normals[i0].y += n.y; grid.normals[i0].z += n.z;
 			grid.normals[i1].x += n.x; grid.normals[i1].y += n.y; grid.normals[i1].z += n.z;
@@ -449,9 +448,9 @@ class Tile extends h3d.scene.Mesh {
 		needAlloc = true;
 	}
 
-	public function getHeight(u : Float, v : Float){
+	public function getHeight(u : Float, v : Float, ?fast = false){
 		var pixels = getHeightPixels();
-		if(heightMap.filter == Linear){
+		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;
 			}
@@ -489,9 +488,17 @@ class Tile extends h3d.scene.Mesh {
 		if(!isReady()) return;
 		if(cachedBounds == null) {
 			cachedBounds = getBounds();
-			cachedBounds.zMax = 10000;  // TODO: Use real low/high Z values
-			cachedBounds.zMin = -10000;
-		}		
+			cachedBounds.zMax = 0;
+			cachedBounds.zMin = 0;
+
+			for( u in 0 ... heightMap.width ){
+				for( v in 0 ... heightMap.height ){
+					var h = getHeight(u, v, true);
+					cachedBounds.zMin = cachedBounds.zMin > h ? h : cachedBounds.zMin;
+					cachedBounds.zMax = cachedBounds.zMax < h ? h : cachedBounds.zMax;
+				}
+			}
+		}
 		if(ctx.camera.frustum.hasBounds(cachedBounds))
 			super.emit(ctx);
 	}

+ 12 - 0
hxd/fmt/hmd/Library.hx

@@ -1,6 +1,10 @@
 package hxd.fmt.hmd;
 import hxd.fmt.hmd.Data;
 
+typedef HideData = {
+	var animations : haxe.DynamicAccess<{ events : Array<{ frame : Int, data : String }> }>;
+}
+
 private class FormatMap {
 	public var size : Int;
 	public var offset : Int;
@@ -29,6 +33,7 @@ class Library {
 	var cachedAnimations : Map<String, h3d.anim.Animation>;
 	var cachedSkin : Map<String, h3d.anim.Skin>;
 	var tmp = haxe.io.Bytes.alloc(4);
+	public var hideData : HideData;
 
 	public function new(res,  header) {
 		this.resource = res;
@@ -398,6 +403,13 @@ class Library {
 		l.loop = a.loop;
 		if( a.events != null ) l.setEvents(a.events);
 
+		if( hideData != null ) {
+			var name = resource.entry.name.split(".")[0];
+			if( StringTools.startsWith(name,"Anim_") ) name = name.substr(5);
+			if(hideData.animations.exists(name))
+				l.setEvents(hideData.animations.get(name).events);
+		}
+
 		var entry = resource.entry;
 		entry.open();
 		entry.skip(header.dataPosition + a.dataPosition);