public static IEnumerable <VFXExpression> SampleEdgeAttribute(VFXExpression source, VFXExpression index, VFXExpression lerp, IEnumerable <VertexAttribute> vertexAttributes) { bool skinnedMesh = source.valueType == UnityEngine.VFX.VFXValueType.SkinnedMeshRenderer; var mesh = !skinnedMesh ? source : new VFXExpressionMeshFromSkinnedMeshRenderer(source); var meshIndexFormat = new VFXExpressionMeshIndexFormat(mesh); var oneInt = VFXOperatorUtility.OneExpression[UnityEngine.VFX.VFXValueType.Int32]; var oneUint = VFXOperatorUtility.OneExpression[UnityEngine.VFX.VFXValueType.Uint32]; var threeUint = VFXOperatorUtility.ThreeExpression[UnityEngine.VFX.VFXValueType.Uint32]; var nextIndex = index + oneUint; //Loop triangle var loop = VFXOperatorUtility.Modulo(nextIndex, threeUint); var predicat = new VFXExpressionCondition(UnityEngine.VFX.VFXValueType.Uint32, VFXCondition.NotEqual, loop, VFXOperatorUtility.ZeroExpression[UnityEngine.VFX.VFXValueType.Uint32]); nextIndex = new VFXExpressionBranch(predicat, nextIndex, nextIndex - threeUint); var sampledIndex_A = new VFXExpressionSampleIndex(mesh, index, meshIndexFormat); var sampledIndex_B = new VFXExpressionSampleIndex(mesh, nextIndex, meshIndexFormat); var sampling_A = SampleVertexAttribute(source, sampledIndex_A, vertexAttributes).ToArray(); var sampling_B = SampleVertexAttribute(source, sampledIndex_B, vertexAttributes).ToArray(); for (int i = 0; i < vertexAttributes.Count(); ++i) { var outputValueType = sampling_A[i].valueType; var s = VFXOperatorUtility.CastFloat(lerp, outputValueType); yield return(VFXOperatorUtility.Lerp(sampling_A[i], sampling_B[i], s)); } }
protected override sealed VFXExpression[] BuildExpression(VFXExpression[] inputExpression) { var t = inputExpression[0]; var posA = inputExpression[1]; var posB = inputExpression[2]; var posC = inputExpression[3]; var posD = inputExpression[4]; var vt = VFXOperatorUtility.CastFloat(t, VFXValueType.Float3); var vtc = VFXOperatorUtility.CastFloat(VFXOperatorUtility.OneExpression[VFXValueType.Float] - t, VFXValueType.Float3); var three = VFXOperatorUtility.CastFloat(VFXValue.Constant(3.0f), VFXValueType.Float3); var six = VFXOperatorUtility.CastFloat(VFXValue.Constant(6.0f), VFXValueType.Float3); // Position var A = posA * vtc * vtc * vtc; var B = three * posB * vtc * vtc * vt; var C = three * posC * vtc * vt * vt; var D = posD * vt * vt * vt; // Derivative var dA = three * vtc * vtc * (posB - posA); var dB = six * vtc * vt * (posC - posB); var dC = three * vt * vt * (posD - posC); return(new[] { A + B + C + D, dA + dB + dC }); }
public void ProcessExpressionBasic() { //Reference some float math operation var a = new Vector2(0.75f, 0.5f); var b = new Vector3(1.3f, 0.2f, 0.7f); var c = 0.8f; var d = 0.1f; var refResultA = new Vector3(a.x + b.x, a.y + b.y, b.z); var refResultB = new Vector3(Mathf.Sin(refResultA.x), Mathf.Sin(refResultA.y), Mathf.Sin(refResultA.z)); var refResultC = refResultB * c; var refResultD = new Vector3(d, d, d) - refResultC; //Using expression system var value_a = new VFXValue <Vector2>(a); var value_b = new VFXValue <Vector3>(b); var value_c = new VFXValue <float>(c); var value_d = new VFXValue <float>(d); var addExpression = VFXOperatorUtility.CastFloat(value_a, value_b.valueType) + value_b; var sinExpression = new VFXExpressionSin(addExpression); var mulExpression = (sinExpression * VFXOperatorUtility.CastFloat(value_c, sinExpression.valueType)); var subtractExpression = VFXOperatorUtility.CastFloat(value_d, mulExpression.valueType) - mulExpression; var context = new VFXExpression.Context(VFXExpressionContextOption.CPUEvaluation); var resultA = context.Compile(addExpression); var resultB = context.Compile(sinExpression); var resultC = context.Compile(mulExpression); var resultD = context.Compile(subtractExpression); Assert.AreEqual(refResultA, resultA.Get <Vector3>()); Assert.AreEqual(refResultB, resultB.Get <Vector3>()); Assert.AreEqual(refResultC, resultC.Get <Vector3>()); Assert.AreEqual(refResultD, resultD.Get <Vector3>()); }
protected override sealed VFXExpression[] BuildExpression(VFXExpression[] inputExpression) { VFXExpression planeDistance = VFXOperatorUtility.SignedDistanceToPlane(inputExpression[0], inputExpression[1], inputExpression[2]); VFXExpression pointOnPlane = (inputExpression[2] - inputExpression[1] * VFXOperatorUtility.CastFloat(planeDistance, inputExpression[1].valueType)); return(new VFXExpression[] { pointOnPlane, planeDistance }); }
protected override sealed VFXExpression[] BuildExpression(VFXExpression[] inputExpression) { VFXExpression sphereDelta = (inputExpression[2] - inputExpression[0]); VFXExpression sphereDeltaLength = VFXOperatorUtility.Length(sphereDelta); VFXExpression sphereDistance = (sphereDeltaLength - inputExpression[1]); VFXExpression pointOnSphere = (inputExpression[1] / sphereDeltaLength); pointOnSphere = (sphereDelta * VFXOperatorUtility.CastFloat(pointOnSphere, inputExpression[0].valueType) + inputExpression[0]); return(new VFXExpression[] { pointOnSphere, sphereDistance }); }
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 }); }
protected override sealed VFXExpression[] BuildExpression(VFXExpression[] inputExpression) { var pos = inputExpression[0]; var center = inputExpression[1]; var wAxis = inputExpression[2]; var angle = inputExpression[3]; var projPoint = center + (wAxis * VFXOperatorUtility.CastFloat(VFXOperatorUtility.Dot(wAxis, pos - center), VFXValueType.Float3)); var uAxis = pos - projPoint; var vAxis = VFXOperatorUtility.Cross(uAxis, wAxis); var sinAngle = VFXOperatorUtility.CastFloat(new VFXExpressionSin(angle), VFXValueType.Float3); var cosAngle = VFXOperatorUtility.CastFloat(new VFXExpressionCos(angle), VFXValueType.Float3); return(new[] { projPoint + (uAxis * cosAngle) + (vAxis * sinAngle) }); }
protected override sealed VFXExpression[] BuildExpression(VFXExpression[] inputExpression) { VFXExpression parameters = new VFXExpressionCombine(inputExpression[1], inputExpression[3], inputExpression[4]); VFXExpression result; if (dimensions == DimensionCount.Two) { if (type == NoiseType.Value) { result = new VFXExpressionValueCurlNoise2D(inputExpression[0], parameters, inputExpression[2]); } else if (type == NoiseType.Perlin) { result = new VFXExpressionPerlinCurlNoise2D(inputExpression[0], parameters, inputExpression[2]); } else { result = new VFXExpressionCellularCurlNoise2D(inputExpression[0], parameters, inputExpression[2]); } } else { if (type == NoiseType.Value) { result = new VFXExpressionValueCurlNoise3D(inputExpression[0], parameters, inputExpression[2]); } else if (type == NoiseType.Perlin) { result = new VFXExpressionPerlinCurlNoise3D(inputExpression[0], parameters, inputExpression[2]); } else { result = new VFXExpressionCellularCurlNoise3D(inputExpression[0], parameters, inputExpression[2]); } } return(new[] { result *VFXOperatorUtility.CastFloat(inputExpression[5], result.valueType) }); }
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); // result = position * VFXToView * ViewToClip VFXExpression positionExpression = inputExpression[0]; VFXExpression viewPosExpression = new VFXExpressionTransformPosition(matricesExpressions.VFXToView.exp, positionExpression); VFXExpression clipPosExpression = new VFXExpressionTransformVector4(matricesExpressions.ViewToClip.exp, VFXOperatorUtility.CastFloat(viewPosExpression, VFXValueType.Float4, 1.0f)); // normalize using w component and renormalize to range [0, 1] VFXExpression halfExpression = VFXValue.Constant(0.5f); VFXExpression normalizedExpression = new VFXExpressionCombine(new VFXExpression[] { (clipPosExpression.x / clipPosExpression.w) * halfExpression + halfExpression, (clipPosExpression.y / clipPosExpression.w) * halfExpression + halfExpression, viewPosExpression.z // The z position is in world units from the camera }); return(new VFXExpression[] { normalizedExpression }); }
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()); }
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); } }
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) }); } }
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) }); }