Feature Texture Colourscheme..

Feature Texture Colourscheme..

Requests for features in the spring code.

Moderator: Moderators

Post Reply
User avatar
PicassoCT
Journeywar Developer & Mapper
Posts: 10450
Joined: 24 Jan 2006, 21:12

Feature Texture Colourscheme..

Post by PicassoCT »

Request for a feature that allows a hueshift of the first rgb texture of a feature by a value in the lua map.

Basically a originally greenish texture of a tree, where the blue channel of the 2nd texture marks the leaves as changeable- gets shifted of to a red/brown/violett colour to better fit into a current desert enviroment.

Addition:
You need this shifts on a per feature base, else something will be terrible off colourwise..
User avatar
PicassoCT
Journeywar Developer & Mapper
Posts: 10450
Joined: 24 Jan 2006, 21:12

Re: Feature Texture Colourscheme..

Post by PicassoCT »

So i ve been informed that one can alter this in a widget on per map basis
[08:34:01] <[LCC]jK>you need RGB->HSL->apply offset->RGB
[08:34:01] <[LCC]jK> you can speed it up with a cubetex, but it's still not trivial
So what is needed?
A list of the features of course- with the feature 2nd texture blue channel to indicate the colour-shift.

A lua widget handling it
User avatar
PicassoCT
Journeywar Developer & Mapper
Posts: 10450
Joined: 24 Jan 2006, 21:12

Re: Feature Texture Colourscheme..

Post by PicassoCT »

Code: Select all

local versionNumber = "1.34"

function widget:GetInfo()
	return {
		name      = "Map dependant ColourShift",
		desc      = "Shifts Colours of mapfeatures according to map",
		author    = "picasso",
		date      = "April 7, 2015",
		license   = "GNU GPL v2",
		layer     = 0,
		enabled   = true
	}
end


-- CONFIGURATION
local debug = false		--generates debug message
local updateInt = 1 	--seconds for the ::update loop
local dontTrackEnemyWrecks =  0
-- END OF CONFIG

local lastTime

local ColourSchemeFeatures = {}
local ignoreFeature = {}
local HueOffsetByDefID={}

local floor                 = math.floor
local max					= math.max
local udefTab				= UnitDefs
local fdefTab				= FeatureDefs
local glColor               = gl.Color
local glDepthTest           = gl.DepthTest
local glRotate				= gl.Rotate
local glTexture             = gl.Texture
local glTexEnv				= gl.TexEnv
local glLineWidth           = gl.LineWidth
local glPopMatrix           = gl.PopMatrix
local glPushMatrix          = gl.PushMatrix
local glTranslate           = gl.Translate
local glFeatureShape		= gl.FeatureShape
local glUnitShape			= gl.UnitShape
local spGetGameSeconds      = Spring.GetGameSeconds
local spGetMyPlayerID       = Spring.GetMyPlayerID
local spGetPlayerInfo       = Spring.GetPlayerInfo
local spGetAllFeatures		= Spring.GetAllFeatures
local spGetFeaturePosition  = Spring.GetFeaturePosition
local spGetFeatureDefID		= Spring.GetFeatureDefID
local spGetMyAllyTeamID		= Spring.GetMyAllyTeamID
local spGetFeatureAllyTeam	= Spring.GetFeatureAllyTeam
local spGetFeatureTeam		= Spring.GetFeatureTeam
local spGetUnitHealth 		= Spring.GetUnitHealth
local spGetFeatureHealth 	= Spring.GetFeatureHealth
local spGetFeatureResurrect = Spring.GetFeatureResurrect
local spGetPositionLosState = Spring.GetPositionLosState
local spIsUnitAllied		= Spring.IsUnitAllied
local spGetUnitDirection    = Spring.GetUnitDirection
local spGetFeatureDirection = Spring.GetFeatureDirection
local spGetUnitBasePosition = Spring.GetUnitBasePosition
local spGetUnitPosition     = Spring.GetUnitPosition
local spGetUnitHealth 	    = Spring.GetUnitHealth
local spEcho                = Spring.Echo
local spGetUnitDefID        = Spring.GetUnitDefID
local spIsSphereInView  	= Spring.IsSphereInView
local spGetUnitBuildFacing  = Spring.GetUnitBuildFacing
local mdeg = math.deg
local matan2 = math.atan2

local DrawColourSchemeFeatures

local ScanFeatures
local DeleteColourSchemeFeatures

