Search in sources :

Example 1 with Material

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

the class FloatingTextRenderer method diposeMeshMap.

private void diposeMeshMap(Map<Material, Mesh> meshMap) {
    for (Map.Entry<Material, Mesh> meshMapEntry : meshMap.entrySet()) {
        Mesh mesh = meshMapEntry.getValue();
        mesh.dispose();
    // Note: Material belongs to font and must not be disposed
    }
}
Also used : Mesh(org.terasology.engine.rendering.assets.mesh.Mesh) Material(org.terasology.engine.rendering.assets.material.Material) Map(java.util.Map)

Example 2 with Material

use of org.terasology.engine.rendering.assets.material.Material 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();
                }
            }
        }
    }
}
Also used : Matrix4f(org.joml.Matrix4f) AABBf(org.terasology.joml.geom.AABBf) Matrix3f(org.joml.Matrix3f) Vector3f(org.joml.Vector3f) Quaternionf(org.joml.Quaternionf) FloatBuffer(java.nio.FloatBuffer) Material(org.terasology.engine.rendering.assets.material.Material) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) LocationComponent(org.terasology.engine.logic.location.LocationComponent)

Example 3 with Material

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

the class SpriteParticleRenderer method renderAlphaBlend.

@Override
public void renderAlphaBlend() {
    if (!opengl33) {
        return;
    }
    PerspectiveCamera camera = (PerspectiveCamera) worldRenderer.getActiveCamera();
    Vector3f cameraPosition = camera.getPosition();
    Matrix4f viewProjection = new Matrix4f(camera.getViewProjectionMatrix()).translate(-cameraPosition.x, -cameraPosition.y, -cameraPosition.z);
    Material material = Assets.getMaterial(PARTICLE_MATERIAL_URI).get();
    material.enable();
    material.setFloat3("camera_position", cameraPosition.x, cameraPosition.y, cameraPosition.z);
    material.setMatrix4("view_projection", viewProjection.get(matrixBuffer));
    particleSystemManager.getParticleEmittersByDataComponent(ParticleDataSpriteComponent.class).forEach(particleSystem -> drawParticles(material, particleSystem));
}
Also used : ParticleDataSpriteComponent(org.terasology.engine.particles.components.ParticleDataSpriteComponent) Matrix4f(org.joml.Matrix4f) Vector3f(org.joml.Vector3f) Material(org.terasology.engine.rendering.assets.material.Material) PerspectiveCamera(org.terasology.engine.rendering.cameras.PerspectiveCamera)

Example 4 with Material

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

the class FontFormat method parsePage.

private void parsePage(FontDataBuilder builder, Name moduleName, String pageInfo) throws IOException {
    Matcher pageMatcher = pagePattern.matcher(pageInfo);
    if (pageMatcher.matches()) {
        int pageId = Integer.parseInt(pageMatcher.group(1));
        Name textureName = new Name(pageMatcher.group(2).substring(0, pageMatcher.group(2).lastIndexOf('.')));
        Optional<Material> material = assetManager.getAsset(new ResourceUrn(moduleName, new Name("font"), textureName), Material.class);
        if (!material.isPresent()) {
            throw new IOException("Failed to load font - unable to resolve font page '" + textureName + "'");
        } else {
            builder.addPage(pageId, assetManager.getAsset(new ResourceUrn(moduleName, textureName), Texture.class).get(), material.get());
        }
    } else {
        throw new IOException("Failed to load font - invalid page line '" + pageInfo + "'");
    }
}
Also used : Matcher(java.util.regex.Matcher) Material(org.terasology.engine.rendering.assets.material.Material) IOException(java.io.IOException) ResourceUrn(org.terasology.gestalt.assets.ResourceUrn) Name(org.terasology.gestalt.naming.Name)

Example 5 with Material

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

the class ShaderManagerLwjgl method addShaderProgram.

public GLSLMaterial addShaderProgram(String title, String providingModule) {
    String uri = providingModule + ":" + title;
    Optional<? extends Shader> shader = Assets.getShader(uri);
    checkState(shader.isPresent(), "Failed to resolve %s", uri);
    shader.get().recompile();
    GLSLMaterial material = (GLSLMaterial) Assets.generateAsset(new ResourceUrn(providingModule + ":prog." + title), new MaterialData(shader.get()), Material.class);
    progamaticShaders.add(material);
    return material;
}
Also used : GLSLMaterial(org.terasology.engine.rendering.opengl.GLSLMaterial) Material(org.terasology.engine.rendering.assets.material.Material) MaterialData(org.terasology.engine.rendering.assets.material.MaterialData) ResourceUrn(org.terasology.gestalt.assets.ResourceUrn) GLSLMaterial(org.terasology.engine.rendering.opengl.GLSLMaterial)

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