Various Shader Ressources

Various Shader Ressources

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

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

Various Shader Ressources

Post by PicassoCT »

The idea is simple.. two Pieces.. and a percentage.. mix and return.. in one widget.. anyone in for a collab?

Code: Select all

function gadget:GetInfo()
  return {
    name      = "Deformation per Piece",
    desc      = "Linear Deforms two equalpieces per animation into another",
    author    = "Everyone and the dog ",
    date      = "Sep. 20014",
    license   = "ARGLWARGL - you just made that, up did you. Yes, and it will cost you 2500 $ with your lawyer to check on              that. Unless you GPL your own stuff  - then its free.",
    layer     = 0,
    enabled   = true,
  }
end

if (gadgetHandler:IsSyncedCode()) then
  
    function gadget:frame(frame)
    if frame % 5 == then
    if GG.MeshDefAnim then
      local MDeform=MeshDefAnim 
      for i=1,#MDeform,1 do
        Deform(MDeform.unitid, MDeform.piecePairTime)
      end  
    end
    end
      
    end
  

    function Deform(unit, piecePairTime)
      Descriptor={id=unit,piecePairTime}
      
					
			SendToUnsynced("DeformAnimation", Descriptor)
		end
	
else  --UNSYNCED
	--shaderCode
	---[[----------------------------------------------------------------------------
	local vertexShaderSource=[[
  
  for (int i=1,i< sizeof(dla)/sizeof(float3),i++)
  {
  vertex[i] =mix (dla[i],dlb[i],percentage);
  }
  
    ]]
	
	
	local shaderTable = {
	vertex= vertexShaderSource,
	-- fragment = fragmentShaderSource,
	 uniformInt = uniformInt,
	}
	--]]--------------------------------------------------------------------------	

	--variables for the task ahead	
	local shaderProgram 	
	local glUseShader 		= gl.UseShader
	local glCopyToTexture 	= gl.CopyToTexture
	local glTexture 		= gl.Texture
	local glTexRect 		= gl.TexRect
	local boolShaderWorking	= true
	local vsx,vsy 
	local screencopy
	local boolWorking=true
	local DeformedUnits ={}
	

	
	--Transfers the Data to the shader
	local function DeformAnimation(callname,Descriptor)		
	--Forge values in which to store the Holes Data
					if gl.CreateShader then
					Spring.Echo(" _DeformationShadder::forging Shader")
        --  Descriptor={id=unit,piecePairTime} 
        --piecePairTime contains pieceA, piece B (equal in Vertexes), a startframe, a    finnishframe
        prevMax=#shaderTable.uniFormInt
        for i=1,#Descriptor,1 do
          for k=1,#Descriptor.piecePairTime, 1 do
            
  materialID=TODO
  
      if (Descriptor.piecePairTime.pieceA) then
        if (dla) then
          dl = gl.CreateList(function()
            gl.MultiTexCoord(4,100)
            gl.UnitPiece(Descriptor.id,Descriptor.piecePairTime.pieceA)
            gl.MultiTexCoord(4,0)
            end)
        end
        Spring.UnitRendering.SetPieceList(Descriptor.id,materialID,Descriptor.piecePairTime.pieceA,dla)
      end   
      
      if (Descriptor.piecePairTime.pieceB) then
        if (dlb) then
            dl = gl.CreateList(function()
            gl.MultiTexCoord(4,100)
            gl.UnitPiece(Descriptor.id,Descriptor.piecePairTime.pieceB)
            gl.MultiTexCoord(4,0)
            end)
        end
        Spring.UnitRendering.SetPieceList(Descriptor.id,materialID,Descriptor.piecePairTime.pieceB,dlb)
      end
 
            
            
             shaderTable.uniFormInt[Max+i]={pieceA=dla,
                                            pieceB=dlb,
                                            percentage=(frame-piecePairTime.startframe)/piecePairTime.totframe
                                            id=Descriptor.id
                                            }
          end
        end
                                          
            shaderProgram=gl.CreateShader(shaderTable)			
						if shaderProgram then 
						DeformedUnits[Descriptor.id]={shader=shaderProgram,
                                          frame=frame, 
                                          oldpercentage=math.abs(startframe-frame/frame),
                                          percentage=math.abs(startframe-frame/frame),
                                          startframe=startframe, 
                                          frame=frame,
                                          shaderTable=shaderTable
                                          shader=shaderProgram} 
						else
						Spring.Echo("shader not created")
						Spring.Echo(gl.GetShaderLog())
						end
					Spring.UnitRendering.SetUnitLuaDraw(Descriptor.id,true)
					else
					Spring.Echo("<DeformShadder>: GLSL not supported.")
					end
	end

	function gadget:Initialize()   
		Spring.Echo("DeformShadder Initialised")
      -- This associate the messages with the functions
      -- So that when the synced sends a message "f" it calls the function f in unsynced
		gadgetHandler:AddSyncAction("DeformAnimation", DeformAnimation)
	end	  
  
  frame=0
	function gadget:frame(f)
  frame=f
  end
  
  function gadget:DrawUnit(unitID, drawMode)
		if DeformedUnits[unitID] then
		-- DODO is that the shader works for the whole unit.. not only for the unitpiece
		--Solutions, store all the piecevertexmatrices in the shader 
    DeformedUnits[unitID].frame=frame

    DeformedUnits[unitID].percentage=math.abs(DeformedUnits[unitID].startframe-frame/DeformedUnits[unitID].frame)
      if (DeformedUnits[unitID].percentage-DeformedUnits[unitID].oldpercentage)>0.01 then
        --update the shadder program.. 
        --i dont know how to pipe it otherwise up, so i frequently recompile the whole shadder
        DeformedUnits[unitID].shader= gl.CreateShader(DeformedUnits[unitID].ShaderProgram) or DeformedUnits[unitID].shader
      end
    
		glUseShader(DeformedUnits[unitID].shader)	
		end
	end 
	
