Page 2 of 2

Re: Lua replacment for set ALPHA_THRESHOLD

Posted: 14 Jun 2009, 22:57
by KDR_11k
The rage for alpha in COB is 0-255, in Lua it should be 0.0-1.0

Re: Lua replacment for set ALPHA_THRESHOLD

Posted: 14 Jun 2009, 23:00
by zwzsg
I guess I shouldn't have skipped writing in my post: Yes I did convert the [0-255] range into a [0-1] range.
zwzsg wrote:GG.Synced_Set_Alpha_Threshold(u,math.max(0,math.min(255,alpha)/255))

Re: Lua replacment for set ALPHA_THRESHOLD

Posted: 07 Mar 2010, 19:24
by KDR_11k
There seems to be an issue with alpha_threshold values above 0.5, they're forced down to 0.5.

Re: Lua replacment for set ALPHA_THRESHOLD

Posted: 07 Mar 2010, 19:41
by zwzsg
I don't know how to fix.

It's probably related to how the "default" alpha is 0.5, so any alpha over is always drawn.

Here is the script I use. The important bit is: gl.AlphaTest(GL.GREATER, Alpha_List[UnitID])

Code: Select all

function gadget:GetInfo()
	return {
		name = "set Alpha Threshold",
		desc = "A LUA replacement for the bos/cob command set ALPHA_THRESHOLD to alpha;",
		author = "jK & lurker & zwzsg",
		date = "9th March, 2009",
		license = "Public Domain",
		layer = 10,
		enabled = true
	}
end

GL_COLOR_BUFFER_BIT = 0x4000

if (gadgetHandler:IsSyncedCode()) then

	local function Synced_Set_Alpha_Threshold(UnitID,Alpha)
		SendToUnsynced("Unsynced_Set_Alpha_Threshold", UnitID, Alpha)
	end
	GG.Synced_Set_Alpha_Threshold = Synced_Set_Alpha_Threshold
	-- LuaCOB.lua wraps it into SetAlphaThreshold then registers it

	function gadget:UnitDestroyed(UnitID)
		SendToUnsynced("Unsynced_UnitDestroyed",UnitID)
	end

else

	local GL_COLOR_BUFFER_BIT = 0x4000

	local Alpha_List={}

	function gadget:DrawUnit(UnitID)
		if Alpha_List[UnitID] then
			gl.PushAttrib(GL_COLOR_BUFFER_BIT)
			gl.AlphaTest(GL.GREATER, Alpha_List[UnitID])
			gl.UnitRaw(UnitID,true)
			gl.PopAttrib()
		end
	end

	local function Unsynced_Set_Alpha_UnitDestroyed(name,UnitID)
		Spring.UnitRendering.SetUnitLuaDraw(UnitID,false)
		Alpha_List[UnitID]=nil;
	end

	local function Unsynced_Set_Alpha_Threshold(name,UnitID,Alpha)
		if Alpha==0 then
			Spring.UnitRendering.SetUnitLuaDraw(UnitID,false)
			Alpha_List[UnitID]=nil
		else
			Spring.UnitRendering.SetUnitLuaDraw(UnitID,true)
			Alpha_List[UnitID]=Alpha
		end
	end

	function gadget:Initialize()
		-- Since Spring 0.81.0 unit texture alpha transparency is used even without AdvUnitShading
		--if(tonumber(Spring.GetConfigInt("AdvUnitShading"))==0) then
		--	gadgetHandler:RemoveGadget()
		--	return
		--else
			gadgetHandler:AddSyncAction("Unsynced_Set_Alpha_Threshold",Unsynced_Set_Alpha_Threshold)
			gadgetHandler:AddSyncAction("Unsynced_Set_Alpha_UnitDestroyed",Unsynced_UnitDestroyed)
		--end
	end
 
end
Here is the alpha value with which it is used when building a single bug in KP. Ingame, you can see the feet of the bug getting alpha animation when it's almost done built, but the body of the bug never get the alpha threshold animation. The body of the bug has an alpha of about 140-255 in Gimp, so about 0.5-1 when translated to OpenGL scale.

I demand a fix from the people who deemed the cob command trivial to reimplement in Lua.