Search in sources :

Example 31 with Color

use of org.terasology.nui.Color in project Terasology by MovingBlocks.

the class LineRenderer method draw.

/**
 * Draws a 2D line segment in OpenGL.
 *
 * @param x1         The X-coordinate of the segment's start point.
 * @param y1         The Y-coordinate of the segment's start point.
 * @param x2         The X-coordinate of the segment's end point.
 * @param y2         The Y-coordinate of the segment's end point.
 * @param width      Thickness of the line in pixels.
 * @param color      The line color.
 * @param background The background color. Ignored if alpha blending is used.
 * @param alpha      The alpha channel. If set to 0, alpha blending is not used.
 * @see <a href="http://artgrammer.blogspot.de/2011/05/drawing-nearly-perfect-2d-line-segments.html">
 * Drawing nearly perfect 2D line segments in OpenGL
 * </a>
 */
public static void draw(float x1, float y1, float x2, float y2, float width, Colorc color, Colorc background, float alpha) {
    if (lineMesh == null) {
        lineMesh = Assets.generateAsset(lineMeshData, Mesh.class);
    }
    GL11.glDisable(GL11.GL_CULL_FACE);
    float t = 0;
    float r = 0;
    float f = width - (int) width;
    float a;
    boolean alphaBlend = alpha > 0;
    float cRed = color.rf();
    float cGreen = color.gf();
    float cBlue = color.bf();
    float bRed = background.rf();
    float bGreen = background.gf();
    float bBlue = background.bf();
    if (alphaBlend) {
        a = alpha;
    } else {
        a = 1.f;
    }
    if (width >= 0.0 && width < 1.0) {
        t = 0.05f;
        r = 0.48f + 0.32f * f;
        if (!alphaBlend) {
            cRed += 0.88f * (1 - f);
            cGreen += 0.88f * (1 - f);
            cBlue += 0.88f * (1 - f);
            if (cRed > 1.0f) {
                cRed = 1.0f;
            }
            if (cGreen > 1.0f) {
                cGreen = 1.0f;
            }
            if (cBlue > 1.0f) {
                cBlue = 1.0f;
            }
        } else {
            a *= f;
        }
    } else if (width >= 1.0 && width < 2.0) {
        t = 0.05f + f * 0.33f;
        r = 0.768f + 0.312f * f;
    } else if (width >= 2.0 && width < 3.0) {
        t = 0.38f + f * 0.58f;
        r = 1.08f;
    } else if (width >= 3.0 && width < 4.0) {
        t = 0.96f + f * 0.48f;
        r = 1.08f;
    } else if (width >= 4.0 && width < 5.0) {
        t = 1.44f + f * 0.46f;
        r = 1.08f;
    } else if (width >= 5.0 && width < 6.0) {
        t = 1.9f + f * 0.6f;
        r = 1.08f;
    } else if (width >= 6.0) {
        float ff = width - 6.0f;
        t = 2.5f + ff * 0.50f;
        r = 1.08f;
    }
    // determine angle of the line to horizontal
    // core thinkness of a line
    float tx = 0;
    float ty = 0;
    // fading edge of a line
    float rx = 0;
    float ry = 0;
    // cap of a line
    float cx = 0;
    float cy = 0;
    float epsilon = 0.01f;
    float dx = x2 - x1;
    float dy = y2 - y1;
    if (Math.abs(dx) < epsilon) {
        // vertical
        tx = t;
        ty = 0;
        rx = r;
        ry = 0;
        if (width > 0.0 && width < 1.0) {
            tx *= 8;
        } else if (width == 1.0) {
            tx *= 10;
        }
    } else if (Math.abs(dy) < epsilon) {
        // horizontal
        tx = 0;
        ty = t;
        rx = 0;
        ry = r;
        if (width > 0.0 && width < 1.0) {
            ty *= 8;
        } else if (width == 1.0) {
            ty *= 10;
        }
    } else {
        if (width < 3) {
            // approximate to make things even faster
            float m = dy / dx;
            // and calculate tx,ty,rx,ry
            if (m > -0.4142 && m <= 0.4142) {
                // -22.5< angle <= 22.5, approximate to 0 (degree)
                tx = t * 0.1f;
                ty = t;
                rx = r * 0.6f;
                ry = r;
            } else if (m > 0.4142 && m <= 2.4142) {
                // 22.5< angle <= 67.5, approximate to 45 (degree)
                tx = t * -0.7071f;
                ty = t * 0.7071f;
                rx = r * -0.7071f;
                ry = r * 0.7071f;
            } else if (m > 2.4142 || m <= -2.4142) {
                // 67.5 < angle <=112.5, approximate to 90 (degree)
                tx = t;
                ty = t * 0.1f;
                rx = r;
                ry = r * 0.6f;
            } else if (m > -2.4142 && m < -0.4142) {
                // 112.5 < angle < 157.5, approximate to 135 (degree)
                tx = t * 0.7071f;
                ty = t * 0.7071f;
                rx = r * 0.7071f;
                ry = r * 0.7071f;
            }
        } else {
            // calculate to exact
            dx = y1 - y2;
            dy = x2 - x1;
            float len = (float) Math.sqrt((double) dx * dx + (double) dy * dy);
            dx /= len;
            dy /= len;
            cx = -0.6f * dy;
            cy = 0.6f * dx;
            tx = t * dx;
            ty = t * dy;
            rx = r * dx;
            ry = r * dy;
        }
    }
    lineMeshData.reallocate(0, 0);
    lineMeshData.indices.rewind();
    lineMeshData.position.rewind();
    lineMeshData.color0.rewind();
    Vector3f v1 = new Vector3f();
    Vector4f v2 = new Vector4f();
    lineMeshData.position.put(v1.set(x1 - tx - rx, y1 - ty - ry, 0.0f));
    lineMeshData.position.put(v1.set(x2 - tx - rx, y2 - ty - ry, 0.0f));
    lineMeshData.position.put(v1.set(x1 - tx, y1 - ty, 0.0f));
    lineMeshData.position.put(v1.set(x2 - tx, y2 - ty, 0.0f));
    lineMeshData.position.put(v1.set(x1 + tx, y1 + ty, 0.0f));
    lineMeshData.position.put(v1.set(x2 + tx, y2 + ty, 0.0f));
    if (!((Math.abs(dx) < epsilon || Math.abs(dy) < epsilon) && width <= 1.0)) {
        lineMeshData.position.put(v1.set(x1 + tx + rx, y1 + ty + ry, 0.0f));
        lineMeshData.position.put(v1.set(x2 + tx + rx, y2 + ty + ry, 0.0f));
    }
    Color c = new Color();
    if (!alphaBlend) {
        lineMeshData.color0.put(c.set(v1.set(bRed, bGreen, bBlue)));
        lineMeshData.color0.put(c.set(v1.set(bRed, bGreen, bBlue)));
        lineMeshData.color0.put(c.set(v1.set(cRed, cGreen, cBlue)));
        lineMeshData.color0.put(c.set(v1.set(cRed, cGreen, cBlue)));
        lineMeshData.color0.put(c.set(v1.set(cRed, cGreen, cBlue)));
        lineMeshData.color0.put(c.set(v1.set(cRed, cGreen, cBlue)));
        lineMeshData.color0.put(c.set(v1.set(bRed, bGreen, bBlue)));
    } else {
        lineMeshData.color0.put(c.set(v2.set(bRed, bGreen, bBlue, 0.0f)));
        lineMeshData.color0.put(c.set(v2.set(bRed, bGreen, bBlue, 0.0f)));
        lineMeshData.color0.put(c.set(v2.set(cRed, cGreen, cBlue, a)));
        lineMeshData.color0.put(c.set(v2.set(cRed, cGreen, cBlue, a)));
        lineMeshData.color0.put(c.set(v2.set(cRed, cGreen, cBlue, a)));
        lineMeshData.color0.put(c.set(v2.set(cRed, cGreen, cBlue, 0.0f)));
        lineMeshData.color0.put(c.set(v2.set(bRed, bGreen, bBlue, 0.0f)));
    }
    lineMesh.reload(lineMeshData);
    lineMesh.render();
    // cap (do not draw if too thin)
    if (width >= 3) {
        lineMeshData.reallocate(0, 0);
        lineMeshData.indices.rewind();
        lineMeshData.position.rewind();
        lineMeshData.color0.rewind();
        lineMeshData.position.put(v1.set(x1 - rx + cx, y1 - ry + cy, 0.0f));
        lineMeshData.position.put(v1.set(x1 + rx + cx, y1 + ry + cy, 0.0f));
        lineMeshData.position.put(v1.set(x1 - tx - rx, y1 - ty - ry, 0.0f));
        lineMeshData.position.put(v1.set(x1 + tx + rx, y1 + ty + ry, 0.0f));
        lineMeshData.position.put(v1.set(x2 - rx - cx, y2 - ry - cy, 0.0f));
        lineMeshData.position.put(v1.set(x2 + rx - cx, y2 + ry - cy, 0.0f));
        lineMeshData.position.put(v1.set(x2 - tx - rx, y2 - ty - ry, 0.0f));
        lineMeshData.position.put(v1.set(x2 + tx + rx, y2 + ty + ry, 0.0f));
        if (!alphaBlend) {
            lineMeshData.color0.put(c.set(v1.set(bRed, bGreen, bBlue)));
            lineMeshData.color0.put(c.set(v1.set(bRed, bGreen, bBlue)));
            lineMeshData.color0.put(c.set(v1.set(cRed, cGreen, cBlue)));
            lineMeshData.color0.put(c.set(v1.set(cRed, cGreen, cBlue)));
            lineMeshData.color0.put(c.set(v1.set(bRed, bGreen, bBlue)));
            lineMeshData.color0.put(c.set(v1.set(bRed, bGreen, bBlue)));
            lineMeshData.color0.put(c.set(v1.set(cRed, cGreen, cBlue)));
        } else {
            lineMeshData.color0.put(c.set(v2.set(cRed, cGreen, cBlue, 0)));
            lineMeshData.color0.put(c.set(v2.set(cRed, cGreen, cBlue, 0)));
            lineMeshData.color0.put(c.set(v2.set(cRed, cGreen, cBlue, a)));
            lineMeshData.color0.put(c.set(v2.set(cRed, cGreen, cBlue, a)));
            lineMeshData.color0.put(c.set(v2.set(cRed, cGreen, cBlue, 0)));
            lineMeshData.color0.put(c.set(v2.set(cRed, cGreen, cBlue, 0)));
            lineMeshData.color0.put(c.set(v2.set(cRed, cGreen, cBlue, a)));
        }
        lineMesh.reload(lineMeshData);
        lineMesh.render();
    }
    GL11.glEnable(GL11.GL_CULL_FACE);
}
Also used : Vector4f(org.joml.Vector4f) Vector3f(org.joml.Vector3f) Color(org.terasology.nui.Color) Mesh(org.terasology.engine.rendering.assets.mesh.Mesh)