local ResetGl
local CheckSpecState
local printDebug
local firstScan = true

function widget:Update()
	local timef = spGetGameSeconds()
	local time = floor(timef)

	-- update timers once every <updateInt> seconds
	if (time % updateInt == 0 and time ~= lastTime) then	
		lastTime = time
		--do update stuff:
		
		if ( CheckSpecState() == false ) then
			return false
		end
		
		ScanFeatures()
		

		DeleteColourSchemeFeatures()
	end
end

function widget:DrawWorld()

	
	DrawColourSchemeFeatures()
	
	ResetGl()
end
local HueOffSet={}
--HueOffSet={[FeatureDefID]= 0-255}
function readInHueOffset()
--ReadIn LuaColourOffset by Map file
	VFS.Include('Map/MapInfo.lua', nil, VFSMODE)
	if not mapinfo.HueOffSet then 	widgetHandler:RemoveWidget() end
	HueOffSet=mapinfo.HueOffSet 
return 
end

function DrawColourSchemeFeatures()
	glColor(1.0, 1.0, 1.0, 0.35 )
  
	glTexture(1,"$units2")
	--glTexture(1,"$units1")

	glTexEnv( GL.TEXTURE_ENV, GL.TEXTURE_ENV_MODE, 34160 )				--GL_COMBINE_RGB_ARB
	--use the alpha given by glColor for the outgoing alpha.
	glTexEnv( GL.TEXTURE_ENV, 34162, GL.REPLACE )			--GL_COMBINE_ALPHA
	glTexEnv( GL.TEXTURE_ENV, 34184, 34167 )			--GL_SOURCE0_ALPHA_ARB			GL_PRIMARY_COLOR_ARB
	
	--------------------------Draw-------------------------------------------------------------
	for unitID, ColourScheme in pairs( ColourSchemeFeatures ) do
		local x, y, z = ColourScheme.x, ColourScheme.y, ColourScheme.z
		local _, losState, _ = spGetPositionLosState(x, y, z)
		
		local udef = fdefTab[ ColourScheme["featDefId"] ]
		local radius = max( udef["xsize"], udef["zsize"] ) * 0.5
		local inView = spIsSphereInView( x, y, z, radius )
	
		if ( losState == false and inView and HueOffSet[udef]  ) then
			--glow effect?
			--gl.Blending(GL.SRC_ALPHA, GL.ONE)
			glPushMatrix()
			glTranslate(x, y, z)
			glRotate(ColourScheme.angle,0,y,0)
			
			glFeatureShape(ColourScheme["featDefId"], ColourScheme["teamId"] )
			  
			glPopMatrix()
		end
	end
	--------------------------Clean up-------------------------------------------------------------
	--TODO: Here be atan resolved the hueshift for the features first texture based upon the second textures rgb
	--local HueShift =  HueOffSet[udef] 
	--for x=1,#Tex1.X, 1 do
	--for y=1,#Tex1.Y, 1 do
	-- if Tex2.Blue[x][y] > 0 do 
	--extract local or, og,ob= Tex1[x][y].r, Tex1[x][y].g, Tex1[x][y].b
	-- alpha= (2*or - og - ob)/2
	--U = math.cos(HueShift* PI/180);
	--W = mathsin(HueShift* PI/180);

	--Taken from stackoverflow : http://stackoverflow.com/questions/5007925/using-m-pi-with-c89-standard
	-- r = (.299+.701*U+.168*W)*in.r
	--    + (.587-.587*U+.330*W)*in.g
	--    + (.114-.114*U-.497*W)*in.b;
	--	
	-- g = (.299-.299*U-.328*W)*in.r
	--    + (.587+.413*U+.035*W)*in.g
	--    + (.114-.114*U+.292*W)*in.b;
	--b = (.299-.3*U+1.25*W)*in.r
	--    + (.587-.588*U-1.05*W)*in.g
	--    + (.114+.886*U-.203*W)*in.b;

	--Store it once computated into a cache- yes this produces a texture copy in lua space- but its still cheaper then computating it all again
	
	glTexEnv( GL.TEXTURE_ENV, GL.TEXTURE_ENV_MODE, 8448 )				--GL_MODULATE
	--use the alpha given by glColor for the outgoing alpha.
	glTexEnv( GL.TEXTURE_ENV, 34162, 8448 )											--GL_MODULATE
	----gl.TexEnv( GL.TEXTURE_ENV, 34184, 5890 )			--GL_SOURCE0_ALPHA_ARB			GL_TEXTURE
