miércoles, 30 de octubre de 2013


: Incrementaly changing an object's color

Postby Xray » Fri Oct 18, 2013 7:07 am
Sure, very simple. There are two types of color properties in every geometry. They are "color" and "colorHSVA". You can change a color's hue, saturation, and opacity with either one, although in a slightly different manner. For the purpose of answering your question, I will use "color" for my example because that's the one that I am most familiar with and have most experience with. The color value is a small array of 4 elements which looks like this: color = [1.0,1.0,1.0,1.0]. Each element can have a value between 0 and 1.0. Reading from left to right, the elements are Red, Green, Blue, Opacity. In the example above, the color is pure white with a maximum opacity value of 1.0 which makes it opaque. The opacity value is a percentage divided by 100. So, for example, color = [1,0,0,0.5] is pure red with an opacity percentage of 50 percent. If you would like to incrementally increase the "brightness" of a color, you simply increment the color value(s) by some small number each time in a loop until you reach the max value of 1.0. You can do the same with the opacity if the scene requires it. There are many different ways you can do that, but I will show you one simple way here....

A sample script that gradually increases the Red from zero to max value is:

PostStep = (e)={color(0) < 1.0 ? {color = color + [0.01,0,0,0]}:{} }

With an increment value of 0.01 (with the sim frequency set to 60Hz), the color red will go from zero (no color) to full brightness in approximately 2 seconds. You can of course change the increment value to change the speed of the color change.

You can also make the color change up and down rhythmically by employing some simple math functions such as sin or cosin, but that goes beyond the scope of your question.

Hope this helps.

EDIT - I forgot about your question concerning collisions. You can do the same thing during a collision by employing the same technique of adding an incremental value to a color value each time an object collides. You of course do that in the "onCollide" property.

onCollide = (e)={color = color + [0.01,0,0,0]}}

If that makes the change too gradual, then simply increase the value to 0.05 or 0.10, etc.
User avatar
Posts: 325
Joined: Sun Jun 17, 2012 6:12 am
Location: Minnesota, USA

No hay comentarios:

Publicar un comentario