Posted: 13th Feb 2017 0:37
SetObjectShaderConstantByName()
Oh wow how could I miss this command

I once made my own solution for that problem with drawobject()
Would love to know how Paul solved it !?

Thanks MikeMax
And I guess...yes there should be SetObjectMeshShaderConstantByName() and SetSpriteShaderConstantByName() then

For your problem...lets see
Hm there is no command for drawing the mesh or setting it invisible
Maybe you create a temporary mesh/object with your shader which you draw over the other object/mesh right before you render the scene
Posted: 13th Feb 2017 8:49
2. Fog activation make the shader to throw an error with the agk_CameraPos declaration (previously declared ? i don't see where)

I have already answered this question here (some posts above).

1. with animated models, when the normal shader is applied, animations no longer works

This is a similar problem to the fog. The animation is realized with a shader. If you use your own shader, then you override the automatically generated code.
A solution would be to write your own code for the bone animation. Or intigrate the existing code in the normalmapping shader.

Here is the standard code that agk generate.
+ Code Snippet
attribute highp vec3 position;
attribute mediump vec3 normal;
varying highp vec3 posVarying;
varying mediump vec3 normalVarying;
varying mediump vec3 lightVarying;
mediump vec3 GetVSLighting( mediump vec3 normal, highp vec3 pos );

uniform highp mat4 agk_ViewProj;
attribute highp vec2 uv;
varying highp vec2 uvVarying;
uniform highp vec4 uvBounds0;

attribute highp vec4 boneweights;
attribute mediump vec4 boneindices;
uniform highp vec4 agk_bonequats1[30];
uniform highp vec4 agk_bonequats2[30];

highp vec3 transformDQ( highp vec3 p, highp vec4 q1, highp vec4 q2 )
{
    p += 2.0 * cross( q1.xyz, cross(q1.xyz, p) + q1.w*p );
    p += 2.0 * (q1.w*q2.xyz - q2.w*q1.xyz + cross(q1.xyz,q2.xyz));
    return p;
}

void main()
{ 
    uvVarying = uv * uvBounds0.xy + uvBounds0.zw;
    highp vec4 q1 = agk_bonequats1[ int(boneindices.x) ] * boneweights.x;
    q1 += agk_bonequats1[ int(boneindices.y) ] * boneweights.y;
    q1 += agk_bonequats1[ int(boneindices.z) ] * boneweights.z;
    q1 += agk_bonequats1[ int(boneindices.w) ] * boneweights.w;
    highp vec4 q2 = agk_bonequats2[ int(boneindices.x) ] * boneweights.x;
    q2 += agk_bonequats2[ int(boneindices.y) ] * boneweights.y;
    q2 += agk_bonequats2[ int(boneindices.z) ] * boneweights.z;
    q2 += agk_bonequats2[ int(boneindices.w) ] * boneweights.w;
    highp float len = 1.0/length(q1);
    q1 *= len;
    q2 = (q2 - q1*dot(q1,q2)) * len;
    highp vec4 pos = vec4( transformDQ(position,q1,q2), 1.0 );
    gl_Position = agk_ViewProj * pos;

    normalVarying = normal + 2.0*cross( q1.xyz, cross(q1.xyz,normal) + q1.w*normal );
    posVarying = pos.xyz;
    lightVarying = GetVSLighting( normalVarying, posVarying );
}
 


I hope it helps.
Posted: 18th Feb 2017 7:09
Really hope to get a 3d shadow system(free or paid)
Posted: 18th Feb 2017 10:38
"


Yes, it's a real need for me too !

The janbo's 3D shadow shader is a great good start but unstable and unusable actually in a "real" 3D environment for the moment.

And we need multiple pass shaders system to combine multiple shaders only when necessary (or an AppGameKit system to compile this "all-in-one shader" at project compilation according to object/mesh options (Bones animation + fog + 3D Shadow casting + normal/spec mapping + lightmapping... it's just an example of a real need !) .
Posted: 19th Feb 2017 4:56
+1. I dont mind if the community is selling plugin those features. But if it is support officially, agk features advertisement will look great
Posted: 19th Feb 2017 17:00
yeah I think AppGameKit should at least include the default tested on all platform shaders.
so we get a head start and adjust and create extras but the default is a must.
Posted: 1st Mar 2017 9:10
@Ched80
Thanks for your DOF shader ....
But could you put a small 3d example please ... sorry .. I'm very noob
Posted: 3rd Mar 2017 6:36
@GolWofael the shader is really a simple tilt shift shader applying blur to the full screen, but increasing the magnitude of the blur the further away from the middle of the screen (in the y axis) the pixel is.

It's not a true DOF shader.
Posted: 7th Mar 2017 4:51
i just noticed this in the documentation;
https://www.appgamekit.com/documentation/guides/13_shaders.htm
Posted: 24th Mar 2017 6:55
2d realistic snow shader with example code:
(shader will be created by the code)

+ Code Snippet

// Project: AGKSnow 
// Created: 24.03.2017

// show all errors
SetErrorMode(2)

// set window properties
SetWindowTitle( "AGKSnow" )
SetWindowSize( 1024, 768, 0 )

// set display properties
SetVirtualResolution( 1024, 768 )
SetOrientationAllowed( 1, 1, 1, 1 )

UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
SetVSync(1)

CreateSnowShader()



shader = LoadSpriteShader("snow.ps")

// set shader to simple
SetShaderConstantByName(shader,"Settings",20.0,0.5,0.3,0.6) // LAYERS, DEPTH, WIDTH, SPEED

img = CreateImageColor(255,255,255,255)
spr=CreateSprite(img)
SetSpriteShader(spr,shader)
setspritesize(spr,1024,768)
do
	
	Print( ScreenFPS() )
	print(" SNOW shader Demo AGK Tier 1 - press 1-5 to change effect")
	
	
	
			if GetRawKeyPressed(49)=1 then SetShaderConstantByName(shader,"Settings",20.0,0.5,0.3,0.6) // simple
			if GetRawKeyPressed(50)=1 then SetShaderConstantByName(shader,"Settings",50.0,0.5,0.3,0.6) // simple, more layers
			if GetRawKeyPressed(51)=1 then SetShaderConstantByName(shader,"Settings",200.0,0.1,0.8,1.5) // heavy
			if GetRawKeyPressed(52)=1 then SetShaderConstantByName(shader,"Settings",20.0,0.5,0.3,2.6) // fast
			if GetRawKeyPressed(53)=1 then SetShaderConstantByName(shader,"Settings",200.0,0.5,0.3,0.2) // slow


    
    Sync()
loop


function CreateSnowShader()
 


file = OpenToWrite("snow.ps")
	WriteLine(file,"varying mediump vec2 uvVarying;")
	WriteLine(file,"uniform float agk_time;")
	WriteLine(file,"uniform vec4 Settings;")
	WriteLine(file,"void main(  )")
	WriteLine(file,"{")
	WriteLine(file,"const mat3 p = mat3(13.323122,23.5112,21.71123,21.1212,28.7312,11.9312,21.8112,14.7212,61.3934);")
	WriteLine(file,"vec2 uv = uvVarying;")
	WriteLine(file,"vec3 acc = vec3(0.0);")
	WriteLine(file,"float dof = 5.*sin(agk_time*.1);")
	WriteLine(file,"for (int i=0;i<int(Settings.r);i++) {")
	WriteLine(file,"float fi = float(i);")
	WriteLine(file,"vec2 q = uv*(1.+fi*Settings.g);")
	WriteLine(file,"q += vec2(q.y*(Settings.b*mod(fi*7.238917,1.)-Settings.b*.5),Settings.a*-agk_time/(1.+fi*Settings.g*.03));")
	WriteLine(file,"vec3 n = vec3(floor(q),31.189+fi);")
	WriteLine(file,"vec3 m = floor(n)*.00001 + fract(n);")
	WriteLine(file,"vec3 mp = (31415.9+m)/fract(p*m);")
	WriteLine(file,"vec3 r = fract(mp);")
	WriteLine(file,"vec2 s = abs(mod(q,1.)-.5+.9*r.xy-.45);")
	WriteLine(file,"s += .01*abs(2.*fract(10.*q.yx)-1.);")
	WriteLine(file,"float d = .6*max(s.x-s.y,s.x+s.y)+max(s.x,s.y)-.01;")
	WriteLine(file,"float edge = .005+.05*min(.5*abs(fi-5.-dof),1.);")
	WriteLine(file,"acc += vec3(smoothstep(edge,-edge,d)*(r.x/(1.+.02*fi*Settings.g)));")
	WriteLine(file,"}")
	WriteLine(file,"gl_FragColor = vec4(vec3(acc),1.0);")
	WriteLine(file,"}")
CloseFile(file)

endfunction

Posted: 24th Mar 2017 15:15
Woah,

nice snow shader.

Man, I really need to learn how these shaders work
Posted: 25th Mar 2017 4:56
Really niice!!
Posted: 29th Mar 2017 17:11
Anyone wanting to learn shaders, I have found a great online book which has links to editors etc:

http://thebookofshaders.com/

Might be useful
Posted: 1st Apr 2017 22:32
Text string, Image Buffer, Sprite Effect:

With different settings in the shader, you can get a lot of variations

// This one builds a starfield:
const float maskStr = 0.0125; // 0.0 - 1.0
const float vignetteStr = 0.0002; // 0.0 - 1.0
const float crtBend = 20.8;
const float crtOverscan = 0.21; // 0.0 - 1.0

Image:

Download:
https://forum.thegamecreators.com/attachment/79074
Posted: 1st Apr 2017 23:51
CRT Shader

Can also be modified in the .ps shader...

Image:


Download:
https://forum.thegamecreators.com/attachment/79076
Posted: 5th Apr 2017 7:14
Lensflare

Position of the sun can be set in screen coordinates.

Image:


Download:
https://forum.thegamecreators.com/attachment/79115

Can become handy sometimes.
Posted: 5th Apr 2017 12:14
Looks nice Jack!
Posted: 26th Apr 2017 2:28
Excellent Shaders Jack

And I believe baxslash 2d for the additive code

I modified the Lensflare and snow shaders by jack and added an additive blend of the background image rather than just use the noise this created
a background that could have a sun scroll down in the background adding a colour blend to the background sprite. Great for what I wanted a sunset
in 2d without the use of a quad image. I didn't change all that much to baxslashes awesome lensflare but now it does what I wanted and I have
commented the changes.

The summer.png that I use can be any 2d background image

The following allows you to switch between sun(lensflare) or snow
+ Code Snippet
// Project: AGK_Lensflare_Shader 
// Created: 05.04.2017

// show all errors
SetErrorMode(2)
#constant xMax=1024
#constant yMax=600
//#constant snowImg=1002:rem		The snow image used with the snowShader sprite
//#constant snowSpr=1002:rem		The sprite used for the snow snowShader image
#constant seasonBg=999:rem 		the season background sprite
#constant seasonAlpha=200:rem	Sets the alpha level for the season image		
#constant spring=991:rem		Spring image	
#constant snowShader=1:rem		The sprite snowShader used for snow 
#constant sunLense=2:rem		The sprite snowShader used for sun 	
// set window properties
SetWindowTitle( "AGK_Lensflare_Shader" )
SetWindowSize( xMax, yMax, 0 )

// set display properties
SetVirtualResolution( xMax, yMax )
SetOrientationAllowed( 1, 1, 1, 1 )
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
SetVSync(1)


//the snow to be used for winter
CreateSnowShader()
LoadSpriteShader(snowShader,"snow.ps")
noise = LoadImage("Summer.png"):rem changed the noise to my background image
SetShaderConstantByName(snowShader,"Settings",50.0,0.1,0.8,.5) // LAYERS, DEPTH, WIDTH, SPEED
//CreateImageColor(snowImg,255,255,255,255)
CreateSprite(seasonBg,noise)
SetSpriteShader(seasonBg,snowShader)
setspritesize(seasonBg,xMax,yMax)

//summer
CreateLenseFlare()
LoadSpriteShader(sunLense,"lensflare.ps")
//SetSpriteShader(seasonBg,sunLense)
SetShaderConstantByName(sunLense,"Position",1024.0,600.0,0.0,0.0)

ResetTimer()
do
	print(" Season shader Demo press 1 for sun 2 for snow")  
    X# = sin(timer())*xMax
    Y# = sin(timer())*yMax
    if GetRawKeyPressed(49)=1 then SetSpriteShader(seasonBg,sunLense) 
	if GetRawKeyPressed(50)=1 then SetSpriteShader(seasonBg,snowShader) 
	SetShaderConstantByName(sunLense,"Position",X#,Y#,0.0,0.0)
    Print( "FPS: "+str(ScreenFPS())+"   Lensflare Position X: "+str(X#)+" Y: "+str(Y#) )

    Sync()
loop

function CreateSnowShader()
file = OpenToWrite("snow.ps")
    WriteLine(file,"varying mediump vec2 uvVarying;")
    WriteLine(file,"uniform float agk_time;")
    WriteLine(file,"uniform vec4 Settings;")
    WriteLine(file,"uniform sampler2D texture0;")   //Added this for colouring
    WriteLine(file,"varying vec4 colorVarying;//")  //Added this colour varying variable so as it will blend with background
    WriteLine(file,"void main(  )")
    WriteLine(file,"{")
    WriteLine(file,"const mat3 p = mat3(13.323122,23.5112,21.71123,21.1212,28.7312,11.9312,21.8112,14.7212,61.3934);")
    WriteLine(file,"vec2 uv = uvVarying;")
    WriteLine(file,"vec3 acc = vec3(0.0);")
    WriteLine(file,"float dof = 5.*sin(agk_time*.1);")
    WriteLine(file,"for (int i=0;i<int(Settings.r);i++) {")
    WriteLine(file,"float fi = float(i);")
    WriteLine(file,"vec2 q = uv*(1.+fi*Settings.g);")
    WriteLine(file,"q += vec2(q.y*(Settings.b*mod(fi*7.238917,1.)-Settings.b*.5),Settings.a*-agk_time/(1.+fi*Settings.g*.03));")
    WriteLine(file,"vec3 n = vec3(floor(q),31.189+fi);")
    WriteLine(file,"vec3 m = floor(n)*.00001 + fract(n);")
    WriteLine(file,"vec3 mp = (31415.9+m)/fract(p*m);")
    WriteLine(file,"vec3 r = fract(mp);")
    WriteLine(file,"vec2 s = abs(mod(q,1.)-.5+.9*r.xy-.45);")
    WriteLine(file,"s += .01*abs(2.*fract(10.*q.yx)-1.);")
    WriteLine(file,"float d = .6*max(s.x-s.y,s.x+s.y)+max(s.x,s.y)-.01;")
    WriteLine(file,"float edge = .005+.05*min(.5*abs(fi-5.-dof),1.);")
    WriteLine(file,"acc += vec3(smoothstep(edge,-edge,d)*(r.x/(.25+.02*fi*Settings.g)));")
    WriteLine(file,"}")
    WriteLine(file,"gl_FragColor = vec4(vec3(acc),1.0);")
    //added an additive colour blend 
    WriteLine(file,"gl_FragColor = texture2D(texture0, uvVarying) + gl_FragColor;")
    //change for a subtractive colour blend 
    //WriteLine(file,"gl_FragColor = texture2D(texture0, uvVarying) - gl_FragColor;")
    //change for a multiply color blend
    //WriteLine(file,"gl_FragColor = texture2D(texture0, uvVarying) * gl_FragColor;")
    WriteLine(file,"}")
CloseFile(file)
 
endfunction
function CreateLenseFlare()
file = OpenToWrite("lensflare.ps")
    WriteLine(file,"uniform float agk_time;")
    WriteLine(file,"uniform sampler2D texture0;") //added this for colouring
    WriteLine(file,"varying mediump vec2 uvVarying;")
    WriteLine(file,"uniform vec2 agk_resolution;")
    WriteLine(file,"uniform mediump vec2 Position;") 
    WriteLine(file,"varying vec4 colorVarying;//")  //Added this colour varying variable so as it will blend with background
    WriteLine(file,"float noise(float t)")
    WriteLine(file,"{")
    WriteLine(file,"return texture2D(texture0,vec2(t, 0.0) / agk_resolution.xy).x;")
    WriteLine(file,"}")
    WriteLine(file,"float noise(vec2 t)")
    WriteLine(file,"{")
    WriteLine(file,"return texture2D(texture0,(t + vec2(agk_time)) / agk_resolution.xy).x;")
    WriteLine(file,"}")
    WriteLine(file,"vec3 lensflare(vec2 uv,vec2 pos)")
    WriteLine(file,"{")
    WriteLine(file,"vec2 main = uv-pos;")
    WriteLine(file,"vec2 uvd = uv*(length(uv));")
    WriteLine(file,"float ang = atan(main.y, main.x);")
    WriteLine(file,"float dist=length(main); dist = pow(dist,.1);")
    WriteLine(file,"float n = noise(vec2((ang-agk_time/9.0)*16.0,dist*32.0));")
    WriteLine(file,"float f0 = 1.0/(length(uv-pos)*16.0+1.0);")
    WriteLine(file,"f0 = f0+f0*(sin((ang+agk_time/18.0 + noise(abs(ang)+n/2.0)*2.0)*12.0)*.1+dist*.1+.8);")
    WriteLine(file,"float f2 = max(1.0/(1.0+32.0*pow(length(uvd+0.8*pos),2.0)),.0)*00.25;")
    WriteLine(file,"float f22 = max(1.0/(1.0+32.0*pow(length(uvd+0.85*pos),2.0)),.0)*00.23;")
    WriteLine(file,"float f23 = max(1.0/(1.0+32.0*pow(length(uvd+0.9*pos),2.0)),.0)*00.21;")
    WriteLine(file,"vec2 uvx = mix(uv,uvd,-0.5);")
    WriteLine(file,"float f4 = max(0.01-pow(length(uvx+0.4*pos),2.4),.0)*6.0;")
    WriteLine(file,"float f42 = max(0.01-pow(length(uvx+0.45*pos),2.4),.0)*5.0;")
    WriteLine(file,"float f43 = max(0.01-pow(length(uvx+0.5*pos),2.4),.0)*3.0;")
    WriteLine(file,"uvx = mix(uv,uvd,-.2);")
    WriteLine(file,"float f5 = max(0.01-pow(length(uvx+0.2*pos),5.5),.0)*2.0;")
    WriteLine(file,"float f52 = max(0.01-pow(length(uvx+0.4*pos),5.5),.0)*2.0;")
    WriteLine(file,"float f53 = max(0.01-pow(length(uvx+0.6*pos),5.5),.0)*2.0;")
    WriteLine(file,"uvx = mix(uv,uvd,-0.5);")
    WriteLine(file,"float f6 = max(0.01-pow(length(uvx-0.3*pos),1.6),.0)*6.0;")
    WriteLine(file,"float f62 = max(0.01-pow(length(uvx-0.325*pos),1.6),.0)*3.0;")
    WriteLine(file,"float f63 = max(0.01-pow(length(uvx-0.35*pos),1.6),.0)*5.0;")
    WriteLine(file,"vec3 c = vec3(.0);")
    WriteLine(file,"c.r+=f2+f4+f5+f6; c.g+=f22+f42+f52+f62; c.b+=f23+f43+f53+f63;")
    WriteLine(file,"c+=vec3(f0);")
    WriteLine(file,"return c;")
    WriteLine(file,"}")
    WriteLine(file,"vec3 cc(vec3 color, float factor,float factor2) // color modifier")
    WriteLine(file,"{")
    WriteLine(file,"float w = color.x+color.y+color.z;")
    WriteLine(file,"return mix(color,vec3(w)*factor,w*factor2);")
    WriteLine(file,"}")  
    WriteLine(file,"void main( )")
    WriteLine(file,"{")
    WriteLine(file,"vec2 uv = uvVarying*2.0;")
    WriteLine(file,"uv.x = uv.x - 1.0;")
    WriteLine(file,"uv.y = uv.y - 1.0;")
    WriteLine(file,"vec3 color = vec3(1.4,1.2,1.0)*lensflare(uv,(vec2((Position.xy/agk_resolution)*2.0)-1.0));")
    WriteLine(file,"color = cc(color,.5,.1);")
    WriteLine(file,"gl_FragColor = vec4(color,2.0);")
    //added an additive colour blend 
    WriteLine(file,"gl_FragColor = texture2D(texture0, uvVarying) + gl_FragColor;")
    //change for a subtractive colour blend 
    //WriteLine(file,"gl_FragColor = texture2D(texture0, uvVarying) - gl_FragColor;")
    //change for a multiply color blend
    //WriteLine(file,"gl_FragColor = texture2D(texture0, uvVarying) * gl_FragColor;")
    
    WriteLine(file,"}")
CloseFile(file)
endfunction
Posted: 22nd May 2017 10:32
Ive been playing around with Blendmans texture blending code as I was hoping with modifications the gl_FragColor could be used to output a morphed texture
+ Code Snippet
gl_FragColor = (texture2D(texture0, uvVarying) + texture2D(texture1, uvVarying))/2.0;
I believe averages the color between texture0 and texture1
so I end up with an image 50percent texture0 and 50 percent texture1.(correct me if I'm wrong but that's what I think I'm seeing)
my question is how could I add and averaging factor in the following so that it might morph 10percent if chosen 20percent or even 100 percent and totally replace
with the second texture

test code
+ Code Snippet
SetWindowSize( 1024, 768, 0 )
SetVirtualResolution( 1024, 768 )
// create a sphere to see the texture
CreateObjectSphere(1,20,40,60)
//CreateObjectplane(1,1024,768)

// position and orientate the camera
SetCameraPosition(1,0,100,-200)
SetCameraLookAt(1,0,0,0,0)
SetGenerateMipmaps(1)

LoadImage(1,"texture.jpg") // no need for alpha with this one
LoadImage(2,"texture2.png") // with alpha ;)

CreatePointLight(1,-5,5,5,500,200,200,200)

// initial rotation values
angle_x# = 0.0

// main loop
SetObjectRotation(1,-60,-60,0.0)
SetObjectScale(1,4,2,2)
createShader()
LoadShader(3, "vertex.vs", "fragment.ps")
SetObjectShader(1,3)
SetObjectImage(1,1,0)
SetObjectImage(1,2,1)

do

	angle_x# = angle_x# + 1 
	SetObjectRotation(1,0, angle_x#,0.0)
	sync()

loop


shaders
+ Code Snippet
function createShader()
file = OpenToWrite("vertex.vs")
WriteLine(file,"attribute vec3 position;")
WriteLine(file,"attribute vec2 uv;")
WriteLine(file,"varying vec2 uvVarying;")
WriteLine(file,"uniform vec4 uvBounds0;")
WriteLine(file,"uniform mat4 agk_World;")
WriteLine(file,"uniform mat4 agk_ViewProj;")
WriteLine(file,"void main()")
WriteLine(file,"{")
WriteLine(file,"vec4 pos = agk_World * vec4(position,1);")
WriteLine(file,"gl_Position = agk_ViewProj * pos;")
WriteLine(file,"uvVarying = uv * uvBounds0.xy + uvBounds0.zw;")
WriteLine(file,"}")
CloseFile(file)

file = OpenToWrite("fragment.ps")
WriteLine(file,"uniform sampler2D texture0;")
WriteLine(file,"uniform sampler2D texture1;")
WriteLine(file,"varying vec2 uvVarying;")
WriteLine(file,"void main()")
WriteLine(file,"{")
WriteLine(file,"gl_FragColor = (texture2D(texture0, uvVarying) + texture2D(texture1, uvVarying))/2.0;")
WriteLine(file,"}")
CloseFile(file)
endfunction
Posted: 22nd May 2017 11:28
you could alter this line
WriteLine(file,"gl_FragColor = (texture2D(texture0, uvVarying) + texture2D(texture1, uvVarying))/2.0;")

This is where it's sets the 50% blend between each pixel colour.