Example 32 with Color

use of org.terasology.nui.Color in project Terasology by MovingBlocks.

the class FloatingTextComponent method copyFrom.

@Override
public void copyFrom(FloatingTextComponent other) {
    this.text = other.text;
    this.textColor = new Color(other.textColor);
    this.textShadowColor = new Color(other.textShadowColor);
    this.scale = other.scale;
    this.isOverlay = other.isOverlay;
}
Also used : Color(org.terasology.nui.Color)

Example 33 with Color

use of org.terasology.nui.Color in project Terasology by MovingBlocks.

the class RegionOutlineComponent method copyFrom.

@Override
public void copyFrom(RegionOutlineComponent other) {
    this.corner1 = new Vector3i(other.corner1);
    this.corner2 = new Vector3i(other.corner2);
    this.color = new Color(other.color);
}
Also used : Color(org.terasology.nui.Color) Vector3i(org.joml.Vector3i)

Example 34 with Color

use of org.terasology.nui.Color in project Terasology by MovingBlocks.

the class SkeletalMeshComponent method copyFrom.

@Override
public void copyFrom(SkeletalMeshComponent other) {
    this.mesh = other.mesh;
    this.material = other.material;
    this.animation = other.animation;
    this.loop = other.loop;
    this.animationPool = Lists.newArrayList(other.animationPool);
    this.animationRate = other.animationRate;
    this.heightOffset = other.heightOffset;
    this.boneEntities = Maps.newHashMap(other.boneEntities);
    this.rootBone = other.rootBone;
    this.animationTime = other.animationTime;
    this.scale = new Vector3f(other.scale);
    this.translate = new Vector3f(other.translate);
    this.color = new Color(other.color);
}
Also used : Vector3f(org.joml.Vector3f) Color(org.terasology.nui.Color)