end 


Last edited by PicassoCT on 12 Jun 2016, 12:47, edited 2 times in total.
User avatar
PicassoCT
Journeywar Developer & Mapper
Posts: 10450
Joined: 24 Jan 2006, 21:12

Re: PieceDeformationShadder

Post by PicassoCT »

Where have all the Eyecandy fireflowers gonne?

to the !No
Image
Attachments
JK_istakingawaytheeyecandy.jpg
(54.88 KiB) Downloaded 1 time
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Re: PieceDeformationShadder

Post by smoth »

I am here, just busy
User avatar
PicassoCT
Journeywar Developer & Mapper
Posts: 10450
Joined: 24 Jan 2006, 21:12

Re: PieceDeformationShadder

Post by PicassoCT »

Lets make a club of people who produce unused source .. ;)

Pictures is mad max styled lego, that will be recombined into scrap vehicles..Image
Attachments
madmaxpieces.jpg
(236.3 KiB) Downloaded 1 time
User avatar
PicassoCT
Journeywar Developer & Mapper
Posts: 10450
Joined: 24 Jan 2006, 21:12

Re: PieceDeformationShadder

Post by PicassoCT »

Image

Mad Max Lego..
Attachments
madmaxleg.jpg
(690.92 KiB) Not downloaded yet
User avatar
PicassoCT
Journeywar Developer & Mapper
Posts: 10450
Joined: 24 Jan 2006, 21:12

Re: PieceDeformationShadder

Post by PicassoCT »

so horray i gave up on piece deformation

and wanted just wear and tear- bulletmarks

resulting in this..

http://goo.gl/Llbvkv

which is not what i wanted..

anyone willing to help, so that the bullets blend in randomized on a per instance nature by distance to points?
Image
Attachments
(PNG Image, 327 × 327 pixels) - Scaled (99%).png
(PNG Image, 327 × 327 pixels) - Scaled (99%).png (87.56 KiB) Viewed 2668 times
User avatar
azaremoth
Cursed Developer
Posts: 549
Joined: 17 Feb 2005, 22:05

Re: PieceDeformationShadder

Post by azaremoth »

PicassoCT wrote:so horray i gave up on piece deformation
Why? It cold be pretty handy. What is missing? Not that I could offer any help with lua shaders...
User avatar
PicassoCT
Journeywar Developer & Mapper
Posts: 10450
Joined: 24 Jan 2006, 21:12

Re: PieceDeformationShadder

