11using System ;
22using System . Linq ;
33using System . Collections . Generic ;
4+ using System . Text ;
45using UnityEngine ;
56
67namespace UnityEditor . VFX . Block
@@ -45,7 +46,7 @@ public override IEnumerable<VFXNamedExpression> GetParameters(PositionShape posi
4546
4647 var extents = new VFXNamedExpression ( new VFXExpressionExtractScaleFromMatrix ( transform ) , "extents" ) ;
4748 yield return new VFXNamedExpression ( VFX . VFXOperatorUtility . Max3 ( extents . exp ) , "scalingFactor" ) ;
48- yield return new VFXNamedExpression ( new VFXExpressionInverseTRSMatrix ( transform ) , "InvFieldTransform " ) ;
49+ yield return new VFXNamedExpression ( new VFXExpressionInverseTRSMatrix ( transform ) , "inverseTransform " ) ;
4950 var minDim = new VFXExpressionCastUintToFloat ( VFX . VFXOperatorUtility . Min3 ( new VFXExpressionTextureHeight ( sdf ) , new VFXExpressionTextureWidth ( sdf ) , new VFXExpressionTextureDepth ( sdf ) ) ) ;
5051 var gradStep = VFXValue . Constant ( 0.01f ) ; //kStep used in SampleSDFDerivativesFast and SampleSDFDerivatives
5152 var margin = VFXValue . Constant ( 0.5f ) / minDim + gradStep + VFXValue . Constant ( 0.001f ) ;
@@ -55,32 +56,33 @@ public override IEnumerable<VFXNamedExpression> GetParameters(PositionShape posi
5556
5657 public override string GetSource ( PositionShape positionBase )
5758 {
58- string outSource = @"float cosPhi = 2.0f * RAND - 1.0f;" ;
59+ var outSource = new StringBuilder ( @"float cosPhi = 2.0f * RAND - 1.0f;" ) ;
5960 if ( positionBase . spawnMode == PositionBase . SpawnMode . Random )
60- outSource += @"float theta = TWO_PI * RAND;" ;
61+ outSource . AppendLine ( @"float theta = TWO_PI * RAND;" ) ;
6162 else
62- outSource += @"float theta = TWO_PI * ArcSequencer;" ;
63+ outSource . AppendLine ( @"float theta = TWO_PI * ArcSequencer;" ) ;
6364 switch ( positionBase . positionMode )
6465 {
6566 case ( PositionBase . PositionMode . Surface ) :
66- outSource += @" float Thickness = 0.0f;" ;
67+ outSource . AppendLine ( @" float Thickness = 0.0f;" ) ;
6768 break ;
6869 case ( PositionBase . PositionMode . Volume ) :
69- outSource += @" float Thickness = scalingFactor;" ;
70+ outSource . AppendLine ( @" float Thickness = scalingFactor;" ) ;
7071 break ;
7172 case ( PositionBase . PositionMode . ThicknessRelative ) :
72- outSource += @" Thickness *= scalingFactor * 0.5f;" ;
73+ outSource . AppendLine ( @" Thickness *= scalingFactor * 0.5f;" ) ;
7374 break ;
7475 }
75- outSource += @"
76+
77+ outSource . Append ( @"
7678//Initialize position within texture bounds
7779float2 sincosTheta;
7880sincos(theta, sincosTheta.x, sincosTheta.y);
7981sincosTheta *= sqrt(1.0f - cosPhi * cosPhi);
80- direction = float3(sincosTheta, cosPhi) * sqrt(3)/2;
81- float maxDir = max(0.5f, max(abs(direction .x), max(abs(direction .y), abs(direction .z))));
82- direction = direction * (projectionRayWithMargin/maxDir);
83- float3 tPos = direction * pow(RAND, 1.0f/3.0f);
82+ float3 currentAxisY = float3(sincosTheta, cosPhi) * sqrt(3)/2;
83+ float maxDir = max(0.5f, max(abs(currentAxisY .x), max(abs(currentAxisY .y), abs(currentAxisY .z))));
84+ currentAxisY = currentAxisY * (projectionRayWithMargin/maxDir);
85+ float3 tPos = currentAxisY * pow(RAND, 1.0f/3.0f);
8486float3 coord = tPos + 0.5f;
8587float3 wPos, n, worldNormal;
8688
@@ -92,33 +94,63 @@ public override string GetSource(PositionShape positionBase)
9294
9395 //Projection on surface/volume
9496 float3 delta;
95- worldNormal = VFXSafeNormalize(mul(float4(n, 0), InvFieldTransform ).xyz);
97+ worldNormal = VFXSafeNormalize(mul(float4(n, 0), inverseTransform ).xyz);
9698 if (dist > 0)
9799 delta = dist * worldNormal;
98100 else
99101 {
100102 delta = min(dist + Thickness, 0) * worldNormal;
101103 }
102104
103- wPos = mul(FieldTransform, float4(tPos,1)).xyz + delta;
104- tPos = mul(InvFieldTransform , float4(wPos,1)).xyz;
105+ wPos = mul(FieldTransform, float4(tPos,1)).xyz + delta;
106+ tPos = mul(inverseTransform , float4(wPos, 1)).xyz;
105107 coord = tPos + 0.5f;
106108
109+ }" ) ;
110+ outSource . AppendFormat ( positionBase . composePositionFormatString , "wPos" ) ;
111+
112+ if ( positionBase . applyOrientation != PositionBase . Orientation . None )
113+ outSource . Append ( @"currentAxisY = -worldNormal;" ) ;
114+
115+ if ( positionBase . applyOrientation . HasFlag ( PositionBase . Orientation . Axes ) )
116+ outSource . Append ( @"
117+ float3 AxisZCandidateA = float3(-sincosTheta.y, sincosTheta.x, 0);
118+ float3 AxisZCandidateB = float3(sign(cosPhi), 0, 0);
119+ AxisZCandidateA = mul(float4(AxisZCandidateA, 0), inverseTransform).xyz;
120+ AxisZCandidateB = mul(float4(AxisZCandidateB, 0), inverseTransform).xyz;
121+
122+ float3 currentAxisZ = AxisZCandidateA;
123+ float3 currentAxisX = cross(currentAxisZ, currentAxisY);
124+ if (dot(currentAxisX, currentAxisX) < 0.00001f)
125+ {
126+ currentAxisZ = AxisZCandidateB;
127+ currentAxisX = cross(currentAxisZ, currentAxisY);
107128}
108- position = wPos;
109- direction = -worldNormal;
110- " ;
111- if ( positionBase . killOutliers )
129+
130+ currentAxisX = normalize(currentAxisX);
131+ currentAxisZ = normalize(cross(currentAxisX, currentAxisY));" ) ;
132+
133+ if ( positionBase . applyOrientation . HasFlag ( PositionBase . Orientation . Direction ) )
112134 {
113- outSource += @"
135+ outSource . AppendFormat ( positionBase . composeDirectionFormatString , "currentAxisY" ) ;
136+ }
114137
138+ if ( positionBase . applyOrientation . HasFlag ( PositionBase . Orientation . Axes ) )
139+ {
140+ outSource . AppendFormat ( VFXBlockUtility . GetComposeString ( positionBase . compositionAxes , "axisX" , "currentAxisX" , "blendAxes" ) ) ;
141+ outSource . AppendFormat ( VFXBlockUtility . GetComposeString ( positionBase . compositionAxes , "axisY" , "currentAxisY" , "blendAxes" ) ) ;
142+ outSource . AppendFormat ( VFXBlockUtility . GetComposeString ( positionBase . compositionAxes , "axisZ" , "currentAxisZ" , "blendAxes" ) ) ;
143+ }
144+
145+ if ( positionBase . killOutliers )
146+ {
147+ outSource . Append ( @"
115148 float dist = SampleSDF(SDF, coord);
116149 if (dist * scalingFactor > 0.01)
117- alive = false;
118- " ;
150+ alive = false;" ) ;
119151 }
120152
121- return outSource ;
153+ return outSource . ToString ( ) ;
122154 }
123155 }
124156}
0 commit comments