Search Unity

Normalizing a divided number

Discussion in 'Scripting' started by danipren, Sep 14, 2017.

  1. danipren

    danipren

    Joined:
    Mar 28, 2014
    Posts:
    11
    hey all,

    sorry if in the wrong section

    have come to a road block and could really do with some help, so any would be appreciated and thanks before hand.

    Now how to explain this



    looking at this really awesome best drawing in the world how would i go about finding a value on the green line if i supply the hour and minute of the hour?

    thanks for any help

    diagram.jpg (80.2 kB)
     
    makeshiftwings likes this.
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    First off I would convert your hours/minutes into a single value between 0.0 and 24.0 - you could do this using:

    Code (CSharp):
    1. int Hours = 15;
    2. int Mins = 48;
    3. float TimeValue = (float)Hours + ((float)Mins / 60.0f );
    4.  
    Then you have 4 quadrants so I would divide that value by 6 to get it between 0.0 and 4.0

    Code (CSharp):
    1. TimeValue /= 6.0f;
    You could then do an If->Then statement to work out which quadrant it's in.

    Code (CSharp):
    1. float OutputValue;
    2.  
    3. if( TimeValue <= 1.0f )
    4. {
    5.     // 1st Quadrant - return value between 0.5 and 1.0
    6.     OutputValue = 0.5f + ( TimeValue / 2.0f );
    7. }
    8. else if( TimeValue <= 2.0f )
    9. {
    10.     // 2nd Quadrant - return value between 0.0 and 1.0
    11.     OutputValue = TimeValue - 1.0f;
    12. }
    13. else if( TimeValue <= 3.0f )
    14. {
    15.     // 3rd Quadrant - return value between 0.0 and 1.0
    16.     OutputValue = TimeValue - 2.0f;
    17. }
    18. else
    19. {
    20.     // 4th Quadrant - return value between 0.0 and 0.5
    21.     OutputValue = ( TimeValue - 3.0f ) / 2.0f;
    22. }
    23.  
     
  3. danipren

    danipren

    Joined:
    Mar 28, 2014
    Posts:
    11
    thanks for your help much appreciated but i still seem to be any closer. i implemented that code in but i still cant get any good of it.

    heres the forementioned part of the script.


    Code (CSharp):
    1. public void LerpSkySetting(float h,float min)
    2.     {
    3.      
    4.         float TimeValue = (float)h + ((float)min / 60.0f);
    5.         TimeValue /= 6.0f;
    6.  
    7.         float OutputValue;
    8.  
    9.         int hour;
    10.         int nextHour;
    11.         if (TimeValue <= 1.0f)
    12.         {
    13.             // 1st Quadrant - return value between 0.5 and 1.0
    14.             hour = 0;
    15.             nextHour = 3;
    16.             OutputValue = TimeValue - 0.5f;
    17.         }
    18.         else if (TimeValue <= 2.0f)
    19.         {
    20.             hour = 1;
    21.             nextHour = 2;
    22.             // 2nd Quadrant - return value between 0.0 and 1.0
    23.             OutputValue = TimeValue - 1.0f;
    24.         }
    25.         else if (TimeValue <= 3.0f)
    26.         {
    27.             hour = 2;
    28.             nextHour = 3;
    29.             // 3rd Quadrant - return value between 0.0 and 1.0
    30.             OutputValue = TimeValue - 2.0f;
    31.         }
    32.         else
    33.         {
    34.             hour = 3;
    35.             nextHour = 0;
    36.             // 4th Quadrant - return value between 0.0 and 0.5
    37.             OutputValue = (TimeValue - 3.0f) / 2.0f;
    38.         }
    39.      
    40.         float t = OutputValue;
    41.  
    42.      
    43.         currentSkySettings.directionalLightColor = Color.Lerp(TODGroups[0].timeSettings[hour].directionalLightColor, TODGroups[0].timeSettings[nextHour].directionalLightColor, t );
    44.         currentSkySettings.directionalLightIntensity = Mathf.Lerp(TODGroups[0].timeSettings[hour].directionalLightIntensity,TODGroups[0].timeSettings[nextHour].directionalLightIntensity, t);
    45.         currentSkySettings.sunSize = Mathf.Lerp(TODGroups[0].timeSettings[hour].sunSize, TODGroups[0].timeSettings[hour].sunSize, t);
    46.         currentSkySettings.shadowIntensity = Mathf.Lerp(TODGroups[0].timeSettings[hour].shadowIntensity,TODGroups[0].timeSettings[nextHour].shadowIntensity, t);
    47.         currentSkySettings.ambientIntensity = Mathf.Lerp(TODGroups[0].timeSettings[hour].ambientIntensity,TODGroups[0].timeSettings[nextHour].ambientIntensity, t);
    48.         currentSkySettings.skyTint = Color.Lerp(TODGroups[0].timeSettings[hour].skyTint,TODGroups[0].timeSettings[nextHour].skyTint, t);
    49.         currentSkySettings.skyGround = Color.Lerp(TODGroups[0].timeSettings[hour].skyGround, TODGroups[0].timeSettings[nextHour].skyGround, t);
    50.         currentSkySettings.exposure = Mathf.Lerp(TODGroups[0].timeSettings[hour].exposure,TODGroups[0].timeSettings[nextHour].exposure, t);
    51.         currentSkySettings.atmosphereThickness = Mathf.Lerp(TODGroups[0].timeSettings[hour].atmosphereThickness,TODGroups[0].timeSettings[nextHour].atmosphereThickness, t);
    52.         InputSetting();
    53.     }
     
  4. Trexug

    Trexug

    Joined:
    Dec 2, 2013
    Posts:
    88
    Code (csharp):
    1.  
    2.         public static float Convert(int hour, int minute)
    3.         {
    4.             float q = (hour / 6f + minute / 360f + 1) % 4;
    5.             return q < 2 ? q / 2f : q % 1;
    6.         }
    7.  
     
  5. danipren

    danipren

    Joined:
    Mar 28, 2014
    Posts:
    11
    im not sure how to implement that trexug

    i have it 90% working now the only problem now is the lerping to and from the first quadrant

    Code (CSharp):
    1. public void LerpSkySetting(float h, float min)
    2.     {
    3.  
    4.         float TimeValue = (float)h + ((float)min / 60.0f);
    5.         TimeValue /= 6.0f;
    6.  
    7.         float OutputValue;
    8.  
    9.         int hour;
    10.         int nextHour;
    11.         if (TimeValue <= 1.0f)
    12.         {
    13.             // 1st Quadrant - return value between 0.5 and 1.0
    14.             hour = 0;
    15.             nextHour = 1;
    16.             OutputValue = TimeValue - 0.5f;
    17.            
    18.         }
    19.         else if (TimeValue <= 2.0f)
    20.         {
    21.             hour = 1;
    22.             nextHour = 2;
    23.             // 2nd Quadrant - return value between 0.0 and 1.0
    24.             OutputValue = TimeValue - 1.0f;
    25.         }
    26.         else if (TimeValue <= 3.0f)
    27.         {
    28.             hour = 2;
    29.             nextHour = 3;
    30.             // 3rd Quadrant - return value between 0.0 and 1.0
    31.             OutputValue = TimeValue - 2.0f;
    32.         }
    33.         else
    34.         {
    35.             hour = 3;
    36.             nextHour = 0;
    37.             // 4th Quadrant - return value between 0.0 and 0.5
    38.             OutputValue = (TimeValue - 3.0f) / 2.0f;
    39.         }
    40.  
    41.         float t = OutputValue;
    42.  
    43.         print(t);
    44.         currentSkySettings.directionalLightColor = Color.Lerp(TODGroups[0].timeSettings[hour].directionalLightColor, TODGroups[0].timeSettings[nextHour].directionalLightColor, t);
    45.         currentSkySettings.directionalLightIntensity = Mathf.Lerp(TODGroups[0].timeSettings[hour].directionalLightIntensity, TODGroups[0].timeSettings[nextHour].directionalLightIntensity, t);
    46.         currentSkySettings.sunSize = Mathf.Lerp(TODGroups[0].timeSettings[hour].sunSize, TODGroups[0].timeSettings[hour].sunSize, t);
    47.         currentSkySettings.shadowIntensity = Mathf.Lerp(TODGroups[0].timeSettings[hour].shadowIntensity, TODGroups[0].timeSettings[nextHour].shadowIntensity, t);
    48.         currentSkySettings.ambientIntensity = Mathf.Lerp(TODGroups[0].timeSettings[hour].ambientIntensity, TODGroups[0].timeSettings[nextHour].ambientIntensity, t);
    49.         currentSkySettings.skyTint = Color.Lerp(TODGroups[0].timeSettings[hour].skyTint, TODGroups[0].timeSettings[nextHour].skyTint, t);
    50.         currentSkySettings.skyGround = Color.Lerp(TODGroups[0].timeSettings[hour].skyGround, TODGroups[0].timeSettings[nextHour].skyGround, t);
    51.         currentSkySettings.exposure = Mathf.Lerp(TODGroups[0].timeSettings[hour].exposure, TODGroups[0].timeSettings[nextHour].exposure, t);
    52.         currentSkySettings.atmosphereThickness = Mathf.Lerp(TODGroups[0].timeSettings[hour].atmosphereThickness, TODGroups[0].timeSettings[nextHour].atmosphereThickness, t);
    53.         InputSetting();
    54.     }
     
  6. Trexug

    Trexug

    Joined:
    Dec 2, 2013
    Posts:
    88
    I was suggesting just calling convert and using the returned value as your OutputValue, but it is possible that I did not quite understand the issue you are having.

    Code (csharp):
    1.  
    2.  
    3. public static float Convert(int hour, int minute)
    4. {
    5.     float q = (hour / 6f + minute / 360f + 1) % 4;
    6.     return q < 2 ? q / 2f : q % 1;
    7. }
    8.  
    9.  
    10. public void LerpSkySetting(int h, int min)
    11. {
    12.     float t = Convert(h, min);;
    13.  
    14.     /// The rest of your code
    15. }
    16.