Post by PicassoCT »

time- also, the concept had flaws..

basically you could deform unit parts that made no sense..

https://vimeo.com/1600741<

I shall post a shader example of how far i got. You will notice the amount of polies needed for it to look good and despair..
This barely would work in a fps
User avatar
azaremoth
Cursed Developer
Posts: 549
Joined: 17 Feb 2005, 22:05

Re: PieceDeformationShadder

Post by azaremoth »

PicassoCT wrote:I shall post a shader example of how far i got.
Yes - please do so. As you are going with a linear interpolation - something like deformation of A => B => C might improve results.
User avatar
PicassoCT
Journeywar Developer & Mapper
Posts: 10450
Joined: 24 Jan 2006, 21:12

Re: PieceDeformationShadder

Post by PicassoCT »

Could not find it.. but recreated the heat death by laser fragment shader

http://goo.gl/hF7hVU

Code: Select all

precision highp float;
uniform float time;
uniform vec2 resolution;
varying vec3 fPosition;
varying vec3 fNormal;


vec3 fixedNormal;


vec3 hit= vec3(1.0,0.8,0.3 );


vec3 glowing= vec3(0.9,0.3,0.1);


vec3 cooldown= vec3(0.4,0.1,0.05);


vec3 wreck=vec3(0.185,0.155,0.155);


vec3 currColor;
float averageShadow;

float PI=3.14159;
float privateTime=0.0;
float fixedTime=0.0;
float totalTime=1.0;
float percenTage=0.0;
vec3 emptyVec = vec3(0.0,0.0,0.0);

vec3 clampAfter(vec3 x, float Time, float TimeLimit)
{
    return (Time < TimeLimit ? x :emptyVec);
}

vec3 openEnded(vec3 x, float Time, float TimeLimit, vec3 deFault)
{
  return Time < TimeLimit ? x :deFault;
}

void main()
{
privateTime+=0.1;
percenTage= (mod((time),totalTime))/totalTime*PI;


averageShadow=(fNormal.x*fNormal.x+fNormal.y*fNormal.y+fNormal.z+fNormal.z)/3.25;

currColor=(
  
 (percenTage < 0.5*PI ?  hit*cos(percenTage)*2.0 :emptyVec)

+ (percenTage < PI ? glowing*sin(percenTage)*2.5 :emptyVec)

+ (percenTage < PI*2.0 ? cooldown*sin(PI+percenTage)*2.0 :emptyVec)

+openEnded(wreck*abs(sin(PI+percenTage)),PI+percenTage,PI*2.0,abs(wreck*0.3))
);

  gl_FragColor = vec4(currColor*(1.0-averageShadow), 1);
}

"Vertex"

Code: Select all

precision highp float;
attribute vec3 position;
attribute vec3 normal;
uniform mat3 normalMatrix;
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
varying vec3 fNormal;
varying vec3 fPosition;
uniform float time;
varying float changeRate;
float totalTime=1.0;
float PI_HALF= 3.14159/2.0;
float PI =3.14159;



void main()
{
   fNormal = normalize(normalMatrix * normal);
  //vec4 pos = modelViewMatrix * vec4(position, 1.0);

    float sigNum=1.0;
    float relTime=abs(mod(time/totalTime,totalTime));
    //melting process - a inverse cosinus rising 
    float meltSpeed=abs(cos(relTime/(PI*2.0)));
    float molten = abs(1.0-cos((time/totalTime)*PI_HALF));
    
    //percentage we scale the vector out or inwards
    float percentage= (sqrt(abs(normal.y)/(abs(normal.x)+abs(normal.z) +0.01)))/42.0; // /50
    
    
    sigNum=(fNormal.y >= 0.0)  ? -1.41  : 1.0;
  
  //computate the total melting and end it at max where the molten metall gets hadr
  //keep Constant Change
  changeRate= relTime < 0.5*totalTime? mix(0.0,percentage*sigNum*molten,relTime/0.5*(totalTime)):percentage*sigNum*molten;
  
    
    // move the position along the normal and transform it
    vec3 newPosition = position + normal * changeRate;
    gl_Position = projectionMatrix * modelViewMatrix * vec4( newPosition, 1.0  );
 
  //gl_Position = projectionMatrix * pos;
}



