Coder Social home page Coder Social logo

riccardoscalco / glsl-simple-alpha-compositing Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 0.0 469 KB

GLSL implementation of the simple alpha compositing function.

Home Page: https://github.com/riccardoscalco/glsl-simple-alpha-compositing

License: MIT License

GLSL 100.00%
glsl alpha compositing

glsl-simple-alpha-compositing's Introduction

glsl-simple-alpha-compositing

GLSL implementation of the simple alpha compositing function, as described in the W3C Editor's draft in Compositing and Blending.

Install

npm install glsl-simple-alpha-compositing

Usage

#pragma glslify: simpleAlphaCompositing = require(glsl-simple-alpha-compositing);

vec4 source = vec4(1., 0., 0., .5);
vec4 backdrop = vec4(0., 1., 0., .3);

vec3 color = simpleAlphaCompositing(source, backdrop);

An example: set the background color in a GLSL fragment shader.

The simple alpha compositing can be used to set the background colour in a glsl fragment shader.

The following code uses the step function to set the alpha channel value to 1 for all the points which distance from the circle centre is less than 0.25, otherwise, the alpha channel is set to 0.

The simpleAlphaCompositing function is used to compose the blue circle with a yellow background.

vec3 circleField (vec2 p) {
	// Colors
	vec3 circleColor = vec3(0., 0., 1.); // blue
	vec3 backgroundColor = vec3(1., 1., 0.); // yellow

	// Background field
	vec4 background = vec4(backgroundColor, 1.);

	// Foreground field, a circle.
	vec2 center = vec2(.5);
	float d = distance(p, center);
	float delta = 0.0005;
	// Note that the circular shape is implemented in the alpha channel.
	vec4 circle = vec4(circleColor, smoothstep(d - delta, d + delta, 0.25));

	return simpleAlphaCompositing(circle, background);
}

void main() {
	vec3 color = circleField(vUv);
	gl_FragColor = vec4(color, 1.0);
}

glsl-simple-alpha-compositing's People

Contributors

dependabot-preview[bot] avatar riccardoscalco avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.