point lights
This commit is contained in:
@ -11,6 +11,7 @@ import com.badlogic.gdx.graphics.g3d.attributes.FloatAttribute
|
||||
import com.badlogic.gdx.graphics.g3d.attributes.IntAttribute
|
||||
import com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute
|
||||
import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight
|
||||
import com.badlogic.gdx.graphics.g3d.environment.PointLight
|
||||
import com.badlogic.gdx.graphics.g3d.model.data.*
|
||||
import com.badlogic.gdx.graphics.g3d.shaders.DefaultShader
|
||||
import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder
|
||||
@ -152,13 +153,13 @@ class Game: ApplicationAdapter()
|
||||
env = Environment()
|
||||
env.set(
|
||||
IntAttribute.createCullFace(GL20.GL_BACK),
|
||||
ColorAttribute.createAmbientLight(XnaColor.DarkSlateGray.lighten(-0.6)),
|
||||
ColorAttribute.createAmbientLight(XnaColor.DarkSlateGray.lighten(-0.666)),
|
||||
ColorAttribute.createFog(XnaColor.CornflowerBlue))
|
||||
env.set(
|
||||
CustomIntAttribute.createFogMode(CustomIntAttribute.FogModes.Depth),
|
||||
CustomIntAttribute.createFogType(CustomIntAttribute.FogTypes.Smooth),
|
||||
CustomFloatAttribute.createFogNear(0.75f),
|
||||
CustomFloatAttribute.createFogFar(20.5f))
|
||||
CustomFloatAttribute.createFogNear(1.25f),
|
||||
CustomFloatAttribute.createFogFar(24.5f))
|
||||
/*
|
||||
env.set(
|
||||
CustomIntAttribute.createFogMode(CustomIntAttribute.FogModes.Distance),
|
||||
@ -166,12 +167,21 @@ class Game: ApplicationAdapter()
|
||||
CustomFloatAttribute.createFogDensity(0.1f))
|
||||
*/
|
||||
env.add(DirectionalLight().set(
|
||||
XnaColor.BlanchedAlmond.lighten(0.01),
|
||||
XnaColor.BlanchedAlmond.lighten(-0.02).mix(XnaColor.LightSlateGray, 0.5f).mix(XnaColor.White, 0.125f),
|
||||
Vector3(1.0f, -1.0f, -1.0f).nor()))
|
||||
env.add(PointLight().set(
|
||||
XnaColor.Green.mix(XnaColor.Gray, 0.5f),
|
||||
Vector3(3.0f, 0.33f, -5.0f), 2.0f))
|
||||
env.add(PointLight().set(
|
||||
XnaColor.Red.mix(XnaColor.Gray, 0.5f),
|
||||
Vector3(5.5f, 0.33f, -6.0f), 2.0f))
|
||||
env.add(PointLight().set(
|
||||
XnaColor.Blue.mix(XnaColor.Gray, 0.5f),
|
||||
Vector3(4.0f, 0.33f, -7.0f), 2.0f))
|
||||
|
||||
val shaderConfig = DefaultShader.Config()
|
||||
shaderConfig.numDirectionalLights = 1
|
||||
shaderConfig.numPointLights = 0
|
||||
shaderConfig.numPointLights = 3
|
||||
shaderConfig.numBones = 0
|
||||
modelBatch = ModelBatch(CustomDefaultShaderProvider(shaderConfig))
|
||||
|
||||
|
@ -89,10 +89,10 @@ varying vec3 v_ambient;
|
||||
|
||||
#endif // lightingFlag
|
||||
|
||||
#if defined(specularFlag) || defined(fogFlag)
|
||||
#if (numPointLights > 0) || defined(specularFlag) || defined(fogFlag)
|
||||
varying vec4 v_worldPosition;
|
||||
uniform vec4 u_cameraPosition;
|
||||
#endif // lightingFlag || fogFlag
|
||||
#endif // numPointLights > 0 || lightingFlag || fogFlag
|
||||
|
||||
#ifdef fogFlag
|
||||
uniform vec4 u_fogColor;
|
||||
@ -159,7 +159,7 @@ void main()
|
||||
vec3 lightVec = -u_dirLights[i].direction;
|
||||
float lambert = dot(normal, lightVec);
|
||||
float phongTerm = max(lambert, 0.0);
|
||||
vec3 value = u_dirLights[i].color * phongTerm;
|
||||
vec3 value = u_dirLights[i].color;
|
||||
value = linearEncode(value);
|
||||
value *= phongTerm;
|
||||
accum += value;
|
||||
@ -172,6 +172,32 @@ void main()
|
||||
}
|
||||
#endif // normalFlag && numDirectionalLights
|
||||
|
||||
#if defined(normalFlag) && (numPointLights > 0)
|
||||
for (int i = 0; i < numPointLights; ++i)
|
||||
{
|
||||
vec3 lightVec = u_pointLights[i].position - v_worldPosition.xyz;
|
||||
float lightDist2 = dot(lightVec, lightVec);
|
||||
vec3 lightDir = lightVec;
|
||||
lightDir *= inversesqrt(lightDist2);
|
||||
float lambert = dot(normal, lightDir);
|
||||
float phongTerm = max(lambert, 0.0);
|
||||
//float lightDist = sqrt(lightDist2);
|
||||
//float attenuation = clamp(1.0 - lightDist * lightDist / (2.0 * 2.0), 0.0, 1.0);
|
||||
//attenuation *= attenuation;
|
||||
float attenuation = 1.0 / (1.0 + lightDist2);
|
||||
vec3 value = u_pointLights[i].color;
|
||||
value = linearEncode(value);
|
||||
value *= phongTerm * attenuation;
|
||||
accum += value;
|
||||
#ifdef specularFlag
|
||||
vec3 halfDir = normalize(lightDir + eyeVec);
|
||||
float specAngle = max(dot(halfDir, normal), 0.0);
|
||||
float specTerm = pow(specAngle, u_shininess);
|
||||
specAccum += value * specTerm;
|
||||
#endif // specularFlag
|
||||
}
|
||||
#endif
|
||||
|
||||
vec3 fragment;
|
||||
#ifdef specularFlag
|
||||
#ifdef specularTextureFlag
|
||||
@ -229,5 +255,6 @@ void main()
|
||||
fogColor = linearEncode(fogColor);
|
||||
fragment = mix(fragment, fogColor, fog);
|
||||
#endif // fogFlag
|
||||
|
||||
gl_FragColor = vec4(linearDecode(fragment), diffuse.a);
|
||||
}
|
||||
|
Reference in New Issue
Block a user