Пример #1
0
        private void Update(EvaluationContext context)
        {
            var count = Count.GetValue(context).Clamp(1, 10000);

            if (_points.Length != count)
            {
                _points = new T3.Core.DataTypes.Point[count];
            }

            var scale     = Scale.GetValue(context);
            var frequency = Frequency.GetValue(context);
            var phase     = Phase.GetValue(context);

            var amplitude = Amplitude.GetValue(context);
            var index     = 0;
            var thickness = Thickness.GetValue(context);
            int seed      = 1337;

            for (var x = 0; x < count; x++)
            {
                var fX       = x / (float)count;
                var position = new Vector3(
                    MathUtils.PerlinNoise(phase + 0.234f + fX, frequency, 2, seed) * amplitude * scale.X,
                    MathUtils.PerlinNoise(phase + 110.637f + fX, frequency, 2, seed) * amplitude * scale.Y,
                    MathUtils.PerlinNoise(phase + 241.221f + fX, frequency, 2, seed) * amplitude * scale.Z);
                _points[index].Position = position;
                _points[index].W        = thickness;
                index++;
            }


            Result.Value = _points;
        }
Пример #2
0
 protected void UpdateSelectedItem()
 {
     if (EditorElement != null)
     {
         EditorElement.SelectedItem = Thickness.GetValue(Side);
     }
 }
