ncannasse 7 роки тому
батько
коміт
7889eac6c4
3 змінених файлів з 71 додано та 4 видалено
  1. 54 0
      h3d/pass/DirShadowMap.hx
  2. 8 0
      h3d/pass/Shadows.hx
  3. 9 4
      hxd/prefab/ContextShared.hx

+ 54 - 0
h3d/pass/DirShadowMap.hx

@@ -117,6 +117,60 @@ class DirShadowMap extends Shadows {
 		dshader.shadowProj = getShadowProj();
 	}
 
+	override function saveStaticData() {
+		if( mode != Mixed && mode != Static )
+			return null;
+		if( staticTexture == null )
+			throw "Data not computed";
+		var bytes = haxe.zip.Compress.run(staticTexture.capturePixels().bytes,9);
+		var buffer = new haxe.io.BytesBuffer();
+		buffer.addInt32(staticTexture.width);
+		buffer.addFloat(lightCamera.pos.x);
+		buffer.addFloat(lightCamera.pos.y);
+		buffer.addFloat(lightCamera.pos.z);
+		buffer.addFloat(lightCamera.target.x);
+		buffer.addFloat(lightCamera.target.y);
+		buffer.addFloat(lightCamera.target.z);
+		buffer.addFloat(lightCamera.orthoBounds.xMin);
+		buffer.addFloat(lightCamera.orthoBounds.yMin);
+		buffer.addFloat(lightCamera.orthoBounds.zMin);
+		buffer.addFloat(lightCamera.orthoBounds.xMax);
+		buffer.addFloat(lightCamera.orthoBounds.yMax);
+		buffer.addFloat(lightCamera.orthoBounds.zMax);
+		buffer.addInt32(bytes.length);
+		buffer.add(bytes);
+		return buffer.getBytes();
+	}
+
+	override function loadStaticData( bytes : haxe.io.Bytes ) {
+		if( (mode != Mixed && mode != Static) || bytes == null )
+			return false;
+		var buffer = new haxe.io.BytesInput(bytes);
+		var size = buffer.readInt32();
+		if( size != this.size )
+			return false;
+		lightCamera.pos.x = buffer.readFloat();
+		lightCamera.pos.y = buffer.readFloat();
+		lightCamera.pos.z = buffer.readFloat();
+		lightCamera.target.x = buffer.readFloat();
+		lightCamera.target.y = buffer.readFloat();
+		lightCamera.target.z = buffer.readFloat();
+		lightCamera.orthoBounds.xMin = buffer.readFloat();
+		lightCamera.orthoBounds.yMin = buffer.readFloat();
+		lightCamera.orthoBounds.zMin = buffer.readFloat();
+		lightCamera.orthoBounds.xMax = buffer.readFloat();
+		lightCamera.orthoBounds.yMax = buffer.readFloat();
+		lightCamera.orthoBounds.zMax = buffer.readFloat();
+		lightCamera.update();
+		var len = buffer.readInt32();
+		var pixels = new hxd.Pixels(size, size, haxe.zip.Uncompress.run(buffer.read(len)), format);
+		if( staticTexture != null ) staticTexture.dispose();
+		staticTexture = new h3d.mat.Texture(size, size, [Target], format);
+		staticTexture.uploadPixels(pixels);
+		syncShader(staticTexture);
+		return true;
+	}
+
 	override function draw( passes ) {
 
 		if( !ctx.computingStatic )

+ 8 - 0
h3d/pass/Shadows.hx

@@ -56,6 +56,14 @@ class Shadows extends Default {
 		return [Swiz(Value("output.depth",1),[X,X,X,X])];
 	}
 
+	public function loadStaticData( bytes : haxe.io.Bytes ) {
+		return false;
+	}
+
+	public function saveStaticData() : haxe.io.Bytes {
+		return null;
+	}
+
 	public function computeStatic( passes : h3d.pass.Object ) {
 		throw "Not implemented";
 	}

+ 9 - 4
hxd/prefab/ContextShared.hx

@@ -75,13 +75,18 @@ class ContextShared {
 
 	public function saveBakedBytes( file : String, bytes : haxe.io.Bytes ) {
 		if( bakedData == null ) loadBakedData();
-		if( bytes == null )
-			bakedData.remove(file);
-		else
+		if( bytes == null ) {
+			if( !bakedData.remove(file) )
+				return;
+		} else
 			bakedData.set(file, bytes);
+		var keys = Lambda.array({ iterator : bakedData.keys });
+		if( keys.length == 0 ) {
+			saveBakedFile(null);
+			return;
+		}
 		var bytes = new haxe.io.BytesOutput();
 		bytes.writeString("BAKE");
-		var keys = Lambda.array({ iterator : bakedData.keys });
 		bytes.writeInt32(keys.length);
 		var headerSize = 8;
 		for( name in keys )