Skip to content

Commit

Permalink
feat: Convert fresnel_crystal.gdshader to Godot 4.3 (#58)
Browse files Browse the repository at this point in the history
* Convert fresnel_crystal.gdshader

* chore: minor refactor and proofreading

---------

Co-authored-by: Nathan Lovato <[email protected]>
  • Loading branch information
tetrapod00 and NathanLovato authored Sep 17, 2024
1 parent 7d1d44a commit c6ee3a0
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 66 deletions.
4 changes: 2 additions & 2 deletions godot/Demos/Crystals/Crystal_Albedo.png.import
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/generate=true
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
Expand All @@ -32,4 +32,4 @@ process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
detect_3d/compress_to=0
4 changes: 2 additions & 2 deletions godot/Demos/Crystals/Crystal_Emission.png.import
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/generate=true
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
Expand All @@ -32,4 +32,4 @@ process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
detect_3d/compress_to=0
6 changes: 3 additions & 3 deletions godot/Demos/Crystals/Crystal_Normal.png.import
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=1
compress/channel_pack=0
mipmaps/generate=false
compress/channel_pack=1
mipmaps/generate=true
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
Expand All @@ -32,4 +32,4 @@ process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
detect_3d/compress_to=0
42 changes: 27 additions & 15 deletions godot/Demos/CrystalsDemo.tscn
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
[gd_scene load_steps=9 format=3 uid="uid://dhup7p8c62ixt"]
[gd_scene load_steps=13 format=3 uid="uid://dhup7p8c62ixt"]

[ext_resource type="PackedScene" uid="uid://clatbtj1agsbm" path="res://Demos/Crystals/crystals.glb" id="4"]
[ext_resource type="PackedScene" path="res://Shared/Demo3DEnvironment.tscn" id="5"]
[ext_resource type="PackedScene" path="res://Shared/DemoInterface.tscn" id="6"]
[ext_resource type="Texture2D" uid="uid://cu2p18bjrwh4f" path="res://Demos/Crystals/Crystal_Albedo.png" id="4_kkods"]
[ext_resource type="PackedScene" uid="uid://dquhei2qv5csj" path="res://Shared/Demo3DEnvironment.tscn" id="5"]
[ext_resource type="Texture2D" uid="uid://cys1t6gtdcrvi" path="res://Demos/Crystals/Crystal_Emission.png" id="5_axjqt"]
[ext_resource type="PackedScene" uid="uid://diofpwcvq5elu" path="res://Shared/DemoInterface.tscn" id="6"]
[ext_resource type="Texture2D" uid="uid://qiewxcg1l806" path="res://Demos/Crystals/Crystal_Normal.png" id="6_1acp2"]
[ext_resource type="Script" path="res://addons/ShaderSecretsHelper/DemoScreen.gd" id="7"]
[ext_resource type="Shader" path="res://Shaders/fresnel_crystal.gdshader" id="8"]

[sub_resource type="ShaderMaterial" id="2"]
[sub_resource type="ShaderMaterial" id="ShaderMaterial_2753f"]
render_priority = 0
shader = ExtResource("8")
shader_parameter/metallic = 1.0
shader_parameter/roughness = 0.1
shader_parameter/emission = Color(0.996078, 0.596078, 0.0313726, 1)
shader_parameter/texture_albedo = ExtResource("4_kkods")
shader_parameter/texture_normal = ExtResource("6_1acp2")
shader_parameter/emission = Color(1, 0.6, 0.1, 1)
shader_parameter/emission_energy = 6.0
shader_parameter/fresnel_power = 6.0
shader_parameter/emission_pulse_speed = 0.75
shader_parameter/texture_emission = ExtResource("5_axjqt")
shader_parameter/fresnel_color = Color(0, 0.9, 0.9, 1)
shader_parameter/fresnel_color_intensity = 1.5
shader_parameter/fresnel_color = Color(0.0666667, 0.952941, 0.952941, 1)
shader_parameter/fresnel_pulse_speed = 0.75
shader_parameter/emission_pulse_speed = 1.0
shader_parameter/fresnel_power = 6.0
shader_parameter/fresnel_pulse_speed = 1.0

[sub_resource type="Animation" id="3"]
length = 0.001
Expand Down Expand Up @@ -51,30 +57,36 @@ tracks/0/keys = {
"values": [Vector3(0, 0, 0), Vector3(0, 360, 0)]
}

[sub_resource type="AnimationLibrary" id="AnimationLibrary_l2111"]
_data = {
"RESET": SubResource("3"),
"Rotate": SubResource("4")
}

[node name="CrystalDemo" type="CanvasLayer"]
script = ExtResource("7")

[node name="crystals" parent="." instance=ExtResource("4")]

[node name="Crystal" parent="crystals" index="0"]
material/0 = SubResource("2")
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -0.256048, -0.02628, 0.132265)
surface_material_override/0 = SubResource("ShaderMaterial_2753f")

[node name="Demo3DEnvironment" parent="." instance=ExtResource("5")]

[node name="Sun" parent="Demo3DEnvironment" index="1"]
shadow_color = Color(0.0823529, 0.690196, 0.901961, 1)

[node name="CameraPivot" type="Node3D" parent="."]

[node name="Camera3D" type="Camera3D" parent="CameraPivot"]
transform = Transform3D(1, 0, 0, 0, 0.92398, 0.382442, 0, -0.382442, 0.92398, 0, 1.55576, 1.65872)

