Skip to content

wangGame/gdx-test

Repository files navigation

# LibGdx效果

libGdx效果

### 橡皮擦除

```java
public class EarserTest extends Group {
    private Image bg = null;
    private Image brush = null;
    private Image cachu = null;
    private FrameBuffer frameBuffer = null;
    private TextureRegion region ;

    public EarserTest(Stage stage) {
        setSize(1080,1920);
//        bg = new Image(new Texture("smiley_color.png"));
        //笔刷是一张20*20的透明图片
        brush = new Image(new Texture("cirwhite.png"));
        //需要擦除的图片
        cachu = new Image(new Texture(Gdx.files.internal("BG.png")));

        //framebuffer
        frameBuffer = new FrameBuffer(Pixmap.Format.RGBA8888, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);

        region = new TextureRegion(frameBuffer.getColorBufferTexture());
        region.flip(false,true);
        //先把被擦除的图片渲染进frameBuffer

        stage.addListener(new ClickListener(){
            @Override
            public void touchDragged(InputEvent event, float x, float y, int pointer) {
                super.touchDragged(event, x, y, pointer);
                System.out.println("-------------");
                brush.setPosition(event.getStageX()*2-brush.getHeight()/2,event.getStageY()*2-brush.getHeight()/2);
            }
        });

        addActor(brush);
        addActor(cachu);
    }

    private boolean isDraw;
    public void drawFrame(Batch batch){
        if (isDraw) {
            return;
        }
        isDraw = true;
        frameBuffer.begin();
        batch.flush();
        cachu.draw(batch,1);
        batch.flush();
        frameBuffer.end();
    }


    @Override
    public void draw(Batch batch, float parentAlpha) {
        drawFrame(batch);
        drawFrameBrush(batch);
        batch.setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
        batch.draw(region, 0f, 0f);
    }

    private void drawFrameBrush(Batch batch) {
        frameBuffer.begin();
        batch.flush();
        batch.setBlendFunction(GL20.GL_ZERO, GL20.GL_ONE_MINUS_SRC_ALPHA);
        batch.enableBlending();
        brush.draw(batch,1);
        batch.flush();

        frameBuffer.end();
    }
}
```



<img src="C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220329141922969.png" alt="image-20220329141922969" style="zoom:50%;" />











### 使用Mesh绘制线



效果:通过拉动贝塞尔曲线上的点控制线的弯曲程度

![image-20220329143120855](C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220329143120855.png)







































/**
 This shader is basically an improvement upon a previous fireworks shader I did a while
 ago (www.shadertoy.com/view/Ws3SRS). Here, each firework spawns at a random position
 instead of the just following the same pattern in a loop, so the fireworks show can be
 watched and enjoyed infinitely. The fireworks spawn from behind the buildings, but in
 front of the mountains. I hope everyone finds watching this relaxing and enjoyable!

 I wish a very happy and a prosperous new year to everyone!
*/

#define PI  3.141592653589793
#define TAU 6.283185307179586

// Helper macros
#define C(x) clamp(x, 0., 1.)
#define S(a, b, x) smoothstep(a, b, x)
#define F(x, f) (floor(x * f) / f)

// Fireworks control variables
#define FIREWORK_COUNT 148
#define FIREWORK_DURATION 8.
#define FIREWORK_LOW .75
#define FIREWORK_HIGH 1.05
#define ROCKET_PARTICES 16
#define ROCKET_DURATION 1.5
#define FLASH_DURATION ROCKET_DURATION + .2
#define THRUSTER_SPEED .25
#define EXPLOSION_STRENGTH .025;
#define EXPLOSION_PARTICLES 100

// Hash functions by Dave_Hoskins
vec2 hash21(float p)
{
	vec3 p3 = fract(vec3(p) * vec3(.1031, .1030, .0973));
	p3 += dot(p3, p3.yzx + 33.33);
    return fract((p3.xx + p3.yz) * p3.zy);

}

vec3 hash31(float p)
{
	uvec3 n = uint(int(p)) * uvec3(1597334673U, 3812015801U, 2798796415U);
	n = (n.x ^ n.y ^ n.z) * uvec3(1597334673U, 3812015801U, 2798796415U);
	return vec3(n) * (1. / float(0xffffffffU));
}

float hash11(float p)
{
	uvec2 n = uint(int(p)) * uvec2(1597334673U, 3812015801U);
	uint q = (n.x ^ n.y) * 1597334673U;
	return float(q) * (1. / float(0xffffffffU));
}

// Function to remap a value from [a, b] to [c, d]
float remap(float x, float a, float b, float c, float d)
{
    return (((x - a) / (b - a)) * (d - c)) + c;
}

// Noise (from iq)
float noise (in vec3 p) {
	vec3 f = fract (p);
	p = floor (p);
	f = f * f * (3. - 2. * f);
	f.xy += p.xy + p.z * vec2 (37., 17.);
	f.xy = texture (iChannel0, (f.xy + .5) / 256., -256.).yx;
	return mix (f.x, f.y, f.z);
}

