소스 검색

Squashed commit of the following:

commit c2f7ab8735bed357555bed7a901f02372cdd5be0
Author: joshua Nuttall <[email protected]>
Date:   Tue Aug 16 13:46:34 2016 +0100

    Physical based light values

commit 60f87b92a85635c20200d1bff16432e6f4a01e7c
Author: joshua Nuttall <[email protected]>
Date:   Tue Aug 16 10:50:45 2016 +0100

    removed unused share code and fixes spacing

commit 04a6961576ba1034d32b1abcf5cd2dab6599e9d4
Author: joshua Nuttall <[email protected]>
Date:   Sat Aug 13 15:47:19 2016 +0100

    cleaned up naming a little

commit 7742eebcb51506c338c7bc93dd9fe683ca7c7293
Author: joshua Nuttall <[email protected]>
Date:   Sat Aug 13 15:43:32 2016 +0100

    Made light temperature calculate outside of the shaders, and optional

commit ff784e3af0a23838ac932a1c54ca508687f1ca78
Author: joshua Nuttall <[email protected]>
Date:   Fri Aug 12 22:40:06 2016 +0100

    Added AS binding

commit d96634a5af41a230d481f13aaed444bbeaa285bf
Author: joshua Nuttall <[email protected]>
Date:   Fri Aug 12 22:25:17 2016 +0100

    Added light temputure
Lasse Öörni 9 년 전
부모
커밋
291b33df99
4개의 변경된 파일91개의 추가작업 그리고 6개의 파일을 삭제
  1. 5 0
      Source/Urho3D/AngelScript/GraphicsAPI.cpp
  2. 57 1
      Source/Urho3D/Graphics/Light.cpp
  3. 21 4
      Source/Urho3D/Graphics/Light.h
  4. 8 1
      Source/Urho3D/LuaScript/pkgs/Graphics/Light.pkg

+ 5 - 0
Source/Urho3D/AngelScript/GraphicsAPI.cpp

@@ -1202,6 +1202,10 @@ static void RegisterLight(asIScriptEngine* engine)
     engine->RegisterObjectMethod("Light", "bool get_perVertex() const", asMETHOD(Light, GetPerVertex), asCALL_THISCALL);
     engine->RegisterObjectMethod("Light", "void set_color(const Color&in)", asMETHOD(Light, SetColor), asCALL_THISCALL);
     engine->RegisterObjectMethod("Light", "const Color& get_color() const", asMETHOD(Light, GetColor), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Light", "void set_temperature(float)", asMETHOD(Light, SetTemperature), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Light", "float get_temperature() const", asMETHOD(Light, GetTemperature), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Light", "void set_usePhysicalValues(bool)", asMETHOD(Light, SetUsePhysicalValues), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Light", "bool get_usePhysicalValues() const", asMETHOD(Light, GetUsePhysicalValues), asCALL_THISCALL);
     engine->RegisterObjectMethod("Light", "void set_specularIntensity(float)", asMETHOD(Light, SetSpecularIntensity), asCALL_THISCALL);
     engine->RegisterObjectMethod("Light", "float get_specularIntensity() const", asMETHOD(Light, GetSpecularIntensity), asCALL_THISCALL);
     engine->RegisterObjectMethod("Light", "void set_brightness(float)", asMETHOD(Light, SetBrightness), asCALL_THISCALL);
@@ -1235,6 +1239,7 @@ static void RegisterLight(asIScriptEngine* engine)
     engine->RegisterObjectMethod("Light", "Frustum get_frustum() const", asMETHOD(Light, GetFrustum), asCALL_THISCALL);
     engine->RegisterObjectMethod("Light", "int get_numShadowSplits() const", asMETHOD(Light, GetNumShadowSplits), asCALL_THISCALL);
     engine->RegisterObjectMethod("Light", "bool get_negative() const", asMETHOD(Light, IsNegative), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Light", "Color get_colorFromTemperature() const", asMETHOD(Light, GetColorFromTemperature), asCALL_THISCALL);
     engine->RegisterObjectMethod("Light", "Color get_effectiveColor() const", asMETHOD(Light, GetEffectiveColor), asCALL_THISCALL);
     engine->RegisterObjectMethod("Light", "float get_effectiveSpecularIntensity() const", asMETHOD(Light, GetEffectiveSpecularIntensity), asCALL_THISCALL);
 }

+ 57 - 1
Source/Urho3D/Graphics/Light.cpp

@@ -56,6 +56,7 @@ static const float DEFAULT_SHADOWQUANTIZE = 0.5f;
 static const float DEFAULT_SHADOWMINVIEW = 3.0f;
 static const float DEFAULT_SHADOWNEARFARRATIO = 0.002f;
 static const float DEFAULT_SHADOWSPLIT = 1000.0f;