[node name="AnimationPlayer" type="AnimationPlayer" parent="CameraPivot"]
libraries = {
"": SubResource("AnimationLibrary_l2111")
}
autoplay = "Rotate"
anims/RESET = SubResource("3")
anims/Rotate = SubResource("4")

[node name="DemoInterface" parent="." instance=ExtResource("6")]
anchors_preset = 10

[editable path="crystals"]
[editable path="Demo3DEnvironment"]
93 changes: 49 additions & 44 deletions godot/Shaders/fresnel_crystal.gdshader
Original file line number Diff line number Diff line change
@@ -1,58 +1,63 @@
// ANCHOR: all
// ANCHOR: setup
shader_type spatial;

uniform float metallic;
uniform float roughness : hint_range(0,1);
group_uniforms BasicPBRValues;
/** Metallic amount. Controls how metallic the material looks. */
uniform float metallic: hint_range(0.0,1.0) = 1.0;
/** Roughness amount. The lower the roughness, the shinier the material. As you increase the roughness, the specular highlights scatter. */
uniform float roughness : hint_range(0.0,1.0) = 0.1;
/** Albedo texture. Base color. */
uniform sampler2D texture_albedo : source_color, repeat_enable, filter_linear_mipmap;
/** Normal texture. */
uniform sampler2D texture_normal : hint_normal, repeat_enable, filter_linear_mipmap;
group_uniforms;

uniform sampler2D texture_albedo : source_color;
uniform sampler2D texture_normal : hint_normal;
// END: setup
// ANCHOR: emission_setup
uniform sampler2D texture_emission : hint_default_black;
uniform vec4 emission : source_color;
group_uniforms Emission;
/** Color of light emission. This light is emitted from the core of the crystal. */
uniform vec4 emission : source_color = vec4(1.0, 0.6, 0.1, 1.0); // Light orange.
/** Energy of emission.
* Values over [code]1.0[/code] will glow, if you have Glow enabled in your environment resource.
*/
uniform float emission_energy: hint_range(0.0, 10.0) = 6.0;
/** Emission texture. Controls the strength of the effect across the mesh. */
uniform sampler2D texture_emission : hint_default_black, repeat_enable, filter_linear_mipmap;
/** Speed of the pulsing emission effect. Higher values mean pulsing more often.
* If this matches the fresnel pulse speed, the effects will sync.
*/
uniform float emission_pulse_speed: hint_range(0.0, 2.0) = .75;
group_uniforms;

uniform float emission_energy;
// END: emission_setup
uniform float fresnel_power = 3.0;
// ANCHOR: fresnel_setup
uniform float fresnel_color_intensity = 1.0;
uniform vec4 fresnel_color : source_color;
group_uniforms Fresnel;
/** Color of fresnel effect. */
uniform vec4 fresnel_color : source_color = vec4(0.0, 0.9, 0.9, 1.0); // Near cyan.
/** Fresnel intensity.
* Values over [code]1.0[/code] will glow, if you have Glow enabled in your environment resource.
*/
uniform float fresnel_color_intensity: hint_range(0.0, 10.0) = 1.0;
/** The power of the fresnel effect.
* High values mean the effect only appears on the very edges, at glancing angles.
*/
uniform float fresnel_power: hint_range(0.5,8.0) = 6.0;
/** Speed of the pulsing fresnel effect. Higher values mean pulsing more often.
* If this matches the emission pulse speed, the effects will sync.
*/
uniform float fresnel_pulse_speed: hint_range(0.0,2.0) = 0.75;
group_uniforms;

uniform float fresnel_pulse_speed = 1.0;
// END: fresnel_setup
// ANCHOR: speed
uniform float emission_pulse_speed = 1.0;
// END: speed
// ANCHOR: setup_fragment
void fragment() {
// Set the PBR values from textures and sliders.
ALBEDO = texture(texture_albedo, UV).rgb;

NORMAL_MAP = texture(texture_normal, UV).rgb;
METALLIC = metallic;
ROUGHNESS = roughness;

NORMAL_MAP = texture(texture_normal, UV).rgb;
// END: setup_fragment
// ANCHOR: emission_tex
vec3 emission_tex = texture(texture_emission, UV).rgb;
// END: emission_tex
// ANCHOR: fresnel

// Emission.
vec3 emission_texture = texture(texture_emission, UV).rgb;
float emission_time_factor = clamp(sin(TIME * emission_pulse_speed) + 0.33, 0.33, 1);
EMISSION = ((emission.rgb * emission_texture) * emission_energy * emission_time_factor);

float fresnel = 1.0 - dot(NORMAL, VIEW);
// END: fresnel
// ANCHOR: fresnel_power
// Raise the fresnel to a power to control the thickness of the edge effect.
fresnel = pow(fresnel, fresnel_power);
// END: fresnel_power
// ANCHOR: fresnel_time
float fresnel_time_factor = clamp(sin(TIME * fresnel_pulse_speed), 0.15, 1);
// END: fresnel_time
// ANCHOR: time_factor
float emission_time_factor = clamp(sin(TIME * emission_pulse_speed) + 0.33, 0.33, 1);
// END: time_factor
// ANCHOR: emission_time
EMISSION = ((emission.rgb * emission_tex) * emission_energy * emission_time_factor);
// END: emission_time
// ANCHOR: emission_fresnel
EMISSION += (fresnel * fresnel_color.rgb * fresnel_color_intensity * fresnel_time_factor);
// END: emission_fresnel
}
// END: all

0 comments on commit c6ee3a0

Please sign in to comment.