Skip to content

Instantly share code, notes, and snippets.

@adamveld12
Created November 29, 2014 02:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamveld12/989acf9e07972565fa55 to your computer and use it in GitHub Desktop.
Save adamveld12/989acf9e07972565fa55 to your computer and use it in GitHub Desktop.
Day/Night cycle UE3 unrealscript file. Everything is self contained and works out of the box.
/******************************************************************************
* DynamicDayNightSystem.uc
* Copyright 2011 Adam Veldhousen
*
* Defines a basic real time animated lightsource with fog, completely customizable. You supply your own material
* and sky box static mesh and this class takes care of the rest.
*
******************************************************************************/
class DynamicDayNightSystem extends Actor hidecategories(Object, Debug, Advanced, Physics, Collision, Attachment)
ClassGroup(Lights, Systems)
placeable;
// Sky editables
var(SkyboxSettings) const editconst DominantDirectionalLightComponent DynamicLightSource;
var(SkyboxSettings) const editconst SkyLightComponent AmbientLight;
var(SkyboxSettings) const editconst ExponentialHeightFogComponent SkyFog;
var(SkyboxSettings) StaticMeshComponent SkyBoxStaticMesh;
struct Time_Of_Day
{
/* Hour in day */
var() int hour;
/* Minute in day */
var() int minute;
/* Second in day */
var() int second;
};
/******************************************
* Instanced Variables
* ****************************************/
/** The current time of day */
var(TimeOfDaySettings) Time_Of_Day Time;
/** The speed in which time increments (Defaults to 1.0 = 24 hour day (yes it will take 24 hours of realtime for a ingame day to elapsed)) */
var(TimeOfDaySettings) float TimeDilation;
/** Prints debug info to the screen */
var(TimeOfDaySettings) bool bDebugOutput;
/** If time should be frozen, false enables dynamic day cycle */
var(TimeOfDaySettings) bool bFrozenTime;
var float elapsed, timeRecord;
var const float ANGLE_PER_TICK;
/*************************************************************************************************
* Events
* *************************************************************************************************/
event PostBeginPlay()
{
`log("In PostbeginPlay for System");
super.PostBeginPlay();
// This gets our initial rotation for the editor's preset time
UpdateScene();
}
event Tick(float delta)
{
elapsed += delta;
if(!bFrozenTime && elapsed >= TimeDilation)
{
IncrementTime();
UpdateScene();
if(bDebugOutput)
{
WorldInfo.Game.Broadcast(self, "Time"@ToString());
}
elapsed = 0;
}
}
/****************************************************************************************************
* Functions
* **************************************************************************************************/
/* Increments the time of day by 1 second */
function IncrementTime()
{
// add seconds together
Time.second = (1 + Time.second);
// then add minutes
Time.minute = Time.minute + (Time.second / 60);
// then add hours
Time.hour = Time.hour + (Time.minute / 60);
// next we modulus the times to get them back into their proper ranges
Time.second = Time.second % 60.0;
Time.minute = Time.minute % 60.0;
Time.hour = Time.hour % 24.0;
}
/* Sets the time of day value, validates it, and then updates the scene */
function SetTime(Time_Of_Day timeOfDay)
{
self.Time = timeOfDay;
UpdateScene();
}
/* Returns a concatenated string representing the current time */
function string ToString()
{
return Time.hour@":"@Time.minute@":"@Time.second;
}
/* Updates the scene based on the current Time */
function UpdateScene()
{
UpdateDirectionalLightRotation();
UpdateSkyAnimations();
}
/* Updates the directional light to point in the correct direction based on the current time */
function UpdateDirectionalLightRotation()
{
local float totalTimeInSeconds;
local Rotator CalculatedRotation;
totalTimeInSeconds = Time.hour * 3600 + Time.minute * 60 + Time.second;
CalculatedRotation.Pitch = (-ANGLE_PER_TICK * TimeDilation * totalTimeInSeconds) + (65535 / 4.0f);
setRotation(CalculatedRotation);
}
/* For updating sky materials */
function UpdateSkyAnimations()
{
}
DefaultProperties
{
begin object class=SkylightComponent Name=SkyLight0
bEnabled=true
CastDynamicShadows=false
CastStaticShadows=true
end object
begin object class=DominantDirectionalLightComponent Name=DominantDirectionalLightComponent0
WholeSceneDynamicShadowRadius=1024
UseDirectLightMap=false
CastStaticShadows=false
CastDynamicShadows=true
LightingChannels=(BSP=TRUE,Static=TRUE,Dynamic=TRUE,bInitialized=TRUE)
end object
begin object class=StaticMeshComponent Name=StaticMeshComponent0
StaticMesh=StaticMesh'JW_LightEffects.SM_GEN_SkyDome_FullSphere'
bAcceptsDynamicDominantLightShadows=false
bCastDynamicShadow=false
CastShadow=false
bCastStaticShadow=false
//MUST BE TRUE in order to use lighting/camera vector dot to derive sun position
bAcceptsLights=true
bAcceptsDynamicLights=false
bAllowAmbientOcclusion=false
bIgnoreOwnerHidden=true
CollideActors=false
LightingChannels=(Skybox=true)
AbsoluteRotation=true
end object
begin object class=ExponentialHeightFogComponent name=ExpHeightFogComp0
bEnabled=true
StartDistance=1024
FogHeightFalloff=0.05f
end object
DynamicLightSource=DominantDirectionalLightComponent0
SkyBoxStaticMesh=StaticMeshComponent0
SkyFog=ExpHeightFogComp0
AmbientLight=Skylight0
Components.Add(DominantDirectionalLightComponent0)
Components.Add(StaticMeshComponent0)
Components.Add(ExpHeightFogComp0)
Components.Add(Skylight0)
Begin Object Class=SpriteComponent Name=Sprite
Sprite=Texture2D'EditorResources.LightIcons.Light_Directional_Stationary_DynamicsAndStatics'
Scale=0.25f
HiddenGame=true
AlwaysLoadOnClient=False
AlwaysLoadOnServer=False
SpriteCategoryName="Lighting"
End Object
Begin Object Class=ArrowComponent Name=ArrowComponent0
ArrowColor=(R=255,G=0,B=0)
bTreatAsASprite=True
AlwaysLoadOnClient=False
AlwaysLoadOnServer=False
SpriteCategoryName="Lighting"
End Object
Components.Add(Sprite)
Components.Add(ArrowComponent0)
Rotation=(Yaw=0,Pitch=0,Roll=0)
bStatic=false
bHidden=TRUE
bNoDelete=TRUE
bEdShouldSnap=true
bFrozenTime=true
bDebugOutput=false
Time=(second=0, minute=0,hour=12)
TimeDilation=1.0f
ANGLE_PER_TICK=0.75851f;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment