use of org.terasology.engine.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class MeshRenderer method renderEntitiesByMaterial.
private void renderEntitiesByMaterial(SetMultimap<Material, EntityRef> meshByMaterial) {
Vector3f cameraPosition = worldRenderer.getActiveCamera().getPosition();
Quaternionf worldRot = new Quaternionf();
Vector3f worldPos = new Vector3f();
Matrix3f normalMatrix = new Matrix3f();
Matrix4f matrixCameraSpace = new Matrix4f();
Matrix4f modelViewMatrix = new Matrix4f();
FloatBuffer tempMatrixBuffer44 = BufferUtils.createFloatBuffer(16);
FloatBuffer tempMatrixBuffer33 = BufferUtils.createFloatBuffer(12);
for (Material material : meshByMaterial.keySet()) {
if (material.isRenderable()) {
material.enable();
material.setFloat("sunlight", 1.0f, true);
material.setFloat("blockLight", 1.0f, true);
material.setMatrix4("projectionMatrix", worldRenderer.getActiveCamera().getProjectionMatrix(), true);
material.bindTextures();
Set<EntityRef> entities = meshByMaterial.get(material);
lastRendered = entities.size();
for (EntityRef entity : entities) {
MeshComponent meshComp = entity.getComponent(MeshComponent.class);
LocationComponent location = entity.getComponent(LocationComponent.class);
if (isHidden(entity, meshComp) || location == null || meshComp.mesh == null) {
continue;
}
Vector3f worldPosition = location.getWorldPosition(new Vector3f());
if (!worldPosition.isFinite() && !isRelevant(entity, worldPosition)) {
continue;
}
if (meshComp.mesh.isDisposed()) {
logger.error("Attempted to render disposed mesh");
continue;
}
worldRot.set(location.getWorldRotation(new Quaternionf()));
worldPos.set(location.getWorldPosition(new Vector3f()));
float worldScale = location.getWorldScale();
Vector3f offsetFromCamera = worldPos.sub(cameraPosition, new Vector3f());
matrixCameraSpace.translationRotateScale(offsetFromCamera, worldRot, worldScale);
AABBf aabb = meshComp.mesh.getAABB().transform(new Matrix4f().translationRotateScale(worldPos, worldRot, worldScale), new AABBf());
if (worldRenderer.getActiveCamera().hasInSight(aabb)) {
modelViewMatrix.set(worldRenderer.getActiveCamera().getViewMatrix()).mul(matrixCameraSpace);
modelViewMatrix.get(tempMatrixBuffer44);
modelViewMatrix.normal(normalMatrix).get(tempMatrixBuffer33);
material.setMatrix4("projectionMatrix", worldRenderer.getActiveCamera().getProjectionMatrix(), true);
material.setMatrix4("modelViewMatrix", tempMatrixBuffer44, true);
material.setMatrix3("normalMatrix", tempMatrixBuffer33, true);
material.setFloat3("colorOffset", meshComp.color.rf(), meshComp.color.gf(), meshComp.color.bf(), true);
material.setFloat("sunlight", worldRenderer.getMainLightIntensityAt(worldPos), true);
material.setFloat("blockLight", Math.max(worldRenderer.getBlockLightIntensityAt(worldPos), meshComp.selfLuminance), true);
meshComp.mesh.render();
}
}
}
}
}
use of org.terasology.engine.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class BlockCommands method replaceBlock.
@Command(shortDescription = "Replaces a block in front of user", helpText = "Replaces a block in front of the user at the specified max distance", runOnServer = true, requiredPermission = PermissionManager.CHEAT_PERMISSION)
public void replaceBlock(@Sender EntityRef sender, @CommandParam("blockName") String uri, @CommandParam(value = "maxDistance", required = false) Integer maxDistanceParam) {
int maxDistance = maxDistanceParam != null ? maxDistanceParam : 12;
EntityRef playerEntity = sender.getComponent(ClientComponent.class).character;
EntityRef gazeEntity = GazeAuthoritySystem.getGazeEntityForCharacter(playerEntity);
LocationComponent gazeLocation = gazeEntity.getComponent(LocationComponent.class);
Set<ResourceUrn> matchingUris = Assets.resolveAssetUri(uri, BlockFamilyDefinition.class);
targetSystem.updateTarget(gazeLocation.getWorldPosition(new Vector3f()), gazeLocation.getWorldDirection(new Vector3f()), maxDistance);
EntityRef target = targetSystem.getTarget();
BlockComponent targetLocation = target.getComponent(BlockComponent.class);
if (matchingUris.size() == 1) {
Optional<BlockFamilyDefinition> def = Assets.get(matchingUris.iterator().next(), BlockFamilyDefinition.class);
if (def.isPresent()) {
BlockFamily blockFamily = blockManager.getBlockFamily(uri);
Block block = blockManager.getBlock(blockFamily.getURI());
world.setBlock(targetLocation.getPosition(), block);
} else if (matchingUris.size() > 1) {
StringBuilder builder = new StringBuilder();
builder.append("Non-unique shape name, possible matches: ");
Iterator<ResourceUrn> shapeUris = sortItems(matchingUris).iterator();
while (shapeUris.hasNext()) {
builder.append(shapeUris.next().toString());
if (shapeUris.hasNext()) {
builder.append(", ");
}
}
}
}
}
use of org.terasology.engine.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class BlockEntitySystem method commonDefaultDropsHandling.
public void commonDefaultDropsHandling(CreateBlockDropsEvent event, EntityRef entity, Vector3ic location, Block block) {
BlockDamageModifierComponent blockDamageModifierComponent = event.getDamageType().getComponent(BlockDamageModifierComponent.class);
float chanceOfBlockDrop = 1;
if (blockDamageModifierComponent != null) {
chanceOfBlockDrop = 1 - blockDamageModifierComponent.blockAnnihilationChance;
}
if (random.nextFloat() < chanceOfBlockDrop) {
EntityRef item = blockItemFactory.newInstance(block.getBlockFamily(), entity);
entity.send(new OnBlockToItem(item));
if (shouldDropToWorld(event, block, blockDamageModifierComponent, item)) {
float impulsePower = 0;
if (blockDamageModifierComponent != null) {
impulsePower = blockDamageModifierComponent.impulsePower;
}
processDropping(item, location, impulsePower);
}
}
}
use of org.terasology.engine.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class AttachSupportRequired method getEntity.
private EntityRef getEntity(Vector3ic location, Map<? extends Vector3ic, Block> blockOverrides) {
final Block overwrittenBlock = blockOverrides.get(location);
if (overwrittenBlock != null) {
return overwrittenBlock.getEntity();
}
EntityRef blockEntity = getBlockEntityRegistry().getExistingBlockEntityAt(location);
if (blockEntity.exists()) {
return blockEntity;
} else {
return getWorldProvider().getBlock(location).getEntity();
}
}
use of org.terasology.engine.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class IterateComponentsBenchmark method iterateMultipleComponent.
@Benchmark
public void iterateMultipleComponent(StateObject state) {
for (EntityRef entity : state.entityManager.getEntitiesWith(MeshComponent.class, LocationComponent.class)) {
LocationComponent loc = entity.getComponent(LocationComponent.class);
MeshComponent meshComp = entity.getComponent(MeshComponent.class);
loc.getLocalPosition();
}
}
Aggregations