And yes the fade out to normal wreckage colour is botched..
Last edited by PicassoCT on 13 Dec 2015, 22:25, edited 5 times in total.
User avatar
PicassoCT
Journeywar Developer & Mapper
Posts: 10450
Joined: 24 Jan 2006, 21:12

Re: PieceDeformationShadder

Post by PicassoCT »

Conga Dancing Rats - not even can i not find my own shader anymore - im not even able to recreate it.

http://goo.gl/O8WTW9

This example shows the principle.. you melt the object, by scaling it along its normals.. up looking normals get scaled inwards slightly, downward normals outward.. all this by percentage of the normal and by a cos(time/maxtime* PI/2)

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

Re: PieceDeformationShadder

Post by PicassoCT »

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

Re: Shader Buisness Is Shady

Post by PicassoCT »

http://goo.gl/d7zUwk

porcelain shader

Code: Select all

precision highp float;
uniform float time;
uniform vec2 resolution;
varying vec3 fPosition;
vec3 pos;
varying vec3 fNormal;
float hueShiftOffset;

vec3 sum(vec3 val)
{
float sumMid=sqrt(abs(val.x+val.y+val.z));
return vec3(sumMid,sumMid,sumMid);
}


float UperMax =1.0;
float LowerLimit= 0.0;
float Range =UperMax - LowerLimit;
vec3 applyOriginalColour()
{
  vec3 LeaveGreenTop=vec3(0.5,0.78,0.56);
  vec3 LeaveGreen=vec3(0.1,0.78,0.56);
  vec3 GroundStem=vec3(0.71,0.18,0.86);
  
  bool UpperLayer= (pos.y < UperMax ? false: true);
  
  if (UpperLayer==true )
  return( (UperMax -pos.y)/Range)*LeaveGreenTop +( (Range -pos.y)/Range)*LeaveGreen; 
  else
  return( (UperMax -pos.y)/Range)*LeaveGreen +( (Range -pos.y)/Range)*GroundStem; 
}

vec3 convertRGBtoHSV(vec3 rgbColor) {
float r = rgbColor[0];
float g = rgbColor[1];
float b = rgbColor[2];
float colorMax = max(max(r,g), b);
float colorMin = min(min(r,g), b);
float delta = colorMax - colorMin;
float h = 0.0;
float s = 0.0;
float v = colorMax;
vec3 hsv = vec3(0.0);
if (colorMax != 0.0) {
s = (colorMax - colorMin ) / colorMax;
}
if (delta != 0.0) {
if (r == colorMax) {
h = (g - b) / delta;
} else if (g == colorMax) {
h = 2.0 + (b - r) / delta;
} else {
h = 4.0 + (r - g) / delta;
}
h *= 60.0;
if (h < 0.0) {
h += 360.0;
}
}
hsv[0] = h;
hsv[1] = s;
hsv[2] = v;
return hsv;
}

vec3 convertHSVtoRGB(vec3 hsvColor) {
float h = hsvColor.x;
float s = hsvColor.y;
float v = hsvColor.z;
if (s == 0.0) {
return vec3(v, v, v);
}
if (h == 360.0) {
h = 0.0;
}
int hi = int(h);
float f = h - float(hi);
float p = v * (1.0 - s);
float q = v * (1.0 - (s * f));
float t = v * (1.0 - (s * (1.0 - f)));
vec3 rgb;
if (hi == 0) {
rgb = vec3(v, t, p);
} else if (hi == 1) {
rgb = vec3(q, v, p);
} else if (hi == 2) {
rgb = vec3(p, v, t);
} if(hi == 3) {
rgb = vec3(p, q, v);
} else if (hi == 4) {
rgb = vec3(t, p, v);
} else {
rgb = vec3(v, p, q);
}
return rgb;
}

