use of org.terasology.rendering.logic.LightComponent in project Terasology by MovingBlocks.
the class ItemCommonSystem method addOrUpdateBlockMeshComponent.
public static void addOrUpdateBlockMeshComponent(BlockItemComponent blockItemComponent, MutableComponentContainer entity) {
if (blockItemComponent != null) {
MeshComponent meshComponent = null;
if (entity.hasComponent(MeshComponent.class)) {
meshComponent = entity.getComponent(MeshComponent.class);
} else {
meshComponent = new MeshComponent();
}
BlockFamily blockFamily = blockItemComponent.blockFamily;
if (blockFamily == null) {
return;
}
meshComponent.mesh = blockFamily.getArchetypeBlock().getMeshGenerator().getStandaloneMesh();
meshComponent.material = Assets.getMaterial("engine:terrain").get();
meshComponent.translucent = blockFamily.getArchetypeBlock().isTranslucent();
float luminance = blockFamily.getArchetypeBlock().getLuminance() / 15f;
meshComponent.selfLuminance = luminance;
if (luminance > 0 && !entity.hasComponent(LightComponent.class)) {
LightComponent lightComponent = entity.addComponent(new LightComponent());
// scale the light back if it is a less bright block
lightComponent.lightAttenuationRange *= luminance;
}
entity.addOrSaveComponent(meshComponent);
}
}
use of org.terasology.rendering.logic.LightComponent in project Terasology by MovingBlocks.
the class BlockItemFactory method newInstance.
public EntityRef newInstance(BlockFamily blockFamily, EntityRef blockEntity) {
if (blockFamily == null) {
return EntityRef.NULL;
}
EntityBuilder builder = entityManager.newBuilder("engine:blockItemBase");
if (blockFamily.getArchetypeBlock().getLuminance() > 0) {
builder.addComponent(new LightComponent());
}
// Copy the components from block prefab into the block item
for (Component component : blockEntity.iterateComponents()) {
if (component.getClass().getAnnotation(AddToBlockBasedItem.class) != null) {
builder.addComponent(entityManager.getComponentLibrary().copy(component));
}
}
DisplayNameComponent displayNameComponent = builder.getComponent(DisplayNameComponent.class);
if (displayNameComponent != null) {
displayNameComponent.name = blockFamily.getDisplayName();
}
ItemComponent item = builder.getComponent(ItemComponent.class);
if (blockFamily.getArchetypeBlock().isStackable()) {
item.stackId = "block:" + blockFamily.getURI().toString();
item.stackCount = (byte) 1;
}
BlockItemComponent blockItem = builder.getComponent(BlockItemComponent.class);
blockItem.blockFamily = blockFamily;
return builder.build();
}
use of org.terasology.rendering.logic.LightComponent in project Terasology by MovingBlocks.
the class BlockItemFactory method newBuilder.
/**
* Use this method instead of {@link #newInstance(BlockFamily)} to modify entity properties like the persistence
* flag before it gets created.
*
* @param blockFamily must not be null
*/
public EntityBuilder newBuilder(BlockFamily blockFamily, int quantity) {
EntityBuilder builder = entityManager.newBuilder("engine:blockItemBase");
if (blockFamily.getArchetypeBlock().getLuminance() > 0) {
builder.addComponent(new LightComponent());
}
// Copy the components from block prefab into the block item
Optional<Prefab> prefab = blockFamily.getArchetypeBlock().getPrefab();
if (prefab.isPresent()) {
for (Component component : prefab.get().iterateComponents()) {
if (component.getClass().getAnnotation(AddToBlockBasedItem.class) != null) {
builder.addComponent(entityManager.getComponentLibrary().copy(component));
}
}
}
DisplayNameComponent displayNameComponent = builder.getComponent(DisplayNameComponent.class);
displayNameComponent.name = blockFamily.getDisplayName();
ItemComponent item = builder.getComponent(ItemComponent.class);
if (blockFamily.getArchetypeBlock().isStackable()) {
item.stackId = "block:" + blockFamily.getURI().toString();
item.stackCount = (byte) quantity;
}
BlockItemComponent blockItem = builder.getComponent(BlockItemComponent.class);
blockItem.blockFamily = blockFamily;
return builder;
}
use of org.terasology.rendering.logic.LightComponent in project Terasology by MovingBlocks.
the class DeferredPointLightsNode method process.
/**
* Iterates over all available point lights and renders them as spheres into the light accumulation buffer.
*
* Furthermore, lights that are further from the camera than their set rendering distance are ignored,
* while lights with a rendering distance set to 0.0 are always considered. However, only lights within
* the camera's field of view (frustrum) are rendered.
*/
@Override
public void process() {
PerformanceMonitor.startActivity("rendering/" + getUri());
lightGeometryMaterial.activateFeature(ShaderProgramFeature.FEATURE_LIGHT_POINT);
// Specific Shader Parameters
cameraPosition = activeCamera.getPosition();
// TODO: This is necessary right now because activateFeature removes all material parameters.
// TODO: Remove this explicit binding once we get rid of activateFeature, or find a way to retain parameters through it.
lightGeometryMaterial.setInt("texSceneOpaqueDepth", 0, true);
lightGeometryMaterial.setInt("texSceneOpaqueNormals", 1, true);
if (renderingConfig.isDynamicShadows()) {
if (renderingConfig.isCloudShadows()) {
lightGeometryMaterial.setFloat("time", worldProvider.getTime().getDays(), true);
lightGeometryMaterial.setFloat3("cameraPosition", cameraPosition, true);
}
}
if (renderingConfig.isDynamicShadows()) {
lightGeometryMaterial.setMatrix4("lightViewProjMatrix", lightCamera.getViewProjectionMatrix(), true);
lightGeometryMaterial.setMatrix4("invViewProjMatrix", activeCamera.getInverseViewProjectionMatrix(), true);
activeCameraToLightSpace.sub(cameraPosition, lightCamera.getPosition());
lightGeometryMaterial.setFloat3("activeCameraToLightSpace", activeCameraToLightSpace.x, activeCameraToLightSpace.y, activeCameraToLightSpace.z, true);
}
for (EntityRef entity : entityManager.getEntitiesWith(LightComponent.class, LocationComponent.class)) {
LightComponent lightComponent = entity.getComponent(LightComponent.class);
if (lightComponent.lightType == LightComponent.LightType.POINT) {
LocationComponent locationComponent = entity.getComponent(LocationComponent.class);
final Vector3f lightPositionInTeraCoords = locationComponent.getWorldPosition();
Vector3f lightPositionRelativeToCamera = new Vector3f();
lightPositionRelativeToCamera.sub(lightPositionInTeraCoords, activeCamera.getPosition());
if (lightIsRenderable(lightComponent, lightPositionRelativeToCamera)) {
lightGeometryMaterial.setCamera(activeCamera);
// setting shader parameters regarding the light's properties
lightGeometryMaterial.setFloat3("lightColorDiffuse", lightComponent.lightColorDiffuse.x, lightComponent.lightColorDiffuse.y, lightComponent.lightColorDiffuse.z, true);
lightGeometryMaterial.setFloat3("lightColorAmbient", lightComponent.lightColorAmbient.x, lightComponent.lightColorAmbient.y, lightComponent.lightColorAmbient.z, true);
lightGeometryMaterial.setFloat3("lightProperties", lightComponent.lightAmbientIntensity, lightComponent.lightDiffuseIntensity, lightComponent.lightSpecularPower, true);
lightGeometryMaterial.setFloat4("lightExtendedProperties", lightComponent.lightAttenuationRange, lightComponent.lightAttenuationFalloff, 0.0f, 0.0f, true);
// setting shader parameters for the light position in camera space
Vector3f lightPositionInViewSpace = new Vector3f(lightPositionRelativeToCamera);
activeCamera.getViewMatrix().transformPoint(lightPositionInViewSpace);
lightGeometryMaterial.setFloat3("lightViewPos", lightPositionInViewSpace.x, lightPositionInViewSpace.y, lightPositionInViewSpace.z, true);
// set the size and location of the sphere to be rendered via shader parameters
Matrix4f modelMatrix = new Matrix4f();
// scales the modelview matrix, effectively scales the light sphere
modelMatrix.set(lightComponent.lightAttenuationRange);
// effectively moves the light sphere in the right position relative to camera
modelMatrix.setTranslation(lightPositionRelativeToCamera);
lightGeometryMaterial.setMatrix4("modelMatrix", modelMatrix, true);
// draws the light sphere
glCallList(lightSphereDisplayList);
}
}
}
lightGeometryMaterial.deactivateFeature(ShaderProgramFeature.FEATURE_LIGHT_POINT);
PerformanceMonitor.endActivity();
}
Aggregations