Guest User

Untitled

a guest
Oct 17th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.38 KB | None | 0 0
  1.    /**
  2.     * Get the effect the light should apply to a given location
  3.     *
  4.     * @param x The x coordinate of the location being considered for lighting
  5.     * @param y The y coordinate of the location being considered for lighting
  6.     * @param colouredLights True if we're supporting coloured lights
  7.     *
  8.     * @return The effect on a given location of the light in terms of colour components (all
  9.     * the same if we don't support coloured lights)
  10.     */
  11.    public float[] getEffectAt(float x, float y, boolean colouredLights) {
  12.       // first work out what propotion of the strength distance the light
  13.       // is from the point. This is a value from 0-1 where 1 is the centre of the
  14.       // light (i.e. full brightness) and 0 is the very edge (or outside) the lights
  15.       // range
  16.       float dx = (x - xpos);
  17.       float dy = (y - ypos);
  18.       float distance2 = (dx*dx)+(dy*dy);
  19.       float effect = 1 - (distance2 / (strength*strength));
  20.      
  21.       if (effect < 0) {
  22.          effect = 0;
  23.       }
  24.      
  25.       // if we doing coloured lights then multiple the colour of the light
  26.       // by the effect. Otherwise just use the effect for all components to
  27.       // give white light
  28.       if (colouredLights) {
  29.          return new float[] {col.r * effect, col.g * effect, col.b * effect};
  30.       } else {
  31.          return new float[] {effect,effect,effect};
  32.       }
  33.    }
Add Comment
Please, Sign In to add comment