Search in sources :

Example 6 with Material

use of org.terasology.engine.rendering.assets.material.Material in project Terasology by MovingBlocks.

the class ShaderManagerLwjgl method recompileAllShaders.

@Override
public void recompileAllShaders() {
    AssetManager assetManager = CoreRegistry.get(AssetManager.class);
    assetManager.getLoadedAssets(Shader.class).forEach(Shader::recompile);
    assetManager.getLoadedAssets(Material.class).forEach(Material::recompile);
    activeMaterial = null;
}
Also used : AssetManager(org.terasology.gestalt.assets.management.AssetManager) GLSLMaterial(org.terasology.engine.rendering.opengl.GLSLMaterial) Material(org.terasology.engine.rendering.assets.material.Material) Shader(org.terasology.engine.rendering.assets.shader.Shader)

Example 7 with Material

use of org.terasology.engine.rendering.assets.material.Material in project Terasology by MovingBlocks.

the class MeshRenderer method renderEntities.

private void renderEntities(Iterable<EntityRef> entityRefs) {
    SetMultimap<Material, EntityRef> entitiesToRender = HashMultimap.create();
    for (EntityRef entity : entityRefs) {
        MeshComponent meshComponent = entity.getComponent(MeshComponent.class);
        if (meshComponent != null && meshComponent.material != null) {
            entitiesToRender.put(meshComponent.material, entity);
        }
    }
    renderEntitiesByMaterial(entitiesToRender);
}
Also used : Material(org.terasology.engine.rendering.assets.material.Material) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Example 8 with Material

use of org.terasology.engine.rendering.assets.material.Material in project Terasology by MovingBlocks.

the class FloatingTextRenderer method render.

private void render(Iterable<EntityRef> floatingTextEntities) {
    Vector3fc cameraPosition = camera.getPosition();
    Matrix4f model = new Matrix4f();
    Matrix4f modelView = new Matrix4f();
    Vector3f worldPos = new Vector3f();
    for (EntityRef entity : floatingTextEntities) {
        FloatingTextComponent floatingText = entity.getComponent(FloatingTextComponent.class);
        LocationComponent location = entity.getComponent(LocationComponent.class);
        if (location == null) {
            logger.warn("location component is not defined can't render text: {}", floatingText.text);
            continue;
        }
        location.getWorldPosition(worldPos);
        if (!worldProvider.isBlockRelevant(worldPos) || !worldPos.isFinite()) {
            continue;
        }
        String[] linesOfText = floatingText.text.split("\n");
        Color baseColor = floatingText.textColor;
        Color shadowColor = floatingText.textShadowColor;
        boolean underline = false;
        int textWidth = 0;
        for (String singleLine : linesOfText) {
            if (font.getWidth(singleLine) > textWidth) {
                textWidth = font.getWidth(singleLine);
            }
        }
        FontMeshBuilder meshBuilder = new FontMeshBuilder(underlineMaterial);
        Map<Material, Mesh> meshMap = entityMeshCache.get(entity);
        if (meshMap == null) {
            meshMap = meshBuilder.createTextMesh(font, Arrays.asList(linesOfText), textWidth, HorizontalAlign.CENTER, baseColor, shadowColor, underline);
            entityMeshCache.put(entity, meshMap);
        }
        if (floatingText.isOverlay) {
            glDisable(GL_DEPTH_TEST);
        }
        float scale = METER_PER_PIXEL * floatingText.scale;
        model.setTranslation(worldPos.sub(cameraPosition));
        modelView.set(camera.getViewMatrix()).mul(model).m00(1.0f).m10(0.0f).m20(0.0f).m01(0.0f).m11(1.0f).m21(0.0f).m02(0.0f).m12(0.0f).m22(1.0f);
        modelView.scale(scale, -scale, scale);
        modelView.translate(-textWidth / 2.0f, 0.0f, 0.0f);
        for (Map.Entry<Material, Mesh> meshMapEntry : meshMap.entrySet()) {
            Mesh mesh = meshMapEntry.getValue();
            Material material = meshMapEntry.getKey();
            material.enable();
            material.bindTextures();
            material.setFloat4("croppingBoundaries", Float.MIN_VALUE, Float.MAX_VALUE, Float.MIN_VALUE, Float.MAX_VALUE);
            material.setMatrix4("modelViewMatrix", modelView);
            material.setMatrix4("projectionMatrix", camera.getProjectionMatrix());
            material.setFloat2("offset", 0.0f, 0.0f);
            material.setFloat("alpha", 1.0f);
            mesh.render();
        }
        // Revert to default state
        if (floatingText.isOverlay) {
            glEnable(GL_DEPTH_TEST);
        }
    }
}
Also used : Color(org.terasology.nui.Color) Mesh(org.terasology.engine.rendering.assets.mesh.Mesh) Material(org.terasology.engine.rendering.assets.material.Material) LocationComponent(org.terasology.engine.logic.location.LocationComponent) Vector3fc(org.joml.Vector3fc) Matrix4f(org.joml.Matrix4f) FontMeshBuilder(org.terasology.engine.rendering.assets.font.FontMeshBuilder) Vector3f(org.joml.Vector3f) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) Map(java.util.Map)