Example 35 with Color

use of org.terasology.nui.Color 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)

Aggregations

Color (org.terasology.nui.Color)39 Test (org.junit.jupiter.api.Test)9 Vector3f (org.joml.Vector3f)5 ByteBuffer (java.nio.ByteBuffer)3 Mesh (org.terasology.engine.rendering.assets.mesh.Mesh)3 ResourceUrn (org.terasology.gestalt.assets.ResourceUrn)3 Colorc (org.terasology.nui.Colorc)3 ColorModel (java.awt.image.ColorModel)2 DataBufferInt (java.awt.image.DataBufferInt)2 Vector2f (org.joml.Vector2f)2 Vector3fc (org.joml.Vector3fc)2 Vector4f (org.joml.Vector4f)2 Test (org.junit.Test)2 ColorConstraint (org.terasology.engine.config.flexible.constraints.ColorConstraint)2 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)2 StandardMeshData (org.terasology.engine.rendering.assets.mesh.StandardMeshData)2 VertexResource (org.terasology.engine.rendering.assets.mesh.resource.VertexResource)2 VertexResourceBuilder (org.terasology.engine.rendering.assets.mesh.resource.VertexResourceBuilder)2 ParagraphRenderStyle (org.terasology.engine.rendering.nui.widgets.browser.ui.style.ParagraphRenderStyle)2 FastRandom (org.terasology.engine.utilities.random.FastRandom)2