// Tiny fbm
float fbm (in vec3 p) {
	return noise (p) + noise (p * 2.) / 2. + noise (p * 4.) / 4.;
}

// Building window lights from www.shadertoy.com/view/wtt3WB
float windows (vec2 uv, float offset)
{
    vec2 grid = vec2(20., 1.);
    uv.x += offset;
    float n1 = fbm((vec2(ivec2(uv * grid)) + .5).xxx);
    uv.x *= n1 * 6.;
    vec2 id = vec2(ivec2(uv * grid)) + .5;
    float n = fbm(id.xxx);
    vec2 lightGrid = vec2(79. * (n + .5), 250. * n);
    float n2 = fbm((vec2(ivec2(uv * lightGrid + floor(iTime * .4) * .2)) + .5).xyx);
    vec2 lPos = fract(uv * lightGrid);
    n2 = (lPos.y < .2 || lPos.y > .7) ? 0. : n2;
    n2 = (lPos.x < .5 || lPos.y > .7) ? 0. : n2;
    n2 = smoothstep(.225, .5, n2);
	return (uv.y < n - 0.01) ? n2 : 0.;
}

// Building skyline
float buildings(vec2 st)
{
    // An fbm style amalgamation of various cos functions
    float b = .1 * F(cos(st.x*4.0 + 1.7), 1.0);
    b += (b + .3) * 0.3 * F(cos(st.x*4.-0.1), 2.0);
    b += (b-.01) * 0.1 * F(cos(st.x*12.0), 4.);
    b += (b-.05) * 0.3 * F(cos(st.x*24.0), 1.0);
    return C((st.y + b - .1) * 100.);
}

// Twinkly stars, derived and modified from: www.shadertoy.com/view/4s33zf
float stars(vec2 st, vec2 fragCoord)
{
    vec2 uv = (2. * fragCoord - iResolution.xy) / iResolution.y;
    uv.y += .3;
    uv.y = abs(uv.y);
    float t = iTime * .1;
    vec2 h = pow(hash21(uv.x * iResolution.y + uv.y), vec2(50.));
    float twinkle = sin((st.x + t + cos(st.y * 50. + t)) * 25.);
    twinkle *= cos((st.y * .187 - t * 4.16 + sin(st.x * 11.8 + t * .347)) * 6.57);
    twinkle = twinkle * .5 + .5;
    return h.x * h.y * twinkle * 1.5;
}

