The following shader changes with an example ready to run
#constant KEY_LEFT = 37 //the left arrow key scancode
#constant KEY_UP = 38 //the up arrow key scancode
#constant KEY_RIGHT = 39 //the right arrow key scancode
#constant KEY_DOWN = 40 //the down arrow key scancode
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default font
x#=0:y#=0:z#=0
createShader()
obj=CreateObjectBox(2,15,2)
SetObjectImage(obj,createTexture(makecolor(255,255,255)),0)
SetObjectPosition(obj,x#,y#,z#)
SetObjectTransparency(obj,1)
SetObjectDepthWrite(obj,1)
fadeShader=LoadShader("fade.vs","fade.ps")
SetObjectShader(obj,fadeShader)
SetShaderConstantByName(fadeShader,"alpha",0.2,0,0,0)
//SetShaderConstantByName(fadeShader,"myColor",3,3,3,3)
do
if GetRawKeyState(KEY_UP)
y#=y#+.2
SetObjectPosition(obj,x#,y#,z#)
endif
if GetRawKeyState(KEY_DOWN)
y#=y#-.2
SetObjectPosition(obj,x#,y#,z#)
endif
//The idea this should be calculated from the player position
SetShaderConstantByName(fadeShader,"myCoord",-5,5,1,1) //the y coords you want it to go transparent notice there are two
sync()
loop
function createTexture(color)
SetClearColor(0,0,0)
ClearScreen()
Render()
drawbox(0,0,10, 10, color, color,color,color, 1)
Render()
//img = getimage(0,0,sizex, sizey)
ImgId=getimage(0,0,10, 10)
Swap()
endfunction imgId
function createShader()
file = OpenToWrite("fade.vs")
WriteLine(file,"attribute highp vec3 position;")
WriteLine(file,"attribute mediump vec3 normal;")
WriteLine(file,"attribute mediump vec2 uv;")
WriteLine(file,"varying highp vec3 posVarying;")
WriteLine(file,"varying mediump vec3 normalVarying;")
WriteLine(file,"varying mediump vec2 uvVarying;")
WriteLine(file,"varying mediump vec3 lightVarying;")
WriteLine(file,"uniform highp mat3 agk_WorldNormal;")
WriteLine(file,"uniform highp mat4 agk_World;")
WriteLine(file,"uniform highp mat4 agk_ViewProj;")
WriteLine(file,"uniform mediump vec4 uvBounds0;")
WriteLine(file,"mediump vec3 GetVSLighting( mediump vec3 normal, highp vec3 pos );")
//added this sectio for bone animations to work proper
WriteLine(file,"attribute highp vec4 boneweights;")
WriteLine(file,"attribute mediump vec4 boneindices;")
WriteLine(file,"uniform highp vec4 agk_bonequats1[30];")
WriteLine(file,"uniform highp vec4 agk_bonequats2[30];")
WriteLine(file,"highp vec3 transformDQ( highp vec3 p, highp vec4 q1, highp vec4 q2 )")
WriteLine(file,"{")
WriteLine(file," p += 2.0 * cross( q1.xyz, cross(q1.xyz, p) + q1.w*p );")
WriteLine(file," p += 2.0 * (q1.w*q2.xyz - q2.w*q1.xyz + cross(q1.xyz,q2.xyz));")
WriteLine(file," return p;")
WriteLine(file,"}")
WriteLine(file,"void main()")
WriteLine(file,"{")
WriteLine(file," uvVarying = uv * uvBounds0.xy + uvBounds0.zw;")
WriteLine(file," highp vec4 pos = agk_World * vec4(position,1.0);")
WriteLine(file," gl_Position = agk_ViewProj * pos;")
WriteLine(file," mediump vec3 norm = normalize(agk_WorldNormal * normal);")
//added this section for bone animation to animate properly aswell
WriteLine(file," highp vec4 q1 = agk_bonequats1[ int(boneindices.x) ] * boneweights.x;")
WriteLine(file," q1 += agk_bonequats1[ int(boneindices.y) ] * boneweights.y;")
WriteLine(file," q1 += agk_bonequats1[ int(boneindices.z) ] * boneweights.z;")
WriteLine(file," q1 += agk_bonequats1[ int(boneindices.w) ] * boneweights.w;")
WriteLine(file," highp vec4 q2 = agk_bonequats2[ int(boneindices.x) ] * boneweights.x;")
WriteLine(file," q2 += agk_bonequats2[ int(boneindices.y) ] * boneweights.y;")
WriteLine(file," q2 += agk_bonequats2[ int(boneindices.z) ] * boneweights.z;")
WriteLine(file," q2 += agk_bonequats2[ int(boneindices.w) ] * boneweights.w;")
WriteLine(file," highp float len = 1.0/length(q1);")
WriteLine(file," q1 *= len;")
WriteLine(file," q2 = (q2 - q1*dot(q1,q2)) * len;")
WriteLine(file,"// highp vec4 pos = vec4( transformDQ(position,q1,q2), 1.0 );")
WriteLine(file," posVarying = pos.xyz;")
WriteLine(file," normalVarying = norm;")
WriteLine(file," lightVarying = GetVSLighting( norm, posVarying );")
WriteLine(file,"}")
CloseFile(file)
//pixel shader
file = OpenToWrite("fade.ps")
WriteLine(file,"uniform sampler2D texture0;")
WriteLine(file,"varying highp vec3 posVarying;")
WriteLine(file,"varying mediump vec3 normalVarying;")
WriteLine(file,"varying mediump vec2 uvVarying;")
WriteLine(file,"varying mediump vec3 lightVarying;")
WriteLine(file,"mediump vec3 GetPSLighting( mediump vec3 normal, highp vec3 pos );")
WriteLine(file,"mediump vec3 ApplyFog( mediump vec3 color, highp vec3 pointPos );")
WriteLine(file,"uniform mediump vec3 agk_MeshDiffuse;")
WriteLine(file,"uniform mediump vec3 myCoord;")
WriteLine(file,"varying vec4 verpos;")
WriteLine(file,"void main()")
WriteLine(file,"{")
WriteLine(file,"mediump vec3 norm = normalize(normalVarying);")
WriteLine(file,"mediump vec3 light = lightVarying + GetPSLighting( norm, posVarying );")
WriteLine(file,"vec3 colorA = texture2D(texture0, uvVarying).rgb*light *agk_MeshDiffuse;")
WriteLine(file,"if (posVarying.y>myCoord.y)")
WriteLine(file,"{")
WriteLine(file," colorA = clamp(colorA,0.0,1.0);") //restricts color range
WriteLine(file," mediump vec3 color = ApplyFog( colorA, posVarying );")
WriteLine(file," gl_FragColor = vec4(color,0.1);")
WriteLine(file,"}else{")
WriteLine(file," if (posVarying.y<myCoord.x)")
WriteLine(file," {")
WriteLine(file," mediump vec3 color = ApplyFog( colorA, posVarying );")
WriteLine(file," gl_FragColor = vec4(color,0.1);")
WriteLine(file," }else{")
WriteLine(file," mediump vec3 color = ApplyFog( colorA, posVarying );")
WriteLine(file," gl_FragColor = vec4(color,1.0);")
WriteLine(file," }")
WriteLine(file,"}")
WriteLine(file,"}")
CloseFile(file)
endfunction