Browse Source

Light icons in editor

Clement Espeute 2 năm trước cách đây
mục cha
commit
266b54bf35

BIN
bin/res/icons/DirLight.png


BIN
bin/res/icons/PointLight.png


BIN
bin/res/icons/SpotLight.png


+ 14 - 1
hide/comp/SceneEditor.hx

@@ -938,7 +938,20 @@ class SceneEditor {
 		else {
 			var refPrefab = new Reference();
 			refPrefab.source = view.config.getLocal("scene.renderProps");
-			refPrefab.makeInstance(context);
+			var ctx2 = refPrefab.makeInstance(context);
+
+			var lights = refPrefab.getAll(hrt.prefab.Light, true);
+			for (light in lights) {
+				var ctxs = ctx2.shared.getContexts(light);
+				for (ctx in ctxs) {
+					var icon = Std.downcast(ctx.custom, hrt.impl.EditorTools.EditorIcon);
+					if (icon != null) {
+						icon.remove();
+						ctx.custom = null;
+					}
+				}
+			}
+
 			if( @:privateAccess refPrefab.ref != null ) {
 				var renderProps = @:privateAccess refPrefab.ref.get(hrt.prefab.RenderProps);
 				if( renderProps != null )

+ 30 - 2
hrt/impl/EditorTools.hx

@@ -3,10 +3,38 @@ package hrt.impl;
 #if editor
 
 class EditorIcon extends hrt.prefab.l3d.Billboard.BillboardObj {
-    override function sync(ctx) {
+    public var scaleWithParent = false;
+
+	public function new(?tile : h3d.mat.Texture,  ?parent : h3d.scene.Object) {
+		super(tile, parent);
+		ignoreCollide = false;
+	}
+
+	override function sync(ctx) {
         visible = hide.Ide.inst.show3DIcons;
         super.sync(ctx);
     }
+
+	override function getLocalCollider():h3d.col.Collider {
+		return new h3d.col.Sphere(0,0,0,billboardScale/2.0);
+	}
+
+	// Ingonre scale and rotation
+	override function calcAbsPos() {
+		absPos.identity();
+		absPos.tx = x;
+		absPos.ty = y;
+		absPos.tx = z;
+
+		if (parent != null) {
+			absPos.tx += parent.absPos.tx;
+			absPos.ty += parent.absPos.ty;
+			absPos.tz += parent.absPos.tz;
+		}
+
+		if( invPos != null )
+			invPos._44 = 0; // mark as invalid
+	}
 }
 
 class EditorTools {
@@ -16,7 +44,7 @@ class EditorTools {
         var icon = new EditorIcon(tex, object);
 
         icon.texture = tex;
-        icon.scale(scale);
+		icon.billboardScale = scale;
 
         return icon;
     }

+ 39 - 29
hrt/prefab/Light.hx

@@ -153,11 +153,26 @@ class Light extends Object3D {
 		}
 		ctx.local3d.name = name;
 
+		#if editor
+		ctx.custom = hrt.impl.EditorTools.create3DIcon(ctx.local3d, "icons/icon-pointlight.png", 0.5);
+		#end
+
 		cookieTex = initTexture(cookiePath);
 		updateInstance(ctx);
 		return ctx;
 	}
 
+	#if editor
+	override function removeInstance(ctx:Context):Bool {
+		var icon = Std.downcast(ctx.custom, hrt.impl.EditorTools.EditorIcon);
+		if (icon != null) {
+			icon.remove();
+			ctx.custom = null;
+		}
+		return super.removeInstance(ctx);
+	}
+	#end
+
 	override function updateInstance( ctx : Context, ?propName : String ) {
 		super.updateInstance(ctx, propName);
 
@@ -230,7 +245,6 @@ class Light extends Object3D {
 		var debugPoint = ctx.local3d.find(c -> if(c.name == "_debugPoint") c else null);
 		var debugDir = ctx.local3d.find(c -> if(c.name == "_debugDir") c else null);
 		var debugSpot = ctx.local3d.find(c -> if(c.name == "_debugSpot") c else null);
-		var mesh : h3d.scene.Mesh = null;
 		var sel : h3d.scene.Object = null;
 
 		switch(kind){
@@ -246,9 +260,7 @@ class Light extends Object3D {
 					debugPoint = new h3d.scene.Object(ctx.local3d);
 					debugPoint.name = "_debugPoint";
 
-					mesh = new h3d.scene.Mesh(h3d.prim.Sphere.defaultUnitSphere(), debugPoint);
-					mesh.ignoreBounds = true;
-					mesh.setScale(0.2);
+
 
 					rangeSphere = new h3d.scene.Sphere(0xffffff, 1, true, debugPoint);
 					rangeSphere.visible = false;
@@ -258,11 +270,9 @@ class Light extends Object3D {
 					rangeSphere.material.shadows = false;
 				}
 				else {
-					mesh = cast debugPoint.getChildAt(0);
-					rangeSphere = cast debugPoint.getChildAt(1);
+					rangeSphere = cast debugPoint.getChildAt(0);
 				}
 
-				mesh.setScale(0.2/range);
 				rangeSphere.material.color.setColor(color);
 				sel = rangeSphere;
 
@@ -275,10 +285,7 @@ class Light extends Object3D {
 					debugDir = new h3d.scene.Object(ctx.local3d);
 					debugDir.name = "_debugDir";
 
-					mesh = new h3d.scene.Mesh(h3d.prim.Sphere.defaultUnitSphere(), debugDir);
-					mesh.ignoreBounds = true;
-					mesh.setScale(0.2);
-					mesh.ignoreParentTransform = true;
+
 
 					var g = new h3d.scene.Graphics(debugDir);
 					g.lineStyle(1, 0xffffff);
@@ -291,11 +298,9 @@ class Light extends Object3D {
 					sel = g;
 				}
 				else {
-					mesh = cast debugDir.getChildAt(0);
-					sel = debugDir.getChildAt(1);
+					sel = debugDir.getChildAt(0);
 				}
 
-				mesh.setPosition(debugDir.getAbsPos().tx, debugDir.getAbsPos().ty, debugDir.getAbsPos().tz);
 
 			case Spot:
 
@@ -306,10 +311,6 @@ class Light extends Object3D {
 					debugSpot = new h3d.scene.Object(ctx.local3d);
 					debugSpot.name = "_debugSpot";
 
-					mesh = new h3d.scene.Mesh(h3d.prim.Sphere.defaultUnitSphere(), debugSpot);
-					mesh.ignoreBounds = true;
-					mesh.setScale(0.2);
-
 					var g = new h3d.scene.Graphics(debugSpot);
 					g.lineStyle(1, this.color);
 					g.moveTo(0,0,0); g.lineTo(1, 1, 1);
@@ -328,7 +329,7 @@ class Light extends Object3D {
 					sel = g;
 				}
 				else{
-					var g : h3d.scene.Graphics = Std.downcast(debugSpot.getChildAt(1), h3d.scene.Graphics);
+					var g : h3d.scene.Graphics = Std.downcast(debugSpot.getChildAt(0), h3d.scene.Graphics);
 					g.clear();
 					g.lineStyle(1, this.color);
 					g.moveTo(0,0,0); g.lineTo(1, 1, 1);
@@ -340,26 +341,35 @@ class Light extends Object3D {
 					g.lineTo(1, -1, 1);
 					g.lineTo(1, -1, -1);
 
-					mesh = cast debugSpot.getChildAt(0);
 					sel = g;
 				}
 
-				mesh.setScale(0.2/range);
 		}
 
-		if(mesh != null){
-			var mat = mesh.material;
-			mat.mainPass.setPassName("overlay");
-			mat.color.setColor(color);
-			mat.shadows = false;
+
+		var icon = Std.downcast(ctx.custom, hrt.impl.EditorTools.EditorIcon);
+		if (icon != null) {
+			icon.color = h3d.Vector.fromColor(color);
+
+			var ide = hide.Ide.inst;
+			switch(kind) {
+				case Directional:
+					icon.texture = ide.getTexture(ide.getHideResPath("icons/DirLight.png"));
+				case Point:
+					icon.texture = ide.getTexture(ide.getHideResPath("icons/PointLight.png"));
+				case Spot:
+					icon.texture = ide.getTexture(ide.getHideResPath("icons/SpotLight.png"));
+				default:
+					throw "unknown light type";
+			}
 		}
 
 		var isSelected = false;
 		if(sel != null){
 			isSelected = sel.visible;
-			if( debugPoint != null ) debugPoint.visible = (isSelected || ctx.shared.editorDisplay) && debugDisplay;
-			if( debugDir != null ) debugDir.visible = (isSelected || ctx.shared.editorDisplay) && debugDisplay;
-			if( debugSpot != null ) debugSpot.visible = (isSelected || ctx.shared.editorDisplay) && debugDisplay;
+			if( debugPoint != null ) debugPoint.visible = (isSelected || ctx.shared.editorDisplay);
+			if( debugDir != null ) debugDir.visible = (isSelected || ctx.shared.editorDisplay);
+			if( debugSpot != null ) debugSpot.visible = (isSelected || ctx.shared.editorDisplay);
 			sel.name = "__selection";
 		}
 

+ 8 - 0
hrt/prefab/Object3D.hx

@@ -273,6 +273,14 @@ class Object3D extends Prefab {
 				continue;
 			}
 
+			var asIcon = Std.downcast(mesh, hrt.impl.EditorTools.EditorIcon);
+			if (asIcon != null) {
+				hasSkin = true; // hack
+				/*var pos = asIcon.getAbsPos();
+				bounds.addSpherePos(pos.tx, pos.ty, pos.tz, asIcon.billboardScale);*/
+				continue;
+			}
+
 			var lb = mesh.primitive.getBounds().clone();
 			lb.transform(localMat);
 			bounds.add(lb);

+ 13 - 5
hrt/prefab/l3d/Billboard.hx

@@ -4,6 +4,8 @@ class BillboardShader extends hxsl.Shader {
 
 	static var SRC = {
 
+		@param var scale : Float;
+
 		@:import h3d.shader.BaseMesh;
 
         function vertex() {
@@ -14,11 +16,11 @@ class BillboardShader extends hxsl.Shader {
                 vec4(0, 0, 0, 1)
             );
 
-            // scale 
+            // 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(scale /*length(global.modelView[0].xyz)*/, 0.0, 0.0, 0.0),
+                vec4(0.0, scale /*length(global.modelView[1].xyz)*/, 0.0, 0.0),
+                vec4(0.0, 0.0, scale /*length(global.modelView[2].xyz)*/, 0.0),
                 vec4(0.0, 0.0, 0.0, 1.0)
             ) * newModelView;
 
@@ -41,6 +43,7 @@ class BillboardObj extends h3d.scene.Mesh {
 
     var prim : h3d.prim.Polygon;
     var shader : BillboardShader;
+	public var billboardScale : Float = 1.0;
 
     public var texture(get, set) : h3d.mat.Texture;
 
@@ -62,6 +65,11 @@ class BillboardObj extends h3d.scene.Mesh {
         return material.color;
     }
 
+	override function sync(ctx:h3d.scene.RenderContext) {
+		shader.scale = billboardScale;
+		super.sync(ctx);
+	}
+
     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();
@@ -74,7 +82,7 @@ class BillboardObj extends h3d.scene.Mesh {
         material.mainPass.addShader(shader);
         material.mainPass.setBlendMode(Alpha);
         material.props = {
-                mode: "BeforeTonemapping",
+                mode: "Overlay",
                 blend: "Alpha",
                 shadows: false,
                 culling: "Back",