void main()
{
pos=normalize(fPosition);
float hueShiftOffset=sin(time*10.0);
//filter out by height
vec3 hueShiftMapValue= vec3(0.3,1.0*hueShiftOffset,0.5);

vec3 vHSV= vec3(0.3*hueShiftOffset,1.0*hueShiftOffset,0.5*hueShiftOffset);

vec3 fragRGB = applyOriginalColour()* sum(1.0 - normalize(fNormal))*0.6;
/*

vec3 fragHSV = convertRGBtoHSV(fragRGB);
fragHSV += vHSV;
fragHSV.x = mod(fragHSV.x, 360.0);
fragHSV.y = mod(fragHSV.y, 1.0);
fragHSV.z = mod(fragHSV.z, 1.0);
fragRGB = convertHSVtoRGB(fragHSV);
*/


gl_FragColor = vec4(fragRGB, 1.0);
}

http://goo.gl/xKQSu0
precision highp float;
uniform float time;
uniform vec2 resolution;
varying vec3 fPosition;
varying vec3 tPosition;
varying vec3 fNormal;
float hueShiftOffset;

vec3 sum(vec3 val)
{
float sumMid=sqrt(abs(val.x+val.y+val.z));
return vec3(sumMid,sumMid,sumMid);
}


float g_percent;
float r_percent;
vec3 applyOriginalColour()
{
vec3 LeaveGreenTop=vec3(0.5,0.98,0.26);

vec3 GroundStem=vec3(0.71,0.48,0.26);


g_percent= 1.0-abs(sin(max(tPosition.y,0.0)));
r_percent=1.0-g_percent;
return mix(LeaveGreenTop,GroundStem,g_percent);
}

vec3 convertRGBtoHSV(vec3 rgbColor) {
float r = rgbColor[0];
float g = rgbColor[1];
float b = rgbColor[2];
float colorMax = max(max(r,g), b);
float colorMin = min(min(r,g), b);
float delta = colorMax - colorMin;
float h = 0.0;
float s = 0.0;
float v = colorMax;
vec3 hsv = vec3(0.0);
if (colorMax != 0.0) {
s = (colorMax - colorMin ) / colorMax;
}
if (delta != 0.0) {
if (r == colorMax) {
h = (g - b) / delta;
} else if (g == colorMax) {
h = 2.0 + (b - r) / delta;
} else {
h = 4.0 + (r - g) / delta;
}
h *= 60.0;
if (h < 0.0) {
h += 360.0;
}
}
hsv[0] = h;
hsv[1] = s;
hsv[2] = v;
return hsv;
}

vec3 convertHSVtoRGB(vec3 hsvColor) {
float h = hsvColor.x;
float s = hsvColor.y;
float v = hsvColor.z;
if (s == 0.0) {
return vec3(v, v, v);
}
if (h == 360.0) {
h = 0.0;
}
int hi = int(h);
float f = h - float(hi);
float p = v * (1.0 - s);
float q = v * (1.0 - (s * f));
float t = v * (1.0 - (s * (1.0 - f)));
vec3 rgb;
if (hi == 0) {
rgb = vec3(v, t, p);
} else if (hi == 1) {
rgb = vec3(q, v, p);
} else if (hi == 2) {
rgb = vec3(p, v, t);
} if(hi == 3) {
rgb = vec3(p, q, v);
} else if (hi == 4) {
rgb = vec3(t, p, v);
} else {
rgb = vec3(v, p, q);
}
return rgb;
}

void main()
{

float hueShiftOffset=sin(time*10.0);
//filter out by height
vec3 hueShiftMapValue= vec3(0.3,1.0*hueShiftOffset,0.5);

vec3 vHSV= vec3(0.3*hueShiftOffset,1.0*hueShiftOffset,0.5*hueShiftOffset);

vec3 fragRGB = (applyOriginalColour()) ;


vec3 fragHSV = convertRGBtoHSV(vec3(0.001,0.0001,0.0000)*cos(time));
fragHSV -= vHSV;
fragHSV.x = mod(fragHSV.x, 360.0);
fragHSV.y = mod(fragHSV.y, 1.0);
fragHSV.z = mod(fragHSV.z, 1.0);
fragRGB = fragRGB*r_percent + convertHSVtoRGB(fragHSV)*g_percent;


fragRGB = fragRGB/sum(1.0 - normalize(fNormal));


gl_FragColor = vec4(fragRGB, 1.0);
}
Image
Come on in - the colours are wonderfull-
Attachments
huesniffty.jpg
(228.75 KiB) Not downloaded yet
Post Reply

Return to “Lua Scripts”