Browse Source

Added subtrails to trails

Also added editor 3d icons
Clement Espeute 2 years ago
parent
commit
26eeeaf033

BIN
bin/res/icons/icon-trails.png


+ 32 - 0
hide/Ide.hx

@@ -55,6 +55,8 @@ class Ide {
 	var scripts : Map<String,Array<Void->Void>> = new Map();
 	var hasReloaded = false;
 
+	public var show3DIcons = true;
+
 	static var firstInit = true;
 
 	function new() {
@@ -539,6 +541,36 @@ class Ide {
 		return resourceDir+"/"+relPath;
 	}
 
+	static var textureCacheKey = "TextureCache";
+
+	public function getHideResPath(basePath:String) {
+		return getPath("${HIDE}/res/" + basePath);
+	}
+
+	// Get a texture from a file on disk. Cache the results
+    public function getTexture(fullPath:String) {
+		if (fullPath == null)
+			return null;
+
+		var engine = h3d.Engine.getCurrent();
+		var cache : Map<String, h3d.mat.Texture> = @:privateAccess engine.resCache.get(textureCacheKey);
+		if(cache == null) {
+			cache = new Map();
+			@:privateAccess engine.resCache.set(textureCacheKey, cache);
+		}
+
+		var tex = cache[fullPath];
+		if (tex != null)
+			return tex;
+
+        var data = sys.io.File.getBytes(fullPath);
+		var res = hxd.res.Any.fromBytes(fullPath, data);
+		tex = res.toImage().toTexture();
+
+		cache.set(fullPath, tex);
+		return tex;
+	}
+
 	public function resolveCDBValue( path : String, key : Dynamic, obj : Dynamic ) : Dynamic {
 
 		// allow Array as key (first choice)

+ 1 - 0
hide/comp/Toolbar.hx

@@ -15,6 +15,7 @@ typedef ToolDef = {
 	?icon : String,
 	?iconStyle: Dynamic,
 	?rightClick : Void -> Void,
+	?defaultValue : Dynamic,
 }
 
 typedef ToolToggle = {

+ 5 - 0
hide/view/FXEditor.hx

@@ -666,6 +666,11 @@ class FXEditor extends FileView {
 			cullingPreview.visible = (is2D) ? false : v;
 			updateGrid();
 		}, showGrid);
+
+
+		tools.addToggle("cube", "Toggle model axis", null, (v) -> { sceneEditor.showBasis = v; sceneEditor.updateBasis(); });
+
+		tools.addToggle("image", "Toggle 3d icons visibility", null, function(v) { hide.Ide.inst.show3DIcons = v; }, true);
 		tools.addColor("Background color", function(v) {
 			scene.engine.backgroundColor = v;
 			updateGrid();

+ 2 - 1
hide/view/Prefab.hx

@@ -397,6 +397,7 @@ class Prefab extends FileView {
 		toolsDefs.push({id: "localTransformsToggle", title : "Local transforms", icon : "compass", type : Toggle((v) -> sceneEditor.localTransform = v)});
 		toolsDefs.push({id: "gridToggle", title : "Toggle grid", icon : "th", type : Toggle((v) -> { showGrid = v; updateGrid(); }) });
 		toolsDefs.push({id: "axisToggle", title : "Toggle model axis", icon : "cube", type : Toggle((v) -> { sceneEditor.showBasis = v; sceneEditor.updateBasis(); }) });
+		toolsDefs.push({id: "iconVisibility", title : "Toggle 3d icons visibility", icon : "image", type : Toggle((v) -> { hide.Ide.inst.show3DIcons = v; }), defaultValue: true });
 
 		var texContent : Element = null;
 		toolsDefs.push({id: "sceneInformationToggle", title : "Scene information", icon : "info-circle", type : Toggle((b) -> statusText.visible = b), rightClick: () -> {
@@ -458,7 +459,7 @@ class Prefab extends FileView {
 				case Button(f):
 					el = tools.addButton(tool.icon, tool.title + shortcut, f, tool.rightClick);
 				case Toggle(f):
-					var toggle = tools.addToggle(tool.icon, tool.title + shortcut, f);
+					var toggle = tools.addToggle(tool.icon, tool.title + shortcut, null, f, tool.defaultValue);
 					el = toggle.element;
 					if( key != null )
 						keys.register("sceneeditor." + tool.id, () -> toggle.toggle(!toggle.isDown()));

+ 24 - 0
hrt/impl/EditorTools.hx

@@ -0,0 +1,24 @@
+package hrt.impl;
+
+#if editor
+
+class EditorIcon extends hrt.prefab.l3d.Billboard.BillboardObj {
+    override function sync(ctx) {
+        visible = hide.Ide.inst.show3DIcons;
+        super.sync(ctx);
+    }
+}
+
+class EditorTools {
+    public static function create3DIcon(object:h3d.scene.Object, iconPath:String, scale:Float = 1.0) : EditorIcon {
+        var ide = hide.Ide.inst;
+        var tex = ide.getTexture(ide.getHideResPath(iconPath));
+        var icon = new EditorIcon(tex, object);
+
+        icon.texture = tex;
+        icon.scale(scale);
+
+        return icon;
+    }
+}
+#end

+ 90 - 0
hrt/prefab/l3d/Billboard.hx

@@ -0,0 +1,90 @@
+package hrt.prefab.l3d;
+
+class BillboardShader extends hxsl.Shader {
+
+	static var SRC = {
+
+		@:import h3d.shader.BaseMesh;
+
+        function vertex() {
+            var newModelView = mat4(
+                vec4(camera.view[0].x, camera.view[1].x, camera.view[2].x, global.modelView[0].w),
+                vec4(camera.view[0].y, camera.view[1].y, camera.view[2].y, global.modelView[1].w),
+                vec4(camera.view[0].z, camera.view[1].z, camera.view[2].z, global.modelView[2].w),
+                vec4(0, 0, 0, 1)
+            );
+
+            // scale 
+            newModelView = mat4(
+                vec4(length(global.modelView[0].xyz), 0.0, 0.0, 0.0),
+                vec4(0.0, length(global.modelView[1].xyz), 0.0, 0.0),
+                vec4(0.0, 0.0, length(global.modelView[2].xyz), 0.0),
+                vec4(0.0, 0.0, 0.0, 1.0)
+            ) * newModelView;
+
+            // Fix rotation
+            newModelView = mat4(
+                vec4(1,0,0,0),
+                vec4(0,-1,0,0),
+                vec4(0,0,-1,0),
+                vec4(0,0,0,1)) * newModelView;
+
+            transformedPosition = relativePosition * newModelView.mat3x4();
+            transformedNormal = (input.normal * newModelView.mat3()).normalize();
+        }
+	};
+
+}
+
+
+class BillboardObj extends h3d.scene.Mesh {
+
+    var prim : h3d.prim.Polygon;
+    var shader : BillboardShader;
+
+    public var texture(get, set) : h3d.mat.Texture;
+
+    function set_texture(tex : h3d.mat.Texture) : h3d.mat.Texture {
+        return material.texture = tex;
+    }
+
+    function get_texture() : h3d.mat.Texture {
+        return material.texture;
+    }
+
+    public var color(get, set) : h3d.Vector;
+
+    function set_color(col : h3d.Vector) : h3d.Vector {
+        return material.color = col;
+    }
+
+    function get_color() : h3d.Vector {
+        return material.color;
+    }
+
+    public function new(?tile : h3d.mat.Texture,  ?parent : h3d.scene.Object) {
+        var shape : hrt.prefab.l3d.Polygon.Shape = Quad(0);
+        var cache = hrt.prefab.l3d.Polygon.getPrimCache();
+        prim = cache.get(shape);
+        if(prim == null)
+            prim = Polygon.createPrimitive(shape);
+        super(prim, null, parent);
+
+        shader = new BillboardShader();
+        material.mainPass.addShader(shader);
+        material.mainPass.setBlendMode(Alpha);
+        material.props = {
+                mode: "BeforeTonemapping",
+                blend: "Alpha",
+                shadows: false,
+                culling: "Back",
+                colorMask: 0xff,
+            };
+        material.refreshProps();
+    }
+
+    /* ignore parent shaders hack*/
+	override public function getMaterials( ?a : Array<h3d.mat.Material>, recursive = true ) {
+        return [];
+    }
+}

+ 89 - 3
hrt/prefab/l3d/Trails.hx

@@ -79,6 +79,11 @@ class TrailObj extends h3d.scene.Mesh {
     var bounds : h3d.col.Bounds;
     var prefab : Trails;
 
+    #if editor
+    var icon : hrt.impl.EditorTools.EditorIcon;
+    #end
+
+
     // Sets whenever we check the position of this object to automaticaly add points to the 0th trail.
     // If set to false, trail can be created by manually calling addPoint()
     public var autoTrackPosition : Bool = true;
@@ -93,6 +98,8 @@ class TrailObj extends h3d.scene.Mesh {
         if (numTrails != new_value) {
             numTrails = new_value;
             allocBuffers();
+            if (dprim != null)
+                dprim.alloc(null);
         }
         return numTrails;
     }
@@ -421,7 +428,14 @@ class TrailObj extends h3d.scene.Mesh {
         prefab = parentPrefab;
         bounds.addPos(0,0,0);
 
-        this.numTrails = numTrails != null ? numTrails : 1;
+        var nTrails = numTrails != null ? numTrails : 1;
+        if (nTrails == 1) {
+            if (parentPrefab.children.length > 1) {
+                nTrails = parentPrefab.children.length;
+            }
+        }
+
+        this.numTrails = nTrails;
 
         dprim = new h3d.prim.RawPrimitive(onDprimContextLost(), true);
         dprim.onContextLost = onDprimContextLost;
@@ -430,6 +444,7 @@ class TrailObj extends h3d.scene.Mesh {
 
         #if editor
         debugPointViz = new h3d.scene.Graphics(parent);
+        icon = hrt.impl.EditorTools.create3DIcon(this, "icons/icon-trails.png");
         #end
 
         material.props = getMaterialProps();
@@ -461,6 +476,38 @@ class TrailObj extends h3d.scene.Mesh {
 
 		super.sync(ctx);
 
+        var numObj = 0;
+        for (child in children) {
+            if (Std.downcast(child, TrailsSubTailObj) == null)
+                continue;
+            numObj ++;
+        }
+
+        if (numObj > 0) {
+            set_numTrails(numObj);
+        }
+
+        #if editor
+            if (numObj > 0) {
+                icon.color.a = 0.50;
+            }
+            else {
+                icon.color.a = 1.0;
+            }
+        #end
+
+        var childObjCount = 0;
+        for (child in children) {
+            if (Std.downcast(child, TrailsSubTailObj) == null)
+                continue;
+            autoTrackPosition = false;
+            var c = child;
+            var t = trails[childObjCount];
+            var pos = c.getAbsPos();
+            addPoint(t, pos.tx, pos.ty, pos.tz, ECamera, 1.0);
+            childObjCount ++;
+        }
+
         if (autoTrackPosition) {
             var x = absPos.tx;
             var y = absPos.ty;
@@ -675,6 +722,44 @@ class TrailObj extends h3d.scene.Mesh {
 	}
 }
 
+// Empty class just for casting purposes
+class TrailsSubTailObj extends h3d.scene.Object {
+
+    public function new(?parent) {
+        super(parent);
+        #if editor
+        var icon = hrt.impl.EditorTools.create3DIcon(this, "icons/icon-trails.png");
+        icon.scale(0.33);
+        #end
+    }
+}
+
+class TrailsSubTrail extends Object3D {
+
+    function new(?parent) {
+		super(parent);
+        name = "SubTrail";
+	}
+
+	override function makeInstance(ctx:Context):Context {
+		ctx = ctx.clone(this);
+		var obj = new TrailsSubTailObj(ctx.local3d);
+        applyTransform(obj);
+		obj.name = name;
+		ctx.local3d = obj;
+		return ctx;
+	}
+
+    #if editor
+	override function getHideProps():HideProps {
+		return { icon : "toggle-on", name : "Sub Trail" , allowChildren: (name) -> name == "Trails"};
+	}
+    #end
+
+	static var _ = Library.register("SubTrail", TrailsSubTrail);
+
+}
+
 
 class Trails extends Object3D {
 
@@ -690,9 +775,10 @@ class Trails extends Object3D {
     @:s public var uvStretch: Float = 1.0;
     @:s public var uvRepeat : UVRepeat = EMod;
 
-
     function new(?parent) {
 		super(parent);
+        name = "Trails";
+
 	}
 
 	public function create( ?parent : h3d.scene.Object, ?numTrails : Int ) {
@@ -736,7 +822,7 @@ class Trails extends Object3D {
         <dl>
             <dt>UV Mode</dt><dd><select field="uvMode"></select></dd>
             <dt>UV Repeat</dt><dd><select field="uvRepeat"></select></dd>
-            <dt>UV Scale</dt><dd><input type="range" field="uvStretch" min="0" max="5"/></dd>
+            <dt>UV Scale</dt><dd><input type="range" field="uvStretch" min="0" max="5" title="Hey look at me i\'m a comment"/></dd>
         </dl>
     </div>
 		'),this, function(name:String) {