[x3d-public] Which color space for Background color interpolation

Andreas Plesch andreasplesch at gmail.com
Thu Oct 15 13:38:21 PDT 2020


On Thu, Oct 15, 2020 at 1:17 PM Michalis Kamburelis
<michalis.kambi at gmail.com> wrote:
>
> Castle Game Engine right now uses RGB interpolation for Background
> colors. So this seems to match the conclusion :)
>
> I agree the interpolation method should be said in the X3D spec explicitly.

I think just adding 'in RGB space' would suffice. I could prepare a PR.

> For CGE, we interpolate in RGB for implementation+simplicity reasons.
> We just render sky and ground as a sphere mesh, with rings at
> appropriate angles (corresponding to sky/ground angles in Background).
> The colors specified at Background node are simply used for the mesh
> vertexes, and then we allow GPU to interpolate them linearly as usual
> for shader varying attributes. So, without really thinking, I
> implemented interpolation in RGB space -- simply by letting GPU do it
> in the simplest fashion.

Yes, this is how x_ite does it as well. x3dom uses a standard sphere
and constructs a 1x512px texture from the color arrays, using color
interpolation.

I am using now this approach to Hue interpolation:

https://github.com/andreasplesch/x3dom/blob/Interp/src/nodes/Interpolation/ColorInterpolator.js#L75

1) a is the first hue, b is the second hue, t is the fraction
2) ensure that b is larger than a by adding 360 to b if necessary
3) if now the difference between a and b is less than 180, we are good
and can just interpolate.
4) if not, add 360 to a. Now a is larger and going back to b will be
on the shorter path. This means again we can just interpolate using t.
This is the tricky part but I think it is correct.

For translating back to RGB we need a mod360 on the resulting,
interpolated Hue. I think it helps to realize that this is possible
since it simplifies the algorithm.

I tested on a few keyValue and it seems to produce the same values as
the wasteful slerp approach. Any second look will be much appreciated.

