public void ProcessOperatorCross() { var a = new Vector3(1.1f, 2.2f, 3.3f); var b = new Vector3(4.4f, 5.5f, 6.6f); var value_a = new VFXValue <Vector3>(a); var value_b = new VFXValue <Vector3>(b); var expressionA = VFXOperatorUtility.Cross(value_a, value_b); var context = new VFXExpression.Context(VFXExpressionContextOption.CPUEvaluation); var resultExpressionA = context.Compile(expressionA); var resultValue = resultExpressionA.Get <Vector3>(); var expectedValue = Vector3.Cross(a, b); Assert.AreEqual(expectedValue.x, resultValue.x, 0.001f); Assert.AreEqual(expectedValue.y, resultValue.y, 0.001f); Assert.AreEqual(expectedValue.z, resultValue.z, 0.001f); }
public void ProcessExpressionMinMax() { var a = -1.5f; var b = 0.2f; var resultA = Mathf.Min(a, b); var resultB = Mathf.Max(a, b); var value_a = new VFXValue <float>(a); var value_b = new VFXValue <float>(b); var expressionA = new VFXExpressionMin(value_a, value_b); var expressionB = new VFXExpressionMax(value_a, value_b); var context = new VFXExpression.Context(VFXExpressionContextOption.CPUEvaluation); var resultExpressionA = context.Compile(expressionA); var resultExpressionB = context.Compile(expressionB); Assert.AreEqual(resultA, resultExpressionA.Get <float>()); Assert.AreEqual(resultB, resultExpressionB.Get <float>()); Assert.Greater(resultExpressionB.Get <float>(), resultExpressionA.Get <float>()); }
public void ProcessExpressionBranch() { var branch0 = VFXValue.Constant(Vector3.right); var branch1 = VFXValue.Constant(Vector3.up); var test0 = new VFXExpressionBranch(VFXValue.Constant(true), branch0, branch1); var test1 = new VFXExpressionBranch(VFXValue.Constant(false), branch0, branch1); var context = new VFXExpression.Context(VFXExpressionContextOption.CPUEvaluation); var resultA = context.Compile(test0); var resultB = context.Compile(test1); Assert.AreEqual(Vector3.right, resultA.Get <Vector3>()); Assert.AreEqual(Vector3.up, resultB.Get <Vector3>()); // Test static branching context = new VFXExpression.Context(VFXExpressionContextOption.Reduction); var resultC = context.Compile(test0); Assert.AreEqual(branch0, resultC); }
public void CheckExpectedSequence_ApplyAddressingMode([ValueSource("ApplyAddressingModeTestCase_ValueSource")] ApplyAddressingModeTestCase addressingMode) { var computedSequence = new uint[addressingMode.expectedSequence.Length]; for (uint index = 0u; index < computedSequence.Length; ++index) { var indexExpr = VFXValue.Constant(index); var countExpr = VFXValue.Constant(addressingMode.count); var computed = VFXOperatorUtility.ApplyAddressingMode(indexExpr, countExpr, addressingMode.mode); var context = new VFXExpression.Context(VFXExpressionContextOption.CPUEvaluation); var result = context.Compile(computed); computedSequence[index] = result.Get <uint>(); } for (uint index = 0u; index < computedSequence.Length; ++index) { Assert.AreEqual(addressingMode.expectedSequence[index], computedSequence[index]); } }
public void ProcessExpressionInverseMatrix() { var t = new Vector3(0.2f, 0.3f, 0.4f); var r = new Vector3(0.5f, 0.6f, 0.7f); var s = new Vector3(0.8f, 0.9f, 1.0f); var q = Quaternion.Euler(r); Matrix4x4 inputMatrix = new Matrix4x4(); inputMatrix.SetTRS(t, q, s); Matrix4x4 outputMatrix = inputMatrix.inverse; var matrixExpression = VFXValue.Constant(inputMatrix); var invMatrixExpressions = new VFXExpressionInverseMatrix(matrixExpression); var context = new VFXExpression.Context(VFXExpressionContextOption.CPUEvaluation); var resultExpression = context.Compile(invMatrixExpressions); Assert.AreEqual(resultExpression.Get <Matrix4x4>(), outputMatrix); }
protected override sealed VFXExpression[] BuildExpression(VFXExpression[] inputExpression) { var mesh = inputExpression[0]; var meshVertexStride = new VFXExpressionMeshVertexStride(mesh); var meshVertexCount = new VFXExpressionMeshVertexCount(mesh); var vertexIndex = VFXOperatorUtility.ApplyAddressingMode(inputExpression[1], meshVertexCount, mode); var outputExpressions = new List <VFXExpression>(); foreach (var vertexAttribute in GetOutputVertexAttributes()) { var meshChannelOffset = new VFXExpressionMeshChannelOffset(mesh, VFXValue.Constant <uint>((uint)GetActualVertexAttribute(vertexAttribute))); var outputType = GetOutputType(vertexAttribute); VFXExpression sampled = null; if (vertexAttribute == VertexAttributeFlag.Color) { sampled = new VFXExpressionSampleMeshColor(mesh, vertexIndex, meshChannelOffset, meshVertexStride); } else if (outputType == typeof(float)) { sampled = new VFXExpressionSampleMeshFloat(mesh, vertexIndex, meshChannelOffset, meshVertexStride); } else if (outputType == typeof(Vector2)) { sampled = new VFXExpressionSampleMeshFloat2(mesh, vertexIndex, meshChannelOffset, meshVertexStride); } else if (outputType == typeof(Vector3)) { sampled = new VFXExpressionSampleMeshFloat3(mesh, vertexIndex, meshChannelOffset, meshVertexStride); } else { sampled = new VFXExpressionSampleMeshFloat4(mesh, vertexIndex, meshChannelOffset, meshVertexStride); } outputExpressions.Add(sampled); } return(outputExpressions.ToArray()); }
protected override IEnumerable <VFXNamedExpression> CollectGPUExpressions(IEnumerable <VFXNamedExpression> slotExpressions) { foreach (var exp in base.CollectGPUExpressions(slotExpressions)) { yield return(exp); } if (GetOrRefreshShaderGraphObject() == null) { yield return(slotExpressions.First(o => o.name == "smoothness")); switch (materialType) { case MaterialType.Standard: case MaterialType.SimpleLit: yield return(slotExpressions.First(o => o.name == "metallic")); break; case MaterialType.SpecularColor: yield return(slotExpressions.First(o => o.name == "specularColor")); break; case MaterialType.Translucent: case MaterialType.SimpleLitTranslucent: { yield return(slotExpressions.First(o => o.name == "thickness")); uint diffusionProfileHash = (diffusionProfileAsset?.profile != null) ? diffusionProfileAsset.profile.hash : 0; yield return(new VFXNamedExpression(VFXValue.Constant(diffusionProfileHash), "diffusionProfileHash")); break; } default: break; } } }
public void ProcessExpressionTrig() { var a = 1.5f; var b = 2.1f; var resultA = Mathf.Cos(a); var resultB = Mathf.Sin(a); var resultC = Mathf.Tan(a); var resultD = Mathf.Acos(a); var resultE = Mathf.Asin(a); var resultF = Mathf.Atan(a); var resultG = Mathf.Atan2(a, b); var value_a = new VFXValue <float>(a); var value_b = new VFXValue <float>(b); var cosExpression = new VFXExpressionCos(value_a); var sinExpression = new VFXExpressionSin(value_a); var tanExpression = new VFXExpressionTan(value_a); var acosExpression = new VFXExpressionACos(value_a); var asinExpression = new VFXExpressionASin(value_a); var atanExpression = new VFXExpressionATan(value_a); var atan2Expression = new VFXExpressionATan2(value_a, value_b); var context = new VFXExpression.Context(VFXExpressionContextOption.CPUEvaluation); var expressionA = context.Compile(cosExpression); var expressionB = context.Compile(sinExpression); var expressionC = context.Compile(tanExpression); var expressionD = context.Compile(acosExpression); var expressionE = context.Compile(asinExpression); var expressionF = context.Compile(atanExpression); var expressionG = context.Compile(atan2Expression); Assert.AreEqual(resultA, expressionA.Get <float>()); Assert.AreEqual(resultB, expressionB.Get <float>()); Assert.AreEqual(resultC, expressionC.Get <float>()); Assert.AreEqual(resultD, expressionD.Get <float>()); Assert.AreEqual(resultE, expressionE.Get <float>()); Assert.AreEqual(resultF, expressionF.Get <float>()); Assert.AreEqual(resultG, expressionG.Get <float>()); }
public void ProcessOperatorPolarToRectangular() { var theta = 0.5f; var distance = 0.2f; var rectangular = new Vector2(Mathf.Cos(theta), Mathf.Sin(theta)) * distance; var value_theta = new VFXValue <float>(theta); var value_distance = new VFXValue <float>(distance); var expressionA = VFXOperatorUtility.PolarToRectangular(value_theta, value_distance); var expressionB = VFXOperatorUtility.RectangularToPolar(expressionA); var context = new VFXExpression.Context(VFXExpressionContextOption.CPUEvaluation); var resultExpressionA = context.Compile(expressionA); var resultExpressionB0 = context.Compile(expressionB[0]); var resultExpressionB1 = context.Compile(expressionB[1]); Assert.AreEqual(rectangular, resultExpressionA.Get <Vector2>()); Assert.AreEqual(theta, resultExpressionB0.Get <float>()); Assert.AreEqual(distance, resultExpressionB1.Get <float>()); }
private static VFXExpression GetParticleMass( InitializeSolver initializeBlock, VFXExpression h, VFXExpression mass, VFXExpression density) { if (initializeBlock?.AutomaticMass ?? true) { var hCubed = h * h * h; var hParticleFactor = VFXValue.Constant(FluvioFXSettings.kAutoParticleSizeFactor); return (VFXValue.Constant(4.0f / 3.0f) * VFXValue.Constant(Mathf.PI) * hCubed * hParticleFactor * density); } else { return(mass); } }
public static IEnumerable <VFXNamedExpression> GetParameters( CollisionBase block, IEnumerable <VFXNamedExpression> baseParameters) { var expressions = baseParameters; expressions = expressions.Concat(InitializeSolver.GetExpressions( block, SolverDataParameters.Fluid_Density | SolverDataParameters.Fluid_MinimumDensity | SolverDataParameters.Fluid_GasConstant | SolverDataParameters.KernelSize | SolverDataParameters.KernelFactors )); foreach (var expression in expressions) { yield return(expression); } ; yield return(new VFXNamedExpression(VFXValue.Constant(1.0f) / VFXBuiltInExpression.DeltaTime, "invDt")); }
protected override sealed VFXExpression[] BuildExpression(VFXExpression[] inputExpression) { VFXExpression[] expressions = new VFXExpression[asset.surfaces.Length + 1]; expressions[0] = VFXValue.Constant((uint)asset.PointCount); for (int i = 0; i < asset.surfaces.Length; i++) { var surfaceExpr = VFXValue.Constant(asset.surfaces[i]); VFXExpression height = new VFXExpressionTextureHeight(surfaceExpr); VFXExpression width = new VFXExpressionTextureWidth(surfaceExpr); VFXExpression u_index = VFXOperatorUtility.ApplyAddressingMode(inputExpression[0], new VFXExpressionMin(height * width, expressions[0]), mode); VFXExpression y = u_index / width; VFXExpression x = u_index - (y * width); Type outputType = GetOutputType(asset.surfaces[i]); var type = typeof(VFXExpressionSampleAttributeMap <>).MakeGenericType(outputType); var outputExpr = Activator.CreateInstance(type, new object[] { surfaceExpr, x, y }); expressions[i + 1] = (VFXExpression)outputExpr; } return(expressions); }
protected override sealed VFXExpression[] BuildExpression(VFXExpression[] inputExpression) { VFXExpression parameters = new VFXExpressionCombine(inputExpression[1], inputExpression[2], inputExpression[4]); if (dimensions == DimensionCount.One) { VFXExpression noise = new VFXExpressionPerlinNoise1D(inputExpression[0], parameters, inputExpression[3]); noise = VFXOperatorUtility.Fit(noise, VFXValue.Constant(new Vector2(-1.0f, -1.0f)), VFXValue.Constant(Vector2.one), VFXOperatorUtility.CastFloat(inputExpression[5].x, noise.valueType), VFXOperatorUtility.CastFloat(inputExpression[5].y, noise.valueType)); return(new[] { noise.x, noise.y }); } else if (dimensions == DimensionCount.Two) { VFXExpression noise = new VFXExpressionPerlinNoise2D(inputExpression[0], parameters, inputExpression[3]); noise = VFXOperatorUtility.Fit(noise, VFXValue.Constant(new Vector3(-1.0f, -1.0f, -1.0f)), VFXValue.Constant(Vector3.one), VFXOperatorUtility.CastFloat(inputExpression[5].x, noise.valueType), VFXOperatorUtility.CastFloat(inputExpression[5].y, noise.valueType)); return(new[] { noise.x, new VFXExpressionCombine(noise.y, noise.z) }); } else { VFXExpression noise = new VFXExpressionPerlinNoise3D(inputExpression[0], parameters, inputExpression[3]); noise = VFXOperatorUtility.Fit(noise, VFXValue.Constant(new Vector4(-1.0f, -1.0f, -1.0f, -1.0f)), VFXValue.Constant(Vector4.one), VFXOperatorUtility.CastFloat(inputExpression[5].x, noise.valueType), VFXOperatorUtility.CastFloat(inputExpression[5].y, noise.valueType)); return(new[] { noise.x, new VFXExpressionCombine(noise.y, noise.z, noise.w) }); } }
public void ProcessExpressionBitwise() { int a = 12345; int b = 2; var resultA = a << b; var resultB = a >> b; var resultC = a | b; var resultD = a & b; var resultE = a ^ b; var resultF = ~a; var value_a = new VFXValue <uint>((uint)a); var value_b = new VFXValue <uint>((uint)b); var expressionA = new VFXExpressionBitwiseLeftShift(value_a, value_b); var expressionB = new VFXExpressionBitwiseRightShift(value_a, value_b); var expressionC = new VFXExpressionBitwiseOr(value_a, value_b); var expressionD = new VFXExpressionBitwiseAnd(value_a, value_b); var expressionE = new VFXExpressionBitwiseXor(value_a, value_b); var expressionF = new VFXExpressionBitwiseComplement(value_a); var context = new VFXExpression.Context(VFXExpressionContextOption.CPUEvaluation); var resultExpressionA = context.Compile(expressionA); var resultExpressionB = context.Compile(expressionB); var resultExpressionC = context.Compile(expressionC); var resultExpressionD = context.Compile(expressionD); var resultExpressionE = context.Compile(expressionE); var resultExpressionF = context.Compile(expressionF); Assert.AreEqual((uint)resultA, resultExpressionA.Get <uint>()); Assert.AreEqual((uint)resultB, resultExpressionB.Get <uint>()); Assert.AreEqual((uint)resultC, resultExpressionC.Get <uint>()); Assert.AreEqual((uint)resultD, resultExpressionD.Get <uint>()); Assert.AreEqual((uint)resultE, resultExpressionE.Get <uint>()); Assert.AreEqual((uint)resultF, resultExpressionF.Get <uint>()); }
public void ProcessOperatorLerp() { var a = new Vector3(0.2f, 0.3f, 0.4f); var b = new Vector3(1.0f, 2.3f, 5.4f); var c = 0.2f; var d = 1.5f; var resultA = Vector3.LerpUnclamped(a, b, c); var resultB = Vector3.LerpUnclamped(a, b, d); var value_a = new VFXValue <Vector3>(a); var value_b = new VFXValue <Vector3>(b); var value_c = new VFXValue <float>(c); var value_d = new VFXValue <float>(d); var expressionA = VFXOperatorUtility.Lerp(value_a, value_b, VFXOperatorUtility.CastFloat(value_c, value_b.valueType)); var expressionB = VFXOperatorUtility.Lerp(value_a, value_b, VFXOperatorUtility.CastFloat(value_d, value_b.valueType)); var context = new VFXExpression.Context(VFXExpressionContextOption.CPUEvaluation); var resultExpressionA = context.Compile(expressionA); var resultExpressionB = context.Compile(expressionB); Assert.AreEqual((resultA - resultExpressionA.Get <Vector3>()).magnitude, 0.0f, 0.001f); Assert.AreEqual((resultB - resultExpressionB.Get <Vector3>()).magnitude, 0.0f, 0.001f); }
protected override sealed VFXExpression[] BuildExpression(VFXExpression[] inputExpression) { var expressions = Block.CameraHelper.AddCameraExpressions(GetExpressionsFromSlots(this), camera); // camera matrix is already in world even in custom mode due to GetOutputSpaceFromSlot returning world space Block.CameraMatricesExpressions matricesExpressions = Block.CameraHelper.GetMatricesExpressions(expressions, VFXCoordinateSpace.World, VFXCoordinateSpace.World); // renormalize XY from viewport position [0, 1] to clip position [-1, 1] VFXExpression viewportPosExpression = inputExpression[0]; VFXExpression oneExpression = VFXValue.Constant(1.0f); VFXExpression twoExpression = VFXValue.Constant(2.0f); VFXExpression clipPosExpression = new VFXExpressionCombine(viewportPosExpression.x * twoExpression - oneExpression, viewportPosExpression.y * twoExpression - oneExpression, oneExpression, oneExpression); // result = clipPos * ClipToView * ViewToVFX VFXExpression viewPosExpression = new VFXExpressionTransformVector4(matricesExpressions.ClipToView.exp, clipPosExpression); viewPosExpression = new VFXExpressionCombine(viewPosExpression.x, viewPosExpression.y, viewPosExpression.z) * viewportPosExpression.zzz; VFXExpression positionExpression = new VFXExpressionTransformPosition(matricesExpressions.ViewToVFX.exp, viewPosExpression); return(new VFXExpression[] { positionExpression }); }
public void ProcessOperatorFit() { var value = 0.4f; var oldRangeMin = 0.2f; var oldRangeMax = 1.2f; var newRangeMin = 3.2f; var newRangeMax = 5.2f; var percent = (value - oldRangeMin) / (oldRangeMax - oldRangeMin); var result = Mathf.LerpUnclamped(newRangeMin, newRangeMax, percent); var value_a = new VFXValue <float>(value); var value_b = new VFXValue <float>(oldRangeMin); var value_c = new VFXValue <float>(oldRangeMax); var value_d = new VFXValue <float>(newRangeMin); var value_e = new VFXValue <float>(newRangeMax); var expression = VFXOperatorUtility.Fit(value_a, value_b, value_c, value_d, value_e); var context = new VFXExpression.Context(VFXExpressionContextOption.CPUEvaluation); var resultExpression = context.Compile(expression); Assert.AreEqual(result, resultExpression.Get <float>()); }
protected override sealed VFXExpression[] BuildExpression(VFXExpression[] inputExpression) { var t = new VFXExpressionSharedRandom(inputExpression[1] * VFXValue.Constant(2u) + VFXValue.Constant(1u)) * VFXValue.Constant(Mathf.PI * 2); var z = new VFXExpressionSharedRandom(inputExpression[1] * VFXValue.Constant(2u)) * VFXValue.Constant(2.0f) - VFXValue.Constant(1.0f); var w = VFXOperatorUtility.Sqrt(VFXValue.Constant(1.0f) - z * z); return(new[] { new VFXExpressionCombine(new VFXExpressionCos(t) * w, new VFXExpressionSin(t) * w, z) * VFXOperatorUtility.CastFloat(inputExpression[0], VFXValueType.Float3) }); }
protected override sealed VFXExpression[] BuildExpression(VFXExpression[] inputExpression) { VFXExpression[] rgb = VFXOperatorUtility.ExtractComponents(new VFXExpressionHSVtoRGB(inputExpression[0])).Take(3).ToArray(); return(new[] { new VFXExpressionCombine(new[] { rgb[0], rgb[1], rgb[2], VFXValue.Constant(1.0f) }) }); }
protected override void CalculateFactor() { // Factor _factor = VFXValue.Constant(15.0f) / (VFXValue.Constant(Mathf.PI) * _kernelSize6); }
protected override sealed VFXExpression[] BuildExpression(VFXExpression[] inputExpression) { VFXExpression parameters = new VFXExpressionCombine(inputExpression[1], inputExpression[3], inputExpression[4]); VFXExpression rangeMultiplier = (inputExpression[5].y - inputExpression[5].x); VFXExpression result; VFXExpression rangeMin = VFXValue.Constant(0.0f); VFXExpression rangeMax = VFXValue.Constant(1.0f); if (dimensions == DimensionCount.One) { if (type == NoiseType.Value) { result = new VFXExpressionValueNoise1D(inputExpression[0], parameters, inputExpression[2]); } else if (type == NoiseType.Perlin) { result = new VFXExpressionPerlinNoise1D(inputExpression[0], parameters, inputExpression[2]); rangeMin = VFXValue.Constant(-1.0f); } else { result = new VFXExpressionCellularNoise1D(inputExpression[0], parameters, inputExpression[2]); } VFXExpression x = VFXOperatorUtility.Fit(result.x, rangeMin, rangeMax, inputExpression[5].x, inputExpression[5].y); VFXExpression y = result.y * rangeMultiplier; return(new[] { x, y }); } else if (dimensions == DimensionCount.Two) { if (type == NoiseType.Value) { result = new VFXExpressionValueNoise2D(inputExpression[0], parameters, inputExpression[2]); } else if (type == NoiseType.Perlin) { result = new VFXExpressionPerlinNoise2D(inputExpression[0], parameters, inputExpression[2]); rangeMin = VFXValue.Constant(-1.0f); } else { result = new VFXExpressionCellularNoise2D(inputExpression[0], parameters, inputExpression[2]); } VFXExpression x = VFXOperatorUtility.Fit(result.x, rangeMin, rangeMax, inputExpression[5].x, inputExpression[5].y); VFXExpression y = result.y * rangeMultiplier; VFXExpression z = result.z * rangeMultiplier; return(new[] { x, new VFXExpressionCombine(y, z) }); } else { if (type == NoiseType.Value) { result = new VFXExpressionValueNoise3D(inputExpression[0], parameters, inputExpression[2]); } else if (type == NoiseType.Perlin) { result = new VFXExpressionPerlinNoise3D(inputExpression[0], parameters, inputExpression[2]); rangeMin = VFXValue.Constant(-1.0f); } else { result = new VFXExpressionCellularNoise3D(inputExpression[0], parameters, inputExpression[2]); } VFXExpression x = VFXOperatorUtility.Fit(result.x, rangeMin, rangeMax, inputExpression[5].x, inputExpression[5].y); VFXExpression y = result.y * rangeMultiplier; VFXExpression z = result.z * rangeMultiplier; VFXExpression w = result.w * rangeMultiplier; return(new[] { x, new VFXExpressionCombine(y, z, w) }); } }
protected override sealed VFXExpression[] BuildExpression(VFXExpression[] inputExpression) { VFXExpression parameters = new VFXExpressionCombine(inputExpression[1], inputExpression[2], inputExpression[4]); if (type == NoiseType.Curl) { if (curlDimensions == CurlDimensionCount.Two) { return(new[] { new VFXExpressionPerlinCurlNoise2D(inputExpression[0], parameters, inputExpression[3]) }); } else { return(new[] { new VFXExpressionPerlinCurlNoise3D(inputExpression[0], parameters, inputExpression[3]) }); } } else { VFXExpression rangeMultiplier = (inputExpression[5].y - inputExpression[5].x); if (dimensions == DimensionCount.One) { VFXExpression noise = new VFXExpressionPerlinNoise1D(inputExpression[0], parameters, inputExpression[3]); VFXExpression x = VFXOperatorUtility.Fit(noise.x, VFXValue.Constant(0.0f), VFXValue.Constant(1.0f), inputExpression[5].x, inputExpression[5].y); VFXExpression y = noise.y * rangeMultiplier; return(new[] { x, y }); } else if (dimensions == DimensionCount.Two) { VFXExpression noise = new VFXExpressionPerlinNoise2D(inputExpression[0], parameters, inputExpression[3]); VFXExpression x = VFXOperatorUtility.Fit(noise.x, VFXValue.Constant(0.0f), VFXValue.Constant(1.0f), inputExpression[5].x, inputExpression[5].y); VFXExpression y = noise.y * rangeMultiplier; VFXExpression z = noise.z * rangeMultiplier; return(new[] { x, new VFXExpressionCombine(y, z) }); } else { VFXExpression noise = new VFXExpressionPerlinNoise3D(inputExpression[0], parameters, inputExpression[3]); VFXExpression x = VFXOperatorUtility.Fit(noise.x, VFXValue.Constant(0.0f), VFXValue.Constant(1.0f), inputExpression[5].x, inputExpression[5].y); VFXExpression y = noise.y * rangeMultiplier; VFXExpression z = noise.z * rangeMultiplier; VFXExpression w = noise.w * rangeMultiplier; return(new[] { x, new VFXExpressionCombine(y, z, w) }); } } }
protected override void CalculateFactor() { _factor = VFXValue.Constant(315.0f) / (VFXValue.Constant(64.0f) * VFXValue.Constant(Mathf.PI) * _kernelSize9); }
protected override sealed VFXExpression[] BuildExpression(VFXExpression[] inputExpression) { // UpdateSolverTexture(true); // KernelSize: x - h, y - h^2, z - h^3, w - simulation scale // KernelFactors: x - poly6, y - spiky, z - viscosity, w - unused VFXExpression h = inputExpression[0]; VFXExpression simulationScale = inputExpression[1]; var poly6 = new Poly6Kernel(h); var spiky = new SpikyKernel(h); var viscosity = new ViscosityKernel(h); return(new VFXExpression[] { new VFXExpressionCombine(new [] { h, h * h, h * h * h, simulationScale }), new VFXExpressionCombine(new [] { poly6.GetFactor(), spiky.GetFactor(), viscosity.GetFactor(), VFXValue.Constant(0.0f) }), new VFXTexture2DValue(FluvioSolverData ? FluvioSolverData : null), new VFXExpressionCombine(new [] { VFXValue.Constant((float)(FluvioSolverData? FluvioSolverData.width : 0)), VFXValue.Constant((float)(FluvioSolverData ? FluvioSolverData.height : 0)) }), }); }
private static IEnumerable <VFXNamedExpression> GetExpressionsImpl( InitializeSolver initializeBlock, SolverDataParameters solverDataParams, IEnumerable <VFXNamedExpression> fluid, VFXExpression h, VFXExpression mass, VFXExpression density, VFXExpression gravity) { // Fluid if (fluid == null || h == null || mass == null || density == null) { var defaultFluid = Fluid.defaultValue; h = VFXValue.Constant(defaultFluid.SmoothingDistance); if (solverDataParams.HasFlag(SolverDataParameters.Fluid_ParticleMass)) { yield return(new VFXNamedExpression( GetParticleMass( initializeBlock, h, VFXValue.Constant(defaultFluid.ParticleMass), VFXValue.Constant(defaultFluid.Density) ), "solverData_Fluid_ParticleMass")); } if (solverDataParams.HasFlag(SolverDataParameters.Fluid_Density)) { yield return(new VFXNamedExpression( VFXValue.Constant(defaultFluid.Density), "solverData_Fluid_Density")); } if (solverDataParams.HasFlag(SolverDataParameters.Fluid_MinimumDensity)) { yield return(new VFXNamedExpression( VFXValue.Constant(defaultFluid.MinimumDensity), "solverData_Fluid_MinimumDensity")); } if (solverDataParams.HasFlag(SolverDataParameters.Fluid_GasConstant)) { yield return(new VFXNamedExpression( VFXValue.Constant(defaultFluid.GasConstant), "solverData_Fluid_GasConstant")); } if (solverDataParams.HasFlag(SolverDataParameters.Fluid_Viscosity)) { yield return(new VFXNamedExpression( VFXValue.Constant(defaultFluid.Viscosity), "solverData_Fluid_Viscosity")); } if (solverDataParams.HasFlag(SolverDataParameters.Fluid_SurfaceTension)) { yield return(new VFXNamedExpression( VFXValue.Constant(defaultFluid.SurfaceTension), "solverData_Fluid_SurfaceTension")); } if (solverDataParams.HasFlag(SolverDataParameters.Fluid_BuoyancyCoefficient)) { yield return(new VFXNamedExpression( VFXValue.Constant(defaultFluid.BuoyancyCoefficient), "solverData_Fluid_BuoyancyCoefficient")); } } else { foreach (var expression in fluid) { if (Enum.TryParse(expression.name, out SolverDataParameters solverDataParameter) && solverDataParams.HasFlag(solverDataParameter)) { if (solverDataParameter == SolverDataParameters.Fluid_ParticleMass) { yield return(new VFXNamedExpression( GetParticleMass(initializeBlock, h, mass, density), "solverData_Fluid_ParticleMass")); continue; } yield return(new VFXNamedExpression(expression.exp, $"solverData_{expression.name}")); } } ; } // KernelSize: x - h, y - h^2, z - h^3, w - h / 2 if (solverDataParams.HasFlag(SolverDataParameters.KernelSize)) { yield return(new VFXNamedExpression(new VFXExpressionCombine(new [] { h, h * h, h * h * h, h * VFXValue.Constant(0.5f), }), "solverData_KernelSize")); } // KernelFactors: x - poly6, y - spiky, z - viscosity if (solverDataParams.HasFlag(SolverDataParameters.KernelFactors)) { var poly6 = new Poly6Kernel(h); var spiky = new SpikyKernel(h); var viscosity = new ViscosityKernel(h); yield return(new VFXNamedExpression(new VFXExpressionCombine(new [] { poly6.GetFactor(), spiky.GetFactor(), viscosity.GetFactor() }), "solverData_KernelFactors")); } // Gravity if (solverDataParams.HasFlag(SolverDataParameters.Gravity)) { if (gravity == null) { gravity = new VFXExpressionCombine(new [] { VFXValue.Constant(0.0f), VFXValue.Constant(-9.81f), VFXValue.Constant(0.0f), }); } yield return(new VFXNamedExpression(gravity, "solverData_Gravity")); } }
public static IEnumerable <VFXExpression> SampleVertexAttribute(VFXExpression source, VFXExpression vertexIndex, IEnumerable <VertexAttribute> vertexAttributes) { bool skinnedMesh = source.valueType == UnityEngine.VFX.VFXValueType.SkinnedMeshRenderer; var mesh = !skinnedMesh ? source : new VFXExpressionMeshFromSkinnedMeshRenderer(source); foreach (var vertexAttribute in vertexAttributes) { var channelIndex = VFXValue.Constant <uint>((uint)vertexAttribute); var meshVertexStride = new VFXExpressionMeshVertexStride(mesh, channelIndex); var meshChannelOffset = new VFXExpressionMeshChannelOffset(mesh, channelIndex); var outputType = GetOutputType(vertexAttribute); VFXExpression sampled = null; var meshChannelFormatAndDimension = new VFXExpressionMeshChannelInfos(mesh, channelIndex); var vertexOffset = vertexIndex * meshVertexStride + meshChannelOffset; if (!skinnedMesh) { if (vertexAttribute == VertexAttribute.Color) { sampled = new VFXExpressionSampleMeshColor(source, vertexOffset, meshChannelFormatAndDimension); } else if (outputType == typeof(float)) { sampled = new VFXExpressionSampleMeshFloat(source, vertexOffset, meshChannelFormatAndDimension); } else if (outputType == typeof(Vector2)) { sampled = new VFXExpressionSampleMeshFloat2(source, vertexOffset, meshChannelFormatAndDimension); } else if (outputType == typeof(Vector3)) { sampled = new VFXExpressionSampleMeshFloat3(source, vertexOffset, meshChannelFormatAndDimension); } else { sampled = new VFXExpressionSampleMeshFloat4(source, vertexOffset, meshChannelFormatAndDimension); } } else { if (vertexAttribute == VertexAttribute.Color) { sampled = new VFXExpressionSampleSkinnedMeshRendererColor(source, vertexOffset, meshChannelFormatAndDimension); } else if (outputType == typeof(float)) { sampled = new VFXExpressionSampleSkinnedMeshRendererFloat(source, vertexOffset, meshChannelFormatAndDimension); } else if (outputType == typeof(Vector2)) { sampled = new VFXExpressionSampleSkinnedMeshRendererFloat2(source, vertexOffset, meshChannelFormatAndDimension); } else if (outputType == typeof(Vector3)) { sampled = new VFXExpressionSampleSkinnedMeshRendererFloat3(source, vertexOffset, meshChannelFormatAndDimension); } else { sampled = new VFXExpressionSampleSkinnedMeshRendererFloat4(source, vertexOffset, meshChannelFormatAndDimension); } } yield return(sampled); } }
public void ProcessOperatorSignUInt() { var value_a = new VFXValue <uint>(0u); Assert.Throws <NotImplementedException>(() => new VFXExpressionSign(value_a)); }
protected override VFXExpression[] BuildExpression(VFXExpression[] inputExpression) { // Offset to compensate for the numerous custom camera generated expressions _customCameraOffset = 0; // Get the extra number of expressions if a custom camera input is used if (camera == CameraMode.Custom) { _customCameraOffset = GetInputSlot(0).children.Count() - 1; } // List to gather all output expressions as their number can vary List <VFXExpression> outputs = new List <VFXExpression>(); // Camera expressions var expressions = Block.CameraHelper.AddCameraExpressions(GetExpressionsFromSlots(this), camera); Block.CameraMatricesExpressions camMatrices = Block.CameraHelper.GetMatricesExpressions(expressions); var Camera_depthBuffer = expressions.First(e => e.name == "Camera_depthBuffer").exp; var CamPixDim = expressions.First(e => e.name == "Camera_pixelDimensions").exp; // Set uvs VFXExpression uv = VFXValue.Constant <Vector2>(); // Determine how the particles are spawned on the screen switch (mode) { case PositionMode.Random: // Random UVs uv = new VFXExpressionCombine(VFXOperatorUtility.FixedRandom(0, VFXSeedMode.PerParticle), VFXOperatorUtility.FixedRandom(1, VFXSeedMode.PerParticle)); break; case PositionMode.Sequential: // Pixel perfect spawn VFXExpression gridStep = inputExpression[inputSlots.IndexOf(inputSlots.First(o => o.name == "GridStep")) + _customCameraOffset]; VFXExpression sSizeX = new VFXExpressionCastFloatToUint(CamPixDim.x / new VFXExpressionCastUintToFloat(gridStep)); VFXExpression sSizeY = new VFXExpressionCastFloatToUint(CamPixDim.y / new VFXExpressionCastUintToFloat(gridStep)); VFXExpression nbPixels = sSizeX * sSizeY; VFXExpression particleID = new VFXAttributeExpression(VFXAttribute.ParticleId); VFXExpression id = VFXOperatorUtility.Modulo(particleID, nbPixels); VFXExpression shift = new VFXExpressionBitwiseRightShift(gridStep, VFXValue.Constant <uint>(1)); VFXExpression U = VFXOperatorUtility.Modulo(id, sSizeX) * gridStep + shift; VFXExpression V = id / sSizeX * gridStep + shift; VFXExpression ids = new VFXExpressionCombine(new VFXExpressionCastUintToFloat(U), new VFXExpressionCastUintToFloat(V)); uv = new VFXExpressionDivide(ids + VFXOperatorUtility.CastFloat(VFXValue.Constant(0.5f), VFXValueType.Float2), CamPixDim); break; case PositionMode.Custom: // Custom UVs uv = inputExpression[inputSlots.IndexOf(inputSlots.FirstOrDefault(o => o.name == "UVSpawn")) + _customCameraOffset]; break; } VFXExpression projpos = uv * VFXValue.Constant <Vector2>(new Vector2(2f, 2f)) - VFXValue.Constant <Vector2>(Vector2.one); VFXExpression uvs = new VFXExpressionCombine(uv.x * CamPixDim.x, uv.y * CamPixDim.y, VFXValue.Constant(0f), VFXValue.Constant(0f)); // Get depth VFXExpression depth = new VFXExpressionExtractComponent(new VFXExpressionLoadTexture2DArray(Camera_depthBuffer, uvs), 0); if (SystemInfo.usesReversedZBuffer) { depth = VFXOperatorUtility.OneExpression[depth.valueType] - depth; } VFXExpression isAlive = VFXValue.Constant(true); // Determine how the particles are culled switch (cullMode) { case CullMode.None: // do nothing break; case CullMode.Range: VFXExpression depthRange = inputExpression[inputSlots.IndexOf(inputSlots.LastOrDefault(o => o.name == "DepthRange")) + _customCameraOffset]; VFXExpression nearRangeCheck = new VFXExpressionCondition(VFXCondition.Less, depth, depthRange.x); VFXExpression farRangeCheck = new VFXExpressionCondition(VFXCondition.Greater, depth, depthRange.y); VFXExpression logicOr = new VFXExpressionLogicalOr(nearRangeCheck, farRangeCheck); isAlive = new VFXExpressionBranch(logicOr, VFXValue.Constant(false), VFXValue.Constant(true)); break; case CullMode.FarPlane: VFXExpression farPlaneCheck = new VFXExpressionCondition(VFXCondition.GreaterOrEqual, depth, VFXValue.Constant(1f) - VFXValue.Constant(Mathf.Epsilon)); isAlive = new VFXExpressionBranch(farPlaneCheck, VFXValue.Constant(false), VFXValue.Constant(true)); break; } VFXExpression zMultiplier = inputExpression[inputSlots.IndexOf(inputSlots.First(o => o.name == "ZMultiplier")) + _customCameraOffset]; VFXExpression clipPos = new VFXExpressionCombine(projpos.x, projpos.y, depth * zMultiplier * VFXValue.Constant(2f) - VFXValue.Constant(1f), VFXValue.Constant(1f) ); VFXExpression clipToVFX = new VFXExpressionTransformMatrix(camMatrices.ViewToVFX.exp, camMatrices.ClipToView.exp); VFXExpression vfxPos = new VFXExpressionTransformVector4(clipToVFX, clipPos); VFXExpression position = new VFXExpressionCombine(vfxPos.x, vfxPos.y, vfxPos.z) / VFXOperatorUtility.CastFloat(vfxPos.w, VFXValueType.Float3); VFXExpression color = VFXValue.Constant <Vector4>(); // Assigning the color output to the corresponding color buffer value if (inheritSceneColor) { VFXExpression Camera_colorBuffer = expressions.First(e => e.name == "Camera_colorBuffer").exp; VFXExpression tempColor = new VFXExpressionLoadTexture2DArray(Camera_colorBuffer, uvs); color = new VFXExpressionCombine(tempColor.x, tempColor.y, tempColor.z, VFXValue.Constant(1.0f)); } // Add expressions in the right output order outputs.Add(position); if (inheritSceneColor) { outputs.Add(color); } if (cullMode != CullMode.None) { outputs.Add(isAlive); } return(outputs.ToArray()); }
protected override sealed VFXExpression[] BuildExpression(VFXExpression[] inputExpression) { VFXExpression lineDelta = (inputExpression[1] - inputExpression[0]); VFXExpression lineLength = new VFXExpressionMax(VFXOperatorUtility.Dot(lineDelta, lineDelta), VFXValue.Constant(Mathf.Epsilon)); VFXExpression t = VFXOperatorUtility.Dot(inputExpression[2] - inputExpression[0], lineDelta); t = VFXOperatorUtility.Clamp(t / lineLength, VFXValue.Constant(0.0f), VFXValue.Constant(1.0f)); VFXExpression pointOnLine = (inputExpression[0] + VFXOperatorUtility.CastFloat(t, lineDelta.valueType) * lineDelta); VFXExpression lineDistance = VFXOperatorUtility.Distance(inputExpression[2], pointOnLine); return(new VFXExpression[] { pointOnLine, lineDistance }); }
public static IEnumerable <VFXExpression> SampleTriangleAttribute(VFXExpression source, VFXExpression triangleIndex, VFXExpression coord, SurfaceCoordinates coordMode, IEnumerable <VertexAttribute> vertexAttributes) { bool skinnedMesh = source.valueType == UnityEngine.VFX.VFXValueType.SkinnedMeshRenderer; var mesh = !skinnedMesh ? source : new VFXExpressionMeshFromSkinnedMeshRenderer(source); var meshIndexCount = new VFXExpressionMeshIndexCount(mesh); var meshIndexFormat = new VFXExpressionMeshIndexFormat(mesh); var threeUint = VFXOperatorUtility.ThreeExpression[UnityEngine.VFX.VFXValueType.Uint32]; var baseIndex = triangleIndex * threeUint; var sampledIndex_A = new VFXExpressionSampleIndex(mesh, baseIndex, meshIndexFormat); var sampledIndex_B = new VFXExpressionSampleIndex(mesh, baseIndex + VFXValue.Constant <uint>(1u), meshIndexFormat); var sampledIndex_C = new VFXExpressionSampleIndex(mesh, baseIndex + VFXValue.Constant <uint>(2u), meshIndexFormat); var allInputValues = new List <VFXExpression>(); var sampling_A = SampleVertexAttribute(source, sampledIndex_A, vertexAttributes).ToArray(); var sampling_B = SampleVertexAttribute(source, sampledIndex_B, vertexAttributes).ToArray(); var sampling_C = SampleVertexAttribute(source, sampledIndex_C, vertexAttributes).ToArray(); VFXExpression barycentricCoordinates = null; var one = VFXOperatorUtility.OneExpression[UnityEngine.VFX.VFXValueType.Float]; if (coordMode == SurfaceCoordinates.Barycentric) { var barycentricCoordinateInput = coord; barycentricCoordinates = new VFXExpressionCombine(barycentricCoordinateInput.x, barycentricCoordinateInput.y, one - barycentricCoordinateInput.x - barycentricCoordinateInput.y); } else if (coordMode == SurfaceCoordinates.Uniform) { //https://hal.archives-ouvertes.fr/hal-02073696v2/document var input = coord; var half2 = VFXOperatorUtility.HalfExpression[UnityEngine.VFX.VFXValueType.Float2]; var zero = VFXOperatorUtility.ZeroExpression[UnityEngine.VFX.VFXValueType.Float]; var t = input * half2; var offset = t.y - t.x; var pred = new VFXExpressionCondition(UnityEngine.VFX.VFXValueType.Float, VFXCondition.Greater, offset, zero); var t2 = new VFXExpressionBranch(pred, t.y + offset, t.y); var t1 = new VFXExpressionBranch(pred, t.x, t.x - offset); var t3 = one - t2 - t1; barycentricCoordinates = new VFXExpressionCombine(t1, t2, t3); /* Possible variant See http://inis.jinr.ru/sl/vol1/CMC/Graphics_Gems_1,ed_A.Glassner.pdf (p24) uniform distribution from two numbers in triangle generating barycentric coordinate * var input = VFXOperatorUtility.Saturate(inputExpression[2]); * var s = input.x; * var t = VFXOperatorUtility.Sqrt(input.y); * var a = one - t; * var b = (one - s) * t; * var c = s * t; * barycentricCoordinates = new VFXExpressionCombine(a, b, c); */ } else { throw new InvalidOperationException("No supported surfaceCoordinates : " + coord); } for (int i = 0; i < vertexAttributes.Count(); ++i) { var outputValueType = sampling_A[i].valueType; var barycentricCoordinateX = VFXOperatorUtility.CastFloat(barycentricCoordinates.x, outputValueType); var barycentricCoordinateY = VFXOperatorUtility.CastFloat(barycentricCoordinates.y, outputValueType); var barycentricCoordinateZ = VFXOperatorUtility.CastFloat(barycentricCoordinates.z, outputValueType); var r = sampling_A[i] * barycentricCoordinateX + sampling_B[i] * barycentricCoordinateY + sampling_C[i] * barycentricCoordinateZ; yield return(r); } }