end


function ScanFeatures()	
	local features = spGetAllFeatures()

	if firstScan then
	  local sfind = string.find
	  for _, fID in ipairs(features) do
	    local fDefId = spGetFeatureDefID(fID)
	    local fName = FeatureDefs[fDefId].name
	    if sfind(fName, 'tree') or sfind(fName, 'rock') then
	      ignoreFeature[fDefId] = true
	    end
	  end
	  firstScan = false
	  return
	end
		
	local myAllyID = spGetMyAllyTeamID()
	
	for _, fID in ipairs(features) do
		local fDefId = spGetFeatureDefID(fID)
		
		if not ignoreFeature[fDefId] then
		
			local fAllyID = spGetFeatureAllyTeam(fID)
			local fTeamID = spGetFeatureTeam( fID )

			local resName, _ = spGetFeatureResurrect(fID)
											
			if ( (resName == "" or dontTrackEnemyWrecks == 0) and fAllyID >= 0 and myAllyID ~= fAllyID and ColourSchemeFeatures[fID] == nil ) then
				printDebug("ColourScheme Feature added: " .. fDefId )
				local x, y, z = spGetFeaturePosition(fID)
				local dx,_,dz = spGetFeatureDirection(fID)
				local angle = mdeg(matan2(dx,dz))			
				ColourSchemeFeatures[fID] = { featDefId = fDefId, x=x, y=y, z=z, teamId = fTeamID, angle=angle }
			end
		end
	end
end

function DeleteColourSchemeFeatures()
	for featureID, ColourScheme in pairs(ColourSchemeFeatures) do
		local a, b, c = spGetPositionLosState(ColourScheme.x, ColourScheme.y, ColourScheme.z)
		local losState = b
		local featDefID = spGetFeatureDefID(featureID)
			
		--local health,_,_ = spGetFeatureHealth( unitID )
	
		if ( featDefID == nil and losState) then	
			printDebug("ColourScheme Feature deleted: " .. featureID )
			ColourSchemeFeatures[featureID] = nil
		end
	end
end


function widget:GameStart()
  if widgetHandler.widgets then
	  for i, widget in ipairs(widgetHandler.widgets) do
		if (widget:GetInfo().name == 'Defense Range') then
		  local version = tonumber(string.match(widget:GetInfo().desc,'%d+%.%d+'))
		  if version and (version < tonumber("6")) then
			spEcho("<ColourScheme Site> Old DefenseRange found! Widget removed.")
			widgetHandler:RemoveWidget()
		  end
		end
	  end
  end
  
  readInHueOffset()	
end

--Commons
function ResetGl() 
	glColor( { 1.0, 1.0, 1.0, 1.0 } )
	glLineWidth( 1.0 )
	glDepthTest(false)
	glTexture(false)
end

function CheckSpecState()
	local playerID = spGetMyPlayerID()
	local _, _, spec, _, _, _, _, _ = spGetPlayerInfo(playerID)
		
	if ( spec == true ) then
		spEcho("<ColourScheme Site> Spectator mode. Widget removed.")
		widgetHandler:RemoveWidget()
		return false
	end
	
	return true	
end

function printDebug( value )
	if ( debug ) then
		if ( type( value ) == "boolean" ) then
			if ( value == true ) then spEcho( "true" )
				else spEcho("false") end
		elseif ( type(value ) == "table" ) then
			spEcho("Dumping table:")
			for key,val in pairs(value) do 
				spEcho(key,val) 
			end
		else
			spEcho( value )
		end
	end
end
A draft based upon the GhostScheme widget - Critique? Suggestions? Caching good, evil, meh?
User avatar
PicassoCT
Journeywar Developer & Mapper
Posts: 10450
Joined: 24 Jan 2006, 21:12

Re: Feature Texture Colourscheme..

Post by PicassoCT »

Okay, this has basically split into two variations of a plan.

One is a hueshift- via a shader

(Somewhere in that strange No Mans Land between EngineCode and GameCode, that anywhere else would be called Tooling and be part of the engine.)
Anyone having a feature shader to go on from here?

Image

The other one is to adapt feature teamcolours to the local groundposition. (Engine)
Attachments
Grafic.jpg
(196.68 KiB) Not downloaded yet
User avatar
PicassoCT
Journeywar Developer & Mapper
Posts: 10450
Joined: 24 Jan 2006, 21:12

Re: Feature Texture Colourscheme..

Post by PicassoCT »

