Skip to content

Commit 9a0f850

Browse files
julienf-unityEvergreen
authored andcommitted
[VFX] Fix ConstructMatrix serialization issue + add symetrical SplitMatrix operator
- Fixes https://jira.unity3d.com/browse/UUM-57915 - Add a settings to select between row and column - Add symetrical operator SplitMatrix Another possible way to have done it would have been through expandable slots but it lacks the row/colum selection and would have been a bit hacky as not handled directly by our system. A proxy struct would have been needed to do it right but it seems more involved/risky. Here the solution chosen is consistent with how Shader Graph is doing it.
1 parent c2c10d8 commit 9a0f850

12 files changed

Lines changed: 327 additions & 83 deletions

File tree

Packages/com.unity.visualeffectgraph/Editor/Expressions/VFXExpressionTransform.cs

Lines changed: 137 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -465,22 +465,22 @@ public override string GetCodeString(string[] parents)
465465
}
466466
}
467467

468-
class VFXExpressionVector3sToMatrix : VFXExpression
468+
class VFXExpressionRowToMatrix : VFXExpression
469469
{
470-
public VFXExpressionVector3sToMatrix() : this(new VFXExpression[] { new VFXValue<Vector3>(Vector3.right), new VFXValue<Vector3>(Vector3.up), new VFXValue<Vector3>(Vector3.forward), VFXValue<Vector3>.Default }
470+
public VFXExpressionRowToMatrix() : this(new VFXExpression[] { new VFXValue<Vector4>(new Vector4(1, 0, 0, 0)), new VFXValue<Vector4>(new Vector4(0, 1, 0, 0)), new VFXValue<Vector4>(new Vector4(0, 0, 1, 0)), new VFXValue<Vector4>(new Vector4(0, 0, 0, 1)) }
471471
)
472472
{
473473
}
474474

475-
public VFXExpressionVector3sToMatrix(params VFXExpression[] parents) : base(VFXExpression.Flags.None, parents)
475+
public VFXExpressionRowToMatrix(params VFXExpression[] parents) : base(VFXExpression.Flags.None, parents)
476476
{
477477
}
478478

479479
public override VFXExpressionOperation operation
480480
{
481481
get
482482
{
483-
return VFXExpressionOperation.Vector3sToMatrix;
483+
return VFXExpressionOperation.RowToMatrix;
484484
}
485485
}
486486

@@ -491,42 +491,42 @@ sealed protected override VFXExpression Evaluate(VFXExpression[] constParents)
491491
var zReduce = constParents[2];
492492
var wReduce = constParents[3];
493493

494-
var x = xReduce.Get<Vector3>();
495-
var y = yReduce.Get<Vector3>();
496-
var z = zReduce.Get<Vector3>();
497-
var w = wReduce.Get<Vector3>();
494+
var x = xReduce.Get<Vector4>();
495+
var y = yReduce.Get<Vector4>();
496+
var z = zReduce.Get<Vector4>();
497+
var w = wReduce.Get<Vector4>();
498498

499499
Matrix4x4 matrix = new Matrix4x4();
500-
matrix.SetColumn(0, new Vector4(x.x, x.y, x.z, 0.0f));
501-
matrix.SetColumn(1, new Vector4(y.x, y.y, y.z, 0.0f));
502-
matrix.SetColumn(2, new Vector4(z.x, z.y, z.z, 0.0f));
503-
matrix.SetColumn(3, new Vector4(w.x, w.y, w.z, 1.0f));
500+
matrix.SetRow(0, x);
501+
matrix.SetRow(1, y);
502+
matrix.SetRow(2, z);
503+
matrix.SetRow(3, w);
504504

505505
return VFXValue.Constant(matrix);
506506
}
507507

508508
public override string GetCodeString(string[] parents)
509509
{
510-
return string.Format("VFXCreateMatrixFromColumns(float4({0}, 0.0), float4({1}, 0.0), float4({2}, 0.0), float4({3}, 1.0));", parents[0], parents[1], parents[2], parents[3]);
510+
return string.Format("VFXCreateMatrixFromRows({0}, {1}, {2}, {3});", parents[0], parents[1], parents[2], parents[3]);
511511
}
512512
}
513513