Пример #3
0
        private void Update(EvaluationContext context)
        {
            try
            {
                var resourceManager = ResourceManager.Instance();

                var majorRadius = Radius.GetValue(context);
                var tubeRadius  = Thickness.GetValue(context);

                var segments       = Segments.GetValue(context);
                var radiusSegments = segments.Width.Clamp(1, 10000) + 1;
                var tubeSegments   = segments.Height.Clamp(1, 10000) + 1;

                var spin           = Spin.GetValue(context);
                var radiusSpin     = spin.X * MathUtils.ToRad;
                var spinMinorInRad = spin.Y * MathUtils.ToRad;

                var fill       = Fill.GetValue(context);
                var fillRadius = fill.X / 360f;
                var tubeFill   = fill.Y / 360f;

                var smoothAngle = SmoothAngle.GetValue(context);

                var useFlatShading = fillRadius / tubeSegments > smoothAngle / 360 || tubeFill / radiusSegments > smoothAngle / 360;
                var faceCount      = (tubeSegments - 1) * (radiusSegments - 1) * 2;
                var verticesCount  = tubeSegments * radiusSegments;

                // Create buffers
                if (_vertexBufferData.Length != verticesCount)
                {
                    _vertexBufferData = new PbrVertex[verticesCount];
                }

                if (_indexBufferData.Length != faceCount)
                {
                    _indexBufferData = new SharpDX.Int3[faceCount];
                }


                // Initialize
                var tubeAngleFraction   = tubeFill / (tubeSegments - 1) * 2.0 * Math.PI;
                var radiusAngleFraction = fillRadius / (radiusSegments - 1) * 2.0 * Math.PI;

                for (int tubeIndex = 0; tubeIndex < tubeSegments; ++tubeIndex)
                {
                    var tubeAngle = tubeIndex * tubeAngleFraction + spinMinorInRad;

                    double tubePosition1X = Math.Sin(tubeAngle) * tubeRadius;
                    double tubePosition1Y = Math.Cos(tubeAngle) * tubeRadius;
                    double tubePosition2X = Math.Sin(tubeAngle + tubeAngleFraction) * tubeRadius;
                    double tubePosition2Y = Math.Cos(tubeAngle + tubeAngleFraction) * tubeRadius;

                    var v0 = tubeIndex / (float)(tubeSegments - 1);
                    var v1 = (tubeIndex + 1) / (float)(tubeSegments - 1);

                    for (int radiusIndex = 0; radiusIndex < radiusSegments; ++radiusIndex)
                    {
                        var vertexIndex = radiusIndex + tubeIndex * radiusSegments;
                        var faceIndex   = 2 * (radiusIndex + tubeIndex * (radiusSegments - 1));

                        var u0 = (radiusIndex) / (float)(radiusSegments - 1);
                        var u1 = (radiusIndex + 1) / (float)(radiusSegments - 1);

                        var radiusAngle = radiusIndex * radiusAngleFraction + radiusSpin;

                        var p = new SharpDX.Vector3((float)(Math.Sin(radiusAngle) * (tubePosition1X + majorRadius)),
                                                    (float)(Math.Cos(radiusAngle) * (tubePosition1X + majorRadius)),
                                                    (float)tubePosition1Y);

                        var p1 = new SharpDX.Vector3((float)(Math.Sin(radiusAngle + radiusAngleFraction) * (tubePosition1X + majorRadius)),
                                                     (float)(Math.Cos(radiusAngle + radiusAngleFraction) * (tubePosition1X + majorRadius)),
                                                     (float)tubePosition1Y);

                        var p2 = new SharpDX.Vector3((float)(Math.Sin(radiusAngle) * (tubePosition2X + majorRadius)),
                                                     (float)(Math.Cos(radiusAngle) * (tubePosition2X + majorRadius)),
                                                     (float)tubePosition2Y);

                        var uv0 = new SharpDX.Vector2(u0, v1);
                        var uv1 = new SharpDX.Vector2(u1, v1);
                        var uv2 = new SharpDX.Vector2(u1, v0);

                        var tubeCenter1 = new SharpDX.Vector3((float)Math.Sin(radiusAngle), (float)Math.Cos(radiusAngle), 0.0f) * majorRadius;
                        var normal0     = SharpDX.Vector3.Normalize(useFlatShading
                                                                    ? SharpDX.Vector3.Cross(p - p1, p - p2)
                                                                    : p - tubeCenter1);

                        MeshUtils.CalcTBNSpace(p, uv0, p1, uv1, p2, uv2, normal0, out var tangent0, out var binormal0);

                        _vertexBufferData[vertexIndex + 0] = new PbrVertex
                        {
                            Position  = p,
                            Normal    = normal0,
                            Tangent   = tangent0,
                            Bitangent = binormal0,
                            Texcoord  = uv0,
                            Selection = 1,
                        };

                        if (tubeIndex >= tubeSegments - 1 || radiusIndex >= radiusSegments - 1)
                        {
                            continue;
                        }

                        _indexBufferData[faceIndex + 0] = new SharpDX.Int3(vertexIndex + 0, vertexIndex + 1, vertexIndex + radiusSegments);
                        _indexBufferData[faceIndex + 1] = new SharpDX.Int3(vertexIndex + radiusSegments, vertexIndex + 1, vertexIndex + radiusSegments + 1);
                    }
                }

                // Write Data
                _vertexBufferWithViews.Buffer = _vertexBuffer;
                resourceManager.SetupStructuredBuffer(_vertexBufferData, PbrVertex.Stride * verticesCount, PbrVertex.Stride, ref _vertexBuffer);
                resourceManager.CreateStructuredBufferSrv(_vertexBuffer, ref _vertexBufferWithViews.Srv);
                resourceManager.CreateStructuredBufferUav(_vertexBuffer, UnorderedAccessViewBufferFlags.None, ref _vertexBufferWithViews.Uav);

                _indexBufferWithViews.Buffer = _indexBuffer;
                const int stride = 3 * 4;
                resourceManager.SetupStructuredBuffer(_indexBufferData, stride * faceCount, stride, ref _indexBuffer);
                resourceManager.CreateStructuredBufferSrv(_indexBuffer, ref _indexBufferWithViews.Srv);
                resourceManager.CreateStructuredBufferUav(_indexBuffer, UnorderedAccessViewBufferFlags.None, ref _indexBufferWithViews.Uav);

                _data.VertexBuffer  = _vertexBufferWithViews;
                _data.IndicesBuffer = _indexBufferWithViews;
                Data.Value          = _data;
                Data.DirtyFlag.Clear();
            }
            catch (Exception e)
            {
                Log.Error("Failed to create torus mesh:" + e.Message);
            }
        }