use ambient brightness for time of day

stable-6.1
Maikel de Vries 2015-04-05 20:05:58 +02:00
parent e9e6fd9dba
commit 871873b9bc
3 changed files with 108 additions and 126 deletions

View File

@ -2,8 +2,4 @@
id=Environment_Celestial
Version=6,0
Category=C4D_Environment|C4D_StaticBack
Width=1
Height=1
Vertices=1
VertexY=0
Mass=1
Picture=0,0,128,128

View File

@ -2,8 +2,4 @@
id=Environment_Time
Version=6,0
Category=C4D_StaticBack|C4D_Environment
Width=1
Height=1
Vertices=1
VertexY=0
Mass=1
Picture=0,0,128,128

View File

@ -1,55 +1,57 @@
/**--
/**
Time Controller
Author:Ringwall
Creates time based on the 24-hour time scheme.
Time is computed in minutes, which are by default
1/2 a second in real life (18 frames). This will
make each complete day/night cycle last 12 minutes
in real life.
--*/
@author Ringwall, Maikel
*/
local time_set;
local time;
local advance_seconds_per_tick;
/** Sets the current time using a 24*60 minute clock scheme. */
/*-- Interface --*/
// Sets the current time using a 24*60 minute clock scheme.
public func SetTime(int to_time)
{
// Set time.
time = (to_time*60) % (24 * 60 * 60);
// hide celestials during day
if(Inside(time, time_set["SunriseEnd"], time_set["SunsetStart"]))
time = (to_time * 60) % (24 * 60 * 60);
// Hide celestials during day.
if (Inside(time, time_set.sunrise_end, time_set.sunset_start))
HideCelestials();
else
ShowCelestials();
// Adjust to time.
AdjustToTime();
return;
}
/** Returns the time in minutes. */
// Returns the time in minutes.
public func GetTime()
{
return time / 60;
}
/** Sets the number of seconds the day will advance each tick (10 frames).
Setting to 0 will stop day-night cycle. Default is 30 seconds. */
// Sets the number of seconds the day will advance each tick (10 frames).
// Setting to 0 will stop day-night cycle. Default is 30 seconds.
public func SetCycleSpeed(int seconds_per_tick)
{
advance_seconds_per_tick = seconds_per_tick;
}
/** Returns the number of seconds the day advances each tick (10 frames). */
// Returns the number of seconds the day advances each tick (10 frames).
public func GetCycleSpeed()
{
return advance_seconds_per_tick;
}
local time_set;
/*-- Code -- */
protected func Initialize()
{
@ -58,10 +60,10 @@ protected func Initialize()
return RemoveObject();
time_set = {
SunriseStart = 10800, // 3:00
SunriseEnd = 32400, // 9:00
SunsetStart = 54000, // 15:00
SunsetEnd = 75600, // 21:00
sunrise_start = 10800, // 3:00
sunrise_end = 32400, // 9:00
sunset_start = 54000, // 15:00
sunset_end = 75600, // 21:00
};
// Create moon and stars.
@ -77,12 +79,13 @@ protected func Initialize()
// Add effect that controls time cycle.
SetCycleSpeed(30);
AddEffect("IntTimeCycle", this, 100, 10, this);
return;
}
public func IsDay()
{
var day_start = (time_set["SunriseStart"] + time_set["SunriseEnd"]) / 2;
var day_end = (time_set["SunsetStart"] + time_set["SunsetEnd"]) / 2;
var day_start = (time_set.sunrise_start + time_set.sunrise_end) / 2;
var day_end = (time_set.sunset_start + time_set.sunset_end) / 2;
if (Inside(time, day_start, day_end))
return true;
return false;
@ -90,8 +93,8 @@ public func IsDay()
public func IsNight()
{
var night_start = (time_set["SunsetStart"] + time_set["SunsetEnd"]) / 2;
var night_end = (time_set["SunriseStart"] + time_set["SunriseEnd"]) / 2;
var night_start = (time_set.sunset_start + time_set.sunset_end) / 2;
var night_end = (time_set.sunrise_start + time_set.sunrise_end) / 2;
if (Inside(time, night_start, night_end))
return true;
return false;
@ -102,7 +105,7 @@ private func PlaceStars()
// since stars are almost completely parallax (=in screen coordinates), we only need
// to place stars for max. a reasonable maximum resolution. Lets say 1600x1200
var lw = Min(LandscapeWidth(), 1600);
var lh = Min(LandscapeHeight(),1200);
var lh = Min(LandscapeHeight(), 1200);
//Star Creation
var maxfailedtries = lw * lh / 40000;
@ -111,7 +114,7 @@ private func PlaceStars()
while (failed != maxfailedtries)
{
var pos = [Random(lw), Random(lh)];
if(!FindObject(Find_ID(Stars),Find_AtPoint(pos[0],pos[1])))
if (!FindObject(Find_ID(Stars), Find_AtPoint(pos[0], pos[1])))
{
CreateObjectAbove(Stars, pos[0], pos[1]);
continue;
@ -123,44 +126,43 @@ private func PlaceStars()
}
// Cycles through day and night.
protected func FxIntTimeCycleTimer(object target)
protected func FxIntTimeCycleTimer(object target, proplist effect)
{
// Adjust to time.
AdjustToTime();
// Advance time.
time += advance_seconds_per_tick;
time += Max(advance_seconds_per_tick * effect.Interval / 10, 1);
time %= (24 * 60 * 60);
return 1;
return FX_OK;
}
private func HideCelestials()
{
// hide celestial objects, they will not be drawn during the day
// Hide celestial objects, they will not be drawn during the day.
for (var celestial in FindObjects(Find_Func("IsCelestial")))
{
celestial.Visibility = VIS_None;
celestial->SetObjAlpha(0);
}
return;
}
private func ShowCelestials()
{
// show celestial objects
// Show celestial objects.
for (var celestial in FindObjects(Find_Func("IsCelestial")))
{
celestial.Visibility = VIS_All;
}
return;
}
private func OnSunriseEnd()
{
// next moon phase
var satellite = FindObject(Find_ID(Moon));
if(satellite)
satellite->NextMoonPhase();
// Next moon phase.
var moon = FindObject(Find_ID(Moon));
if (moon)
moon->NextMoonPhase();
HideCelestials();
}
@ -169,30 +171,42 @@ private func OnSunsetStart()
ShowCelestials();
}
// Adjusts the sky, celestial and others to the current time. Use SetTime() at runtime, not this.
private func AdjustToTime()
{
if (time >= time_set.sunrise_end && time < time_set.sunrise_end + advance_seconds_per_tick)
OnSunriseEnd();
else if (time >= time_set.sunset_start && time < time_set.sunset_start + advance_seconds_per_tick)
OnSunsetStart();
DoSkyShade();
return;
}
private func DoSkyShade()
{
// first determine the time phase we are in
// First determine the time phase we are in.
var sunrise, sunset, night, day;
sunrise = sunset = night = day = false;
if (Inside(time, time_set["SunriseStart"], time_set["SunriseEnd"]))
if (Inside(time, time_set.sunrise_start, time_set.sunrise_end))
sunrise = true;
else if(Inside(time, time_set["SunriseEnd"], time_set["SunsetStart"]))
else if(Inside(time, time_set.sunrise_end, time_set.sunset_start))
day = true;
else if(Inside(time, time_set["SunsetStart"], time_set["SunsetEnd"]))
else if(Inside(time, time_set.sunset_start, time_set.sunset_end))
sunset = true;
else
night = true;
var skyshade = [0,0,0,0]; //R,G,B,A
var nightcolour = [10,25,40]; // default darkest-night colour
var daycolour = [255,255,255];
var sunsetcolour = [140,45,10];
var sunrisecolour = [140,100,70];
// Specify colors in terms of R, G, B, A arrays.
var skyshade = [0, 0, 0, 0];
var nightcolour = [10, 25, 40];
var daycolour = [255, 255, 255];
var sunsetcolour = [140, 45, 10];
var sunrisecolour = [140, 100, 70];
// Darkness of night dependent on the moon-phase.
if (!day)
{
// Darkness of night dependent on the moon-phase
var satellite = FindObject(Find_ID(Moon));
if(satellite)
{
@ -201,114 +215,90 @@ private func DoSkyShade()
}
}
// Sunrise
// Sunrise.
if (sunrise)
{
var time_since_sunrise = time - time_set["SunriseStart"];
var time_since_sunrise = time - time_set.sunrise_start;
// progress in 0..1800
var progress = time_since_sunrise * 1800 / (time_set["SunriseEnd"] - time_set["SunriseStart"]);
var progress = time_since_sunrise * 1800 / (time_set.sunrise_end - time_set.sunrise_start);
for(var i=0; i<3; ++i)
for (var i = 0; i < 3; ++i)
{
var nightfade = Cos(progress/2, nightcolour[i],10);
var dayfade = daycolour[i] - Cos(progress/2, daycolour[i],10);
var sunrisefade = Sin(progress, sunrisecolour[i],10);
skyshade[i] = Min(255,dayfade + nightfade + sunrisefade);
var nightfade = Cos(progress / 2, nightcolour[i], 10);
var dayfade = daycolour[i] - Cos(progress / 2, daycolour[i], 10);
var sunrisefade = Sin(progress, sunrisecolour[i], 10);
skyshade[i] = Min(255, dayfade + nightfade + sunrisefade);
}
skyshade[3] = Min(255,progress/2);
skyshade[3] = Min(255, progress / 2);
}
// Day
// Day.
else if (day)
{
skyshade[0] = 255;
skyshade[1] = 255;
skyshade[2] = 255;
skyshade[3] = 255;
skyshade = [255, 255, 255, 255];
}
// Sunset
// Sunset.
else if (sunset)
{
var time_since_sunset = time - time_set["SunsetStart"];
var time_since_sunset = time - time_set.sunset_start;
// progress in 0..1800
var progress = time_since_sunset * 1800 / (time_set["SunsetEnd"] - time_set["SunsetStart"]);
var progress = time_since_sunset * 1800 / (time_set.sunset_end - time_set.sunset_start);
for(var i=0; i<3; ++i)
for (var i = 0; i < 3; ++i)
{
var dayfade = Cos(progress/2, daycolour[i],10);
var nightfade = nightcolour[i] - Cos(progress/2, nightcolour[i],10);
var sunsetfade = Sin(progress, sunsetcolour[i],10);
skyshade[i] = Min(255,dayfade + nightfade + sunsetfade);
}
skyshade[3] = Min(255,900-progress/2);
var dayfade = Cos(progress / 2, daycolour[i], 10);
var nightfade = nightcolour[i] - Cos(progress / 2, nightcolour[i], 10);
var sunsetfade = Sin(progress, sunsetcolour[i], 10);
skyshade[i] = Min(255, dayfade + nightfade + sunsetfade);
}
skyshade[3] = Min(255, 900 - progress / 2);
}
// Night
// Night.
else if (night)
{
skyshade[0] = nightcolour[0];
skyshade[1] = nightcolour[1];
skyshade[2] = nightcolour[2];
skyshade = nightcolour;
skyshade[3] = 0;
}
// Shade sky.
// Shade the sky using sky adjust.
SetSkyAdjust(RGB(skyshade[0], skyshade[1], skyshade[2]));
// Shade landscape.
var gamma = [0,0,0];
var min_gamma = [30,75,120];
gamma[0] = BoundBy(skyshade[0], min_gamma[0], 128);
gamma[1] = BoundBy(skyshade[1], min_gamma[1], 128);
gamma[2] = BoundBy(skyshade[2], min_gamma[2], 128);
// Shade the landscape and the general feeling by reducing the ambient light.
var new_ambient = 100 * skyshade[2] / 255;
if (GetAmbientBrightness() != new_ambient)
SetAmbientBrightness(new_ambient);
//SetGamma(0, RGB(gamma[0], gamma[1], gamma[2]), RGB(127+gamma[0], 127+gamma[1], 127+gamma[2]), 3);
if(!day && !night)
// Adjust celestial objects and clouds.
if (!day && !night)
{
// Adjust celestial objects.
for (var celestial in FindObjects(Find_Func("IsCelestial")))
celestial->SetObjAlpha(255 - skyshade[3]);
// Adjust clouds
for(var cloud in FindObjects(Find_ID(Cloud))){
for (var cloud in FindObjects(Find_ID(Cloud)))
cloud->SetLightingShade(255 - skyshade[2]);
}
}
}
// Adjusts the sky, celestial and others to the current time. Use SetTime() at runtime, not this.
private func AdjustToTime()
{
if (Abs(time - time_set["SunriseEnd"]) <= advance_seconds_per_tick)
OnSunriseEnd();
else if (Abs(time - time_set["SunsetStart"]) <= advance_seconds_per_tick)
OnSunsetStart();
DoSkyShade();
return;
}
/* Scenario saving */
/*-- Scenario saving --*/
func SaveScenarioObject(props)
public func SaveScenarioObject(props)
{
if (!inherited(props, ...)) return false;
// Initialize function depends on this object implicitely
// So make sure it's created before this
if (!inherited(props, ...))
return false;
// Initialize function depends on this object implicitely.
// So make sure it's created before this.
var celestial_env = FindObject(Find_ID(Environment_Celestial));
if (celestial_env) celestial_env->MakeScenarioSaveName();
// Save time props
if (GetTime() != 43200) props->AddCall("Time", this, "SetTime", GetTime());
if (GetCycleSpeed() != 30) props->AddCall("CycleSpeed", this, "SetCycleSpeed", GetCycleSpeed());
if (celestial_env)
celestial_env->MakeScenarioSaveName();
// Save time props.
if (GetTime() != 43200)
props->AddCall("Time", this, "SetTime", GetTime());
if (GetCycleSpeed() != 30)
props->AddCall("CycleSpeed", this, "SetCycleSpeed", GetCycleSpeed());
return true;
}
/* Properties */
/*-- Properties --*/
local Name = "Time";