Skip to content

MizunagiKB/edge_detect_shader

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

61 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Edge Detect Shader

What's is this?

This application is used as a reference for layout when drawing manga and illustrations.

You can overlay the 3D model on the illustration you are drawing.

Normal normal render

Transparent Type1 transparent 1

Transparent Type2 transparent 2

Overview

This shader is implemented by referring to the following.

The Sobel filter samples the surrounding information to determine its own information. A two-dimensional image is used for sampling.

For Godot Engine, you can use SCREEN_TEXTURE or DEPTH_TEXTURE. My implementation uses only DEPTH_TEXTURE.

Technique

My implementation has been crafted to work with the depth buffer.

The depth buffer is expressed as 1.0 for the distance and 0.0 for the near. Therefore, create an expression that makes the transparency of a flat place 0.0 when the Sobel filter is applied.

Specifically, it is the following part.

vec2 screen_unit = vec2(edge_size / screen_w, edge_size / screen_h);

float lin_depth = -8.0 * texture(DEPTH_TEXTURE, SCREEN_UV).r;

lin_depth += texture(DEPTH_TEXTURE, SCREEN_UV + vec2(0.0, screen_unit.y)).r;
lin_depth += texture(DEPTH_TEXTURE, SCREEN_UV + vec2(0.0, -screen_unit.y)).r;
lin_depth += texture(DEPTH_TEXTURE, SCREEN_UV + vec2(screen_unit.x, 0.0)).r;
lin_depth += texture(DEPTH_TEXTURE, SCREEN_UV + vec2(-screen_unit.x, 0.0)).r;
lin_depth += texture(DEPTH_TEXTURE, SCREEN_UV + screen_unit.xy).r;
lin_depth += texture(DEPTH_TEXTURE, SCREEN_UV - screen_unit.xy).r;
lin_depth += texture(DEPTH_TEXTURE, SCREEN_UV + vec2(-screen_unit.x, screen_unit.y)).r;
lin_depth += texture(DEPTH_TEXTURE, SCREEN_UV + vec2(screen_unit.x, -screen_unit.y)).r;

The reason for multiplying by -8.0 is that the Sobel filter samples the surrounding area eight times.

It's probably easier to understand if you think in 1D rather than 2D, so I'll show the figure below. In 2D, the number of surrounding samples is 8, but in 1D it is 2.

Sobel filter

Cheat sheet cheetsheet

Fonts

Shaders

Models

The attached data is downloaded from 3D Warehouse. These data are not outside the scope of this software license.