+static const float DEFAULT_TEMPERATURE = 6590.0f;
 
 static const char* typeNames[] =
 {
@@ -92,6 +93,7 @@ Light::Light(Context* context) :
     shadowCascade_(CascadeParameters(DEFAULT_SHADOWSPLIT, 0.0f, 0.0f, 0.0f, DEFAULT_SHADOWFADESTART)),
     shadowFocus_(FocusParameters(true, true, true, DEFAULT_SHADOWQUANTIZE, DEFAULT_SHADOWMINVIEW)),
     lightQueue_(0),
+    temperature_(6590.0f),
     specularIntensity_(DEFAULT_SPECULARINTENSITY),
     brightness_(DEFAULT_BRIGHTNESS),
     range_(DEFAULT_RANGE),
@@ -102,7 +104,8 @@ Light::Light(Context* context) :
     shadowIntensity_(0.0f),
     shadowResolution_(1.0f),
     shadowNearFarRatio_(DEFAULT_SHADOWNEARFARRATIO),
-    perVertex_(false)
+    perVertex_(false),
+    usePhysicalValues_(false)
 {
 }
 
@@ -117,6 +120,8 @@ void Light::RegisterObject(Context* context)
     URHO3D_ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
     URHO3D_ENUM_ACCESSOR_ATTRIBUTE("Light Type", GetLightType, SetLightType, LightType, typeNames, DEFAULT_LIGHTTYPE, AM_DEFAULT);
     URHO3D_ACCESSOR_ATTRIBUTE("Color", GetColor, SetColor, Color, Color::WHITE, AM_DEFAULT);
+    URHO3D_ACCESSOR_ATTRIBUTE("Temperature", GetTemperature, SetTemperature, float, DEFAULT_TEMPERATURE, AM_DEFAULT);
+    URHO3D_ATTRIBUTE("UsePhysicalValues", bool, usePhysicalValues_, false, AM_DEFAULT);
     URHO3D_ACCESSOR_ATTRIBUTE("Specular Intensity", GetSpecularIntensity, SetSpecularIntensity, float, DEFAULT_SPECULARINTENSITY,
         AM_DEFAULT);
     URHO3D_ACCESSOR_ATTRIBUTE("Brightness Multiplier", GetBrightness, SetBrightness, float, DEFAULT_BRIGHTNESS, AM_DEFAULT);
@@ -290,6 +295,18 @@ void Light::SetColor(const Color& color)
     MarkNetworkUpdate();
 }
 
+void Light::SetTemperature(float temperature)
+{
+    temperature_ = Clamp(temperature, 1000.0f, 10000.0f);
+    MarkNetworkUpdate();
+}
+
+void Light::SetUsePhysicalValues(bool enable)
+{
+    usePhysicalValues_ = enable;
+    MarkNetworkUpdate();
+}
+
 void Light::SetSpecularIntensity(float intensity)
 {
     specularIntensity_ = Max(intensity, 0.0f);
@@ -386,6 +403,45 @@ void Light::SetShapeTexture(Texture* texture)
     MarkNetworkUpdate();
 }
 