> Regards,
> Michalis
>
> śr., 14 paź 2020 o 04:49 Andreas Plesch <andreasplesch at gmail.com> napisał(a):
> >
> > As a result of this exploration, x3dom will use HSV space for
> > ColorInterpolator as required, and RGB space for Background color
> > interpolation. Since x3dom uses ColorInterpolator internally for
> > Background, there will be an additional custom field for
> > ColorInterpolator which lets an author choose to use RGB space there
> > as well, SFBool "RGB" false. This option may be handy if gray scale
> > colors need to be interpolated.
> >
> > Cheers, -Andreas
> >
> > On Tue, Oct 13, 2020 at 4:53 PM Andreas Plesch <andreasplesch at gmail.com> wrote:
> > >
> > > Tests indicate that just using Quaternion slerp for Hue interpolation
> > > (seen as 3d rotations around a fixed axis)  works. Less code but some
> > > useless overhead.
> > >
> > > I also tested Background color interpolation in HSV space with these
> > > backgrounds:
> > >
> > > https://x3dgraphics.com/examples/X3dForAdvancedModeling/Visualization/BackgroundCollectionIndex.html
> > >
> > > Many involve pure grey scale colors which do not have a well defined
> > > hue and therefore give unexpected results during interpolation. They
> > > can be easily tweaked to shift the grey scale colors just a bit to the
> > > expected hue (say very dark blue) and they then look like the short
> > > descriptive name.
> > >
> > > But it seems that these examples were constructed with RGB color space
> > > interpolation in mind. So this is probably what the spec. intended.
> > >
> > > So perhaps it is not too late to add "in RGB color space" to
> > >
> > > https://www.web3d.org/documents/specifications/19775-1/V3.3/Part01/components/enveffects.html#Backgrounds
> > >
> > > ?
> > >
> > > -Andreas
> > >
> > > On Tue, Oct 13, 2020 at 3:25 PM Andreas Plesch <andreasplesch at gmail.com> wrote:
> > > >
> > > > For the Background sphere x_ite just assigns the (closest) color to
> > > > the row of vertices at the corresponding angle position on a
> > > > constructed sphere and then lets GL do the interpolation:
> > > >
> > > > https://github.com/create3000/x_ite/blob/4a900d8b36bfb4aac66671554cc2f4db634efa37/src/x_ite/Components/EnvironmentalEffects/X3DBackgroundNode.js#L385
> > > >
> > > > So this means linear interpolation in RGB space.
> > > >
> > > > -Andreas
> > > >
> > > > On Tue, Oct 13, 2020 at 1:35 PM Andreas Plesch <andreasplesch at gmail.com> wrote:
> > > > >
> > > > > Thanks, Michalis. I should check x-ite also:
> > > > >
> > > > > https://github.com/create3000/x_ite/blob/382252fbc23115803a0fa621e4fae1326277e148/src/standard/Math/Numbers/Color3.js#L222
> > > > >
> > > > > It looks like it is also trying to do something like that.
> > > > >
> > > > > Is CGE interpolating in RGB space for skyColor/groundColor interpolation ?
> > > > >
> > > > > Thanks, -Andreas
> > > > >
> > > > > On Tue, Oct 13, 2020 at 9:03 AM Michalis Kamburelis
> > > > > <michalis.kambi at gmail.com> wrote:
> > > > > >
> > > > > > CGE implementation takes care to interpolate hue along the shortest
> > > > > > path (i.e. sometimes clockwise, sometimes counter-clockwise). See
> > > > > > https://github.com/castle-engine/castle-engine/blob/master/src/base/castlecolors.pas#L373
> > > > > > .
> > > > > >
> > > > > > Indeed the code is a little ugly, needs to look separately at 3 cases:
> > > > > >
> > > > > > - if HueDiff > 3 then
> > > > > > - f HueDiff < -3 then
> > > > > > - otherwise
> > > > > >
> > > > > > Regards,
> > > > > > Michalis
> > > > > >
> > > > > > wt., 13 paź 2020 o 06:32 Andreas Plesch <andreasplesch at gmail.com> napisał(a):
> > > > > > >
> > > > > > > While trying to linearly interpolate in HSV space, a question came up
> > > > > > > for me with regards to Hue. Hue is represented as an angle in a color
> > > > > > > circle. This means there are two ways to linearly interpolate, in the
> > > > > > > acute segment defined by the two hues, or in the obtuse segment. Say
> > > > > > > the first hue is 10 degrees and the second 300. At a fraction of 0.5,
> > > > > > > the interpolated value may be 155 ( in the obtuse segment ) or 335 (
> > > > > > > in the acute segment ).
> > > > > > >
> > > > > > > One rule may be to always go counterclockwise in the circle, eg. from
> > > > > > > 10 to 300 in the example. But then it is hard to predict if the
> > > > > > > interpolation between two similar shades of a color (say for a blue
> > > > > > > sky) results in similar shades, or actually goes around most of the
> > > > > > > full color circle, generating a very colorful result.
> > > > > > >
> > > > > > > So a more reasonable rule may be to always interpolate in the acute
> > > > > > > segment, clockwise or counterclockwise as necessary. I think slerp
> > > > > > > always takes the shorter path as well.
> > > > > > >
> > > > > > > I hope that there is a more straightforward interpretation of what it
> > > > > > > means to interpolate in HSV space, and I just overlooked something.
> > > > > > >
> > > > > > > -Andreas
> > > > > > >
> > > > > > > On Mon, Oct 12, 2020 at 6:17 PM Andreas Plesch <andreasplesch at gmail.com> wrote:
> > > > > > > >
> > > > > > > > The Background specification in
> > > > > > > >
> > > > > > > > https://www.web3d.org/documents/specifications/19775-1/V3.3/Part01/components/enveffects.html#Backgrounds
> > > > > > > >
> > > > > > > > states that
> > > > > > > >
> > > > > > > > "The sky colour is linearly interpolated between the specified
> > > > > > > > skyColor values." and
> > > > > > > > "The ground colour is linearly interpolated between the specified
> > > > > > > > groundColor values."
> > > > > > > >
> > > > > > > > The ColorInterpolator specification in
> > > > > > > >
> > > > > > > > https://www.web3d.org/documents/specifications/19775-1/V3.3/Part01/components/interp.html#ColorInterpolator
> > > > > > > >
> > > > > > > > states that
> > > > > > > >
> > > > > > > > "A linear interpolation using the value of set_fraction as input is
> > > > > > > > performed in HSV space."
> > > > > > > >
> > > > > > > > The ColorInterpolator language is more precise in giving the space in
> > > > > > > > which linear interpolation should be performed.
> > > > > > > >
> > > > > > > > I think there is a need to also become more precise for Background
> > > > > > > > color interpolation. The consistent approach would be to choose HSV
> > > > > > > > space there as well:
> > > > > > > >
> > > > > > > > "The sky colour is linearly interpolated in HSV space between the
> > > > > > > > specified skyColor values." and
> > > > > > > > "The ground colour is linearly interpolated in HSV space between the
> > > > > > > > specified groundColor values."
> > > > > > > >
> > > > > > > > HSV space probably also leads to more aesthetically pleasing backgrounds.
> > > > > > > >
> > > > > > > > [x3dom currently performs all color interpolation in RGB space].
> > > > > > > >
> > > > > > > > Do we need such a clarification ?
> > > > > > > >
> > > > > > > > -Andreas
> > > > > > > > --
> > > > > > > > Andreas Plesch
> > > > > > > > Waltham, MA 02453
> > > > > > >
> > > > > > >
> > > > > > >
> > > > > > > --
> > > > > > > Andreas Plesch
> > > > > > > Waltham, MA 02453
> > > > > > >
> > > > > > > _______________________________________________
> > > > > > > x3d-public mailing list
> > > > > > > x3d-public at web3d.org
> > > > > > > http://web3d.org/mailman/listinfo/x3d-public_web3d.org
> > > > >
> > > > >
> > > > >
> > > > > --
> > > > > Andreas Plesch
> > > > > Waltham, MA 02453
> > > >
> > > >
> > > >
> > > > --
> > > > Andreas Plesch
> > > > Waltham, MA 02453
> > >
> > >
> > >
> > > --
> > > Andreas Plesch
> > > Waltham, MA 02453
> >
> >
> >
> > --
> > Andreas Plesch
> > Waltham, MA 02453



-- 
Andreas Plesch
Waltham, MA 02453



More information about the x3d-public mailing list