TGC Codebase Backup



Sky System by Webber

11th Apr 2017 8:39
Summary

Creates a day night sky system.



Description

Uses the built in procedural AGK sky box system in order to create a day night sky cycle. In testing a procedural AGK sky box uses 4900 polygons, so if you are making a game that has indoor scenes it would be wise to toggle the sky box visibility off when it is out of view. For simplicity the day night cycle is circular in motion and the moon orbits on the opposite side of the sun. The moon does not have moon cycles, it is always a full moon, but it could be replaced with your own image. The clouds move on the UVs of a ellipsoid but do not change in density. The image used for the clouds could be replaced with your own in order to make denser or less dense of clouds. The sun and moon are also used for the environments directional lighting and change in color based on their positions. This sky system can add a lot to the visual aspect of your game and is mobile friendly.Controls:Moment: ASDW, Space, and CtrlSky time adjust: T, Shift+TExit: EscNote: Controls and screen orientation are designed for PC use (RawKeys and RawMouse) and will need to be adjusted for mobile game development.Runs in full screen 1080p



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    ` This code was downloaded from The Game Creators
` It is reproduced here with full permission
` http://www.thegamecreators.com


SetErrorMode(2)

SetWindowTitle( "Sky System" )
SetWindowSize( 1920, 1080, 1 )

SetVirtualResolution( 1920, 1080 )
SetOrientationAllowed( 0, 0, 1, 0 )
SetVSync(1)```
UseNewDefaultFonts( 1 ) 
setSkyBoxVisible(1)
setSkyBoxSunSize(6,700)

SetDefaultWrapU(1) : SetDefaultWrapV(1) 

gosub Load_Sky

textTime = createText("Controls: Movement with ASDW,Space & Ctrl to move up and down,T & Shift+T to change time,Esc to exit")
setTextSize(textTime,24)
setTextPosition(textTime,0,0)
textTime = createText("")
setTextSize(textTime,24)
setTextPosition(textTime,0,24)
textPoly = createText("")
setTextSize(textPoly,24)
setTextPosition(textPoly,0,48)
textFPS = createText("")
setTextSize(textFPS,24)
setTextPosition(textFPS,0,72)


setCameraPosition(1,0,2,0)
setCameraRotation(1,0,90,0)

cax# = 0
cay# = 0
caz# = 0

DXH = getScreenBoundsRight()/2
DYH = getScreenBoundsBottom()/2

setCameraRange(1,0.01,6000)
setFogRange(128,2048)
setFogMode(1)

dayTime# = 3600+91

cxs# = 0
cys# = 0
czs# = 0

setRawMouseVisible(0)