514-
class VFXExpressionVector4sToMatrix : VFXExpression
514+
class VFXExpressionColumnToMatrix : VFXExpression
515515
{
516-
public VFXExpressionVector4sToMatrix() : this(new VFXExpression[] { new VFXValue<Vector4>(new Vector4(1, 0, 0, 0)), new VFXValue<Vector4>(new Vector4(0, 1, 0, 0)), new VFXValue<Vector4>(new Vector4(0, 0, 1, 0)), new VFXValue<Vector4>(new Vector4(0, 0, 0, 1)) }
516+
public VFXExpressionColumnToMatrix() : this(new VFXExpression[] { new VFXValue<Vector4>(new Vector4(1, 0, 0, 0)), new VFXValue<Vector4>(new Vector4(0, 1, 0, 0)), new VFXValue<Vector4>(new Vector4(0, 0, 1, 0)), new VFXValue<Vector4>(new Vector4(0, 0, 0, 1)) }
517517
)
518518
{
519519
}
520520

521-
public VFXExpressionVector4sToMatrix(params VFXExpression[] parents) : base(VFXExpression.Flags.None, parents)
521+
public VFXExpressionColumnToMatrix(params VFXExpression[] parents) : base(VFXExpression.Flags.None, parents)
522522
{
523523
}
524524

525525
public override VFXExpressionOperation operation
526526
{
527527
get
528528
{
529-
return VFXExpressionOperation.Vector4sToMatrix;
529+
return VFXExpressionOperation.ColumnToMatrix;
530530
}
531531
}
532532

@@ -557,25 +557,80 @@ public override string GetCodeString(string[] parents)
557557
}
558558
}
559559

