Sfoglia il codice sorgente

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

trethaller 5 anni fa
parent
commit
79684b5f58
6 ha cambiato i file con 328 aggiunte e 26 eliminazioni
  1. 24 5
      hide/Config.hx
  2. 14 13
      hide/Ide.hx
  3. 1 3
      hide/Renderer.hx
  4. 7 3
      hide/comp/cdb/ScriptTable.hx
  5. 2 2
      hide/view/Model.hx
  6. 280 0
      hrt/prefab/l3d/Environment.hx

+ 24 - 5
hide/Config.hx

@@ -5,20 +5,23 @@ typedef LayoutState = {
 	var fullScreen : { name : String, state : Any };
 }
 
-typedef HideConfig = {
+typedef HideGlobalConfig = {
 	var autoSaveLayout : Null<Bool>;
-	var layouts : Array<{ name : String, state : LayoutState }>;
 
 	var currentProject : String;
 	var recentProjects : Array<String>;
 
 	var windowPos : { x : Int, y : Int, w : Int, h : Int, max : Bool };
+}
+
+typedef HideProjectConfig = {
+	var layouts : Array<{ name : String, state : LayoutState }>;
 	var renderer : String;
 };
 
 typedef ConfigDef = {
 
-	var hide : HideConfig;
+	var hide : {};
 
 };
 
@@ -36,6 +39,11 @@ class Config {
 		sync();
 	}
 
+	public function isLocal() {
+		if( path == null && parent != null ) return parent.isLocal();
+		return path == null || StringTools.startsWith(path, ide.projectDir);
+	}
+
 	public function load( path : String ) {
 		this.path = path;
 		var fullPath = ide.getPath(path);
@@ -90,6 +98,14 @@ class Config {
 		return defaultVal;
 	}
 
+	public function getLocal( key : String, ?defaultVal : Dynamic ) : Dynamic {
+		var v = get(key);
+		if( v == null ) return defaultVal;
+		if( isLocal() ) return v;
+		if( parent == null ) return defaultVal;
+		return parent.getLocal(key,defaultVal);
+	}
+
 	public function set( key : String, val : Dynamic ) {
 		if( val == null )
 			Reflect.deleteField(source, key);
@@ -110,7 +126,7 @@ class Config {
 		if( userGlobals.source.hide == null )
 			userGlobals.source.hide = {
 				autoSaveLayout : true,
-				layouts : [],
+				layouts : null,
 				recentProjects : [],
 				currentProject : projectPath,
 				windowPos : null,
@@ -121,7 +137,10 @@ class Config {
 		perProject.load(resourcePath + "/props.json");
 
 		var projectUserCustom = new Config(perProject);
-		projectUserCustom.load(nw.App.dataPath + "/" + projectPath.split("/").join("_").split(":").join("_") + ".json");
+		projectUserCustom.load(nw.App.dataPath + "/" + projectPath.split("\\").join("/").split("/").join("_").split(":").join("_") + ".json");
+		var p = projectUserCustom;
+		if( p.source.hide == null )
+			p.source.hide = ({ layouts : [], renderer : null } : HideProjectConfig);
 
 		var current = new Config(projectUserCustom);
 

+ 14 - 13
hide/Ide.hx

@@ -31,7 +31,8 @@ class Ide {
 		user : Config,
 		current : Config,
 	};
-	var ideConfig(get, never) : hide.Config.HideConfig;
+	var ideConfig(get, never) : hide.Config.HideGlobalConfig;
+	var projectConfig(get, never) : hide.Config.HideProjectConfig;
 
 	var window : nw.Window;
 	var saveMenu : nw.Menu;
@@ -85,7 +86,7 @@ class Ide {
 		}
 
 		if( subView == null ) {
-			var wp = config.global.current.hide.windowPos;
+			var wp = ideConfig.windowPos;
 			if( wp != null ) {
 				if( wp.w > 400 && wp.h > 300 )
 					window.resizeBy(wp.w - Std.int(window.window.outerWidth), wp.h - Std.int(window.window.outerHeight));
@@ -265,7 +266,7 @@ class Ide {
 		defaultLayout = null;
 		var layoutName = isCDB ? "CDB" : "Default";
 		var emptyLayout : Config.LayoutState = { content : [], fullScreen : null };
-		for( p in config.current.current.hide.layouts )
+		for( p in projectConfig.layouts )
 			if( p.name == layoutName ) {
 				if( p.state.content == null ) continue; // old version
 				defaultLayout = p;
@@ -273,9 +274,9 @@ class Ide {
 			}
 		if( defaultLayout == null ) {
 			defaultLayout = { name : layoutName, state : emptyLayout };
-			ideConfig.layouts.push(defaultLayout);
+			projectConfig.layouts.push(defaultLayout);
 			config.current.sync();
-			config.global.save();
+			config.user.save();
 		}
 		if( state == null )
 			state = defaultLayout;
@@ -389,7 +390,7 @@ class Ide {
 		if( initializing || !ideConfig.autoSaveLayout || isCDB )
 			return;
 		defaultLayout.state = saveLayout();
-		if( subView == null ) this.config.global.save();
+		if( subView == null ) this.config.user.save();
 	}
 
 	function saveLayout() : Config.LayoutState {
@@ -399,7 +400,8 @@ class Ide {
 		};
 	}
 
-	function get_ideConfig() return config.global.source.hide;
+	function get_ideConfig() return cast config.global.source.hide;
+	function get_projectConfig() return cast config.user.source.hide;
 	function get_currentConfig() return config.user;
 
 	function get_appPath() {
@@ -542,7 +544,7 @@ class Ide {
 
 			var render = renderers[0];
 			for( r in renderers )
-				if( r.name == config.current.current.hide.renderer ) {
+				if( r.name == projectConfig.renderer ) {
 					render = r;
 					break;
 				}
@@ -830,8 +832,7 @@ class Ide {
 		for( r in renderers ) {
 			new Element("<menu type='checkbox'>").attr("label", r.name).prop("checked",r == h3d.mat.MaterialSetup.current).appendTo(menu.find(".project .renderers")).click(function(_) {
 				if( r != h3d.mat.MaterialSetup.current ) {
-					if( config.user.source.hide == null ) config.user.source.hide = cast {};
-					config.user.source.hide.renderer = r.name;
+					projectConfig.renderer = r.name;
 					config.user.save();
 					setProject(ideConfig.currentProject);
 				}
@@ -880,7 +881,7 @@ class Ide {
 		// layout
 		var layouts = menu.find(".layout .content");
 		layouts.html("");
-		for( l in config.current.current.hide.layouts ) {
+		for( l in projectConfig.layouts ) {
 			if( l.name == "Default" ) continue;
 			new Element("<menu>").attr("label",l.name).addClass(l.name).appendTo(layouts).click(function(_) {
 				initLayout(l);
@@ -894,8 +895,8 @@ class Ide {
 		menu.find(".layout .saveas").click(function(_) {
 			var name = ask("Please enter a layout name:");
 			if( name == null || name == "" ) return;
-			ideConfig.layouts.push({ name : name, state : saveLayout() });
-			config.global.save();
+			projectConfig.layouts.push({ name : name, state : saveLayout() });
+			config.user.save();
 			initMenu();
 		});
 		menu.find(".layout .save").click(function(_) {

+ 1 - 3
hide/Renderer.hx

@@ -100,9 +100,7 @@ class PbrSetup extends h3d.mat.PbrMaterialSetup {
 	}
 
     override function createRenderer() {
-		var env = new h3d.scene.pbr.Environment(getEnvMap());
-		env.compute();
-		return new PbrRenderer(env);
+		return new PbrRenderer(null);
 	}
 
 	override function getDefaults( ?type : String ) : Any {

+ 7 - 3
hide/comp/cdb/ScriptTable.hx

@@ -20,11 +20,15 @@ class ScriptTable extends SubTable {
 	}
 
 	override public function close() {
-		if( script != null )
-			cell.setValue(script.code);
+		if( script != null ) saveValue();
 		super.close();
 	}
 
+	function saveValue() {
+		var code = [for( line in script.code.split("\r\n").join("\n").split("\n") ) StringTools.rtrim(line)].join("\n");
+		cell.setValue(code);
+	}
+
 	override function refresh() {
 		var first = script == null;
 		element.html("<div class='cdb-script'></div>");
@@ -32,7 +36,7 @@ class ScriptTable extends SubTable {
 		div.on("keypress keydown keyup", (e) -> e.stopPropagation());
 		var checker = new ScriptEditor.ScriptChecker(editor.config,"cdb."+cell.getDocumentName(),[ "cdb."+cell.table.sheet.name => cell.line.obj ]);
 		script = new ScriptEditor(cell.value, checker, div);
-		script.onSave = function() cell.setValue(script.code);
+		script.onSave = saveValue;
 		script.onClose = function() { close(); cell.focus(); }
 		lines = [new Line(this,[],0,script.element)];
 		if( first ) script.focus();

+ 2 - 2
hide/view/Model.hx

@@ -117,7 +117,7 @@ class Model extends FileView {
 		eventList = element.find(".event-editor");
 
 		if( rootPath == null )
-			rootPath = config.get("scene.renderProps");
+			rootPath = config.getLocal("scene.renderProps");
 
 		if( rootPath != null )
 			root = ide.loadPrefab(rootPath, hrt.prefab.Library);
@@ -651,7 +651,7 @@ class Model extends FileView {
 									var newFrame = Math.round(( (curPos + (e.relX - 2.5) * dragIcon.scaleX ) / W ) * obj.currentAnimation.frameCount);
 									if( newFrame >= 0 && newFrame <= obj.currentAnimation.frameCount ) {
 										events[curFrame].remove(event);
-										if(events[newFrame] == null) 
+										if(events[newFrame] == null)
 											events[newFrame] = [];
 										events[newFrame].insert(0, event);
 										curFrame = newFrame;

+ 280 - 0
hrt/prefab/l3d/Environment.hx

@@ -0,0 +1,280 @@
+package hrt.prefab.l3d;
+
+
+@:access(h3d.scene.pbr.Environment)
+class Environment extends Object3D {
+
+	var sourceMapPath : String;
+	var env : h3d.scene.pbr.Environment;
+
+	public var power : Float = 1.0;
+	public var threshold : Float = 1.0;
+	public var scale : Float = 1.0;
+	public var rotation : Float = 0.0;
+	public var sampleBits : Int = 12;
+	public var diffSize : Int = 64;
+	public var specSize : Int = 512;
+	public var ignoredSpecLevels : Int = 1;
+
+	public function new( ?parent ) {
+		super(parent);
+		type = "environment";
+	}
+
+	override function load( obj : Dynamic ) {
+		super.load(obj);
+	 	power = obj.power != null ? obj.power : 1.0;
+		threshold = obj.threshold != null ? obj.threshold : 1.0;
+		scale = obj.scale != null ? obj.scale : 1.0;
+		rotation = obj.rotation != null ? obj.rotation : 0.0;
+		sampleBits = obj.sampleBits != null ? obj.sampleBits : 12;
+		diffSize = obj.diffSize != null ? obj.diffSize : 64;
+		specSize = obj.specSize != null ? obj.specSize : 512;
+		ignoredSpecLevels = obj.ignoredSpecLevels != null ? obj.ignoredSpecLevels : 1;
+		sourceMapPath = obj.sourceMapPath != null ? obj.sourceMapPath : null;
+	}
+
+	override function save() {
+		var obj : Dynamic = super.save();
+		obj.power = power;
+		obj.threshold = threshold;
+		obj.scale = scale;
+		obj.rotation = rotation;
+		obj.sampleBits = sampleBits;
+		obj.diffSize = diffSize;
+		obj.specSize = specSize;
+		obj.ignoredSpecLevels = ignoredSpecLevels;
+		obj.sourceMapPath = sourceMapPath;
+		return obj;
+	}
+
+	function saveAsBinary( ctx : Context ) {
+		if( env == null || env.diffuse == null || env.specular == null || env.lut == null )
+			return;
+		var bytes = convertToBinary();
+		ctx.shared.savePrefabDat("environment", "bake", name, bytes);
+	}
+
+	function loadFromBinary( ctx : Context ) : Bool {
+		if( env == null || env.diffuse == null || env.specular == null || env.lut == null )
+			return false;
+		var res = ctx.shared.loadPrefabDat("environment", "bake", name);
+		return res != null ? convertFromBinary(res.entry.getBytes()) : false;
+	}
+
+	function convertToBinary() : haxe.io.Bytes {
+		var lutPixels = env.lut.capturePixels();
+		var diffusePixels : Array<hxd.Pixels.PixelsFloat> = [ for( i in 0 ... 6) env.diffuse.capturePixels(i) ];
+
+		var mipLevels = env.getMipLevels();
+		var specLevels = mipLevels - ignoredSpecLevels;
+		var specularPixels : Array<hxd.Pixels.PixelsFloat> =
+		 [
+			for( i in 0 ... 6 ) {
+				for( m in 0 ... specLevels ) {
+					env.specular.capturePixels(i, m);
+				}
+			}
+		];
+
+		var totalBytes = 0;
+		totalBytes += 4 + 4 + 4 + 8 + 8; // diffSize + specSize + sampleBits + threshold + scale
+		totalBytes += lutPixels.bytes.length;
+		for( p in diffusePixels )
+			totalBytes += p.bytes.length;
+		for( p in specularPixels )
+			totalBytes += p.bytes.length;
+		var bytes = haxe.io.Bytes.alloc(totalBytes);
+		var curPos = 0;
+		bytes.setInt32(curPos, sampleBits);
+		curPos += 4;
+		bytes.setInt32(curPos, diffSize);
+		curPos += 4;
+		bytes.setInt32(curPos, specSize);
+		curPos += 4;
+		bytes.setDouble(curPos, threshold);
+		curPos += 8;
+		bytes.setDouble(curPos, scale);
+		curPos += 8;
+		bytes.blit(curPos, lutPixels.bytes, 0, lutPixels.bytes.length);
+		curPos += lutPixels.bytes.length;
+		for( p in diffusePixels ) {
+			bytes.blit(curPos, p.bytes, 0, p.bytes.length);
+			curPos += p.bytes.length;
+		}
+		for( p in specularPixels ) {
+			bytes.blit(curPos, p.bytes, 0, p.bytes.length);
+			curPos += p.bytes.length;
+		}
+		return bytes;
+	}
+
+	function convertFromBinary( bytes : haxe.io.Bytes ) {
+		var curPos = 0;
+
+		var headerSize = 4 + 4 + 4 + 8 + 8;
+		if( headerSize > bytes.length )
+			return false;
+
+		var bakedSampleBits = bytes.getInt32(curPos);
+		curPos += 4;
+		var bakedDiffSize = bytes.getInt32(curPos);
+		curPos += 4;
+		var bakedSpecSize = bytes.getInt32(curPos);
+		curPos += 4;
+		var bakedThreshold = bytes.getDouble(curPos);
+		curPos += 8;
+		var bakedScale = bytes.getDouble(curPos);
+		curPos += 8;
+
+		if( bakedDiffSize != diffSize || bakedSpecSize != specSize || bakedSampleBits != sampleBits || bakedThreshold != threshold || bakedScale != scale )
+			return false;
+
+		var lutBytes = bytes.sub(curPos, hxd.Pixels.calcStride(env.lut.width, env.lut.format) * env.lut.height);
+		curPos += lutBytes.length;
+		if( curPos > bytes.length ) return false;
+		var lutPixels : hxd.Pixels.PixelsFloat = new hxd.Pixels(env.lut.width, env.lut.height, lutBytes, env.lut.format);
+		env.lut.uploadPixels(lutPixels);
+
+		var diffSize = hxd.Pixels.calcStride(env.diffuse.width, env.diffuse.format) * env.diffuse.height;
+		for( i in 0 ... 6 ) {
+			var diffByte = bytes.sub(curPos, diffSize);
+			curPos += diffByte.length;
+			if( curPos > bytes.length ) return false;
+			var diffPixels : hxd.Pixels.PixelsFloat = new hxd.Pixels(env.diffuse.width, env.diffuse.height, diffByte, env.diffuse.format);
+			env.diffuse.uploadPixels(diffPixels, 0, i);
+		}
+
+		var mipLevels = env.getMipLevels();
+		var specLevels = mipLevels - ignoredSpecLevels;
+		for( i in 0 ... 6 ) {
+			for( m in 0 ... specLevels ) {
+				var mipMapSize = hxd.Pixels.calcStride(env.specular.width >> m, env.specular.format) * env.specular.height >> m;
+				var specByte = bytes.sub(curPos, mipMapSize);
+				curPos += specByte.length;
+				if( curPos > bytes.length ) return false;
+				var specPixels : hxd.Pixels.PixelsFloat = new hxd.Pixels(env.specular.width >> m, env.specular.height >> m, specByte, env.specular.format);
+				env.specular.uploadPixels(specPixels, m, i);
+			}
+		}
+
+		return true;
+	}
+
+	function compute( ctx: Context ) {
+		env.compute();
+		#if editor
+		saveAsBinary(ctx);
+		#end
+	}
+
+	override function makeInstance( ctx : Context ) : Context {
+		ctx = ctx.clone(this);
+		var obj = new h3d.scene.Object(ctx.local3d);
+		ctx.local3d = obj;
+		ctx.local3d.name = name;
+		updateInstance(ctx);
+		return ctx;
+	}
+
+	override function updateInstance( ctx : Context, ?propName : String ) {
+		super.updateInstance(ctx, propName);
+
+		var sourceMap = sourceMapPath != null ? ctx.loadTexture(sourceMapPath) : null;
+		if( sourceMap == null )
+			return;
+
+		var needCompute = false;
+
+		if( env == null ) {
+			env = new h3d.scene.pbr.Environment(sourceMap);
+			needCompute = true;
+		}
+
+		if( sourceMap != env.source ) {
+			env.source = sourceMap;
+			needCompute = true;
+		}
+
+		if( env.specSize != specSize ) {
+			if( env.specular != null ) env.specular.dispose();
+			env.specular = null;
+			needCompute = true;
+		}
+		env.specSize = specSize;
+
+		if( env.diffSize != diffSize ) {
+			if( env.diffuse != null ) env.diffuse.dispose();
+			env.diffuse = null;
+			needCompute = true;
+		}
+		env.diffSize = diffSize;
+
+		if( propName == null || propName == "sampleBits" || propName == "ignoredSpecLevels" || propName == "threshold" || propName == "scale" ) {
+			env.sampleBits = sampleBits;
+			env.ignoredSpecLevels = ignoredSpecLevels;
+			env.threshold = threshold;
+			env.scale = scale;
+			needCompute = true;
+		}
+
+		env.rot = rotation;
+		env.power = power;
+
+		env.createTextures();
+
+		if( needCompute ) {
+			var loadFromBinarySucces = loadFromBinary(ctx);
+			if( !loadFromBinarySucces )
+				compute(ctx);
+		}
+
+		applyToRenderer(ctx);
+	}
+
+	function applyToRenderer( ctx : Context ) {
+		var pbrRenderer = Std.downcast(ctx.local3d.getScene().renderer, h3d.scene.pbr.Renderer);
+		if( pbrRenderer != null ) {
+			pbrRenderer.env = env;
+		}
+	}
+
+	#if editor
+
+	override function getHideProps() : HideProps {
+		return { icon : "sun-o", name : "Environment" };
+	}
+
+	override function edit( ctx : EditContext ) {
+		// super.edit(ctx);
+		ctx.properties.add(new hide.Element('
+			<div class="group" name="Environment">
+				<dl>
+					<dt>SkyBox</dt><dd><input type="texturepath" field="sourceMapPath"/></dd>
+					<dt>Rotation</dt><dd><input type="range" min="0" max="360" field="rotation"/></dd>
+					<dt>Power</dt><dd><input type="range" min="0" max="10" field="power"/></dd>
+				</dl>
+			</div>
+			<div class="group" name="Resolution">
+				<dl>
+					<dt>Diffuse</dt><dd><input type="range" min="1" max="512" step="1" field="diffSize"/></dd>
+					<dt>Specular</dt><dd><input type="range" min="1" max="2048" step="1" field="specSize"/></dd>
+					<dt>Sample Count</dt><dd><input type="range" min="1" max="12" step="1" field="sampleBits"/></dd>
+					<dt>Ignored Spec Levels</dt><dd><input type="range" min="0" max="3" step="1" field="ignoredSpecLevels"/></dd>
+				</dl>
+			</div>
+			<div class="group" name="HDR">
+				<dl>
+					<dt>Threshold</dt><dd><input type="range" min="0" max="1" step="0.1" field="threshold"/></dd>
+					<dt>Scale</dt><dd><input type="range" min="0" max="10" field="scale"/></dd>
+				</dl>
+			</div>
+			'), this, function(pname) {
+			ctx.onChange(this, pname);
+		});
+	}
+
+	#end
+
+	static var _ = Library.register("environment", Environment);
+}