KFlxShader parse and stringify uniforms to optional json
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
# @install: lix --silent download "gh://github.com/kiss-lang/kiss-tools#df38b28e32e05d7940041192c36d73924844d9cc" into kiss-tools/0.0.0/github/df38b28e32e05d7940041192c36d73924844d9cc
|
# @install: lix --silent download "gh://github.com/kiss-lang/kiss-tools#93ad23d10bd1e6446113f7fa1b1ec9595ed03d7d" into kiss-tools/0.0.0/github/93ad23d10bd1e6446113f7fa1b1ec9595ed03d7d
|
||||||
-lib kiss
|
-lib kiss
|
||||||
-cp ${HAXE_LIBCACHE}/kiss-tools/0.0.0/github/df38b28e32e05d7940041192c36d73924844d9cc/src/
|
-cp ${HAXE_LIBCACHE}/kiss-tools/0.0.0/github/93ad23d10bd1e6446113f7fa1b1ec9595ed03d7d/src/
|
||||||
-D kiss-tools=0.0.0
|
-D kiss-tools=0.0.0
|
@@ -1,12 +1,12 @@
|
|||||||
# @install: lix --silent download "gh://github.com/kiss-lang/kiss#e3f1997409ed0e9f6109f3075eaa50e36ab9e5b1" into kiss/0.0.1/github/e3f1997409ed0e9f6109f3075eaa50e36ab9e5b1
|
# @install: lix --silent download "gh://github.com/kiss-lang/kiss#a2aaca9f7ed54ba7d8b09f510dd3a1962ce0af5a" into kiss/0.0.1/github/a2aaca9f7ed54ba7d8b09f510dd3a1962ce0af5a
|
||||||
# @run: haxelib run-dir kiss "${HAXE_LIBCACHE}/kiss/0.0.1/github/e3f1997409ed0e9f6109f3075eaa50e36ab9e5b1"
|
# @run: haxelib run-dir kiss "${HAXE_LIBCACHE}/kiss/0.0.1/github/a2aaca9f7ed54ba7d8b09f510dd3a1962ce0af5a"
|
||||||
-lib haxe-strings
|
-lib haxe-strings
|
||||||
-lib hscript
|
-lib hscript
|
||||||
-lib tink_json
|
-lib tink_json
|
||||||
-lib tink_macro
|
-lib tink_macro
|
||||||
-lib tink_syntaxhub
|
-lib tink_syntaxhub
|
||||||
-lib uuid
|
-lib uuid
|
||||||
-cp ${HAXE_LIBCACHE}/kiss/0.0.1/github/e3f1997409ed0e9f6109f3075eaa50e36ab9e5b1/src
|
-cp ${HAXE_LIBCACHE}/kiss/0.0.1/github/a2aaca9f7ed54ba7d8b09f510dd3a1962ce0af5a/src
|
||||||
-D kiss=0.0.1
|
-D kiss=0.0.1
|
||||||
-w -WUnusedPattern
|
-w -WUnusedPattern
|
||||||
--macro kiss.KissFrontend.use()
|
--macro kiss.KissFrontend.use()
|
@@ -18,7 +18,7 @@
|
|||||||
(bg.screenCenter)
|
(bg.screenCenter)
|
||||||
(set bg.alpha 0.5)
|
(set bg.alpha 0.5)
|
||||||
(add bg))
|
(add bg))
|
||||||
(set shader (new kiss_flixel.shaders.MirroredOrnament))
|
(set shader (new kiss_flixel.shaders.MirroredOrnament "mirroredOrnament.json"))
|
||||||
~shader.uniforms
|
~shader.uniforms
|
||||||
(kiss_flixel.CameraTools.addBackgroundShaders (array FlxShader shader)))
|
(kiss_flixel.CameraTools.addBackgroundShaders (array FlxShader shader)))
|
||||||
|
|
||||||
|
@@ -278,15 +278,23 @@ class ShaderFrontend implements FrontendPlugin {
|
|||||||
type.fields.push({
|
type.fields.push({
|
||||||
pos: pos,
|
pos: pos,
|
||||||
name: propName,
|
name: propName,
|
||||||
kind: FProp("get", "set", Helpers.parseComplexType(_type)),
|
kind: FProp("get", "set", Helpers.parseComplexType('Null<$_type>')),
|
||||||
access: [APublic]
|
access: [APublic]
|
||||||
});
|
});
|
||||||
|
var jsonType = {
|
||||||
|
pack: ["kiss_tools"],
|
||||||
|
name: "Json" + _type
|
||||||
|
};
|
||||||
type.fields.push({
|
type.fields.push({
|
||||||
pos: pos,
|
pos: pos,
|
||||||
name: 'set_${name}${_type}',
|
name: 'set_${name}${_type}',
|
||||||
kind: FFun({
|
kind: FFun({
|
||||||
args: [{type: Helpers.parseComplexType(_type), name: "value"}],
|
args: [{type: Helpers.parseComplexType('Null<$_type>'), name: "value"}],
|
||||||
expr: macro {this.data.$name.value = [value]; return value;}
|
expr: macro {
|
||||||
|
this.data.$name.value = [value];
|
||||||
|
this.json?.put($v{propName}, new kiss_tools.JsonString(new $jsonType(value).stringify()));
|
||||||
|
return value;
|
||||||
|
}
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
type.fields.push({
|
type.fields.push({
|
||||||
@@ -294,7 +302,13 @@ class ShaderFrontend implements FrontendPlugin {
|
|||||||
name: 'get_${name}${_type}',
|
name: 'get_${name}${_type}',
|
||||||
kind: FFun({
|
kind: FFun({
|
||||||
args: [],
|
args: [],
|
||||||
expr: macro return this.data.$name.value[0]
|
expr: macro {
|
||||||
|
var v = this.data.$name.value;
|
||||||
|
return if (v == null || v.length == 0)
|
||||||
|
null;
|
||||||
|
else
|
||||||
|
v[0];
|
||||||
|
}
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -308,14 +322,14 @@ class ShaderFrontend implements FrontendPlugin {
|
|||||||
type.fields.push({
|
type.fields.push({
|
||||||
pos: pos,
|
pos: pos,
|
||||||
name: propName,
|
name: propName,
|
||||||
kind: FProp("get", "set", Helpers.parseComplexType(_type)),
|
kind: FProp("get", "set", Helpers.parseComplexType('Null<$_type>')),
|
||||||
access: [APublic]
|
access: [APublic]
|
||||||
});
|
});
|
||||||
type.fields.push({
|
type.fields.push({
|
||||||
pos: pos,
|
pos: pos,
|
||||||
name: 'set_${name}${suffix}',
|
name: 'set_${name}${suffix}',
|
||||||
kind: FFun({
|
kind: FFun({
|
||||||
args: [{type: Helpers.parseComplexType(_type), name: "value"}],
|
args: [{type: Helpers.parseComplexType('Null<$_type>'), name: "value"}],
|
||||||
expr: macro {
|
expr: macro {
|
||||||
this.data.$name.value = [value.x, value.y];
|
this.data.$name.value = [value.x, value.y];
|
||||||
return value;
|
return value;
|
||||||
@@ -351,20 +365,22 @@ class ShaderFrontend implements FrontendPlugin {
|
|||||||
type.fields.push({
|
type.fields.push({
|
||||||
pos: pos,
|
pos: pos,
|
||||||
name: propName,
|
name: propName,
|
||||||
kind: FProp("get", "set", Helpers.parseComplexType(_type)),
|
kind: FProp("get", "set", Helpers.parseComplexType('Null<$_type>')),
|
||||||
access: [APublic]
|
access: [APublic]
|
||||||
});
|
});
|
||||||
type.fields.push({
|
type.fields.push({
|
||||||
pos: pos,
|
pos: pos,
|
||||||
name: 'set_${name}${suffix}',
|
name: 'set_${name}${suffix}',
|
||||||
kind: FFun({
|
kind: FFun({
|
||||||
args: [{type: Helpers.parseComplexType(_type), name: "value"}],
|
args: [{type: Helpers.parseComplexType('Null<$_type>'), name: "value"}],
|
||||||
expr: macro {
|
expr: macro {
|
||||||
|
if (!$v{withAlpha} && value.alphaFloat != 1.0) {
|
||||||
|
throw "vec3 uniform cannot be assigned to a color with transparency";
|
||||||
|
}
|
||||||
|
this.json?.put($v{propName}, new kiss_tools.JsonString(new JsonFlxColor(value).stringify()));
|
||||||
if ($v{withAlpha}) {
|
if ($v{withAlpha}) {
|
||||||
this.data.$name.value = [value.redFloat, value.greenFloat, value.blueFloat, value.alphaFloat];
|
this.data.$name.value = [value.redFloat, value.greenFloat, value.blueFloat, value.alphaFloat];
|
||||||
return value;
|
return value;
|
||||||
} else if (value.alphaFloat != 1.0) {
|
|
||||||
throw "vec3 uniform cannot be assigned to a color with transparency";
|
|
||||||
} else {
|
} else {
|
||||||
this.data.$name.value = [value.redFloat, value.greenFloat, value.blueFloat];
|
this.data.$name.value = [value.redFloat, value.greenFloat, value.blueFloat];
|
||||||
return value;
|
return value;
|
||||||
@@ -379,6 +395,8 @@ class ShaderFrontend implements FrontendPlugin {
|
|||||||
args: [],
|
args: [],
|
||||||
expr: macro {
|
expr: macro {
|
||||||
var components = this.data.$name.value;
|
var components = this.data.$name.value;
|
||||||
|
if (components == null || components.length == 0)
|
||||||
|
return null;
|
||||||
var alpha = if ($v{withAlpha}) components[3] else 1.0;
|
var alpha = if ($v{withAlpha}) components[3] else 1.0;
|
||||||
return flixel.util.FlxColor.fromRGBFloat(components[0], components[1], components[2], alpha);
|
return flixel.util.FlxColor.fromRGBFloat(components[0], components[1], components[2], alpha);
|
||||||
}
|
}
|
||||||
@@ -410,7 +428,8 @@ class ShaderFrontend implements FrontendPlugin {
|
|||||||
expressionInterpreted = macro $v{expressionInterpreted};
|
expressionInterpreted = macro $v{expressionInterpreted};
|
||||||
}
|
}
|
||||||
|
|
||||||
defaultSetterExps.push(macro $i{name + suffix} = $expressionInterpreted);
|
var propName = macro $i{name + suffix};
|
||||||
|
defaultSetterExps.push(macro if ($propName == null) $propName = $expressionInterpreted);
|
||||||
} else {
|
} else {
|
||||||
trace('Warning! uniform $uType $name in $file may have its default value of ${expression} ignored!');
|
trace('Warning! uniform $uType $name in $file may have its default value of ${expression} ignored!');
|
||||||
}
|
}
|
||||||
@@ -446,11 +465,14 @@ class ShaderFrontend implements FrontendPlugin {
|
|||||||
args: [{
|
args: [{
|
||||||
name: "camera",
|
name: "camera",
|
||||||
opt: true
|
opt: true
|
||||||
|
}, {
|
||||||
|
name: "jsonMapFile",
|
||||||
|
opt: true
|
||||||
}],
|
}],
|
||||||
expr: macro {
|
expr: macro {
|
||||||
uniforms = $a{uniformMapExps};
|
this.uniforms = [$a{uniformMapExps}];
|
||||||
|
super(jsonMapFile);
|
||||||
super();
|
$b{defaultSetterExps};
|
||||||
data.iTime.value = [0.0];
|
data.iTime.value = [0.0];
|
||||||
if (camera == null) {
|
if (camera == null) {
|
||||||
camera = flixel.FlxG.camera;
|
camera = flixel.FlxG.camera;
|
||||||
@@ -458,7 +480,6 @@ class ShaderFrontend implements FrontendPlugin {
|
|||||||
this.camera = camera;
|
this.camera = camera;
|
||||||
data.cameraPos.value = [camera.viewLeft, camera.viewTop];
|
data.cameraPos.value = [camera.viewLeft, camera.viewTop];
|
||||||
data.cameraZoom.value = [1.0];
|
data.cameraZoom.value = [1.0];
|
||||||
$b{defaultSetterExps}
|
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
access: [APublic]
|
access: [APublic]
|
||||||
|
@@ -1,11 +1,46 @@
|
|||||||
package kiss_flixel.shaders;
|
package kiss_flixel.shaders;
|
||||||
|
|
||||||
import kiss_flixel.shaders.Uniform;
|
import kiss_flixel.shaders.Uniform;
|
||||||
|
import kiss_tools.JsonMap;
|
||||||
|
import kiss_tools.JsonString;
|
||||||
|
import kiss_tools.JsonInt;
|
||||||
|
import kiss_tools.JsonFloat;
|
||||||
|
import kiss_tools.JsonBool;
|
||||||
|
import kiss_flixel.JsonFlxColor;
|
||||||
|
import kiss_flixel.JsonFlxPoint;
|
||||||
|
|
||||||
class KFlxShader extends flixel.system.FlxAssets.FlxShader {
|
class KFlxShader extends flixel.system.FlxAssets.FlxShader {
|
||||||
public var uniforms(default, null):Map<String,Uniform> = new Map();
|
public var uniforms(default, null):Map<String,Uniform>;
|
||||||
|
|
||||||
public function new() {
|
var json:JsonStringMap = null;
|
||||||
|
|
||||||
|
public function new(?jsonMapFile:String) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
|
if (jsonMapFile != null) {
|
||||||
|
json = new JsonMap(jsonMapFile, new JsonString(""));
|
||||||
|
|
||||||
|
for (name => uniform in uniforms) {
|
||||||
|
if (json.exists(name)) {
|
||||||
|
switch (uniform) {
|
||||||
|
case Boolean:
|
||||||
|
Reflect.setProperty(this, name, new JsonBool(false).parse(json.get(name).value).value);
|
||||||
|
case AnyInt | IntRange(_, _) | IntRangeStep(_, _, _):
|
||||||
|
Reflect.setProperty(this, name, new JsonInt(0).parse(json.get(name).value).value);
|
||||||
|
case AnyFloat | FloatRange(_, _) | FloatRangeStep(_, _, _):
|
||||||
|
var value = new JsonFloat(0).parse(json.get(name).value).value;
|
||||||
|
Reflect.setProperty(this, name, value);
|
||||||
|
case ColorSolid | ColorWithAlpha:
|
||||||
|
var value = new JsonFlxColor(0).parse(json.get(name).value).value;
|
||||||
|
Reflect.setProperty(this, name, value);
|
||||||
|
case Vector2:
|
||||||
|
Reflect.setProperty(this, name, new JsonFlxPoint(flixel.math.FlxPoint.get()).parse(json.get(name).value).value);
|
||||||
|
// TODO
|
||||||
|
case Vector3:
|
||||||
|
trace('Warning! Shader uniform $name of type $uniform is not handled in kiss-flixel json');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -4,10 +4,10 @@
|
|||||||
// by FencerDevLog (CC0)
|
// by FencerDevLog (CC0)
|
||||||
// Ported to kiss-flixel by NQNStudios
|
// Ported to kiss-flixel by NQNStudios
|
||||||
|
|
||||||
uniform vec3 color_a = vec3(0.5);
|
uniform vec3 color_a: hint_color = vec3(0.5);
|
||||||
uniform vec3 color_b = vec3(0.5);
|
uniform vec3 color_b: hint_color = vec3(0.5);
|
||||||
uniform vec3 color_c = vec3(1.0);
|
uniform vec3 color_c: hint_color = vec3(1.0);
|
||||||
uniform vec3 color_d = vec3(0.0, 0.33, 0.67);
|
uniform vec3 color_d: hint_color = vec3(0.0, 0.33, 0.67);
|
||||||
uniform int iterations: hint_range(1, 50, 1) = 10;
|
uniform int iterations: hint_range(1, 50, 1) = 10;
|
||||||
uniform float speed: hint_range(0.1, 10.0) = 1.0;
|
uniform float speed: hint_range(0.1, 10.0) = 1.0;
|
||||||
uniform float zoom: hint_range(0.1, 5.0) = 1.0;
|
uniform float zoom: hint_range(0.1, 5.0) = 1.0;
|
||||||
|
Reference in New Issue
Block a user