Glass Square Effect Shader

Use with Particles Node.

Shader code
/*
	魔法的硝子エフェクトシェーダー by あるる(きのもと 結衣) @arlez80
	Magical Glass Effect Shader by KINOMOTO Yui

	MIT License
*/

shader_type spatial;
render_mode blend_mix, depth_draw_opaque, cull_back, diffuse_burley, specular_schlick_ggx, unshaded;

const float edge_detection_coef = 40.0;

uniform vec4 albedo_color : hint_color = vec4( 0.02, 0.15, 1.0, 0.4 );
uniform float box_size: hint_range( 0.0, 1.0 ) = 0.3;
uniform float edge_size: hint_range( 0.0, 1.0 ) = 0.1;
uniform vec2 glass_scale = vec2( 0.03, 0.03 );

void vertex( )
{
	mat4 mat_world = mat4(
		// ビルボード化
		normalize( CAMERA_MATRIX[0] ) * length( WORLD_MATRIX[0] )
	,	normalize( CAMERA_MATRIX[1] ) * length( WORLD_MATRIX[0] )
	,	normalize( CAMERA_MATRIX[2] ) * length( WORLD_MATRIX[2] )
	,	WORLD_MATRIX[3]
	) * mat4(
		// パーティクルの回転を入れる
		vec4(
			cos( INSTANCE_CUSTOM.x )
		,	-sin( INSTANCE_CUSTOM.x )
		,	0.0
		,	0.0
		)
	,	vec4(
			sin(INSTANCE_CUSTOM.x)
		,	cos(INSTANCE_CUSTOM.x)
		,	0.0
		,	0.0
		)
	,	vec4( 0.0, 0.0, 1.0, 0.0 ),vec4( 0.0, 0.0, 0.0, 1.0 ) );
	MODELVIEW_MATRIX = INV_CAMERA_MATRIX * mat_world;
}


void fragment( )
{
	vec2 centered_uv = UV - vec2( 0.5 );

	// 枠か否か
	float is_edge = min(
		(
			max( box_size + edge_size*8.0 - abs( box_size + edge_size * 0.8 - abs( centered_uv.x ) ) * edge_detection_coef, 0.0 )
		+	max( box_size + edge_size*8.0 - abs( box_size + edge_size * 0.8 - abs( centered_uv.y ) ) * edge_detection_coef, 0.0 )
		)
	,	1.0
	);
	// 中心のガラスか?
	float is_glass = float( -box_size - edge_size < centered_uv.x ) * float( -box_size - edge_size < centered_uv.y ) * float( centered_uv.x < box_size + edge_size ) * float( centered_uv.y < box_size + edge_size );
	// もやもや
	float moya = 0.5 - length( centered_uv );

	ALBEDO = (
		vec3( UV, 1.0 ) * is_edge
	+	texture( SCREEN_TEXTURE, SCREEN_UV - centered_uv * glass_scale, 0.0 ).rgb * is_glass
	+	albedo_color.rgb * ( max( moya, 1.0 - is_glass ) * albedo_color.a )
	);
	ALPHA = clamp(
		max(
			// 縁 + 四角内部
			is_edge * is_glass + is_glass
			// もやもや
		,	moya
		)
	,	0.0
	,	1.0
	);
}
Tags
glass, particles
The shader code and all code snippets in this post are under MIT license and can be used freely. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

More from arlez80

Electric Hatch Background Shader

Procedural Electric Current Shader

Screen Noise Effect Shader

Related shaders

Procedural Stained-Glass Shader

Frosted glass

Simple 3D Glass

guest

0 Comments
Inline Feedbacks
View all comments