2013-09-30 15 views
10

Tôi đang cố gắng tạo hiệu ứng nền like this cho menu tạm dừng của mình. Ý tưởng hiện tại của tôi là chụp ảnh màn hình tạm dừng, lưu nó, mở nó ra, Gaussian làm mờ nó, sau đó đưa nó lên màn hình và hiển thị menu ontop. Vấn đề duy nhất là tôi không biết cách lưu ảnh chụp màn hình một cách hiệu quả.Làm mờ màn hình trong LibGDX

Tôi đã thử sử dụng batch.setColor(0,0,0,0.7f); để hiển thị hình ảnh mờ dần trên nền nhưng nó không cho tôi hiệu ứng mờ ảo mà tôi đang tìm kiếm, thay vì chỉ là một màu như tôi đã giả định.

Ví dụ/tài liệu được đánh giá cao.

EDIT: tìm thấy mã này

package com.me.mygdxgame; 

import java.awt.Point; 
import java.awt.color.ColorSpace; 
import java.awt.image.BufferedImage; 
import java.awt.image.ColorModel; 
import java.awt.image.ComponentColorModel; 
import java.awt.image.DataBuffer; 
import java.awt.image.DataBufferByte; 
import java.awt.image.PixelInterleavedSampleModel; 
import java.awt.image.Raster; 
import java.awt.image.WritableRaster; 
import java.io.File; 
import java.io.IOException; 

import javax.imageio.ImageIO; 

import com.badlogic.gdx.Application.ApplicationType; 
import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.utils.ScreenUtils; 

public class ScreenShot { 

    private static final int[] RGBA_OFFSETS = { 0, 1, 2, 3 }; 
    private static final int[] RGB_OFFSETS = { 0, 1, 2 }; 

    public static void saveScreenshot(String baseName) throws IOException { 
     File createTempFile = File.createTempFile(baseName, ".png"); 
     saveScreenshot(createTempFile); 
    } 

    public static void saveScreenshot(File file) throws IOException { 
     saveScreenshot(file, false); 
    } 

    public static void saveScreenshot(File file, boolean hasAlpha) throws IOException { 
     if (Gdx.app.getType() == ApplicationType.Android) 
      return; 

     byte[] screenshotPixels = ScreenUtils.getFrameBufferPixels(true); 

     int width = Gdx.graphics.getWidth(); 
     int height = Gdx.graphics.getHeight(); 

     saveScreenshot(file, screenshotPixels, width, height, hasAlpha); 
    } 

    public static void saveScreenshot(File file, byte[] pixels, int width, int height, boolean hasAlpha) throws IOException { 
     DataBufferByte dataBuffer = new DataBufferByte(pixels, pixels.length); 

     PixelInterleavedSampleModel sampleModel = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, width, height, 4, 4 * width, getOffsets(hasAlpha)); 

     WritableRaster raster = Raster.createWritableRaster(sampleModel, dataBuffer, new Point(0, 0)); 

     BufferedImage img = new BufferedImage(getColorModel(hasAlpha), raster, false, null); 

     ImageIO.write(img, "png", file); 
    } 

    private static ColorModel getColorModel(boolean alpha) { 
     if (alpha) 
      return new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[] { 8, 8, 8, 8 }, true, false, ComponentColorModel.TRANSLUCENT, DataBuffer.TYPE_BYTE); 
     return new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[] { 8, 8, 8 }, false, false, ComponentColorModel.OPAQUE, DataBuffer.TYPE_BYTE); 
    } 

    private static int[] getOffsets(boolean alpha) { 
     if (alpha) 
      return RGBA_OFFSETS; 
     return RGB_OFFSETS; 
    } 

} 

nhưng nó mang lại cho tôi lỗi này khi tôi cố gắng saveScreenshot("output");

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: java.awt.image.RasterFormatException: Incorrect scanline stride: 3200 
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:111) 
Caused by: java.awt.image.RasterFormatException: Incorrect scanline stride: 3200 
(phiên bản máy tính để bàn runnign, nếu mà làm cho một sự khác biệt đã không thử nghiệm trên một android.)

dòng lỗi: ImageIO.write(img, "png", file);

+2

Làm cách nào để tạo hiệu ứng Gaussian blur trên ảnh? –

Trả lời

10

Tôi trễ bữa tiệc nhưng đây chính xác là những gì bạn đang tìm kiếm:

https://github.com/mattdesl/lwjgl-basics/wiki/ShaderLesson5

Render đến một đối tượng đệm lĩnh vực sử dụng một Shader để tạo ra vệt mờ, sau đó rút ra nó từ bộ đệm khung lên màn hình.

+0

Bạn nói đúng, đây chính xác là câu trả lời! Cảm ơn. – raidensan

Các vấn đề liên quan