http://hastebin.com/vehukekudo.avrasm

default shader altered - still missing the vec3 in which the hueshift is transproted to the gpu-code

The material - a currently not working draft, based upon the normal texture. Im quite out of my depth here, i ll admit that.

http://hastebin.com/ibepuritaq.js
gajop
Moderator
Posts: 3051
Joined: 05 Aug 2009, 20:42

Re: Feature Texture Colourscheme..

Post by gajop »

Shader is a much better option. Team color is really hacky (just consider the fact you can only have a limited number of teams) and shouldn't be used for such purposes imo.
That said, I should point out that WC3 had the option of changing unit/feature color, and they probably didn't use shaders or require a modern PC to run it.
User avatar
PicassoCT
Journeywar Developer & Mapper
Posts: 10450
Joined: 24 Jan 2006, 21:12

Re: Feature Texture Colourscheme..

Post by PicassoCT »

My first approach was with OpenGL- but JK informed me that the Pipe is to hardcoded for this.

Also seems others failed with this approach: http://stackoverflow.com/questions/223 ... ut-shaders

http://stackoverflow.com/questions/9234 ... -with-glsl

So yes, shader is the way to go here.
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Re: Feature Texture Colourscheme..

Post by smoth »

gajop wrote:Shader is a much better option.
Talk to jk about that. Supposedly it is more expensive than units
User avatar
Anarchid
Posts: 1384
Joined: 30 Nov 2008, 04:31

Re: Feature Texture Colourscheme..

Post by Anarchid »

Shader is a much better option. Team color is really hacky
You'll still have two puzzles remaining:
1) Where to get the blend mask from (beside teamcolor, can also use texture2 blue channel i guess).
2) How to sample the ground texture beneath to blend nicely (though maybe one color+detailtex will be enough).

Features already use the standard unit shader afaict. They get emission and specularity and whatnot.
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Re: Feature Texture Colourscheme..

Post by smoth »

last I remember looking into it, they had their own shander in spring. Problem comes in when you want to do something like the material work I was doing.
User avatar
PicassoCT
Journeywar Developer & Mapper
Posts: 10450
Joined: 24 Jan 2006, 21:12

Re: Feature Texture Colourscheme..

Post by PicassoCT »

1) That one is really easy. JKs Shader Framework allready loads a texture for normal mapping so you only load another texture for that.

2) That one is a nut breaker. Could sample Minimap. But Minimap is allready processed info.
User avatar
PicassoCT
Journeywar Developer & Mapper
Posts: 10450
Joined: 24 Jan 2006, 21:12

Re: Feature Texture Colourscheme..

Post by PicassoCT »

The Story so far

ShaderCode (Version of jks-GitHub Version)
http://hastebin.com/qigezeduqe.avrasm

Material Definition
http://hastebin.com/gawoniqofe.js
User avatar
Anarchid
Posts: 1384
Joined: 30 Nov 2008, 04:31

Re: Feature Texture Colourscheme..

Post by Anarchid »

In Lua GL:
1) Bind mapDiffuse texture to the shader.
2) Pass the rendered feature's world position in an uniform.

In vertex shader:
3) calculate vertex world position based on vertex local position and feature world-position uniform
4) adjust texture coordinates of the vertex on the mapDiffuse texture to match vertex's world position

In fragment shader (or somewhere else?)
5) Blend between mapDiffuse and unitDiffuse based on texture2.blue.
User avatar
PicassoCT
Journeywar Developer & Mapper
Posts: 10450
Joined: 24 Jan 2006, 21:12

Re: Feature Texture Colourscheme..

Post by PicassoCT »

Your assumption Sir, that SAO could not be filterd with the Z-Buffer for nearness of Source, is plainly wrong.

Let me ilustrate this with a picture. With LOTS OF SCARLETT LETTERs.

Image
Attachments
zbufferCloseNessToSource.jpg
(175.44 KiB) Not downloaded yet
Orfelius
Posts: 103
Joined: 17 Nov 2014, 20:57

Re: Feature Texture Colourscheme..

Post by Orfelius »

Is there any progress on this Pic?
User avatar
PicassoCT
Journeywar Developer & Mapper
Posts: 10450
Joined: 24 Jan 2006, 21:12

Re: Feature Texture Colourscheme..

Post by PicassoCT »

Hacked on it yesterday. Problem is my laptop doesent execute shaders wil post more from desktop at home.
Post Reply

Return to “Feature Requests”