use of org.terasology.rendering.assets.mesh.Mesh in project Terasology by MovingBlocks.
the class LwjglCanvasRenderer method drawTexture.
@Override
public void drawTexture(TextureRegion texture, Color color, ScaleMode mode, Rect2i absoluteRegion, float ux, float uy, float uw, float uh, float alpha) {
if (!texture.getTexture().isLoaded()) {
return;
}
if (!currentTextureCropRegion.equals(requestedCropRegion) && !(currentTextureCropRegion.contains(absoluteRegion) && requestedCropRegion.contains(absoluteRegion))) {
textureMat.setFloat4(CROPPING_BOUNDARIES_PARAM, requestedCropRegion.minX(), requestedCropRegion.maxX(), requestedCropRegion.minY(), requestedCropRegion.maxY());
currentTextureCropRegion = requestedCropRegion;
}
Vector2f scale = mode.scaleForRegion(absoluteRegion, texture.getWidth(), texture.getHeight());
Rect2f textureArea = texture.getRegion();
Mesh mesh = billboard;
switch(mode) {
case TILED:
{
TextureCacheKey key = new TextureCacheKey(texture.size(), absoluteRegion.size());
usedTextures.add(key);
mesh = cachedTextures.get(key);
if (mesh == null || mesh.isDisposed()) {
MeshBuilder builder = new MeshBuilder();
addTiles(builder, absoluteRegion, FULL_REGION, texture.size(), FULL_REGION);
mesh = builder.build();
cachedTextures.put(key, mesh);
}
textureMat.setFloat2("scale", scale);
textureMat.setFloat2("offset", absoluteRegion.minX(), absoluteRegion.minY());
textureMat.setFloat2("texOffset", textureArea.minX() + ux * textureArea.width(), textureArea.minY() + uy * textureArea.height());
textureMat.setFloat2("texSize", uw * textureArea.width(), uh * textureArea.height());
break;
}
case SCALE_FILL:
{
textureMat.setFloat2("offset", absoluteRegion.minX(), absoluteRegion.minY());
textureMat.setFloat2("scale", absoluteRegion.width(), absoluteRegion.height());
float texBorderX = (scale.x - absoluteRegion.width()) / scale.x * uw;
float texBorderY = (scale.y - absoluteRegion.height()) / scale.y * uh;
textureMat.setFloat2("texOffset", textureArea.minX() + (ux + 0.5f * texBorderX) * textureArea.width(), textureArea.minY() + (uy + 0.5f * texBorderY) * textureArea.height());
textureMat.setFloat2("texSize", (uw - texBorderX) * textureArea.width(), (uh - texBorderY) * textureArea.height());
break;
}
default:
{
textureMat.setFloat2("scale", scale);
textureMat.setFloat2("offset", absoluteRegion.minX() + 0.5f * (absoluteRegion.width() - scale.x), absoluteRegion.minY() + 0.5f * (absoluteRegion.height() - scale.y));
textureMat.setFloat2("texOffset", textureArea.minX() + ux * textureArea.width(), textureArea.minY() + uy * textureArea.height());
textureMat.setFloat2("texSize", uw * textureArea.width(), uh * textureArea.height());
break;
}
}
textureMat.setTexture("texture", texture.getTexture());
textureMat.setFloat4("color", color.rf(), color.gf(), color.bf(), color.af() * alpha);
textureMat.bindTextures();
mesh.render();
}
use of org.terasology.rendering.assets.mesh.Mesh in project Terasology by MovingBlocks.
the class Block method renderWithLightValue.
public void renderWithLightValue(float sunlight, float blockLight) {
if (meshGenerator == null) {
return;
}
Material mat = Assets.getMaterial("engine:prog.block").orElseThrow(() -> new RuntimeException("Missing engine material"));
mat.activateFeature(ShaderProgramFeature.FEATURE_USE_MATRIX_STACK);
mat.enable();
mat.setFloat("sunlight", sunlight);
mat.setFloat("blockLight", blockLight);
Mesh mesh = meshGenerator.getStandaloneMesh();
if (mesh != null) {
mesh.render();
}
mat.deactivateFeature(ShaderProgramFeature.FEATURE_USE_MATRIX_STACK);
}
use of org.terasology.rendering.assets.mesh.Mesh in project Terasology by MovingBlocks.
the class HeadlessGraphics method registerCoreAssetTypes.
@Override
public void registerCoreAssetTypes(ModuleAwareAssetTypeManager assetTypeManager) {
assetTypeManager.registerCoreAssetType(Font.class, (AssetFactory<Font, FontData>) FontImpl::new, "fonts");
assetTypeManager.registerCoreAssetType(Texture.class, (AssetFactory<Texture, TextureData>) HeadlessTexture::new, "textures", "fonts");
assetTypeManager.registerCoreFormat(Texture.class, new PNGTextureFormat(Texture.FilterMode.NEAREST, path -> path.getName(2).toString().equals("textures")));
assetTypeManager.registerCoreFormat(Texture.class, new PNGTextureFormat(Texture.FilterMode.LINEAR, path -> path.getName(2).toString().equals("fonts")));
assetTypeManager.registerCoreAssetType(Shader.class, (AssetFactory<Shader, ShaderData>) HeadlessShader::new, "shaders");
assetTypeManager.registerCoreAssetType(Material.class, (AssetFactory<Material, MaterialData>) HeadlessMaterial::new, "materials");
assetTypeManager.registerCoreAssetType(Mesh.class, (AssetFactory<Mesh, MeshData>) HeadlessMesh::new, "mesh");
assetTypeManager.registerCoreAssetType(SkeletalMesh.class, (AssetFactory<SkeletalMesh, SkeletalMeshData>) HeadlessSkeletalMesh::new, "skeletalMesh");
assetTypeManager.registerCoreAssetType(MeshAnimation.class, (AssetFactory<MeshAnimation, MeshAnimationData>) MeshAnimationImpl::new, "animations");
assetTypeManager.registerCoreAssetType(Atlas.class, (AssetFactory<Atlas, AtlasData>) Atlas::new, "atlas");
assetTypeManager.registerCoreAssetType(Subtexture.class, (AssetFactory<Subtexture, SubtextureData>) Subtexture::new);
}
use of org.terasology.rendering.assets.mesh.Mesh in project Terasology by MovingBlocks.
the class LwjglCanvasRenderer method drawText.
@Override
public void drawText(String text, Font font, HorizontalAlign hAlign, VerticalAlign vAlign, Rect2i absoluteRegion, Color color, Color shadowColor, float alpha, boolean underlined) {
TextCacheKey key = new TextCacheKey(text, font, absoluteRegion.width(), hAlign, color, shadowColor, underlined);
usedText.add(key);
Map<Material, Mesh> fontMesh = cachedText.get(key);
List<String> lines = TextLineBuilder.getLines(font, text, absoluteRegion.width());
if (fontMesh != null) {
for (Mesh mesh : fontMesh.values()) {
if (mesh.isDisposed()) {
fontMesh = null;
break;
}
}
}
if (fontMesh == null) {
fontMesh = fontMeshBuilder.createTextMesh(font, lines, absoluteRegion.width(), 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.height());
fontMesh.entrySet().stream().filter(entry -> entry.getKey().isRenderable()).forEach(entry -> {
entry.getKey().bindTextures();
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();
});
}
use of org.terasology.rendering.assets.mesh.Mesh in project Terasology by MovingBlocks.
the class LwjglGraphics method registerCoreAssetTypes.
@Override
public void registerCoreAssetTypes(ModuleAwareAssetTypeManager assetTypeManager) {
// cast lambdas explicitly to avoid inconsistent compiler behavior wrt. type inference
assetTypeManager.registerCoreAssetType(Font.class, (AssetFactory<Font, FontData>) FontImpl::new, "fonts");
assetTypeManager.registerCoreAssetType(Texture.class, (AssetFactory<Texture, TextureData>) (urn, assetType, data) -> (new OpenGLTexture(urn, assetType, data, this)), "textures", "fonts");
assetTypeManager.registerCoreFormat(Texture.class, new PNGTextureFormat(Texture.FilterMode.NEAREST, path -> {
if (path.getName(1).toString().equals(ModuleAssetDataProducer.OVERRIDE_FOLDER)) {
return path.getName(3).toString().equals("textures");
} else {
return path.getName(2).toString().equals("textures");
}
}));
assetTypeManager.registerCoreFormat(Texture.class, new PNGTextureFormat(Texture.FilterMode.LINEAR, path -> {
if (path.getName(1).toString().equals(ModuleAssetDataProducer.OVERRIDE_FOLDER)) {
return path.getName(3).toString().equals("fonts");
} else {
return path.getName(2).toString().equals("fonts");
}
}));
assetTypeManager.registerCoreAssetType(Shader.class, (AssetFactory<Shader, ShaderData>) GLSLShader::new, "shaders");
assetTypeManager.registerCoreAssetType(Material.class, (AssetFactory<Material, MaterialData>) GLSLMaterial::new, "materials");
assetTypeManager.registerCoreAssetType(Mesh.class, (AssetFactory<Mesh, MeshData>) (urn, assetType, data) -> new OpenGLMesh(urn, assetType, bufferPool, data), "mesh");
assetTypeManager.registerCoreAssetType(SkeletalMesh.class, (AssetFactory<SkeletalMesh, SkeletalMeshData>) (urn, assetType, data) -> new OpenGLSkeletalMesh(urn, assetType, data, bufferPool), "skeletalMesh");
assetTypeManager.registerCoreAssetType(MeshAnimation.class, (AssetFactory<MeshAnimation, MeshAnimationData>) MeshAnimationImpl::new, "animations");
assetTypeManager.registerCoreAssetType(Atlas.class, (AssetFactory<Atlas, AtlasData>) Atlas::new, "atlas");
assetTypeManager.registerCoreAssetType(Subtexture.class, (AssetFactory<Subtexture, SubtextureData>) Subtexture::new);
}
Aggregations