560-
class VFXExpressionMatrixToVector3s : VFXExpression
560+
class VFXExpressionAxisToMatrix : VFXExpression
561+
{
562+
public VFXExpressionAxisToMatrix() : this(new VFXExpression[] { new VFXValue<Vector3>(Vector3.right), new VFXValue<Vector3>(Vector3.up), new VFXValue<Vector3>(Vector3.forward), VFXValue<Vector3>.Default }
563+
)
564+
{
565+
}
566+
567+
public VFXExpressionAxisToMatrix(params VFXExpression[] parents) : base(VFXExpression.Flags.None, parents)
568+
{
569+
}
570+
571+
public override VFXExpressionOperation operation
572+
{
573+
get
574+
{
575+
return VFXExpressionOperation.AxisToMatrix;
576+
}
577+
}
578+
579+
sealed protected override VFXExpression Evaluate(VFXExpression[] constParents)
580+
{
581+
var xReduce = constParents[0];
582+
var yReduce = constParents[1];
583+
var zReduce = constParents[2];
584+
var wReduce = constParents[3];
585+
586+
var x = xReduce.Get<Vector3>();
587+
var y = yReduce.Get<Vector3>();
588+
var z = zReduce.Get<Vector3>();
589+
var w = wReduce.Get<Vector3>();
590+
591+
Matrix4x4 matrix = new Matrix4x4();
592+
matrix.SetColumn(0, new Vector4(x.x, x.y, x.z, 0.0f));
593+
matrix.SetColumn(1, new Vector4(y.x, y.y, y.z, 0.0f));
594+
matrix.SetColumn(2, new Vector4(z.x, z.y, z.z, 0.0f));
595+
matrix.SetColumn(3, new Vector4(w.x, w.y, w.z, 1.0f));
596+
597+
return VFXValue.Constant(matrix);
598+
}
599+
600+
public override string GetCodeString(string[] parents)
601+
{
602+
return string.Format("VFXCreateMatrixFromColumns(float4({0}, 0.0), float4({1}, 0.0), float4({2}, 0.0), float4({3}, 1.0));", parents[0], parents[1], parents[2], parents[3]);
603+
}
604+
}
605+
606+
class VFXExpressionMatrixToRow : VFXExpression
561607
{
562-
public VFXExpressionMatrixToVector3s() : this(new VFXExpression[] { VFXValue<Matrix4x4>.Default, VFXValue.Constant<int>(0) } // TODO row index should not be an expression!
608+
public VFXExpressionMatrixToRow() : this(new VFXExpression[] { VFXValue<Matrix4x4>.Default, VFXValue.Constant<int>(0) }
563609
)
564610
{
565611
}
566612

567-
public VFXExpressionMatrixToVector3s(params VFXExpression[] parents) : base(VFXExpression.Flags.None, parents)
613+
public VFXExpressionMatrixToRow(params VFXExpression[] parents) : base(VFXExpression.Flags.None, parents)
568614
{
569615
}
570616

571617
public override VFXExpressionOperation operation
572618
{
573619
get
574620
{
575-
return VFXExpressionOperation.MatrixToVector3s;
621+
return VFXExpressionOperation.MatrixToRow;
576622
}
577623
}
578624

625+
protected sealed override VFXExpression Reduce(VFXExpression[] reducedParents)
626+
{
627+
var parent = reducedParents[0];
628+
if (parent is VFXExpressionRowToMatrix && reducedParents[1].Is(Flags.Constant))
629+
return parent.parents[reducedParents[1].Get<int>()];
630+
631+
return base.Reduce(reducedParents);
632+
}
633+
579634
sealed protected override VFXExpression Evaluate(VFXExpression[] constParents)
580635
{
581636
var matReduce = constParents[0];
@@ -584,34 +639,43 @@ sealed protected override VFXExpression Evaluate(VFXExpression[] constParents)
584639
var mat = matReduce.Get<Matrix4x4>();
585640
var axis = axisReduce.Get<int>();
586641

587-
return VFXValue.Constant<Vector3>(mat.GetColumn(axis));
642+
return VFXValue.Constant(mat.GetRow(axis));
588643
}
589644

590645
public override string GetCodeString(string[] parents)
591646
{
592-
return string.Format("VFXGetColumnFromMatrix({0}, {1}).xyz", parents[0], parents[1]);
647+
return string.Format("VFXGetRowFromMatrix({0}, {1})", parents[0], parents[1]);
593648
}
594649
}
595650

596-
class VFXExpressionMatrixToVector4s : VFXExpression
651+
class VFXExpressionMatrixToColumn : VFXExpression
597652
{
598-
public VFXExpressionMatrixToVector4s() : this(new VFXExpression[] { VFXValue<Matrix4x4>.Default, VFXValue.Constant<int>(0) } // TODO row index should not be an expression!
653+
public VFXExpressionMatrixToColumn() : this(new VFXExpression[] { VFXValue<Matrix4x4>.Default, VFXValue.Constant<int>(0) }
599654
)
600655
{
601656
}
602657

603-
public VFXExpressionMatrixToVector4s(params VFXExpression[] parents) : base(VFXExpression.Flags.None, parents)
658+
public VFXExpressionMatrixToColumn(params VFXExpression[] parents) : base(VFXExpression.Flags.None, parents)
604659
{
605660
}
606661

607662
public override VFXExpressionOperation operation
608663
{
609664
get
610665
{
611-
return VFXExpressionOperation.MatrixToVector4s;
666+
return VFXExpressionOperation.MatrixToColumn;
612667
}
613668
}
614669

670+
protected sealed override VFXExpression Reduce(VFXExpression[] reducedParents)
671+
{
672+
var parent = reducedParents[0];
673+
if (parent is VFXExpressionColumnToMatrix && reducedParents[1].Is(Flags.Constant))
674+
return parent.parents[reducedParents[1].Get<int>()];
675+
676+
return base.Reduce(reducedParents);
677+
}
678+
615679
sealed protected override VFXExpression Evaluate(VFXExpression[] constParents)
616680
{
617681
var matReduce = constParents[0];
@@ -628,4 +692,50 @@ public override string GetCodeString(string[] parents)
628692
return string.Format("VFXGetColumnFromMatrix({0}, {1})", parents[0], parents[1]);
629693
}
630694
}
695+
696+
class VFXExpressionMatrixToAxis : VFXExpression
697+
{
698+
public VFXExpressionMatrixToAxis() : this(new VFXExpression[] { VFXValue<Matrix4x4>.Default, VFXValue.Constant<int>(0) }
699+
)
700+
{
701+
}
702+
703+
public VFXExpressionMatrixToAxis(params VFXExpression[] parents) : base(VFXExpression.Flags.None, parents)
704+
{
705+
}
706+
707+
public override VFXExpressionOperation operation
708+
{
709+
get
710+
{
711+
return VFXExpressionOperation.MatrixToAxis;
712+
}
713+
}
714+
715+
protected sealed override VFXExpression Reduce(VFXExpression[] reducedParents)
716+
{
717+
var parent = reducedParents[0];
718+
if (parent is VFXExpressionAxisToMatrix && reducedParents[1].Is(Flags.Constant))
719+
return parent.parents[reducedParents[1].Get<int>()];
720+
721+
return base.Reduce(reducedParents);
722+
}
723+
724+
sealed protected override VFXExpression Evaluate(VFXExpression[] constParents)
725+
{
726+
var matReduce = constParents[0];
727+
var axisReduce = constParents[1];
728+
729+
var mat = matReduce.Get<Matrix4x4>();
730+
var axis = axisReduce.Get<int>();
731+
732+
Vector4 c = mat.GetColumn(axis);
733+
return VFXValue.Constant(new Vector3(c.x, c.y, c.z));
734+
}
735+
736+
public override string GetCodeString(string[] parents)
737+
{
738+
return string.Format("VFXGetColumnFromMatrix({0}, {1}).xyz", parents[0], parents[1]);
739+
}
740+
}
631741
}

Packages/com.unity.visualeffectgraph/Editor/Models/Blocks/Implementations/Position/PositionMesh.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,10 @@ public override IEnumerable<VFXNamedExpression> parameters
280280
{
281281
var sourceTransform = sampling[0];
282282

283-
var i = new VFXExpressionMatrixToVector3s(sourceTransform, VFXValue.Constant(0));
284-
var j = new VFXExpressionMatrixToVector3s(sourceTransform, VFXValue.Constant(1));
285-
var k = new VFXExpressionMatrixToVector3s(sourceTransform, VFXValue.Constant(2));
286-
var p = new VFXExpressionMatrixToVector3s(sourceTransform, VFXValue.Constant(3));
283+
var i = new VFXExpressionMatrixToAxis(sourceTransform, VFXValue.Constant(0));
284+
var j = new VFXExpressionMatrixToAxis(sourceTransform, VFXValue.Constant(1));
285+
var k = new VFXExpressionMatrixToAxis(sourceTransform, VFXValue.Constant(2));
286+
var p = new VFXExpressionMatrixToAxis(sourceTransform, VFXValue.Constant(3));
287287

288288
yield return new VFXNamedExpression(i, "readAxisX");
289289
yield return new VFXNamedExpression(j, "readAxisY");

Packages/com.unity.visualeffectgraph/Editor/Models/Operators/Implementations/ConstructMatrix.cs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,46 @@ namespace UnityEditor.VFX.Operator
44
{
55
[VFXHelpURL("Operator-ConstructMatrix")]
66
[VFXInfo(name = "Construct Matrix", category = "Math/Vector")]
7-
class MatrixFromVector4 : VFXOperator
7+
class ConstructMatrix : VFXOperator
88
{
9+
public enum Order
10+
{
11+
Row,
12+
Column,
13+
}
14+
15+
[VFXSetting, Tooltip("Specifies the order of the vectors, either row or column")]
16+
public Order order = Order.Column;
17+
918
public class InputProperties
1019
{
11-
[Tooltip("The first column of the matrix to construct.")]
12-
public Vector4 c0 = new Vector4(1, 0, 0, 0);
13-
[Tooltip("The second column of the matrix to construct.")]
14-
public Vector4 c1 = new Vector4(0, 1, 0, 0);
15-
[Tooltip("The third column of the matrix to construct.")]
16-
public Vector4 c2 = new Vector4(0, 0, 1, 0);
17-
[Tooltip("The fourth column of the matrix to construct.")]
18-
public Vector4 c3 = new Vector4(0, 0, 0, 1);
20+
[Tooltip("The first vector of the matrix to construct.")]
21+
public Vector4 m0 = new Vector4(1, 0, 0, 0);
22+
[Tooltip("The second vector of the matrix to construct.")]
23+
public Vector4 m1 = new Vector4(0, 1, 0, 0);
24+
[Tooltip("The third vector of the matrix to construct.")]
25+
public Vector4 m2 = new Vector4(0, 0, 1, 0);
26+
[Tooltip("The fourth vector of the matrix to construct.")]
27+
public Vector4 m3 = new Vector4(0, 0, 0, 1);
1928
}
2029
public class OutputProperties
2130
{
2231
public Matrix4x4 m = Matrix4x4.identity;
2332
}
33+
2434
override public string name { get { return "Construct Matrix"; } }
2535

2636
override protected VFXExpression[] BuildExpression(VFXExpression[] inputExpression)
2737
{
28-
VFXExpression c0 = inputExpression[0];
29-
VFXExpression c1 = inputExpression[1];
30-
VFXExpression c2 = inputExpression[2];
31-
VFXExpression c3 = inputExpression[3];
38+
VFXExpression m0 = inputExpression[0];
39+
VFXExpression m1 = inputExpression[1];
40+
VFXExpression m2 = inputExpression[2];
41+
VFXExpression m3 = inputExpression[3];
3242

33-
VFXExpression matrix = new VFXExpressionVector4sToMatrix(c0, c1, c2, c3);
34-
return new[] { matrix };
43+
if (order == Order.Row)
44+
return new[] { new VFXExpressionRowToMatrix(m0, m1, m2, m3) };
45+
else
46+
return new[] { new VFXExpressionColumnToMatrix(m0, m1, m2, m3) };
3547
}
3648
}
3749
}

Packages/com.unity.visualeffectgraph/Editor/Models/Operators/Implementations/LookAt.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected override VFXExpression[] BuildExpression(VFXExpression[] inputExpressi
3636
VFXExpression x = VFXOperatorUtility.Normalize(VFXOperatorUtility.Cross(up, z));
3737
VFXExpression y = VFXOperatorUtility.Cross(z, x);
3838

39-
VFXExpression matrix = new VFXExpressionVector3sToMatrix(x, y, z, from);
39+
VFXExpression matrix = new VFXExpressionAxisToMatrix(x, y, z, from);
4040
return new[] { matrix };
4141
}
4242

Packages/com.unity.visualeffectgraph/Editor/Models/Operators/Implementations/SampleMesh.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ private static VFXExpression ComputeVertexAttribute(IEnumerable<VFXExpression> s
586586

587587
//insure tangent orthonormal with normal (cross of normalized input, not need to renormalize)
588588
tangent = VFXOperatorUtility.Cross(bitangent, normal);
589-
sampled = new VFXExpressionVector3sToMatrix(bitangent, normal, tangent, position);
589+
sampled = new VFXExpressionAxisToMatrix(bitangent, normal, tangent, position);
590590
}
591591
else if (currentAttribute == VertexAttributeFlag.Velocity)
592592
{

0 commit comments

Comments
 (0)