Ive been playing with shaders again and reminded again I know very little of them lol
The below example code adjusts the colour of the objects depending on their y location ideally I just want
it to set the alpha channel but having probs with that and this is what I have. (writes shader from the code)
SetWindowSize( 1024, 768, 0 )
SetVirtualResolution( 1024, 768 )
#constant fadeShader =1
SetFogMode( 1 )
SetFogColor(10,10,10)
SetFogRange(6,16)
createTexture()
createShader()
LoadShader(fadeShader,"fade.vs", "fade.ps")
num=1
for y = 1 to 20
for x = y to 20-y
for z = y to 20-y
CreateObjectBox(num,1,1,1)
SetObjectImage( num, random(1,20), 0)
SetObjectPosition(num,x-10,y,-z)
SetObjectShader( num, fadeShader)
inc num
next z
next x
next y
mask=CreateObjectBox(1.1,1.1,1.1)
SetObjectColor(mask,255,255,255,180)
SetObjectTransparency(mask,1)
SetObjectFogMode(mask,0)
do
selectedObj=mouseOver()
if GetObjectExists(selectedObj)
SetObjectVisible( mask, 1 )
SetObjectPosition (mask,GetObjectX(selectedObj),GetObjectY(selectedObj),GetObjectZ(selectedObj))
lastObj=selectedObj
endif
if GetObjectExists(lastObj) and lastObj<>selectedObj
SetObjectVisible( mask, 0 )
lastObj=selectedObj
endif
if getpointerpressed()=1
end
endif
//SetShaderConstantByName(fadeShader,"pointPos",1,1,1,1)
SetShaderConstantByName(fadeShader,"myColor",3,3,3,0.2)
SetShaderConstantByName(fadeShader,"myCoord",1,GetObjectY(selectedObj),1,1)
print(Get3DVectorYFromScreen(GetPointerX(),GetPointerY()))
sync()
loop
function createTexture ()
for num = 1 to 20
r=random(50,255): g=random(50,255):b=random(50,255)
DrawBox(0,0,32,32,MakeColor(r,g,b),MakeColor(r,g,b),MakeColor(r,g,b),MakeColor(r,g,b),1)
render()
GetImage(num,0,0,32,32)
next num
endfunction
function mouseOver()
myX as float
myY as float
my3dX as float
my3dY as float
my3dZ as float
rayStartX as float
rayStartY as float
rayStartZ as float
rayEndX as float
rayEndY as float
rayEndZ as float
myX=GetPointerX()
myY=GetPointerY()
my3dX=Get3DVectorXFromScreen(myX,myY)
my3dY=Get3DVectorYFromScreen(myX,myY)
my3dZ=Get3DVectorZFromScreen(myX,myY)
rayStartX=my3dX+GetCameraX(1)
rayStartY=my3dY+GetCameraY(1)
rayStartZ=my3dZ+GetCameraZ(1)
rayEndX=800*my3dX+GetCameraX(1)
rayEndY=800*my3dY+GetCameraY(1)
rayEndZ=800*my3dZ+GetCameraZ(1)
theObjectHit= ObjectRayCast(0,rayStartX,rayStartY,rayStartZ,rayEndX,rayEndY,rayEndZ)
endfunction theObjectHit
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,"uniform mat4 view_matrix;)
//WriteLine(file,"uniform mat4 view_matrix;")
//WriteLine(file,"uniform mat4 model_matrix;")
WriteLine(file,"mediump vec3 GetVSLighting( mediump vec3 normal, highp vec3 pos );")
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);")
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 vec4 myColor;")
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.r-=(myColor.x*myColor.w);")
WriteLine(file," colorA.g-=(myColor.y*myColor.w);")
WriteLine(file," colorA.b-=(myColor.z*myColor.w);")
WriteLine(file," colorA = clamp(colorA,0.0,1.0);") //restricts color range
WriteLine(file,"}")
WriteLine(file,"mediump vec3 color = ApplyFog( colorA, posVarying );")
WriteLine(file,"gl_FragColor = vec4(color,1.0);")
WriteLine(file,"}")
CloseFile(file)
endfunction
PS the idea is to give objects partial visibility