Advertisement
dnnkeeper

UDK Day/Night cycle script v3.0 (with replication)

Sep 4th, 2012
1,573
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  *  DominantDirectionalLightDayNight
  3.  *
  4.  *  Creation date: 24.08.2012 19:25
  5.  *  Copyright 2012, Boris Novikov
  6.  */
  7. class DominantDirectionalLightDayNight extends DominantDirectionalLightMovable
  8. ClassGroup(Lights,DirectionalLights)
  9. placeable;
  10.  
  11. var (DayNight) float DayNightFreq;
  12. var (DayNight) int DayNightStep;
  13. var (DayNight) float Correction;
  14. var (DayNight) int TurnOffHour;
  15. var (DayNight) int TurnOnHour;
  16.  
  17. var (DayNight) array <float> SunBrightness;
  18. var (DayNight) float BrightnessInterpolationSpeed;
  19. var (DayNight) bool ConstBrightnessInterpolationSpeed;
  20.  
  21. var (DayNight) array <Color> SunColor;
  22. var (DayNight) float ColorInterpolationSpeed;
  23. var (DayNight) bool ConstColorInterpolationSpeed;
  24.  
  25. var (DayNight) InterpActor SkyActor;
  26. var (DayNight) array <MaterialInterface> SkyMaterials;
  27.  
  28. var (DayNight) array <Name> ScalarParamNames;
  29. var (DayNight) float ScalarInterpolationSpeed;
  30. var (DayNight) bool ConstScalarInterpolationSpeed;
  31.  
  32. var (DayNight) array <Name> ColorParamNames;
  33. var (DayNight) float VectorInterpolationSpeed;
  34. var (DayNight) bool ConstVectorInterpolationSpeed;
  35.  
  36.  
  37. var float TimeStamp;
  38. var bool MatInstanced;
  39.  
  40. var repnotify rotator SunRotation;
  41.  
  42. var int Days, Hours, Minutes, i;
  43.  
  44. replication
  45. {
  46.     if (bNetInitial)
  47.         SunRotation;
  48. }
  49.  
  50. simulated event ReplicatedEvent(name VarName)
  51. {
  52.     if (VarName == 'SunRotation')
  53.     {
  54.         //`log(self@"REPLICATED"@SunRotation);
  55.         SetRotation(SunRotation);
  56.     }
  57.     else    if (VarName == 'bEnabled')
  58.     {
  59.         LightComponent.SetEnabled(bEnabled);
  60.     }
  61.     else
  62.     {
  63.         Super.ReplicatedEvent(VarName);
  64.     }
  65. }
  66.  
  67. simulated event PostBeginPlay()
  68. {
  69.     super.PostBeginPlay();
  70.     TimeStamp = WorldInfo.TimeSeconds;
  71.     SetTimer(DayNightFreq, true, 'DayNightTimer');
  72. }
  73.  
  74.  
  75. simulated function DayNightTimer()
  76. {
  77.     local Rotator R;
  78.  
  79.     local MaterialInterface NewMaterial, OldMaterial;
  80.     local MaterialInstance CurrentMaterial;
  81.     local float fOldScalarParam, fScalarParam, fCurrentParam;
  82.     local LinearColor vOldParam, vParam;
  83.     local color cColor, cNextColor;
  84.     local int index, NextIndex, Progress, TotalProgress;
  85.     local float DeltaTime;
  86.    
  87.     DeltaTime=WorldInfo.TimeSeconds-TimeStamp;
  88.     TimeStamp = WorldInfo.TimeSeconds;
  89.    
  90.     R=self.Rotation;
  91.     R.Pitch += DayNightStep;
  92.     SunRotation=R;
  93.     if (Role == Role_Authority){
  94.         SunRotation=R;
  95.     }
  96.  
  97.     self.SetRotation(R);
  98.    
  99.     //`Log(self@Role@SunRotation);
  100.    
  101.     Days = (Rotation.Pitch + Correction*(65536/24)) / 65536;
  102.     Hours = ( ( (Rotation.Pitch - (Days*65536) ) / 65536.) * 24.)+Correction;
  103.     Minutes = ( ( (Rotation.Pitch+(Correction*(65536/24)) - (Days*65536) ) ) - (Hours*(65536/24) ) ) / 45.51;
  104.    
  105.     //HERE YOU CAN SET CALCULATED TIME SOMEWHERE TO WorldInfo
  106.     //XGame(WorldInfo.Game).DayTimeHours = ( ( (Rotation.Pitch - (Days*65536) ) / 65536.) * 24.)+Correction;
  107.    
  108.     //WorldInfo.Game.Broadcast(self, "Day"@Days@"Hours ="@Hours@"Minutes= "@Minutes );
  109.    
  110.     if (Hours == TurnOffHour && LightComponent.bEnabled)
  111.     {
  112.         bEnabled=FALSE;
  113.         LightComponent.SetEnabled(FALSE);
  114.         //WorldInfo.Game.Broadcast(self, "Day"@Days@"Hours ="@Hours@"Minutes= "@Minutes );
  115.         //WorldInfo.Game.Broadcast(self, "TURN OFF" );
  116.     }
  117.     if (Hours == TurnOnHour && !LightComponent.bEnabled)
  118.     {
  119.         bEnabled=TRUE;
  120.         LightComponent.SetEnabled(TRUE);
  121.         LightComponent.BloomScale=LightComponent.Default.BloomScale;
  122.         LightComponent.BloomThreshold=LightComponent.Default.BloomThreshold;
  123.         LightComponent.BloomScreenBlendThreshold=LightComponent.Default.BloomScreenBlendThreshold;
  124.        
  125.         LightComponent.SetLightProperties(LightComponent.Default.Brightness, LightComponent.Default.LightColor, );
  126.         LightComponent.UpdateLightShaftParameters();
  127.         LightComponent.UpdateColorAndBrightness();
  128.  
  129.         //WorldInfo.Game.Broadcast(self, "Day"@Days@"Hours ="@Hours@"Minutes= "@Minutes );
  130.         //WorldInfo.Game.Broadcast(self, "TURN ON" );
  131.     }
  132.  
  133.     TotalProgress=( (self.Rotation.Pitch+Correction*(65536/24)) % 65536);
  134.    
  135.     i = SunBrightness.Length;
  136.     if (i !=0 )
  137.     {
  138.         index = TotalProgress*i / 65536;
  139.         Progress = TotalProgress - ( index * (65536 / i) );
  140.         fOldScalarParam = SunBrightness[ index ];
  141.        
  142.         NextIndex=( index+1 )%i;
  143.  
  144.         if (NextIndex > -1 && NextIndex < SunBrightness.Length)
  145.         {
  146.             fScalarParam = SunBrightness[ NextIndex ];
  147.            
  148.             if (fOldScalarParam != fScalarParam)
  149.             {
  150.                 //`Log(fOldScalarParam@"["$index$"]"@"->"@fScalarParam@"["$NextIndex$"]"@" = "@LightComponent.Brightness );
  151.                 //`Log( Progress@"/"@(ABS(fScalarParam - fOldScalarParam) / (65536 / SunBrightness.Length)) );
  152.                 if (ConstBrightnessInterpolationSpeed)
  153.                     fCurrentParam= FInterpConstantTo(fOldScalarParam, fScalarParam, Progress, ABS(fScalarParam - fOldScalarParam) / (65536 / SunBrightness.Length) );
  154.                 else
  155.                     fCurrentParam= FInterpTo(LightComponent.Brightness, SunBrightness[ Hours/( 24 / i ) ], DeltaTime, BrightnessInterpolationSpeed);
  156.                
  157.                 LightComponent.SetLightProperties(fCurrentParam);              
  158.                 //WorldInfo.Game.Broadcast(self, fOldScalarParam@" => "@fScalarParam@"="@fCurrentParam@"progress="$Progress@"speed="$ABS(fScalarParam-fOldScalarParam)/(65536 / SunBrightness.Length)@" step="$(ABS(fScalarParam-fOldScalarParam)/(65536 / SunBrightness.Length))*Progress  );
  159.             }
  160.         }
  161.     }
  162.     i=0;
  163.    
  164.     i = SunColor.Length;
  165.     if (i !=0 )
  166.     {
  167.  
  168.         index = TotalProgress*i / 65536;
  169.         cColor = SunColor[index];
  170.  
  171.         NextIndex=( index+1 )%i;
  172.         cNextColor = SunColor[NextIndex];
  173.        
  174.         //WorldInfo.Game.Broadcast(self, "R="$cColor.R@"G="$cColor.G@"B="$cColor.B@" => "@"R="$cNextColor.R@"G="$cNextColor.G@"B="$cNextColor.B);
  175.        
  176.         Progress = TotalProgress - ( index * (65536 / i) );
  177.        
  178.         if (ConstColorInterpolationSpeed)
  179.         {      
  180.             cColor.R += (cNextColor.R - cColor.R) * Progress / (65536 / i);
  181.             cColor.G += (cNextColor.G - cColor.G) * Progress / (65536 / i);
  182.             cColor.B += (cNextColor.B - cColor.B) * Progress / (65536 / i);
  183.  
  184.         }
  185.         else
  186.         {
  187.             cColor.R=FInterpTo( cColor.R, cNextColor.R, DeltaTime, ABS(cNextColor.R - cColor.R) /  (65536 / i) );
  188.             cColor.G=FInterpTo( cColor.G, cNextColor.G, DeltaTime, ABS(cNextColor.G - cColor.G) /  (65536 / i) );
  189.             cColor.B=FInterpTo( cColor.B, cNextColor.B, DeltaTime, ABS(cNextColor.B - cColor.B) /  (65536 / i) );
  190.         }
  191.         //WorldInfo.Game.Broadcast(self, "R="$cColor.R@"G="$cColor.G@"B="$cColor.B);
  192.         LightComponent.BloomTint = cColor;
  193.         LightComponent.SetLightProperties( ,cColor );
  194.  
  195.     }
  196.     i=0;
  197.     index=0;
  198.    
  199.     if (SkyActor != none && SkyMaterials.Length > 0)
  200.     {
  201.  
  202.         i = SkyMaterials.Length;
  203.         if (i !=0 )
  204.         {
  205.             index = TotalProgress*i / 65536;
  206.             Progress = TotalProgress - ( index * (65536 / i) );
  207.             OldMaterial = SkyMaterials[index];
  208.            
  209.             NextIndex=( index+1 )%i;
  210.             NewMaterial = SkyMaterials[NextIndex];
  211.            
  212.             //WorldInfo.Game.Broadcast(self, "SkyMat"@index@SkyMaterials[index] );
  213.            
  214.             if (!MatInstanced){
  215.                 CurrentMaterial=SkyActor.StaticMeshComponent.CreateAndSetMaterialInstanceConstant(0);
  216.                 MatInstanced=TRUE;
  217.             }
  218.             else
  219.                 CurrentMaterial=MaterialInstance(SkyActor.StaticMeshComponent.GetMaterial(0));
  220.                
  221.             i=0;   
  222.            
  223.             while ( i < ScalarParamNames.Length )
  224.             {
  225.                 NewMaterial.GetScalarParameterValue(ScalarParamNames[i], fScalarParam);
  226.                 OldMaterial.GetScalarParameterValue(ScalarParamNames[i], fOldScalarParam);
  227.                    
  228.                 if (fOldScalarParam != fScalarParam)
  229.                 {
  230.                     if (ConstScalarInterpolationSpeed){
  231.                         fCurrentParam=FInterpConstantTo(fOldScalarParam, fScalarParam, Progress, ABS(fScalarParam-fOldScalarParam)/(65536 / SkyMaterials.Length)  );
  232.                     }
  233.                     else
  234.                         fCurrentParam=FInterpTo(fOldScalarParam, fScalarParam, DeltaTime, ScalarInterpolationSpeed);
  235.                    
  236.                     CurrentMaterial.SetScalarParameterValue(ScalarParamNames[i], fCurrentParam);
  237.                 }
  238.                
  239.                 //WorldInfo.Game.Broadcast(self, ScalarParamNames[i]@fOldScalarParam@" => "@fScalarParam@"="@fCurrentParam@"progress="$Progress@"speed="$ABS(fScalarParam-fOldScalarParam)/(65536 / SkyMaterials.Length)@" step="$(ABS(fScalarParam-fOldScalarParam)/(65536 / SkyMaterials.Length))*Progress  );
  240.                 //`log(ScalarParamNames[i]@fOldScalarParam@" => "@fScalarParam@"="@fCurrentParam@"progress="$Progress@"speed="$ABS(fScalarParam-fOldScalarParam)/(65536 / SkyMaterials.Length)@" step="$(ABS(fScalarParam-fOldScalarParam)/(65536 / SkyMaterials.Length))*Progress  );
  241.                 i++;
  242.             }
  243.            
  244.             i=0;
  245.  
  246.             //WorldInfo.Game.Broadcast(self, "ColorParamNames.Length="@ColorParamNames.Length );
  247.             while ( i < ColorParamNames.Length )
  248.             {
  249.                 NewMaterial.GetVectorParameterValue(ColorParamNames[i],vParam);
  250.                 OldMaterial.GetVectorParameterValue(ColorParamNames[i], vOldParam);
  251.                 if (ConstVectorInterpolationSpeed)
  252.                 {
  253.                     //WorldInfo.Game.Broadcast(self, ColorParamNames[i]@"R="$vOldParam.R@" => "@"R="$vParam.R@"progress="$Progress@"speed="$ABS(vParam.R-vOldParam.R)/(65536 / SkyMaterials.Length)@" diff="$(ABS(vParam.R-vOldParam.R)/(65536 / SkyMaterials.Length))*Progress);  
  254.                     //`log(ColorParamNames[i]@"R="$vOldParam.R@" => "@"R="$vParam.R@"progress="$Progress@"speed="$ABS(vParam.R-vOldParam.R)/(65536 / SkyMaterials.Length)@" diff="$(ABS(vParam.R-vOldParam.R)/(65536 / SkyMaterials.Length))*Progress);
  255.                     //`log(ColorParamNames[i]@"G="$vOldParam.R@" => "@"G="$vParam.R@"progress="$Progress@"speed="$ABS(vParam.G-vOldParam.G)/(65536 / SkyMaterials.Length)@" diff="$(ABS(vParam.G-vOldParam.G)/(65536 / SkyMaterials.Length))*Progress);
  256.                     //`log(ColorParamNames[i]@"B="$vOldParam.R@" => "@"B="$vParam.R@"progress="$Progress@"speed="$ABS(vParam.B-vOldParam.B)/(65536 / SkyMaterials.Length)@" diff="$(ABS(vParam.B-vOldParam.B)/(65536 / SkyMaterials.Length))*Progress);
  257.                     //`log(ColorParamNames[i]@"A="$vOldParam.R@" => "@"A="$vParam.R@"progress="$Progress@"speed="$ABS(vParam.A-vOldParam.A)/(65536 / SkyMaterials.Length)@" diff="$(ABS(vParam.A-vOldParam.A)/(65536 / SkyMaterials.Length))*Progress);
  258.                     vParam.R = FInterpConstantTo(vOldParam.R, vParam.R, Progress, ABS(vParam.R-vOldParam.R)/(65536 / SkyMaterials.Length)  );
  259.                     vParam.G = FInterpConstantTo(vOldParam.G, vParam.G, Progress, ABS(vParam.G-vOldParam.G)/(65536 / SkyMaterials.Length)  );
  260.                     vParam.B = FInterpConstantTo(vOldParam.B, vParam.B, Progress, ABS(vParam.B-vOldParam.B)/(65536 / SkyMaterials.Length)  );
  261.                     vParam.A = vOldParam.A;
  262.                     //WorldInfo.Game.Broadcast(self, ColorParamNames[i]@"STEP="$ (Progress*ABS(vParam.B-vOldParam.B)/(65536 / SkyMaterials.Length) ) );
  263.                    
  264.                 }
  265.                 else
  266.                 {
  267.                     vParam.R=FInterpTo(vOldParam.R, vParam.R, DeltaTime, VectorInterpolationSpeed);
  268.                     vParam.G=FInterpTo(vOldParam.G, vParam.G, DeltaTime, VectorInterpolationSpeed);
  269.                     vParam.B=FInterpTo(vOldParam.B, vParam.B, DeltaTime, VectorInterpolationSpeed);
  270.                     vParam.A=FInterpTo(vOldParam.A, vParam.A, DeltaTime, VectorInterpolationSpeed);
  271.                 }
  272.                 //WorldInfo.Game.Broadcast(self,ColorParamNames[i]@"R="$vParam.R@"G="$vParam.G@"B="$vParam.B);
  273.                 //CurrentMaterial.SetVectorParameterValue(ColorParamNames[i], vParam);
  274.                 //`log(ColorParamNames[i]@"R="$vParam.R@"G="$vParam.G@"B="$vParam.B@"A="$vParam.A);
  275.                 CurrentMaterial.SetVectorParameterValue(ColorParamNames[i], vParam);
  276.                 i++;
  277.             }
  278.             i=0;
  279.    
  280.         }  
  281.    
  282.     }  
  283. }
  284.  
  285. defaultproperties
  286. {
  287.     bHidden=FALSE //REPLICATION NEEDS THIS
  288.     bNoDelete=TRUE
  289.  
  290.     bRouteBeginPlayEvenIfStatic=FALSE
  291.     bEdShouldSnap=FALSE
  292.    
  293.  
  294.     Begin Object Name=DominantDirectionalLightComponent0
  295.  
  296.        
  297.         ModShadowFadeoutExponent=3.0
  298.    
  299.         bRenderLightShafts=True
  300.  
  301.         LightAffectsClassification=LAC_DYNAMIC_AND_STATIC_AFFECTING
  302.  
  303.         CastShadows=TRUE
  304.         CastStaticShadows=TRUE
  305.         CastDynamicShadows=TRUE
  306.         bForceDynamicLight=FALSE
  307.         UseDirectLightMap=FALSE
  308.         bAllowPreShadow=TRUE
  309.  
  310.         LightingChannels=(BSP=TRUE,Static=TRUE,Dynamic=TRUE,bInitialized=TRUE)
  311.         LightmassSettings=(LightSourceAngle=.2)
  312.  
  313.     End Object
  314.  
  315.     bStatic=FALSE
  316.     bHardAttach=TRUE
  317.     bMovable=TRUE
  318.     Physics=PHYS_Interpolating
  319.    
  320.     RemoteRole=ROLE_SimulatedProxy
  321.     Role=ROLE_Authority
  322.     bNetInitialRotation=TRUE
  323.     bUpdateSimulatedPosition=TRUE
  324.     bReplicateMovement=TRUE
  325.    
  326.     Correction=22;
  327.     DayNightStep = 1;
  328.     DayNightFreq = 0.001;
  329.     TurnOffHour=-1;
  330.     TurnOnHour=-1;
  331.    
  332.     SkyMaterials[0]=MaterialInstanceConstant'MapTemplates.Sky.M_Procedural_Sky_Night'
  333.     SkyMaterials[1]=MaterialInstanceConstant'MapTemplates.Sky.M_Procedural_Sky_Morning'
  334.     SkyMaterials[2]=MaterialInstanceConstant'MapTemplates.Sky.M_Procedural_Sky_Daytime'
  335.     SkyMaterials[3]=MaterialInstanceConstant'MapTemplates.Sky.M_Procedural_Sky_Afternoon'
  336.    
  337.    
  338.     ScalarParamNames[0]=CloudBrightness
  339.     ScalarParamNames[1]=CloudDarkness
  340.     ScalarParamNames[2]=CloudOpacity
  341.     ScalarParamNames[3]=Desaturation
  342.     ScalarParamNames[4]=RimBrightness
  343.     ScalarParamNames[5]=SkyBrightness
  344.     ScalarParamNames[6]=Speed
  345.    
  346.    
  347.     ColorParamNames[0]=HorizonColor
  348.     ColorParamNames[1]=RimColor
  349.     ColorParamNames[2]=Sun
  350.     ColorParamNames[3]=ZenithColor
  351.  
  352.    
  353.     BrightnessInterpolationSpeed=1.;
  354.     ColorInterpolationSpeed=100.;
  355.     ScalarInterpolationSpeed=1.;
  356.     VectorInterpolationSpeed=1.;
  357.  
  358. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement