From ff7950c2add1ac869ab76231f790bfd55ef4c763 Mon Sep 17 00:00:00 2001 From: Rob Kelly Date: Sat, 20 Jul 2024 17:29:41 -0600 Subject: [PATCH] Simple cel shader --- src/player/cel_shader.gdshader | 51 ++++++++++++++++++++++++++++++++++ src/player/player.tscn | 36 ++++++++++++++++++++---- 2 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 src/player/cel_shader.gdshader diff --git a/src/player/cel_shader.gdshader b/src/player/cel_shader.gdshader new file mode 100644 index 0000000..7519a93 --- /dev/null +++ b/src/player/cel_shader.gdshader @@ -0,0 +1,51 @@ +// NOTE: Shader automatically converted from Godot Engine 4.2.2.stable's StandardMaterial3D. + +shader_type spatial; +render_mode blend_mix,depth_draw_opaque,cull_disabled,diffuse_toon,specular_toon; +uniform vec4 albedo : source_color; +uniform sampler2D texture_albedo : source_color,filter_linear_mipmap,repeat_enable; +uniform float roughness : hint_range(0,1); +uniform sampler2D texture_metallic : hint_default_white,filter_linear_mipmap,repeat_enable; +uniform vec4 metallic_texture_channel; +uniform sampler2D texture_roughness : hint_roughness_r,filter_linear_mipmap,repeat_enable; +uniform float metallic; +uniform vec3 uv1_scale; +uniform vec3 uv1_offset; +uniform vec3 uv2_scale; +uniform vec3 uv2_offset; + +uniform sampler2D albedo_gradient; +uniform sampler2D shadow_gradient; +uniform sampler2D fresnel_ramp; + + +float fresnel(float amount, vec3 normal, vec3 view) { + return pow((1.0 - clamp(dot(normalize(normal), normalize(view)), 0.0, 1.0)), amount); +} + +void vertex() { + UV=UV*uv1_scale.xy+uv1_offset.xy; +} + +void fragment() { + float basic_fresnel = fresnel(4.0, NORMAL, VIEW); + + vec2 base_uv = UV; + vec4 albedo_tex = texture(texture_albedo, base_uv); + ALBEDO = albedo.rgb * albedo_tex.rgb + (basic_fresnel * texture(fresnel_ramp, vec2(basic_fresnel, 0.0)).rgb * 1.0); + + float metallic_tex = dot(texture(texture_metallic,base_uv),metallic_texture_channel); + METALLIC = metallic_tex * metallic; + vec4 roughness_texture_channel = vec4(1.0,0.0,0.0,0.0); + float roughness_tex = dot(texture(texture_roughness,base_uv),roughness_texture_channel); + ROUGHNESS = roughness_tex * roughness; + SPECULAR = 0.0; +} + + +void light() { + float dotproduct = clamp(dot(NORMAL, LIGHT), -0.9, 0.9); + float sample = clamp((dotproduct + 1.0) * ATTENUATION / 2.1, 0.05, 0.95); + vec4 shaded = texture(shadow_gradient, vec2(sample, 0.0)); + DIFFUSE_LIGHT += (shaded.rgb * LIGHT_COLOR) / 2.0; +} \ No newline at end of file diff --git a/src/player/player.tscn b/src/player/player.tscn index 68f8483..cc8bbd1 100644 --- a/src/player/player.tscn +++ b/src/player/player.tscn @@ -1,16 +1,40 @@ -[gd_scene load_steps=7 format=3 uid="uid://bqp5mwyat3asv"] +[gd_scene load_steps=12 format=3 uid="uid://bqp5mwyat3asv"] [ext_resource type="Script" path="res://src/player/player.gd" id="1_h16ke"] [ext_resource type="Texture2D" uid="uid://b0yefajw1cqo" path="res://assets/textures/character_placeholder.png" id="1_iys0p"] +[ext_resource type="Shader" path="res://src/player/cel_shader.gdshader" id="2_lc05i"] [ext_resource type="Script" path="res://src/player/third_person_camera.gd" id="3_gichr"] -[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_ccqgr"] -albedo_texture = ExtResource("1_iys0p") -distance_fade_mode = 2 -distance_fade_max_distance = 1.0 +[sub_resource type="Gradient" id="Gradient_n24ft"] +offsets = PackedFloat32Array(0.443836, 0.852055) + +[sub_resource type="GradientTexture1D" id="GradientTexture1D_k01mp"] +gradient = SubResource("Gradient_n24ft") + +[sub_resource type="Gradient" id="Gradient_s23j5"] +interpolation_mode = 1 +offsets = PackedFloat32Array(0.49, 0.5) + +[sub_resource type="GradientTexture1D" id="GradientTexture1D_5ivp0"] +gradient = SubResource("Gradient_s23j5") + +[sub_resource type="ShaderMaterial" id="ShaderMaterial_4kmto"] +render_priority = 0 +shader = ExtResource("2_lc05i") +shader_parameter/albedo = Color(1, 1, 1, 1) +shader_parameter/roughness = 1.0 +shader_parameter/metallic_texture_channel = null +shader_parameter/metallic = 0.0 +shader_parameter/uv1_scale = Vector3(1, 1, 1) +shader_parameter/uv1_offset = Vector3(0, 0, 0) +shader_parameter/uv2_scale = Vector3(1, 1, 1) +shader_parameter/uv2_offset = Vector3(0, 0, 0) +shader_parameter/texture_albedo = ExtResource("1_iys0p") +shader_parameter/shadow_gradient = SubResource("GradientTexture1D_5ivp0") +shader_parameter/fresnel_ramp = SubResource("GradientTexture1D_k01mp") [sub_resource type="CapsuleMesh" id="CapsuleMesh_8sedy"] -material = SubResource("StandardMaterial3D_ccqgr") +material = SubResource("ShaderMaterial_4kmto") [sub_resource type="CapsuleShape3D" id="CapsuleShape3D_otw5j"]