dora_ssr/dora/
pass.rs

1/* Copyright (c) 2016-2025 Li Jin <dragon-fly@qq.com>
2
3Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
5The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
7THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
8
9extern "C" {
10	fn pass_type() -> i32;
11	fn pass_set_grab_pass(slf: i64, val: i32);
12	fn pass_is_grab_pass(slf: i64) -> i32;
13	fn pass_set(slf: i64, name: i64, val: f32);
14	fn pass_set_vec4(slf: i64, name: i64, val_1: f32, val_2: f32, val_3: f32, val_4: f32);
15	fn pass_set_color(slf: i64, name: i64, val: i32);
16	fn pass_new(vert_shader: i64, frag_shader: i64) -> i64;
17}
18use crate::dora::IObject;
19/// A struct representing a shader pass.
20pub struct Pass { raw: i64 }
21crate::dora_object!(Pass);
22impl Pass {
23	pub(crate) fn type_info() -> (i32, fn(i64) -> Option<Box<dyn IObject>>) {
24		(unsafe { pass_type() }, |raw: i64| -> Option<Box<dyn IObject>> {
25			match raw {
26				0 => None,
27				_ => Some(Box::new(Pass { raw: raw }))
28			}
29		})
30	}
31	/// Sets whether this Pass should be a grab pass.
32	/// A grab pass will render a portion of game scene into a texture frame buffer.
33	/// Then use this texture frame buffer as an input for next render pass.
34	pub fn set_grab_pass(&mut self, val: bool) {
35		unsafe { pass_set_grab_pass(self.raw(), if val { 1 } else { 0 }) };
36	}
37	/// Gets whether this Pass should be a grab pass.
38	/// A grab pass will render a portion of game scene into a texture frame buffer.
39	/// Then use this texture frame buffer as an input for next render pass.
40	pub fn is_grab_pass(&self) -> bool {
41		return unsafe { pass_is_grab_pass(self.raw()) != 0 };
42	}
43	/// Sets the value of shader parameters.
44	///
45	/// # Arguments
46	///
47	/// * `name` - The name of the parameter to set.
48	/// * `val` - The numeric value to set.
49	pub fn set(&mut self, name: &str, val: f32) {
50		unsafe { pass_set(self.raw(), crate::dora::from_string(name), val); }
51	}
52	/// Sets the values of shader parameters.
53	///
54	/// # Arguments
55	///
56	/// * `name` - The name of the parameter to set.
57	/// * `val1` - The first numeric value to set.
58	/// * `val2` - An optional second numeric value to set.
59	/// * `val3` - An optional third numeric value to set.
60	/// * `val4` - An optional fourth numeric value to set.
61	pub fn set_vec4(&mut self, name: &str, val_1: f32, val_2: f32, val_3: f32, val_4: f32) {
62		unsafe { pass_set_vec4(self.raw(), crate::dora::from_string(name), val_1, val_2, val_3, val_4); }
63	}
64	/// Another function that sets the values of shader parameters.
65	///
66	/// Works the same as:
67	/// pass.set("varName", color.r / 255.0, color.g / 255.0, color.b / 255.0, color.opacity);
68	///
69	/// # Arguments
70	///
71	/// * `name` - The name of the parameter to set.
72	/// * `val` - The Color object to set.
73	pub fn set_color(&mut self, name: &str, val: &crate::dora::Color) {
74		unsafe { pass_set_color(self.raw(), crate::dora::from_string(name), val.to_argb() as i32); }
75	}
76	/// Creates a new Pass object.
77	///
78	/// # Arguments
79	///
80	/// * `vert_shader` - The vertex shader in binary form file string.
81	/// * `frag_shader` - The fragment shader file string. A shader file string must be one of the formats:
82	///     * "builtin:" + theBuiltinShaderName
83	///     * "Shader/compiled_shader_file.bin"
84	///
85	/// # Returns
86	///
87	/// * `Pass` - A new Pass object.
88	pub fn new(vert_shader: &str, frag_shader: &str) -> Pass {
89		unsafe { return Pass { raw: pass_new(crate::dora::from_string(vert_shader), crate::dora::from_string(frag_shader)) }; }
90	}
91}