Fix missing STEP variable by making it mandatory to be defined as a uniform in the shader file
This commit is contained in:
parent
7ad9ef002a
commit
cc59ba9b9e
3 changed files with 16 additions and 10 deletions
|
@ -62,6 +62,8 @@ Example:
|
||||||
//!load ...
|
//!load ...
|
||||||
//!steps 5
|
//!steps 5
|
||||||
|
|
||||||
|
uniform int STEP; // this is mandatory!
|
||||||
|
|
||||||
void fragment() {
|
void fragment() {
|
||||||
if (STEP == 0) {
|
if (STEP == 0) {
|
||||||
...
|
...
|
||||||
|
|
|
@ -3,6 +3,8 @@ shader_type canvas_item;
|
||||||
//!steps 9
|
//!steps 9
|
||||||
//!load ./images/swamp.jpg
|
//!load ./images/swamp.jpg
|
||||||
|
|
||||||
|
uniform int STEP;
|
||||||
|
|
||||||
const float strength = 0.01;
|
const float strength = 0.01;
|
||||||
|
|
||||||
void fragment() {
|
void fragment() {
|
||||||
|
|
|
@ -37,20 +37,21 @@ func validate_shader_compilation(shader: Shader) -> bool:
|
||||||
# test if uniform list is empty -> if it is empty, the shader compilation failed
|
# test if uniform list is empty -> if it is empty, the shader compilation failed
|
||||||
return len(shader.get_shader_uniform_list()) > 0
|
return len(shader.get_shader_uniform_list()) > 0
|
||||||
|
|
||||||
func inject_step_uniform(shader_code: String) -> Shader:
|
func shader_has_step_uniform(shader: Shader) -> bool:
|
||||||
var shader = Shader.new()
|
for u in shader.get_shader_uniform_list():
|
||||||
# this should run after validate_shader_compilation()
|
if u["name"] == "STEP" && u["type"] == 2:
|
||||||
var fragment_function_match = _fragment_function_regex.search(shader_code)
|
return true
|
||||||
shader.code = shader_code.insert(fragment_function_match.get_start(), "\nuniform int STEP;")
|
return false
|
||||||
return shader
|
|
||||||
|
|
||||||
func update(overwrite_image_path: String = "") -> Array: # returns error messages (strings)
|
func update(overwrite_image_path: String = "") -> Array: # returns error messages (strings)
|
||||||
var original_shader_code = Filesystem.shader_code # read from disk
|
var original_shader_code = Filesystem.shader_code # read from disk
|
||||||
if original_shader_code == "":
|
if original_shader_code == "":
|
||||||
return ["No shader loaded!"]
|
return ["No shader loaded!"]
|
||||||
# inject STEP uniform & get number of steps
|
var shader: Shader = Shader.new()
|
||||||
var shader: Shader = inject_step_uniform(original_shader_code)
|
shader.code = original_shader_code
|
||||||
|
# get number of steps & check if code has STEP uniform
|
||||||
var steps: int = ShaderDirectiveParser.parse_steps_directive(shader.code)
|
var steps: int = ShaderDirectiveParser.parse_steps_directive(shader.code)
|
||||||
|
var has_step_uniform: bool = shader_has_step_uniform(shader)
|
||||||
# validate shader
|
# validate shader
|
||||||
if not validate_shader_compilation(shader):
|
if not validate_shader_compilation(shader):
|
||||||
return ["Shader compilation failed!"]
|
return ["Shader compilation failed!"]
|
||||||
|
@ -97,6 +98,7 @@ func update(overwrite_image_path: String = "") -> Array: # returns error message
|
||||||
image_sprite.material = mat
|
image_sprite.material = mat
|
||||||
# iterate n times
|
# iterate n times
|
||||||
for i in range(steps):
|
for i in range(steps):
|
||||||
|
if has_step_uniform:
|
||||||
# set STEP param
|
# set STEP param
|
||||||
mat.set_shader_parameter("STEP", i)
|
mat.set_shader_parameter("STEP", i)
|
||||||
# Get viewport texture
|
# Get viewport texture
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue