rainbowfx iwa

This commit is contained in:
shun-iwasawa 2021-01-20 15:33:21 +09:00 committed by manongjohn
parent b0698e3da6
commit 33d08f0866
9 changed files with 3206 additions and 0 deletions

View file

@ -1430,6 +1430,15 @@
<item>"STD_iwa_BloomFx.alpha_mode" "Alpha Mode" </item>
<item>"STD_iwa_BloomFx.alpha_rendering" "Alpha Rendering" </item>
<item>"STD_iwa_RainbowFx" "Rainbow Iwa" </item>
<item>"STD_iwa_RainbowFx.center" "Center" </item>
<item>"STD_iwa_RainbowFx.radius" "Radius" </item>
<item>"STD_iwa_RainbowFx.intensity" "Intensity" </item>
<item>"STD_iwa_RainbowFx.width_scale" "Width Scale" </item>
<item>"STD_iwa_RainbowFx.inside" "Inside Intensity" </item>
<item>"STD_iwa_RainbowFx.secondary_rainbow" "Secondary Rainbow Intesity" </item>
<item>"STD_iwa_RainbowFx.alpha_rendering" "Alpha Rendering" </item>
<!------------------------------ Tiled Particles Iwa ------------------------------------------->
<item>STD_iwa_TiledParticlesFx "Tiled Particles Iwa" </item>

View file

@ -0,0 +1,11 @@
<fxlayout>
<page name="Rainbow Iwa">
<control>center</control>
<control>radius</control>
<control>intensity</control>
<control>width_scale</control>
<control>inside</control>
<control>secondary_rainbow</control>
<control>alpha_rendering</control>
</page>
</fxlayout>

View file

@ -126,6 +126,7 @@
STD_targetSpotFx
STD_iwa_GlareFx
STD_iwa_BloomFx
STD_iwa_RainbowFx
</Light>
<Matte>
STD_erodeDilateFx

View file

@ -68,6 +68,8 @@ public:
RAYLIT,
RAINBOW_WIDTH,
TYPESCOUNT
};

View file

