Skip to content

Commit

Permalink
[shgraph] Added support SourceTexture to screenShaderGraph
Browse files Browse the repository at this point in the history
  • Loading branch information
EspeuteClement committed Nov 19, 2024
1 parent 5d26ddc commit 46e8212
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 11 deletions.
25 changes: 22 additions & 3 deletions hide/view/shadereditor/ShaderEditor.hx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class PreviewShaderBase extends hxsl.Shader {

@global var global : {
var time : Float;
var screenShaderInput : Sampler2D;
};

@global var camera : {
Expand Down Expand Up @@ -130,6 +131,8 @@ class PreviewSettings {
public var unlit : Bool = false;
public var previewAlpha : Bool = false;


public var screenFXusePrevTarget : Bool = false;
public var screenFXBlend : h3d.mat.PbrMaterial.PbrBlend = Alpha;
public var width : Int = 300;
public var height : Int = 300;
Expand Down Expand Up @@ -992,7 +995,7 @@ class ShaderEditor extends hide.view.FileView implements GraphInterface.IGraphEd
menu.appendTo(group);

function getScreenFXBlend(blend: h3d.mat.PbrMaterial.PbrBlend) : hide.comp.ContextMenu.MenuItem {
return {label: cast blend, click: () -> {
return {label: "Blend " + cast blend, click: () -> {
previewSettings.screenFXBlend = blend;

for (fx in meshPreviewScreenFX) {
Expand All @@ -1014,6 +1017,9 @@ class ShaderEditor extends hide.view.FileView implements GraphInterface.IGraphEd
AlphaMultiply,
];



// for (blend in blends) getScreenFXBlend(blend)
menu.click((e) -> {
hide.comp.ContextMenu.createDropdown(menu.get(0), [
{label: "Reset Camera", click: resetPreviewCamera},
Expand All @@ -1030,8 +1036,9 @@ class ShaderEditor extends hide.view.FileView implements GraphInterface.IGraphEd
{label: "Backface Cull", click: () -> {previewSettings.backfaceCulling = !previewSettings.backfaceCulling; meshPreviewShader = null; savePreviewSettings();}, stayOpen: true, checked: previewSettings.backfaceCulling},
{label: "Unlit", click: () -> {previewSettings.unlit = !previewSettings.unlit; meshPreviewShader = null; savePreviewSettings();}, stayOpen: true, checked: previewSettings.unlit},
], enabled: meshPreviewPrefab == null},
{label: "Screen FX Blend", enabled: meshPreviewPrefab == null && meshPreviewScreenFX.length > 0, menu: [
for (blend in blends) getScreenFXBlend(blend)
{label: "Screen FX", enabled: meshPreviewPrefab == null && meshPreviewScreenFX.length > 0, menu: [
{label: "Use Prev Target", click: () -> setPreviewScreenFXUsePrevTarget(!previewSettings.screenFXusePrevTarget), checked: previewSettings.screenFXusePrevTarget},
{isSeparator: true},
]},
{label: "Render Settings", menu: [
{label: "Background Color", click: openBackgroundColorMenu},
Expand All @@ -1042,6 +1049,15 @@ class ShaderEditor extends hide.view.FileView implements GraphInterface.IGraphEd
});
}

public function setPreviewScreenFXUsePrevTarget(value: Bool) {
previewSettings.screenFXusePrevTarget = value;
for (fx in meshPreviewScreenFX) {
fx.usePrevTarget = value;
}

savePreviewSettings();
}

public function setPrefabAndRenderDelayed(prefab: String, renderProps: String) {
if (previewSettings == null)
loadSettings();
Expand Down Expand Up @@ -1246,6 +1262,7 @@ class ShaderEditor extends hide.view.FileView implements GraphInterface.IGraphEd
meshPreviewShader = null;

for (fx in meshPreviewScreenFX) {
fx.usePrevTarget = previewSettings.screenFXusePrevTarget;
fx.blend = previewSettings.screenFXBlend;
}
savePreviewSettings();
Expand Down Expand Up @@ -1449,6 +1466,8 @@ class ShaderEditor extends hide.view.FileView implements GraphInterface.IGraphEd
var t = engine.getCurrentTarget();
graphEditor.previewsScene.s2d.ctx.globals.set("global.pixelSize", new h3d.Vector(2 / (t == null ? engine.width : t.width), 2 / (t == null ? engine.height : t.height)));
graphEditor.previewsScene.s2d.ctx.globals.set("blackChannel", h3d.mat.Texture.fromColor(0));
graphEditor.previewsScene.s2d.ctx.globals.set("global.screenShaderInput", h3d.mat.Texture.fromColor(0xFF00FF));

}

@:privateAccess
Expand Down
13 changes: 8 additions & 5 deletions hrt/prefab/rfx/ScreenShaderGraph.hx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ private class GraphShader extends h3d.shader.ScreenShader {
static var SRC = {
@const var USE_PREV_TARGET : Bool = false;

@param var source : Sampler2D;
@global var global : {
var screenShaderInput : Sampler2D;
}

function fragment() {
if ( USE_PREV_TARGET )
pixelColor = source.get(calculatedUV);
pixelColor = global.screenShaderInput.get(calculatedUV);
}
}
}
Expand Down Expand Up @@ -54,10 +56,11 @@ class ScreenShaderGraph extends RendererFX {
r.mark("ScreenShaderGraph");
if (shader != null) {
shaderPass.shader.USE_PREV_TARGET = usePrevTarget;
if ( usePrevTarget ) {
if ( usePrevTarget) {
var ctx = r.ctx;
var target = r.allocTarget("ppTarget", false);
shaderPass.shader.source = ctx.getGlobal("ldrMap");
r.ctx.setGlobal("global.screenShaderInput", ctx.getGlobal("ldrMap"));
// shaderPass.shader.source = ctx.getGlobal("ldrMap");

ctx.engine.pushTarget(target);
shaderPass.render();
Expand All @@ -77,7 +80,7 @@ class ScreenShaderGraph extends RendererFX {
if ( usePrevTarget ) {
var ctx = r.ctx;
var target = r.allocTarget("ppTarget", false, 1.0, RGBA16F);
shaderPass.shader.source = ctx.getGlobal("hdrMap");
r.ctx.setGlobal("global.screenShaderInput", ctx.getGlobal("ldrMap"));

ctx.engine.pushTarget(target);
shaderPass.render();
Expand Down
21 changes: 18 additions & 3 deletions hrt/shgraph/ShaderGlobalInput.hx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class ShaderGlobalInput extends ShaderNode {
{display: "Time", g: Time},
{display: "Pixel Size", g: PixelSize},
{display: "Camera Global Position", g: CameraPosition},
{display: "Screen shader source", g: SourceTexture},
];

public function new(idx: Null<Int>) {
Expand All @@ -32,12 +33,26 @@ class ShaderGlobalInput extends ShaderNode {
}

override function generate(ctx: NodeGenContext) {
var input = ctx.getGlobalInput(globalInputs[variableIdx].g);
var v = ctx.getGlobalInput(globalInputs[variableIdx].g);

ctx.setOutput(0, input);
ctx.addPreview(input);
ctx.setOutput(0, v);
if (v.t.match(TSampler(_,_))) {
var uv = ctx.getGlobalInput(CalculatedUV);
var sample = AstTools.makeGlobalCall(Texture, [v, uv], TVec(4, VFloat));
ctx.addPreview(sample);
}
else {
ctx.addPreview(v);
}
}

// override function generate(ctx: NodeGenContext) {
// var input = ctx.getGlobalInput(globalInputs[variableIdx].g);

// ctx.setOutput(0, input);
// ctx.addPreview(input);
// }

override public function getAliases(name: String, group: String, description: String) {
var aliases = super.getAliases(name, group, description);
for (i => input in globalInputs) {
Expand Down
4 changes: 4 additions & 0 deletions hrt/shgraph/Variables.hx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ enum abstract Global(Int) to Int {
var SGPixelAlpha;

var EmitterPosition;

var SourceTexture;
}

enum VariableKind {
Expand Down Expand Up @@ -97,6 +99,8 @@ class Variables {

g[EmitterPosition] = {type: TVec(3, VFloat), name: "emitterPosition", varkind: KVar(Local, null), __init__: AstTools.makeVec([0.0,0.0,0.0])};

g[SourceTexture] = {type: TSampler(T2D, false), name: "screenShaderInput", varkind: KVar(Global, Global)};

g;
};

Expand Down

0 comments on commit 46e8212

Please sign in to comment.