Example 9 with Material

use of org.terasology.engine.rendering.assets.material.Material in project Terasology by MovingBlocks.

the class LwjglCanvasRenderer method drawText.

@Override
public void drawText(String text, Font font, HorizontalAlign hAlign, VerticalAlign vAlign, Rectanglei absoluteRegionRectangle, Colorc color, Colorc shadowColor, float alpha, boolean underlined) {
    Rectanglei absoluteRegion = new Rectanglei(absoluteRegionRectangle);
    TextCacheKey key = new TextCacheKey(text, font, absoluteRegion.getSizeX(), hAlign, color, shadowColor, underlined);
    usedText.add(key);
    Map<Material, Mesh> fontMesh = cachedText.get(key);
    List<String> lines = TextLineBuilder.getLines(font, text, absoluteRegion.getSizeX());
    if (fontMesh != null) {
        for (Mesh mesh : fontMesh.values()) {
            if (mesh.isDisposed()) {
                fontMesh = null;
                break;
            }
        }
    }
    if (fontMesh == null) {
        fontMesh = fontMeshBuilder.createTextMesh((org.terasology.engine.rendering.assets.font.Font) font, lines, absoluteRegion.getSizeX(), hAlign, color, shadowColor, underlined);
        cachedText.put(key, fontMesh);
    }
    Vector2i offset = new Vector2i(absoluteRegion.minX, absoluteRegion.minY);
    offset.y += vAlign.getOffset(lines.size() * font.getLineHeight(), absoluteRegion.lengthY());
    fontMesh.entrySet().stream().filter(entry -> entry.getKey().isRenderable()).forEach(entry -> {
        entry.getKey().bindTextures();
        entry.getKey().setMatrix4("projectionMatrix", projMatrix);
        entry.getKey().setMatrix4("modelViewMatrix", modelMatrixStack);
        entry.getKey().setFloat4(CROPPING_BOUNDARIES_PARAM, requestedCropRegion.minX, requestedCropRegion.maxX, requestedCropRegion.minY, requestedCropRegion.maxY);
        entry.getKey().setFloat2("offset", offset.x, offset.y);
        entry.getKey().setFloat("alpha", alpha);
        entry.getValue().render();
    });
}
Also used : Quaternionf(org.joml.Quaternionf) GL11.glDisable(org.lwjgl.opengl.GL11.glDisable) GL11.glViewport(org.lwjgl.opengl.GL11.glViewport) Vector2ic(org.joml.Vector2ic) DisplayDevice(org.terasology.engine.core.subsystem.DisplayDevice) UITextureRegion(org.terasology.nui.UITextureRegion) Map(java.util.Map) Vector3f(org.joml.Vector3f) GL11(org.lwjgl.opengl.GL11) TeraMath(org.terasology.math.TeraMath) VerticalAlign(org.terasology.nui.VerticalAlign) Quaternionfc(org.joml.Quaternionfc) FrameBufferObject(org.terasology.engine.rendering.opengl.FrameBufferObject) GL_SRC_ALPHA(org.lwjgl.opengl.GL11.GL_SRC_ALPHA) RenderingConfig(org.terasology.engine.config.RenderingConfig) GL_DEPTH_TEST(org.lwjgl.opengl.GL11.GL_DEPTH_TEST) TextLineBuilder(org.terasology.nui.TextLineBuilder) Set(java.util.Set) ScaleMode(org.terasology.nui.ScaleMode) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) MeshBuilder(org.terasology.engine.rendering.assets.mesh.MeshBuilder) List(java.util.List) Context(org.terasology.engine.context.Context) TextureRegion(org.terasology.engine.rendering.assets.texture.TextureRegion) Vector2i(org.joml.Vector2i) Border(org.terasology.nui.Border) PropertyChangeListener(java.beans.PropertyChangeListener) Vector2f(org.joml.Vector2f) Matrix3f(org.joml.Matrix3f) LwjglFrameBufferObject(org.terasology.engine.rendering.opengl.LwjglFrameBufferObject) Matrix4fStack(org.joml.Matrix4fStack) Rectanglef(org.terasology.joml.geom.Rectanglef) Rectanglei(org.terasology.joml.geom.Rectanglei) Colorc(org.terasology.nui.Colorc) ResourceUrn(org.terasology.gestalt.assets.ResourceUrn) AABBfc(org.terasology.joml.geom.AABBfc) GL11.glClear(org.lwjgl.opengl.GL11.glClear) Font(org.terasology.nui.asset.font.Font) Matrix4f(org.joml.Matrix4f) FontMeshBuilder(org.terasology.engine.rendering.assets.font.FontMeshBuilder) PropertyChangeEvent(java.beans.PropertyChangeEvent) AssetManager(org.terasology.gestalt.assets.management.AssetManager) GL11.glEnable(org.lwjgl.opengl.GL11.glEnable) Assets(org.terasology.engine.utilities.Assets) Iterator(java.util.Iterator) Maps(com.google.common.collect.Maps) GL11.glBlendFunc(org.lwjgl.opengl.GL11.glBlendFunc) GL_ONE_MINUS_SRC_ALPHA(org.lwjgl.opengl.GL11.GL_ONE_MINUS_SRC_ALPHA) Vector3fc(org.joml.Vector3fc) Config(org.terasology.engine.config.Config) Material(org.terasology.engine.rendering.assets.material.Material) GL_BLEND(org.lwjgl.opengl.GL11.GL_BLEND) Mesh(org.terasology.engine.rendering.assets.mesh.Mesh) HorizontalAlign(org.terasology.nui.HorizontalAlign) Mesh(org.terasology.engine.rendering.assets.mesh.Mesh) Material(org.terasology.engine.rendering.assets.material.Material) Rectanglei(org.terasology.joml.geom.Rectanglei) Font(org.terasology.nui.asset.font.Font) Vector2i(org.joml.Vector2i)

Aggregations

Material (org.terasology.engine.rendering.assets.material.Material)9 Matrix4f (org.joml.Matrix4f)4 Vector3f (org.joml.Vector3f)4 Map (java.util.Map)3 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)3 Mesh (org.terasology.engine.rendering.assets.mesh.Mesh)3 Matrix3f (org.joml.Matrix3f)2 Quaternionf (org.joml.Quaternionf)2 Vector3fc (org.joml.Vector3fc)2 LocationComponent (org.terasology.engine.logic.location.LocationComponent)2 FontMeshBuilder (org.terasology.engine.rendering.assets.font.FontMeshBuilder)2 GLSLMaterial (org.terasology.engine.rendering.opengl.GLSLMaterial)2 ResourceUrn (org.terasology.gestalt.assets.ResourceUrn)2 Maps (com.google.common.collect.Maps)1 Sets (com.google.common.collect.Sets)1 PropertyChangeEvent (java.beans.PropertyChangeEvent)1 PropertyChangeListener (java.beans.PropertyChangeListener)1 IOException (java.io.IOException)1 FloatBuffer (java.nio.FloatBuffer)1 Iterator (java.util.Iterator)1