do
	
	gosub SkySystem
	gosub Controls
	
	num = Mod(dayTime#*4,1440)/60
	num2 = Mod(Mod(dayTime#*4,1440),60)
	
	if num > 12
num = num -12
if num2 > 9
st$ = "Time: "+str(num)+":"+str(num2)+" PM: "+str(mod(dayTime#,360))+"º"
else
st$ = "Time: "+str(num)+":0"+str(num2)+" PM: "+str(mod(dayTime#,360))+"º"
endif
	else
if num2 > 9
st$ = "Time: "+str(num)+":"+str(num2)+" AM: "+str(mod(dayTime#,360))+"º"
else
st$ = "Time: "+str(num)+":0"+str(num2)+" AM: "+str(mod(dayTime#,360))+"º"
endif
	endif
	
	setTextString(textTime,st$)
	
setTextString(textPoly,"Tris: "+str(GetPolygonsDrawn()))
	
setTextString(textFPS,"FPS: "+str(ScreenFPS()))
	
if getRawKeyState(27) then end`#
Sync()
loop















SkySystem:
	
dayTime# = dayTime# + 0.0042`time rate of speed
if getRawKeyState(16) = 1
if getRawKeyState(84) = 1 then dayTime# = dayTime# - 0.5
	else
if getRawKeyState(84) = 1 then dayTime# = dayTime# + 0.5
	endif
	
setObjectUVOffset(clouds,0,dayTime#*0.001,0)
	setObjectUVOffset(clouds2,0,dayTime#*0.003,0.3)
	setObjectUVOffset(clouds3,0,dayTime#*0.008,0.6)
	
setObjectPosition(clouds,getCameraX(1),-20,getCameraZ(1))
	setObjectPosition(clouds2,getCameraX(1),0,getCameraZ(1))
	setObjectPosition(clouds3,getCameraX(1),20,getCameraZ(1))
	
	
	
	num# = -cos(dayTime#)*3
	if num# >= 0`#
if num# < 0.04 then num# = 0.04
if num# > 1 then num# = 1

setSunDirection(cos(dayTime#+90),sin(dayTime#+90),0)
setSkyBoxSkyColor(128*num#,196*num#,255*num#)

if num# = 1
r = 255
g = 255
b = 255
else
r = (num#*1.053-0.05)*1024
g = (num#*1.053-0.05)*512
b = (num#*1.053-0.05)*255
if r > 255 then r = 255
if g > 255 then g = 255
if b > 255 then b = 255
endif

setAmbientColor(32+r*0.15,32+g*0.15,32+b*0.15)
setSkyBoxHorizonColor(r,g,b)
setSkyBoxSunColor(r,g,b)
setSunColor(r,g,b)
setFogColor(r,g,b)

if b = 255 
if getObjectVisible(starsSphere)
setObjectVisible(starsSphere,0)
endif
else
if getObjectVisible(starsSphere) = 0
setObjectVisible(starsSphere,1)
endif
endif

setObjectColor(starsSphere,255,255,255,-b*5+255)
setObjectRotation(starsSphere,0,0,dayTime#)
setObjectPosition(starsSphere,getCameraX(1),getCameraY(1),getCameraZ(1))

setAmbientColor(r/2.5+2,g/2.5+4,b/2.5+8)

if getObjectVisible(moonGlow) then setObjectVisible(moonGlow,0)

	else
setSkyBoxSkyColor(5,8,10)

setSkyBoxHorizonColor(2,1,0)
setFogColor(2,1,0)

setObjectRotation(starsSphere,0,0,dayTime#)
setObjectPosition(starsSphere,getCameraX(1),getCameraY(1),getCameraZ(1))

setSkyBoxSunColor(16,32,64)
num# = sin(dayTime#-90)
setSunColor(16+num#*13,32+num#*26,64+num#*52)

setSunDirection(cos(dayTime#-90),sin(dayTime#-90),0)

setObjectPosition(moonGlow,getCameraX(1)+cos(dayTime#+90)*4400,getCameraY(1)+sin(dayTime#+90)*4400,getCameraZ(1))
setObjectLookAt(moonGlow,getCameraX(1),getCameraY(1),getCameraZ(1),0)

if getObjectVisible(moonGlow) = 0 then setObjectVisible(moonGlow,1)

	endif

return
































Load_Sky:

for y = 0 to 7
for x = 0 to 7
	drawLine(x*2,y*2,x*2,y*2,128,128,128)
	drawLine(x*2+1,y*2+1,x*2+1,y*2+1,128,128,128)
	
	drawLine(x*2+1,y*2,x*2+1,y*2,64,64,64)
	drawLine(x*2,y*2+1,x*2,y*2+1,64,64,64)
next x
next y

gridImg = getImage(0,0,16,16)
setImageMagFilter(gridImg,0)
setImageMinFilter(gridImg,0)

gridPlane = createObjectPlane(4096,4096)

setObjectRotation(gridPlane,90,0,0)
setObjectImage(gridPlane,gridImg,0)
setObjectUVScale(gridPlane,0,512,512)








starsSphere = createObjectSphere(10000,8,6)

color = 0
ClearScreen()
drawBox(0,0,256,256,color,color,color,color,0)
for x = 0 to 64
	num = random2(0,255)
	num2 = random2(0,255)
	color = MakeColor(255,255,255)+random2(0,255)*256*256*256
	drawLine(num,num2,num,num2,color,color)
next x
starsImg = getImage(0,0,256,256)

`starsImg = loadImage("Stars.png")
setObjectImage(starsSphere,starsImg,0)
setImageMagFilter(starsImg,0)
setImageMinFilter(starsImg,0)

setObjectVisible(starsSphere,0)
setObjectScale(starsSphere,1,-1,1)
setObjectUVScale(starsSphere,0,8,8)
setObjectLightMode(starsSphere,0)
setObjectTransparency(starsSphere,1)
setObjectFogMode(starsSphere,0)








moonGlow = createObjectPlane(400,400)

color = 0
ClearScreen()
drawBox(0,0,256,256,color,color,color,color,0)

for i = 1 to 51
Draw_Circle(128,128,128-i*1.1,makeColor(255,255,255)+256*256*256*i*5)
next i

for j = 15 to 5 step -5
for i = 0 to 250-j*10
rad2 = random2(0,60)
numb = random2(140,180)+rad2
num2 = random2(0,360)
num = cos(num2)*rad2+128
num2 = sin(num2)*rad2+128
Draw_Circle(num,num2,random2(5,j),makeColor(numb,numb,numb))
next i
next j
moonGlowImg = getImage(0,0,256,256)

setObjectImage(moonGlow,moonGlowImg,0)
setObjectLightMode(moonGlow,0)
setObjectFOgMode(moonGlow,0)
setObjectTransparency(moonGlow,1)
setObjectColorEmissive(moonGlow,5,20,50)










clouds = createObjectSphere(10000,8,4)

color = 0
ClearScreen()
drawBox(0,0,256,256,color,color,color,color,0)
for j = 1 to 80
x = random2(25,225)
y = random2(25,225)
offset = random2(1,8)
for i = 1 to 5
Draw_Circle(x,y,12-i*1.1,makeColor(255,255,255)+256*256*256*i*20)
next i
next j
cloudsImg = getImage(0,0,256,256)

setObjectImage(clouds,cloudsImg,0)
setObjectScale(clouds,1,1,0.04)
setObjectRotation(clouds,90,0,0)
setObjectPosition(clouds,0,0,0)
setObjectUVScale(clouds,0,3,3)
setObjectTransparency(clouds,1)

setObjectCullMode(clouds,0)
clouds2 = cloneObject(clouds)
clouds3 = cloneObject(clouds)

return





















Controls:
	
mmx = mx-getRawMouseX()
	mmy = my-getRawMouseY()
	setRawMousePosition(DXH,DYH)
	
mx = getRawMouseX()
	my = getRawMouseY()
	mz = GetRawMouseWheelDelta()
	
cax# = cax# - mmy*0.1
	cay# = cay# - mmx*0.1
	
	setCameraRotation(1,cax#,cay#,0)
	
cxs# = cxs# * 0.95
	cys# = cys# * 0.95
	czs# = czs# * 0.95
	
	moveCameraLocalX(1,cxs#)
	moveCameraLocalY(1,cys#)
	moveCameraLocalZ(1,czs#)
	
if getRawKeyState(16)`
	num = 2
	else
num = 1
	endif
	
	if getRawKeyState(68) `DMoves Right
cxs# = cxs# + 0.02*num
	endif
	
	if getRawKeyState(65) `AMoves Left
cxs# = cxs# - 0.02*num
	endif
	
	if getRawKeyState(87) `WMoves Forward
czs# = czs# + 0.02*num
	endif
	
	if getRawKeyState(83) `SMoves Backward
czs# = czs# - 0.02*num
	endif
	
	if getRawKeyState(32) `SpaceMoves Up
cys# = cys# + 0.02*num
	endif
	
	if getRawKeyState(17) `CtrlMoves Down
cys# = cys# - 0.02*num
	endif
	
return


function Draw_Circle(x,y,radius,color)
	
	for ny = -radius to radius
	nx = sqrt(-ny*ny+radius*radius)
	drawLine(nx+x,ny+y,-nx+x,ny+y,color,color)
	next ny
	
endfunction