TGC Codebase Backup



3D dissolve shader by fubarpk

11th Apr 2018 6:52
Summary

allows a method of color tinting a group of objects a set amount in one step



Description

allows a method of color tinting a group of objects a set amount in one step



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    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)

fadeIn()
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
		fadeOut()
		end
	endif
	SetShaderConstantByName(fadeShader,"pointPos",1,1,1,1)
	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 fadeIn()
resetTimer()
time#=Timer()
repeat
	time#=Timer()
	r#=7-time#
	g#=7-time#
	b#=7-time#
	SetShaderConstantByName(fadeShader,"myColor",r#,g#,b#,0.2) //red green blue factor
	sync()	
until timer()>7.0
endfunction	

function fadeOut()
resetTimer()
time#=Timer()
repeat
	time#=Timer()
	r#=time#
	g#=time#
	b#=time#
	SetShaderConstantByName(fadeShader,"myColor",r#,g#,b#,0.2) //red green blue factor
	sync()	
until timer()>7.0
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,"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 vec4 agk_MeshDiffuse;")
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,"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,"mediump vec3 color = ApplyFog( colorA, posVarying );")
WriteLine(file,"gl_FragColor = vec4(color,1.0);")
WriteLine(file,"}")
CloseFile(file)
endfunction