@ -81,6 +81,8 @@ set(HEADERS
iwa_lineargradientfx.h
iwa_glarefx.h
iwa_fractalnoisefx.h
iwa_rainbow_intensity.h
iwa_rainbowfx.h
)
if(OpenCV_FOUND)
@ -272,6 +274,7 @@ set(SOURCES
iwa_lineargradientfx.cpp
iwa_glarefx.cpp
iwa_fractalnoisefx.cpp
iwa_rainbowfx.cpp
)
if(OpenCV_FOUND)

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,266 @@
#include "iwa_rainbowfx.h"
#include "iwa_cie_d65.h"
#include "iwa_xyz.h"
#include "iwa_rainbow_intensity.h"
#include "tparamuiconcept.h"
//--------------------------------------------------------------
double Iwa_RainbowFx::getSizePixelAmount(const double val,
const TAffine affine) {
/*--- Convert to vector --- */
TPointD vect;
vect.x = val;
vect.y = 0.0;
/*--- Apply geometrical transformation ---*/
// For the following lines I referred to lines 586-592 of
// sources/stdfx/motionblurfx.cpp
TAffine aff(affine);
aff.a13 = aff.a23 = 0; /* ignore translation */
vect = aff * vect;
/*--- return the length of the vector ---*/
return sqrt(vect.x * vect.x + vect.y * vect.y);
}
//------------------------------------------------------------
void Iwa_RainbowFx::buildRanbowColorMap(double3* core, double3* wide,
double intensity, double inside,
double secondary) {
auto clamp01 = [](double val) { return std::min(1.0, std::max(0.0, val)); };
int mapSize[2] = {301, 91};
// secondary rainbow : gradually darken from 133 to 136 degrees
double secondary_grad_range[2] = {133, 136};
// supernumerary rainbow inside the primary rainbow : gradually darken from
// 139.75 to 139.2 degrees
double inside_grad_width = 0.57;
double inside_grad_start[2] = {139.75, 139.2};
for (int m = 0; m < 2; m++) {
double3* out_p = (m == 0) ? core : wide;
double xyz_sum[3];
for (int a = 0; a < mapSize[m]; a++, out_p++) {
double angle = (m == 0) ? 120.0 + (double)a * 0.1 : 90.0 + (double)a;
double second_ratio = 1.0;
if (angle <= secondary_grad_range[0]) {
second_ratio = secondary;
} else if (angle < secondary_grad_range[1]) {
double r = (angle - (double)secondary_grad_range[0]) /
(double)(secondary_grad_range[1] - secondary_grad_range[0]);
second_ratio = (1.0 - r) * secondary + r;
}
xyz_sum[0] = 0.0;
xyz_sum[1] = 0.0;
xyz_sum[2] = 0.0;
// sum for each wavelength (in the range of visible light, 380nm-710nm)
for (int ram = 0; ram < 34; ram++) {
double start =
inside_grad_start[0] +
(inside_grad_start[1] - inside_grad_start[0]) * (double)ram / 33.0;
double end = start + inside_grad_width;
double inside_ratio = 1.0;
if (angle >= end) {
inside_ratio = inside;
} else if (angle > start) {
double r = (angle - start) / inside_grad_width;
inside_ratio = (1.0 - r) + r * inside;
}
double* data =
(m == 0) ? &rainbow_core_data[a][0] : &rainbow_wide_data[a][0];
// accumulate XYZ channel values
for (int c = 0; c < 3; c++)
xyz_sum[c] +=
cie_d65[ram] * data[ram] * xyz[ram * 3 + c] * inside_ratio;
}
double tmp_intensity = intensity * 25000.0 * second_ratio;
out_p->r = clamp01((3.240479 * xyz_sum[0] - 1.537150 * xyz_sum[1] -
0.498535 * xyz_sum[2]) *
tmp_intensity);
out_p->g = clamp01((-0.969256 * xyz_sum[0] + 1.875992 * xyz_sum[1] +
0.041556 * xyz_sum[2]) *
tmp_intensity);
out_p->b = clamp01((0.055648 * xyz_sum[0] - 0.204043 * xyz_sum[1] +
1.057311f * xyz_sum[2]) *
tmp_intensity);
}
}
}
//------------------------------------------------------------
template <typename RASTER, typename PIXEL>
void Iwa_RainbowFx::setOutputRaster(const RASTER ras, TDimensionI dim,
double3* outBuf_p) {
bool withAlpha = m_alpha_rendering->getValue();
double maxi = static_cast<double>(PIXEL::maxChannelValue); // 255or65535
double3* out_p = outBuf_p;
for (int j = 0; j < dim.ly; j++) {
PIXEL* pix = ras->pixels(j);
for (int i = 0; i < dim.lx; i++, out_p++, pix++) {
pix->r = (typename PIXEL::Channel)(out_p->r * (maxi + 0.999999));
pix->g = (typename PIXEL::Channel)(out_p->g * (maxi + 0.999999));
pix->b = (typename PIXEL::Channel)(out_p->b * (maxi + 0.999999));
if (withAlpha) {
double chan_a = std::max(std::max(out_p->r, out_p->g), out_p->b);
pix->m = (typename PIXEL::Channel)(chan_a * (maxi + 0.999999));
} else
pix->m = (typename PIXEL::Channel)(PIXEL::maxChannelValue);
}
}
}
//------------------------------------------------------------
Iwa_RainbowFx::Iwa_RainbowFx()
: m_center(TPointD(0.0, 0.0))
, m_radius(200.0)
, m_intensity(1.0)
, m_width_scale(1.0)
, m_inside(1.0)
, m_secondary_rainbow(1.0)
, m_alpha_rendering(false) {
bindParam(this, "center", m_center);
bindParam(this, "radius", m_radius);
bindParam(this, "intensity", m_intensity);
bindParam(this, "width_scale", m_width_scale);
bindParam(this, "inside", m_inside);
bindParam(this, "secondary_rainbow", m_secondary_rainbow);
bindParam(this, "alpha_rendering", m_alpha_rendering);
m_radius->setValueRange(0.0, std::numeric_limits<double>::max());
m_intensity->setValueRange(0.1, 10.0);
m_inside->setValueRange(0.0, 1.0);
m_secondary_rainbow->setValueRange(0.0, 10.0);
m_width_scale->setValueRange(0.1, 50.0);
}
//------------------------------------------------------------
bool Iwa_RainbowFx::doGetBBox(double frame, TRectD& bBox,
const TRenderSettings& ri) {
bBox = TConsts::infiniteRectD;
return true;
}
//------------------------------------------------------------
inline double3 Iwa_RainbowFx::angleToColor(double angle, double3* core,
double3* wide) {
// boundary conditions
if (angle <= 90.0)
return wide[0];
else if (angle >= 180.0)
return wide[90];
// inside of the range of hi-res color table
if (angle > 120.0 && angle < 150.0) {
double tablePos = (angle - 120) / 0.1;
int tableId = (int)std::floor(tablePos);
double ratio = tablePos - (double)tableId;
return {core[tableId].r * (1.0 - ratio) + core[tableId + 1].r * ratio,
core[tableId].g * (1.0 - ratio) + core[tableId + 1].g * ratio,
core[tableId].b * (1.0 - ratio) + core[tableId + 1].b * ratio};
}
// low-res color table
double tablePos = (angle - 90) / 1.0;
int tableId = (int)std::floor(tablePos);
double ratio = tablePos - (double)tableId;
return {wide[tableId].r * (1.0 - ratio) + wide[tableId + 1].r * ratio,
wide[tableId].g * (1.0 - ratio) + wide[tableId + 1].g * ratio,
wide[tableId].b * (1.0 - ratio) + wide[tableId + 1].b * ratio};
}
//------------------------------------------------------------
void Iwa_RainbowFx::doCompute(TTile& tile, double frame,
const TRenderSettings& ri) {
// build raibow color map
TRasterGR8P rainbowColorCore_ras(sizeof(double3) * 301, 1);
rainbowColorCore_ras->lock();
double3* rainbowColorCore_p = (double3*)rainbowColorCore_ras->getRawData();
TRasterGR8P rainbowColorWide_ras(sizeof(double3) * 91, 1);
rainbowColorWide_ras->lock();
double3* rainbowColorWide_p = (double3*)rainbowColorWide_ras->getRawData();
double intensity = m_intensity->getValue(frame);
double inside = m_inside->getValue(frame);
double secondary = m_secondary_rainbow->getValue(frame);
buildRanbowColorMap(rainbowColorCore_p, rainbowColorWide_p, intensity, inside,
secondary);
// convert center position to render region coordinate
TAffine aff = ri.m_affine;
TDimensionI dimOut(tile.getRaster()->getLx(), tile.getRaster()->getLy());
TPointD dimOffset((float)dimOut.lx / 2.0f, (float)dimOut.ly / 2.0f);
TPointD centerPos = m_center->getValue(frame);
centerPos = aff * centerPos - (tile.m_pos + tile.getRaster()->getCenterD()) +
dimOffset;
// result image buffer
TRasterGR8P outBuf_ras(sizeof(double3) * dimOut.lx * dimOut.ly, 1);
outBuf_ras->lock();
double3* outBuf_p = (double3*)outBuf_ras->getRawData();
double theta_peak = 41.3;
double peakRadius =
getSizePixelAmount(m_radius->getValue(frame), ri.m_affine);
double anglePerPixel = theta_peak / peakRadius;
double widthScale = m_width_scale->getValue(frame);
double3* out_p = outBuf_p;
// loop for all pixels
for (int y = 0; y < dimOut.ly; y++) {
for (int x = 0; x < dimOut.lx; x++, out_p++) {
// convert pixel distance from the center to scattering angle (degrees)
double s = x - centerPos.x;
double t = y - centerPos.y;
double theta = std::sqrt(s * s + t * t) * anglePerPixel;
double phi = 180.0 - theta_peak + (theta_peak - theta) / widthScale;
*out_p = angleToColor(phi, rainbowColorCore_p, rainbowColorWide_p);
}
}
rainbowColorCore_ras->unlock();
rainbowColorWide_ras->unlock();
// convert to channel values
tile.getRaster()->clear();
TRaster32P outRas32 = (TRaster32P)tile.getRaster();
TRaster64P outRas64 = (TRaster64P)tile.getRaster();
if (outRas32)
setOutputRaster<TRaster32P, TPixel32>(outRas32, dimOut, outBuf_p);
else if (outRas64)
setOutputRaster<TRaster64P, TPixel64>(outRas64, dimOut, outBuf_p);
outBuf_ras->unlock();
}
//------------------------------------------------------------
void Iwa_RainbowFx::getParamUIs(TParamUIConcept*& concepts, int& length) {
concepts = new TParamUIConcept[length = 3];
concepts[0].m_type = TParamUIConcept::POINT;
concepts[0].m_label = "Center";
concepts[0].m_params.push_back(m_center);
concepts[1].m_type = TParamUIConcept::RADIUS;
concepts[1].m_label = "Radius";
concepts[1].m_params.push_back(m_radius);
concepts[1].m_params.push_back(m_center);
concepts[2].m_type = TParamUIConcept::RAINBOW_WIDTH;
concepts[2].m_label = "Width";
concepts[2].m_params.push_back(m_width_scale);
concepts[2].m_params.push_back(m_radius);
concepts[2].m_params.push_back(m_center);
}
//==============================================================================
FX_PLUGIN_IDENTIFIER(Iwa_RainbowFx, "iwa_RainbowFx");

View file

@ -0,0 +1,45 @@
#pragma once
#ifndef IWA_CORRIDORGRADIENTFX_H
#define IWA_CORRIDORGRADIENTFX_H
#include "tfxparam.h"
#include "stdfx.h"
#include "tparamset.h"
struct double3 {
double r, g, b;
};
class Iwa_RainbowFx final : public TStandardZeraryFx {
FX_PLUGIN_DECLARATION(Iwa_RainbowFx)
TPointParamP m_center;
TDoubleParamP m_intensity;
TDoubleParamP m_radius;
TDoubleParamP m_width_scale;
TDoubleParamP m_inside;
TDoubleParamP m_secondary_rainbow;
TBoolParamP m_alpha_rendering;
double getSizePixelAmount(const double val, const TAffine affine);
void buildRanbowColorMap(double3 *core, double3 *wide, double intensity,
double inside, double secondary);
inline double3 angleToColor(double angle, double3 *core, double3 *wide);
template <typename RASTER, typename PIXEL>
void setOutputRaster(const RASTER ras, TDimensionI dim, double3 *outBuf_p);
public:
Iwa_RainbowFx();
bool canHandle(const TRenderSettings &info, double frame) override {
return true;
}
bool doGetBBox(double frame, TRectD &bBox,
const TRenderSettings &ri) override;
void doCompute(TTile &tile, double frame, const TRenderSettings &ri) override;
void getParamUIs(TParamUIConcept *&concepts, int &length) override;
};
#endif

View file

@ -1699,6 +1699,101 @@ void RayLitFxGadget::leftButtonUp(const TPointD &pos, const TMouseEvent &) {
m_handle = None;
}
//=============================================================================
class RainbowWidthFxGadget final : public FxGadget {
TDoubleParamP m_widthScale;
TDoubleParamP m_radius;
TPointParamP m_center;
enum HANDLE { Outside = 0, Inside, None } m_handle = None;
public:
RainbowWidthFxGadget(FxGadgetController *controller,
const TDoubleParamP &widthScale,
const TDoubleParamP &radius, const TPointParamP &center)
: FxGadget(controller, 2)
, m_widthScale(widthScale)
, m_radius(radius)
, m_center(center) {
addParam(widthScale);
}
void draw(bool picking) override;
void leftButtonDown(const TPointD &pos, const TMouseEvent &) override;
void leftButtonDrag(const TPointD &pos, const TMouseEvent &) override;
void leftButtonUp(const TPointD &pos, const TMouseEvent &) override;
};
//---------------------------------------------------------------------------
void RainbowWidthFxGadget::draw(bool picking) {
setPixelSize();
if (isSelected())
glColor3dv(m_selectedColor);
else
glColor3d(0, 0, 1);
double radius = getValue(m_radius);
TPointD center = getValue(m_center);
double widthScale = getValue(m_widthScale);
double w = widthScale * radius / 41.3;
glPushName(getId() + Outside);
glLineStipple(1, 0x1C47);
glEnable(GL_LINE_STIPPLE);
tglDrawCircle(center, radius + w);
glDisable(GL_LINE_STIPPLE);
drawDot(center + TPointD(0.707, 0.707) * (radius + w));
glPopName();
if (isSelected(Outside)) {
drawTooltip(center + TPointD(0.707, 0.707) * (radius + w), getLabel());
}
glPushName(getId() + Inside);
glLineStipple(1, 0x1C47);
glEnable(GL_LINE_STIPPLE);
tglDrawCircle(center, radius - w);
glDisable(GL_LINE_STIPPLE);
drawDot(center + TPointD(0.707, 0.707) * (radius - w));
glPopName();
if (isSelected(Inside)) {
drawTooltip(center + TPointD(0.707, 0.707) * (radius - w), getLabel());
}
}
//---------------------------------------------------------------------------
void RainbowWidthFxGadget::leftButtonDown(const TPointD &pos,
const TMouseEvent &) {
m_handle = (HANDLE)m_selected;
}
//---------------------------------------------------------------------------
void RainbowWidthFxGadget::leftButtonDrag(const TPointD &pos,
const TMouseEvent &) {
if (m_handle == None) return;
double radius = getValue(m_radius);
double wpos = norm(pos - getValue(m_center));
double width = (m_handle == Outside) ? wpos - radius : radius - wpos;
double scale = (width * 41.3) / (radius * 1.0);
double min, max, step;
m_widthScale->getValueRange(min, max, step);
setValue(m_widthScale, std::min(max, std::max(min, scale)));
}
//---------------------------------------------------------------------------
void RainbowWidthFxGadget::leftButtonUp(const TPointD &pos,
const TMouseEvent &) {}
//*************************************************************************************
// FxGadgetController implementation
//*************************************************************************************
@ -1889,6 +1984,14 @@ FxGadget *FxGadgetController::allocateGadget(const TParamUIConcept &uiConcept) {
gadget = new RayLitFxGadget(this, uiConcept.m_params[0]);
break;
}
case TParamUIConcept::RAINBOW_WIDTH: {
assert(uiConcept.m_params.size() == 3);
gadget =
new RainbowWidthFxGadget(this, uiConcept.m_params[0],
uiConcept.m_params[1], uiConcept.m_params[2]);
break;
}
default:
break;
}