1 /******************************************************************************* 2 * Copyright 2011 See AUTHORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 package com.badlogic.gdx.graphics.g3d.environment; 18 19 import com.badlogic.gdx.graphics.Color; 20 import com.badlogic.gdx.math.MathUtils; 21 import com.badlogic.gdx.math.Vector3; 22 23 /** Note that the default shader doesn't support spot lights, you'll have to supply your own shader to use this class. 24 * @author realitix */ 25 public class SpotLight extends BaseLight<SpotLight> { 26 public final Vector3 position = new Vector3(); 27 public final Vector3 direction = new Vector3(); 28 public float intensity; 29 public float cutoffAngle; 30 public float exponent; 31 setPosition(float positionX, float positionY, float positionZ)32 public SpotLight setPosition(float positionX, float positionY, float positionZ) { 33 this.position.set(positionX, positionY, positionZ); 34 return this; 35 } 36 setPosition(Vector3 position)37 public SpotLight setPosition(Vector3 position) { 38 this.position.set(position); 39 return this; 40 } 41 setDirection(float directionX, float directionY, float directionZ)42 public SpotLight setDirection(float directionX, float directionY, float directionZ) { 43 this.direction.set(directionX, directionY, directionZ); 44 return this; 45 } 46 setDirection(Vector3 direction)47 public SpotLight setDirection(Vector3 direction) { 48 this.direction.set(direction); 49 return this; 50 } 51 setIntensity(float intensity)52 public SpotLight setIntensity(float intensity) { 53 this.intensity = intensity; 54 return this; 55 } 56 setCutoffAngle(float cutoffAngle)57 public SpotLight setCutoffAngle(float cutoffAngle) { 58 this.cutoffAngle = cutoffAngle; 59 return this; 60 } 61 setExponent(float exponent)62 public SpotLight setExponent(float exponent) { 63 this.exponent = exponent; 64 return this; 65 } 66 set(final SpotLight copyFrom)67 public SpotLight set (final SpotLight copyFrom) { 68 return set(copyFrom.color, copyFrom.position, copyFrom.direction, copyFrom.intensity, copyFrom.cutoffAngle, copyFrom.exponent); 69 } 70 set(final Color color, final Vector3 position, final Vector3 direction, final float intensity, final float cutoffAngle, final float exponent)71 public SpotLight set (final Color color, final Vector3 position, final Vector3 direction, final float intensity, 72 final float cutoffAngle, final float exponent) { 73 if (color != null) this.color.set(color); 74 if (position != null) this.position.set(position); 75 if (direction != null) this.direction.set(direction).nor(); 76 this.intensity = intensity; 77 this.cutoffAngle = cutoffAngle; 78 this.exponent = exponent; 79 return this; 80 } 81 set(final float r, final float g, final float b, final Vector3 position, final Vector3 direction, final float intensity, final float cutoffAngle, final float exponent)82 public SpotLight set (final float r, final float g, final float b, final Vector3 position, final Vector3 direction, 83 final float intensity, final float cutoffAngle, final float exponent) { 84 this.color.set(r, g, b, 1f); 85 if (position != null) this.position.set(position); 86 if (direction != null) this.direction.set(direction).nor(); 87 this.intensity = intensity; 88 this.cutoffAngle = cutoffAngle; 89 this.exponent = exponent; 90 return this; 91 } 92 set(final Color color, final float posX, final float posY, final float posZ, final float dirX, final float dirY, final float dirZ, final float intensity, final float cutoffAngle, final float exponent)93 public SpotLight set (final Color color, final float posX, final float posY, final float posZ, final float dirX, 94 final float dirY, final float dirZ, final float intensity, final float cutoffAngle, final float exponent) { 95 if (color != null) this.color.set(color); 96 this.position.set(posX, posY, posZ); 97 this.direction.set(dirX, dirY, dirZ).nor(); 98 this.intensity = intensity; 99 this.cutoffAngle = cutoffAngle; 100 this.exponent = exponent; 101 return this; 102 } 103 set(final float r, final float g, final float b, final float posX, final float posY, final float posZ, final float dirX, final float dirY, final float dirZ, final float intensity, final float cutoffAngle, final float exponent)104 public SpotLight set (final float r, final float g, final float b, final float posX, final float posY, final float posZ, 105 final float dirX, final float dirY, final float dirZ, final float intensity, final float cutoffAngle, final float exponent) { 106 this.color.set(r, g, b, 1f); 107 this.position.set(posX, posY, posZ); 108 this.direction.set(dirX, dirY, dirZ).nor(); 109 this.intensity = intensity; 110 this.cutoffAngle = cutoffAngle; 111 this.exponent = exponent; 112 return this; 113 } 114 setTarget(final Vector3 target)115 public SpotLight setTarget (final Vector3 target) { 116 direction.set(target).sub(position).nor(); 117 return this; 118 } 119 120 @Override equals(Object obj)121 public boolean equals (Object obj) { 122 return (obj instanceof SpotLight) ? equals((SpotLight)obj) : false; 123 } 124 equals(SpotLight other)125 public boolean equals (SpotLight other) { 126 return (other != null && (other == this || (color.equals(other.color) && position.equals(other.position) 127 && direction.equals(other.direction) && MathUtils.isEqual(intensity, other.intensity) && MathUtils.isEqual(cutoffAngle, 128 other.cutoffAngle) && MathUtils.isEqual(exponent, other.exponent) ))); 129 } 130 } 131