Search Unity

Having two floats affect another

Discussion in 'Scripting' started by Murgilod, Feb 17, 2018.

  1. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,144
    So I'm afraid this is more of a "basic maths question" than a scripting question, but I've got no better place to ask.

    I have three floats, exposureValue01, exposure01, aperture01. As the names imply, they're all values from 0 to 1. I'd like to make it so that as I increase exposure01, exposureValue01 increases. At the same time however, I'd like to make it so that increasing aperture01 decreases the value of exposureValue01. I'm sure there's a simple solution for this but the entire method is escaping me. Any help would be great, thanks.
     
  2. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    Something like this?
    Code (CSharp):
    1. // Depending on where these values changes I would either use OnValidate or a property.
    2.  
    3. // This triggers when a value in the inspector has changed
    4. private void OnValidate(){
    5.     // Example.
    6.     // Exposure of 0.71 means that Aperture is (1 - 0.71) which equals 0.29
    7.     exposureValue01 = (1 - aperture01);
    8. }
     
  3. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,144
    That just takes one of the variables I'm working with into account though. I need to make it so that both aperture01 and exposure01 are doing something.

    Right now I have exposureValue01 = (exposure01 + aperture01) / 2f;, but this gives an "incomplete" value.
     
  4. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    That's why its an example, I dont know your specific code.

    Looks fine to me.

    Using quotes on a word doesnt help me at all, what do you mean by 'incomplete', spell it out for me, it will be easier to understand.
     
  5. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,144
    Whoops, looks like the rest of my post got cut off for some reason.

    Basically, I always want the key value to be able to move between its set values of 0.1f and 5f. By using the average, there are configurations where the key value will only make it to 2.55f, which is a problem. Here's the relevant code in Update:

    Code (CSharp):
    1.     private void Update()
    2.     {
    3.         cam.fieldOfView = Mathf.Rad2Deg * 2.0f * Mathf.Atan(filmHeight / (2.0f * focalLength));
    4.         motionBlurModel.shutterAngle = Remap(exposureTimeDelta, 1f, 100f, 1f, 360f);
    5.  
    6.         grainModel.intensity = Remap(exposureTimeDelta, 1f, 100f, maxNoise, minNoise);
    7.  
    8.         exposure01 = Remap(exposureTimeDelta, 1f, 100f, 0f, 1f);
    9.         aperture01 = Remap(aperture, 0.1f, 32f, 0f, 1f);
    10.  
    11.         exposureValue01 = (exposure01 + aperture01) / 2f;
    12.         Debug.Log (exposureValue01);
    13.  
    14.         eyeAdaptationModel.keyValue = Mathf.Lerp(minBrightness, maxBrightness, exposureValue01);
    15.  
    16.         depthOfFieldModel.aperture = aperture;
    17.  
    18.         postProcessingProfile.grain.settings = grainModel;
    19.         postProcessingProfile.motionBlur.settings = motionBlurModel;
    20.         postProcessingProfile.eyeAdaptation.settings = eyeAdaptationModel;
    21.         postProcessingProfile.depthOfField.settings = depthOfFieldModel;
    22.     }
    The Remap function is just the following:

    Code (CSharp):
    1.     float Remap(float input, float min1, float max1, float min2, float max2)
    2.     {
    3.         return min2 + (input - min1) * (max2 - min2) / (max1 - min1);
    4.     }
     
  6. McDev02

    McDev02

    Joined:
    Nov 22, 2010
    Posts:
    664
    This sounds like plain game design to me. What is your goal? Do you have fixed numbers as examples? For instance if exposure = 0.2 and apeture = 0.3 then exposureValue = ?
    There are many solutions but the rules are the important thing and we can't answer that for you.

    But here is another random example that does what you want: exposureValue = exposure - apeture; Now you could scale the values or add limitations to adjust it the way you want.
     
  7. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    How about exposureValue01 = exposure01 * (1f - aperature01) ?