Units: add support for light power units in Watt. Use for Eevee lights.

This affects point, spot and area lights. Sun light strength remains without
a unit. This change does not affect .blend file compatibility in any way, as
with the rest of the unit system it's purely a display and editing feature.

Not used for Cycles yet, that will be done after unifying the settings with
Eevee.
This commit is contained in:
Brecht Van Lommel 2019-02-08 12:31:28 +01:00
parent 6c24de95c0
commit ce5d079a49
9 changed files with 66 additions and 20 deletions

View File

@ -77,7 +77,8 @@ enum {
B_UNIT_VELOCITY = 7,
B_UNIT_ACCELERATION = 8,
B_UNIT_CAMERA = 9,
B_UNIT_TYPE_TOT = 10,
B_UNIT_POWER = 10,
B_UNIT_TYPE_TOT = 11,
};
#ifdef __cplusplus

View File

@ -1801,6 +1801,7 @@ double BKE_scene_unit_scale(const UnitSettings *unit, const int unit_type, doubl
case B_UNIT_LENGTH:
return value * (double)unit->scale_length;
case B_UNIT_AREA:
case B_UNIT_POWER:
return value * pow(unit->scale_length, 2);
case B_UNIT_VOLUME:
return value * pow(unit->scale_length, 3);

View File

@ -299,13 +299,25 @@ static struct bUnitDef buCameraLenDef[] = {
};
static struct bUnitCollection buCameraLenCollection = {buCameraLenDef, 3, 0, UNIT_COLLECTION_LENGTH(buCameraLenDef)};
/* (Light) Power */
static struct bUnitDef buPowerDef[] = {
{"gigawatt", "gigawatts", "GW", NULL, "Gigawatts", NULL, 1e9f, 0.0, B_UNIT_DEF_NONE},
{"megawatt", "megawatts", "MW", NULL, "Megawatts", NULL, 1e6f, 0.0, B_UNIT_DEF_NONE},
{"kilowatt", "kilowatts", "kW", NULL, "Kilowatts", NULL, 1e3f, 0.0, B_UNIT_DEF_SUPPRESS},
{"watt", "watts", "W", NULL, "Watts", NULL, 1.0f, 0.0, B_UNIT_DEF_NONE},
{"milliwatt", "milliwatts", "mW", NULL, "Milliwatts", NULL, 1e-3f, 0.0, B_UNIT_DEF_NONE},
{"microwatt", "microwatts", "µW", "uW", "Microwatts", NULL, 1e-6f, 0.0, B_UNIT_DEF_NONE},
{"nanowatt", "nanowatts", "nW", NULL, "Nannowatts", NULL, 1e-9f, 0.0, B_UNIT_DEF_NONE},
};
static struct bUnitCollection buPowerCollection = {buPowerDef, 3, 0, UNIT_COLLECTION_LENGTH(buPowerDef)};
#define UNIT_SYSTEM_TOT (((sizeof(bUnitSystems) / B_UNIT_TYPE_TOT) / sizeof(void *)) - 1)
static const struct bUnitCollection *bUnitSystems[][B_UNIT_TYPE_TOT] = {
{NULL, NULL, NULL, NULL, NULL, &buNaturalRotCollection, &buNaturalTimeCollection, NULL, NULL, NULL},
{NULL, &buMetricLenCollection, &buMetricAreaCollection, &buMetricVolCollection, &buMetricMassCollection, &buNaturalRotCollection, &buNaturalTimeCollection, &buMetricVelCollection, &buMetricAclCollection, &buCameraLenCollection}, /* metric */
{NULL, &buImperialLenCollection, &buImperialAreaCollection, &buImperialVolCollection, &buImperialMassCollection, &buNaturalRotCollection, &buNaturalTimeCollection, &buImperialVelCollection, &buImperialAclCollection, &buCameraLenCollection}, /* imperial */
{NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
{NULL, NULL, NULL, NULL, NULL, &buNaturalRotCollection, &buNaturalTimeCollection, NULL, NULL, NULL, NULL},
{NULL, &buMetricLenCollection, &buMetricAreaCollection, &buMetricVolCollection, &buMetricMassCollection, &buNaturalRotCollection, &buNaturalTimeCollection, &buMetricVelCollection, &buMetricAclCollection, &buCameraLenCollection, &buPowerCollection}, /* metric */
{NULL, &buImperialLenCollection, &buImperialAreaCollection, &buImperialVolCollection, &buImperialMassCollection, &buNaturalRotCollection, &buNaturalTimeCollection, &buImperialVelCollection, &buImperialAclCollection, &buCameraLenCollection, &buPowerCollection}, /* imperial */
{NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
};
@ -570,7 +582,8 @@ static const char *unit_find_str(const char *str, const char *substr)
{
if (substr && substr[0] != '\0') {
while (true) {
const char *str_found = strstr(str, substr);
/* Unit detection is case insensitive. */
const char *str_found = BLI_strcasestr(str, substr);
if (str_found) {
/* Previous char cannot be a letter. */
@ -774,7 +787,7 @@ bool bUnit_ReplaceString(char *str, int len_max, const char *str_prev, double sc
char str_tmp[TEMP_STR_SIZE];
bool changed = false;
/* make lowercase */
/* Convert to lowercase, to make unit detection case insensitive. */
BLI_str_tolower_ascii(str, len_max);
/* Try to find a default unit from current or previous string. */

View File

@ -90,6 +90,7 @@ typedef enum PropertyUnit {
PROP_UNIT_VELOCITY = (7 << 16), /* m/s */
PROP_UNIT_ACCELERATION = (8 << 16), /* m/(s^2) */
PROP_UNIT_CAMERA = (9 << 16), /* mm */
PROP_UNIT_POWER = (10 << 16), /* W */
} PropertyUnit;
#define RNA_SUBTYPE_UNIT(subtype) ((subtype) & 0x00FF0000)
@ -146,6 +147,9 @@ typedef enum PropertySubType {
/* booleans */
PROP_LAYER = 40,
PROP_LAYER_MEMBER = 41,
/* light */
PROP_POWER = 42 | PROP_UNIT_POWER,
} PropertySubType;
/* Make sure enums are updated with these */

View File

@ -2579,6 +2579,7 @@ static const char *rna_property_subtypename(PropertySubType type)
case PROP_LAYER: return "PROP_LAYER";
case PROP_LAYER_MEMBER: return "PROP_LAYER_MEMBER";
case PROP_PASSWORD: return "PROP_PASSWORD";
case PROP_POWER: return "PROP_POWER";
default:
{
/* in case we don't have a type preset that includes the subtype */
@ -2605,6 +2606,7 @@ static const char *rna_property_subtype_unit(PropertySubType type)
case PROP_UNIT_VELOCITY: return "PROP_UNIT_VELOCITY";
case PROP_UNIT_ACCELERATION: return "PROP_UNIT_ACCELERATION";
case PROP_UNIT_CAMERA: return "PROP_UNIT_CAMERA";
case PROP_UNIT_POWER: return "PROP_UNIT_POWER";
default: return "PROP_UNIT_UNKNOWN";
}
}

View File

@ -137,12 +137,6 @@ static void rna_def_light(BlenderRNA *brna)
"Falloff distance - the light is at half the original intensity at this point");
RNA_def_property_update(prop, 0, "rna_Light_draw_update");
prop = RNA_def_property(srna, "energy", PROP_FLOAT, PROP_NONE);
RNA_def_property_float_default(prop, 10.0f);
RNA_def_property_ui_range(prop, 0, 1000000.0f, 1, 3);
RNA_def_property_ui_text(prop, "Energy", "Amount of light emitted");
RNA_def_property_update(prop, 0, "rna_Light_draw_update");
prop = RNA_def_property(srna, "color", PROP_FLOAT, PROP_COLOR);
RNA_def_property_float_sdna(prop, NULL, "r");
RNA_def_property_array(prop, 3);
@ -187,6 +181,30 @@ static void rna_def_light(BlenderRNA *brna)
rna_def_animdata_common(srna);
}
static void rna_def_light_energy(StructRNA *srna, bool distant)
{
PropertyRNA *prop;
if(distant) {
/* Distant light strength has no unit defined, it's proportional to
* Watt/m^2 and is not sensitive to scene unit scale. */
prop = RNA_def_property(srna, "energy", PROP_FLOAT, PROP_NONE);
RNA_def_property_float_default(prop, 10.0f);
RNA_def_property_ui_range(prop, 0.0f, 10.0f, 1, 3);
RNA_def_property_ui_text(prop, "Strength", "Amount of light emitted");
RNA_def_property_update(prop, 0, "rna_Light_draw_update");
}
else {
/* Lights with a location have power in Watt, which is sensitive to
* scene unit scale. */
prop = RNA_def_property(srna, "energy", PROP_FLOAT, PROP_POWER);
RNA_def_property_float_default(prop, 10.0f);
RNA_def_property_ui_range(prop, 0.0f, 1000000.0f, 1, 5);
RNA_def_property_ui_text(prop, "Power", "Amount of light emitted");
RNA_def_property_update(prop, 0, "rna_Light_draw_update");
}
}
static void rna_def_light_falloff(StructRNA *srna)
{
PropertyRNA *prop;
@ -245,7 +263,7 @@ static void rna_def_light_falloff(StructRNA *srna)
RNA_def_property_update(prop, 0, "rna_Light_draw_update");
}
static void rna_def_light_shadow(StructRNA *srna, int sun)
static void rna_def_light_shadow(StructRNA *srna, bool sun)
{
PropertyRNA *prop;
@ -405,8 +423,9 @@ static void rna_def_point_light(BlenderRNA *brna)
RNA_def_struct_ui_text(srna, "Point Light", "Omnidirectional point Light");
RNA_def_struct_ui_icon(srna, ICON_LIGHT_POINT);
rna_def_light_energy(srna, false);
rna_def_light_falloff(srna);
rna_def_light_shadow(srna, 0);
rna_def_light_shadow(srna, false);
}
static void rna_def_area_light(BlenderRNA *brna)
@ -427,7 +446,8 @@ static void rna_def_area_light(BlenderRNA *brna)
RNA_def_struct_ui_text(srna, "Area Light", "Directional area Light");
RNA_def_struct_ui_icon(srna, ICON_LIGHT_AREA);
rna_def_light_shadow(srna, 0);
rna_def_light_energy(srna, false);
rna_def_light_shadow(srna, false);
rna_def_light_falloff(srna);
prop = RNA_def_property(srna, "shape", PROP_ENUM, PROP_NONE);
@ -464,8 +484,9 @@ static void rna_def_spot_light(BlenderRNA *brna)
RNA_def_struct_ui_text(srna, "Spot Light", "Directional cone Light");
RNA_def_struct_ui_icon(srna, ICON_LIGHT_SPOT);
rna_def_light_energy(srna, false);
rna_def_light_falloff(srna);
rna_def_light_shadow(srna, 0);
rna_def_light_shadow(srna, false);
prop = RNA_def_property(srna, "use_square", PROP_BOOLEAN, PROP_NONE);
RNA_def_property_boolean_sdna(prop, NULL, "mode", LA_SQUARE);
@ -502,7 +523,8 @@ static void rna_def_sun_light(BlenderRNA *brna)
RNA_def_struct_ui_text(srna, "Sun Light", "Constant direction parallel ray Light");
RNA_def_struct_ui_icon(srna, ICON_LIGHT_SUN);
rna_def_light_shadow(srna, 1);
rna_def_light_energy(srna, true);
rna_def_light_shadow(srna, true);
}
void RNA_def_light(BlenderRNA *brna)

View File

@ -116,6 +116,7 @@ const EnumPropertyItem rna_enum_property_unit_items[] = {
{PROP_UNIT_ACCELERATION, "ACCELERATION", 0, "Acceleration", ""},
{PROP_UNIT_MASS, "MASS", 0, "Mass", ""},
{PROP_UNIT_CAMERA, "CAMERA", 0, "Camera", ""},
{PROP_UNIT_POWER, "POWER", 0, "Power", ""},
{0, NULL, 0, NULL, NULL},
};

View File

@ -131,6 +131,7 @@ static const EnumPropertyItem property_subtype_array_items[] = {
{PROP_COLOR_GAMMA, "COLOR_GAMMA", 0, "Color Gamma", ""},
{PROP_LAYER, "LAYER", 0, "Layer", ""},
{PROP_LAYER_MEMBER, "LAYER_MEMBER", 0, "Layer Member", ""},
{PROP_POWER, "Power", 0, "Power", ""},
{PROP_NONE, "NONE", 0, "None", ""},
{0, NULL, 0, NULL, NULL},
@ -139,7 +140,7 @@ static const EnumPropertyItem property_subtype_array_items[] = {
#define BPY_PROPDEF_SUBTYPE_ARRAY_DOC \
" :arg subtype: Enumerator in ['COLOR', 'TRANSLATION', 'DIRECTION', " \
"'VELOCITY', 'ACCELERATION', 'MATRIX', 'EULER', 'QUATERNION', 'AXISANGLE', " \
"'XYZ', 'COLOR_GAMMA', 'LAYER', 'LAYER_MEMBER', 'NONE'].\n" \
"'XYZ', 'COLOR_GAMMA', 'LAYER', 'LAYER_MEMBER', 'POWER', 'NONE'].\n" \
" :type subtype: string\n"
/* PyObject's */
@ -1902,7 +1903,7 @@ static void bpy_prop_callback_assign_enum(struct PropertyRNA *prop, PyObject *ge
" :type description: string\n" \
#define BPY_PROPDEF_UNIT_DOC \
" :arg unit: Enumerator in ['NONE', 'LENGTH', 'AREA', 'VOLUME', 'ROTATION', 'TIME', 'VELOCITY', 'ACCELERATION', 'MASS', 'CAMERA'].\n" \
" :arg unit: Enumerator in ['NONE', 'LENGTH', 'AREA', 'VOLUME', 'ROTATION', 'TIME', 'VELOCITY', 'ACCELERATION', 'MASS', 'CAMERA', 'POWER'].\n" \
" :type unit: string\n" \
#define BPY_PROPDEF_NUM_MIN_DOC \

View File

@ -59,6 +59,7 @@ static const char *bpyunits_ucategorie_items[] = {
"VELOCITY",
"ACCELERATION",
"CAMERA",
"POWER",
NULL,
};