Skip to content

Commit

Permalink
ShaderTarget: clean filterObj and fix shaderTarget not working withou…
Browse files Browse the repository at this point in the history
…t material selector
  • Loading branch information
LeoVgr committed Jan 23, 2025
1 parent 184b0e7 commit 2ce39bd
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 22 deletions.
4 changes: 1 addition & 3 deletions hrt/prefab/Material.hx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ class Material extends Prefab {
@:s public var refMatLib : String;
@:s public var overrides : Array<Dynamic> = [];

public var filterObj : h3d.scene.Object -> Bool;

#if editor
var previewSphere : h3d.scene.Object;
var gradientFollower : GradientFollower;
Expand Down Expand Up @@ -65,7 +63,7 @@ class Material extends Prefab {
return r;
}

public function getMaterials(local3d: h3d.scene.Object = null, includePreviewMats : Bool = false) {
public function getMaterials(local3d: h3d.scene.Object = null, includePreviewMats : Bool = false, filterObj : (obj : h3d.scene.Object) -> Bool = null) {
if (local3d == null)
local3d = findFirstLocal3d();
var mats = [];
Expand Down
3 changes: 1 addition & 2 deletions hrt/prefab/MaterialSelector.hx
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ class MaterialSelector extends hrt.prefab.Prefab {
passName : "all",
}];
@:s public var blendModesSelected : Array<String> = [];
public var filterObj : h3d.scene.Object -> Bool;

public function getPasses(local3d: h3d.scene.Object = null) : Array<SelectedPass> {
public function getPasses(local3d: h3d.scene.Object = null, filterObj : (obj : h3d.scene.Object) -> Bool = null) : Array<SelectedPass> {
if (local3d == null)
local3d = findFirstLocal3d();
var mats = [];
Expand Down
20 changes: 5 additions & 15 deletions hrt/prefab/Shader.hx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class Shader extends Prefab {
@:s var recursiveApply = true;

public var shader : hxsl.Shader;
public var filterObj : h3d.scene.Object -> Bool;

function new(parent, sh: ContextShared) {
super(parent, sh);
Expand Down Expand Up @@ -100,31 +99,23 @@ class Shader extends Prefab {
return targetMaterial == null || targetMaterial == mat.name;
}

function iterMaterials(callb) {
function iterMaterials(callb, filterObj : (obj : h3d.scene.Object) -> Bool = null) {
if (parent == null)
return;
var parent = parent;

if( Std.isOfType(parent, Material) ) {
var material : Material = cast parent;
var prevFilterObj = material.filterObj;
if ( filterObj != null )
material.filterObj = filterObj;
for( m in material.getMaterials(true) )
for( m in material.getMaterials(true, filterObj) )
callb(null, m);
material.filterObj = prevFilterObj;
} else if ( Std.isOfType(parent, MaterialSelector) ) {
var materialSelector = cast(parent, MaterialSelector);
var prevFilterObj = materialSelector.filterObj;
if ( filterObj != null )
materialSelector.filterObj = filterObj;
var passSelect = h3d.mat.MaterialSetup.current.createMaterial();
for ( p in materialSelector.getPasses() ) {
for ( p in materialSelector.getPasses(null, filterObj) ) {
passSelect.name = p.all ? "" : PASS_SELECT;
@:privateAccess passSelect.passes = p.pass;
callb(null, passSelect);
}
materialSelector.filterObj = prevFilterObj;
} else {
var objs = [];
function pushUnique(obj : h3d.scene.Object ) {
Expand Down Expand Up @@ -156,13 +147,12 @@ class Shader extends Prefab {
override function dispose() {
if( shared.current3d != null )
iterMaterials(function(obj,mat) if(checkMaterial(mat)) removeShader(obj, mat, shader));
filterObj = null;
super.dispose();
}

public function apply3d() {
public function apply3d(filterObj : (obj : h3d.scene.Object) -> Bool = null) {
if( shared.current3d != null )
iterMaterials(function(obj,mat) if(checkMaterial(mat)) applyShader(obj, mat, shader));
iterMaterials(function(obj,mat) if(checkMaterial(mat)) applyShader(obj, mat, shader), filterObj);
}

override function makeInstance() {
Expand Down
19 changes: 17 additions & 2 deletions hrt/prefab/fx/ShaderTarget.hx
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,30 @@ class ShaderTargetObj extends h3d.scene.Object {
public var shadersRoot : hrt.prefab.Object3D;

public function apply(fx : hrt.prefab.fx.FX) {
function reparentChildren(obj : hrt.prefab.Object3D) {
var i = obj.children.length;
while ( i-- > 0 ) {
var c = obj.children[i];
if ( Std.isOfType(c, Shader) || Std.isOfType(c, Material) || Std.isOfType(c, MaterialSelector) )
c.parent = shadersRoot;
}
}

var children = shadersRoot.children.copy();
shadersRoot.children = [];
for (c in children) {
if (Std.isOfType(c, Object3D))
reparentChildren(cast c);
}

var fxAnim : hrt.prefab.fx.FX.FXAnimation = cast fx.local3d;
shadersRoot.local3d = parent;
for ( s in shadersRoot.findAll(Shader) ) {
s.filterObj = o -> return o != fxAnim;
if (s.shader == null) {
s.makeShader();
@:privateAccess s.updateInstance();
}
s.apply3d();
s.apply3d((o) -> return o != fxAnim);
}

if (fxAnim == null)
Expand Down

0 comments on commit 2ce39bd

Please sign in to comment.