+Color Light::GetColorFromTemperature() const
+{
+    // Approximate Planckian locus in CIE 1960 UCS
+    float u = (0.860117757f + 1.54118254e-4f * temperature_ + 1.28641212e-7f * temperature_ * temperature_) /
+        (1.0f + 8.42420235e-4f * temperature_ + 7.08145163e-7f * temperature_ * temperature_);
+    float v = (0.317398726f + 4.22806245e-5f * temperature_ + 4.20481691e-8f * temperature_ * temperature_) /
+        (1.0f - 2.89741816e-5f * temperature_ + 1.61456053e-7f * temperature_ * temperature_);
+
+    float x = 3.0f * u / (2.0f * u - 8.0f * v + 4.0f);
+    float y = 2.0f * v / (2.0f * u - 8.0f * v + 4.0f);
+    float z = 1.0f - x - y;
+
+    float y_ = 1.0f;
+    float x_ = y_ / y * x;
+    float z_ = y_ / y * z;
+
+    float red = 3.2404542f * x_ + -1.5371385f * y_ + -0.4985314f * z_;
+    float green = -0.9692660f * x_ + 1.8760108f * y_ + 0.0415560f * z_;
+    float blue = 0.0556434f * x_ + -0.2040259f * y_ + 1.0572252f * z_;
+
+    return Color(red, green, blue);
+}
+
+Color Light::GetEffectiveColor() const
+{
+    if (usePhysicalValues_)
+    {
+        // Light color in kelvin.
+        Color tempColor = GetColorFromTemperature();
+        // Light brightness in lumens
+        float energy = (brightness_ * 4.0f * M_PI) * 16.0f / (100.0f * 100.0f) / M_PI;
+        return Color(tempColor.r_ * color_.r_ * energy, tempColor.g_ * color_.g_ * energy, tempColor.b_ * color_.b_ * energy, 1.0f);
+    }
+    else
+    {
+        return Color(color_ * brightness_, 1.0f);
+    }
+}
+
 Frustum Light::GetFrustum() const
 {
     // Note: frustum is unaffected by node or parent scale

+ 21 - 4
Source/Urho3D/Graphics/Light.h

@@ -178,9 +178,13 @@ public:
     void SetPerVertex(bool enable);
     /// Set color.
     void SetColor(const Color& color);
+    /// Set temperature of the light in Kelvin. Modulates the light color when "use physical values" is enabled.
+    void SetTemperature(float temperature);
+    /// Set use physical light values.
+    void SetUsePhysicalValues(bool enable);
     /// Set specular intensity. Zero disables specular calculations.
     void SetSpecularIntensity(float intensity);
-    /// Set light brightness multiplier. Both the color and specular intensity are multiplied with this to get final values for rendering.
+    /// Set light brightness multiplier. Both the color and specular intensity are multiplied with this. When "use physical values" is enabled, the value is specified in lumens.
     void SetBrightness(float brightness);
     /// Set range.
     void SetRange(float range);
@@ -218,14 +222,23 @@ public:
     /// Return color.
     const Color& GetColor() const { return color_; }
 
+    /// Return the temperature of the light in Kelvin.
+    float GetTemperature() const { return temperature_; }
+
+    /// Return if light uses temperature and brightness in lumens.
+    bool GetUsePhysicalValues() const { return usePhysicalValues_; }
+
+    /// Return the color value of the temperature in Kelvin.
+    Color GetColorFromTemperature() const;
+
     /// Return specular intensity.
     float GetSpecularIntensity() const { return specularIntensity_; }
 
-    /// Return brightness multiplier.
+    /// Return brightness multiplier. Specified in lumens when "use physical values" is enabled.
     float GetBrightness() const { return brightness_; }
 
-    /// Return effective color, multiplied by brightness. Do not multiply the alpha so that can compare against the default black color to detect a light with no effect.
-    Color GetEffectiveColor() const { return Color(color_ * brightness_, 1.0f); }
+    /// Return effective color, multiplied by brightness and affected by temperature when "use physical values" is enabled. Alpha is always 1 so that can compare against the default black color to detect a light with no effect.
+    Color GetEffectiveColor() const;
 
     /// Return effective specular intensity, multiplied by absolute value of brightness.
     float GetEffectiveSpecularIntensity() const { return specularIntensity_ * Abs(brightness_); }
@@ -319,6 +332,8 @@ private:
     LightType lightType_;
     /// Color.
     Color color_;
+    /// Light Temperature.
+    float temperature_;
     /// Shadow depth bias parameters.
     BiasParameters shadowBias_;
     /// Directional light cascaded shadow parameters.
@@ -355,6 +370,8 @@ private:
     float shadowNearFarRatio_;
     /// Per-vertex lighting flag.
     bool perVertex_;
+    /// Use physical light values flag.
+    bool usePhysicalValues_;
 };
 
 inline bool CompareLights(Light* lhs, Light* rhs)

+ 8 - 1
Source/Urho3D/LuaScript/pkgs/Graphics/Light.pkg

@@ -46,6 +46,8 @@ class Light : public Drawable
     void SetLightType(LightType type);
     void SetPerVertex(bool enable);
     void SetColor(const Color& color);
+    void SetTemperature(float temperature);
+    void SetUsePhysicalValues(bool enable);
     void SetSpecularIntensity(float intensity);
     void SetBrightness(float brightness);
     void SetRange(float range);
@@ -61,13 +63,16 @@ class Light : public Drawable
     void SetShadowNearFarRatio(float nearFarRatio);
     void SetRampTexture(Texture* texture);
     void SetShapeTexture(Texture* texture);
-    
+
     LightType GetLightType() const;
     bool GetPerVertex() const;
     const Color& GetColor() const;
+    float GetTemperature() const;
     float GetSpecularIntensity() const;
     float GetBrightness() const;
     Color GetEffectiveColor() const;
+    Color GetColorFromTemperature() const;
+    bool GetUsePhysicalValues() const;
     float GetEffectiveSpecularIntensity() const;
     float GetRange() const;
     float GetFov() const;
@@ -89,6 +94,8 @@ class Light : public Drawable
     tolua_property__get_set LightType lightType;
     tolua_property__get_set bool perVertex;
     tolua_property__get_set Color& color;
+    tolua_property__get_set float temperature;
+    tolua_property__get_set bool usePhysicalValues;
     tolua_property__get_set float specularIntensity;
     tolua_property__get_set float brightness;
     tolua_property__get_set float range;