// Fireworks improved upon my previous shader: https://www.shadertoy.com/view/Ws3SRS
vec3 fireworks(vec2 st)
{
	vec2 fireworkPos, particlePos;
    float radius, theta, radiusScale, spark, sparkDistFromOrigin, shimmer,
        shimmerThreshold, fade, timeHash, timeOffset, rocketPath;
    vec3 particleHash, fireworkHash, fireworkCol, finalCol;
    for (int j = 0; j < FIREWORK_COUNT; ++j)
    {
        timeHash = hash11(float(j + 1) * 2.6144 + 118.6118);
        timeOffset = float(j + 1) + float(j + 1) * timeHash;
        // This hash changes after each firework cycle (rocket + explosion)
        fireworkHash = hash31(471.5277 * float(j) + 226.9146
			+ float(int((iTime+timeOffset) / FIREWORK_DURATION))) * 2. - 1.;
        fireworkCol = fireworkHash * .5 + .5;
    	fireworkHash.y = remap(fireworkHash.y, -1., 1., FIREWORK_LOW, FIREWORK_HIGH);
        // Random firework x coordinate but confined to a cer7ain column based on j
        fireworkHash.x = ((float(j) + 1.5 + fireworkHash.x * 642.25)
			/ float(FIREWORK_COUNT)) * 2. - 1.;

        // Duration of each firework with a random start time
        float time = mod(iTime + timeOffset, FIREWORK_DURATION);
        if (time > ROCKET_DURATION)
        {
            fireworkPos = vec2(fireworkHash.x, fireworkHash.y);
            for (int i = 0; i < EXPLOSION_PARTICLES; ++i)
            {
                // Unique hash that yeilds a separate spread pattern for each explosion
                particleHash = hash31(float(j) * 1291.1978 + float(i)
					* 1619.8196 + 469.7119);
                theta = remap(particleHash.x, 0., 1., 0., TAU); // [0, 2.*PI]
                radiusScale = particleHash.y * EXPLOSION_STRENGTH;
                // Radius expands exponentially over time, i.e. explosion effect
                radius = radiusScale * time * time;
                particlePos = vec2(radius * cos(theta), radius * sin(theta));
                particlePos.y -= 8. * pow(particlePos.x, 4.); // fake-ish gravity
                spark = .0003 / pow(length(st - particlePos - fireworkPos), 1.7);
                sparkDistFromOrigin = 2. * length(fireworkPos - particlePos);
                // Shimmering effect for explosion particles
                shimmer = max(0., sqrt(sparkDistFromOrigin)
                    * (sin((iTime + particleHash.y * TAU) * 18.)));
                shimmerThreshold = FIREWORK_DURATION * .6;
                // Fade after a certain time threshold
                fade = C((FIREWORK_DURATION * 2.) * radiusScale - radius);
                finalCol += mix(spark, spark * shimmer, smoothstep(shimmerThreshold
					* radiusScale, (shimmerThreshold + 1.) * radiusScale , radius))
                    * fade * fireworkCol;
            }

            // Initial instant flash for the explosion
            if(time < FLASH_DURATION)
            	finalCol += spark / (.01 + mod(time, ROCKET_DURATION));
        }
        else
        {
            rocketPath = mod(time, ROCKET_DURATION) / ROCKET_DURATION;
            // ease out sine
            rocketPath = sin(rocketPath / (ROCKET_DURATION * .75) * PI * .5);
            fireworkPos = vec2(fireworkHash.x,
                    rocketPath * fireworkHash.y);
            // Slight random wiggle for the rocket's path
    		fireworkPos.x += sin(st.y * 50. + time) * fireworkCol.z * .0035;

            // Rockets flying before the explosion
            for (int i = 0; i < ROCKET_PARTICES; ++i)
            {
                particleHash = hash31((float(i) * 603.6837) + 1472.3486);
                // rocket trail size
                float t = time * (2. - time);
                radius = mod(time + particleHash.y, THRUSTER_SPEED) / THRUSTER_SPEED
					* particleHash.z * .1;
                // Confine theta to a small value for a vertical thrust effect
                theta = remap(particleHash.x, 0., 1., 0., PI * .1) + PI * 1.45;
                particlePos = vec2(radius * cos(theta), radius * sin(theta));
                finalCol += 8e-5 / pow(length(st - particlePos - fireworkPos), 1.1)
                    * mix(vec3(1.4, .7, .2), vec3(1.4), radius * 16.);
            }
        }
    }
    return finalCol;
}

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{

    vec2 uv = (5.3 * fragCoord - iResolution.xy) / min(iResolution.y, iResolution.x);
    uv.y += .0; // shift the horizon a bit lower
    float reflection = 0.;

    if (uv.y < 0.)
    {
        reflection = 3.;
        // watery distortion in the lake (improved)
        /**
        uv.x += sin((uv.y * 64. + sin(iTime) * .2)
			* cos(uv.y * 128. - cos(iTime) * .1)) * .15;
		*/

        // slightly better looking water waves
        uv.x += cos(uv.y * 192. - iTime * .6) * sin(uv.y * 96. + iTime * .75) * .042;
    }

    // Our special uv coord that gives us reflection effect for pratically free
    vec2 st = vec2(uv.x, abs(uv.y));
    vec3 col = vec3(0.01);

    // Background mountain
    float mountain = sin(1.69 * st.x * 1.38 * cos(2.74 * st.x) + 4.87
		* sin(1.17 * st.x)) * .1 - .18 + st.y;
    mountain = C(S(-.005, .005, mountain));

    float building = buildings(st);

    // Finally blend everything together

    // Sky color
    col += vec3(.24 - st.y * .1, .18 - st.y * .1, .1 + st.y * .1);
    // Blend the mountain and the sky
    col = col * mountain + vec3(.1 - st.y * .1, .1 - st.y * .1, .08) * (1. - mountain);
    // Occlude the mountain with the building skyline
    col *= building;

    // Yellow-ish window color tint
    col += windows(st * .8, 2.) * (1.-building) * vec3(1.2, 1., .8);

    // Moon white circle
    float moon = smoothstep(.3, .29, length(st-vec2(1., .8)));

    //Twinkly stars, masked by the buildings, mountain, and the moon
    col += stars(st, fragCoord) * mountain * building * (1. - moon);

    // Cut the main moon circle with an offset inverted one to make a crescent
    moon *= smoothstep(.32, .48, length(st-vec2(.92, .88))) * 1.25;

    // Add the moon to the scene
    col += moon * vec3(1.2, 1.18, 1.);


    // Fireworks launch from behind the buildings, but in front of the mountains
    col += C(fireworks(st)) * (building + moon);

    // Slightly change of the reflections to watery blueish-green
    col.r -= reflection * .02;
    col.gb += reflection * .008;

    fragColor = vec4(col, 1.0);
}


## 使用shader完成曲线

感觉不是很好,绘制区域有限定,对于有的曲线是无能为力的, 处理方法就是绘制一个大一些的画布,让在上面会绘制
这个方法可行。

但是不会去写,都是去抄写公式,实际意义不大。

About

gdx写的一些效果

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published