Skip to content

Commit

Permalink
Fixed bug preventing use
Browse files Browse the repository at this point in the history
apparently, Godot doesn't like it when you preloaded resource that references a texture.
  • Loading branch information
SIsilicon authored Apr 19, 2018
1 parent 400ce90 commit 5d2654f
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 9 deletions.
4 changes: 2 additions & 2 deletions addons/SIsilicon.vfx.lens flare/lens-flare-plugin.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ extends EditorPlugin
func _enter_tree():
add_custom_type(
"LensFlare", "Node",
preload("res://addons/sisilicon.vfx.lensflare/lens-flare.gd"),
preload("res://addons/sisilicon.vfx.lensflare/lens_flare_icon.png")
preload("lens-flare.gd"),
preload("lens_flare_icon.png")
)

func _exit_tree():
Expand Down
92 changes: 92 additions & 0 deletions addons/SIsilicon.vfx.lens flare/lens-flare-shader.tres
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
[gd_resource type="ShaderMaterial" load_steps=4 format=2]

[sub_resource type="Shader" id=1]

code = "shader_type spatial;
render_mode skip_vertex_transform, unshaded, blend_add;

void vertex() {
VERTEX = (INV_PROJECTION_MATRIX * vec4(VERTEX, 1.0)).xyz;
VERTEX.z += 0.95;
VERTEX.xy *= 20.0;
}

uniform int ghosts = 4;
uniform float ghost_dispersal = 0.5;
uniform float halo_width = 0.25;
uniform float distort = 0.25;

uniform float bloom_scale = 10.0;
uniform float bloom_bias = 0.95;

uniform sampler2D lens_color;
uniform sampler2D lens_dirt: hint_color_white;

float weight(vec2 pos) {
float w = length(vec2(0.5) - pos) / length(vec2(0.5));
return pow(1.0 - w, 5.0);
}

vec4 bloomtex(in sampler2D tex, in vec2 texcoord, in float lod) {
return max(vec4(0.0), texture(tex, texcoord, lod) - bloom_bias) * bloom_scale;
}

vec4 textureDistorted(in sampler2D tex, in vec2 texcoord, in vec2 direction, in vec3 distortion) {
return vec4(
bloomtex(tex, texcoord + direction * distortion.r, 2.0).r,
bloomtex(tex, texcoord + direction * distortion.g, 2.0).g,
bloomtex(tex, texcoord + direction * distortion.b, 2.0).b,
1.0
);
}

void fragment() {
vec2 texcoord = 1.0 - SCREEN_UV;
vec2 ghostVec = (vec2(0.5) - texcoord) * ghost_dispersal;

float pixelSizeX = 1.0 / float(textureSize(SCREEN_TEXTURE, 0).x);

vec3 distortion = vec3(-pixelSizeX * distort, 0.0, pixelSizeX * distort);
vec2 direction = normalize(ghostVec);

vec3 result = vec3(0.0);
for(int i = 0; i < ghosts; ++i) {
vec2 offset = fract(texcoord + ghostVec * float(i));
result += textureDistorted(SCREEN_TEXTURE, offset, direction, distortion).rgb * weight(offset);
}
result *= texture(lens_color, vec2(length(vec2(0.5) - texcoord) / length(vec2(0.5)), 0)).rgb;


vec2 haloVec = normalize(ghostVec) * halo_width;
result += textureDistorted(SCREEN_TEXTURE, texcoord + haloVec, direction, distortion).rgb * weight(fract(texcoord + haloVec));

ALBEDO = result * mix(texture(lens_dirt, texcoord).rgb, vec3(0.5), 0.4);

//uncomment to debug bright point extraction
//COLOR = bloomtex(SCREEN_TEXTURE, SCREEN_UV, 2.0);
}"

[sub_resource type="Gradient" id=2]

offsets = PoolRealArray( 0, 0.106509, 0.142012, 0.189349, 0.295858, 0.461538, 0.538462, 0.550296, 0.928994, 1 )
colors = PoolColorArray( 1, 1, 1, 1, 0.65625, 1, 0.97583, 1, 1, 1, 1, 1, 0.531006, 0.78125, 0.570107, 1, 0.309998, 0.168106, 0.522199, 1, 0.261653, 0.0339813, 0.511719, 1, 0.691406, 0.108032, 0.244761, 1, 0.706731, 0.417368, 0.109337, 1, 0.774481, 0.756324, 0.309654, 1, 0.890625, 0.125, 1, 1 )

[sub_resource type="GradientTexture" id=3]

flags = 4
gradient = SubResource( 2 )
width = 2048

[resource]

render_priority = 0
shader = SubResource( 1 )
shader_param/ghosts = 7
shader_param/ghost_dispersal = 0.3
shader_param/halo_width = 0.25
shader_param/distort = 2
shader_param/bloom_scale = 10.0
shader_param/bloom_bias = 1.05
shader_param/lens_color = SubResource( 3 )
_sections_unfolded = [ "Resource", "shader_param" ]

8 changes: 2 additions & 6 deletions addons/SIsilicon.vfx.lens flare/lens-flare.gd
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,9 @@ func _init():
screen.mesh = CubeMesh.new()
screen.scale = Vector3(1,1,1) * pow(2.0,30);
add_child(screen)

screen.material_override = preload("res://addons/sisilicon.vfx.lensflare/lens_flare_shader3D.tres")
screen.material_override = preload("lens-flare-shader.tres")
material = screen.material_override

#func _ready():
# print(get_node('/root').get_hdr())

func set_flare_strength(strength):
flareStrength = strength
material.set_shader_param('bloom_scale', strength)
Expand All @@ -52,4 +48,4 @@ func set_halo_width(width):

func set_lens_dirt(tex):
lensDirt = tex
material.set_shader_param('lens_dirt', tex)
material.set_shader_param('lens_dirt', tex)
2 changes: 1 addition & 1 deletion addons/SIsilicon.vfx.lens flare/plugin.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
name="LensFlare Effect"
description="A post-processing effect that generates the lens flare phenomenon caused by real world cameras. It may not be physically accurate, but it can suffice for most scenarios."
author="SIsilicon"
version="1.0.0"
version="1.0.1"
script="lens-flare-plugin.gd"

0 comments on commit 5d2654f

Please sign in to comment.