Esempio n. 1
0
    List <Room> DivideByX(Room room)
    {
        List <Room> rooms = new List <Room>();

        Room roomLeft;
        Room roomRight;

        //Value for cut
        //float posX = Random.Range(0 + roomSizeX * 0.5f, room.extends.x - roomSizeX * 0.5f);
        float posX = RandomSeed.GetValue() * (room.extends.x - roomSizeX * 0.5f) + roomSizeX * 0.5f;

        //Extends
        roomRight.extends = new Vector2(posX, room.extends.y);
        roomLeft.extends  = new Vector2(room.extends.x - posX, room.extends.y);

        //Center
        roomRight.center = new Vector2(room.center.x + room.extends.x * 0.5f - roomRight.extends.x * 0.5f, room.center.y);
        roomLeft.center  = new Vector2(room.center.x - room.extends.x * 0.5f + roomLeft.extends.x * 0.5f, room.center.y);

        //Children
        roomRight.children = new List <Room>();
        roomLeft.children  = new List <Room>();

        //Add children
        room.children.Add(roomRight);
        room.children.Add(roomLeft);

        roomRight.children.AddRange(CheckDivision(roomRight));
        roomLeft.children.AddRange(CheckDivision(roomLeft));

        return(rooms);
    }
Esempio n. 2
0
    List <Room> DivideByProbability(Room room)
    {
        //float probability = Random.Range(0f, 1f);
        float probability = RandomSeed.GetValue();

        return(probability > 0.5 ? DivideByX(room) : DivideByY(room));
    }
Esempio n. 3
0
    List <Room> DivideByY(Room room)
    {
        List <Room> rooms = new List <Room>();

        Room roomUp;
        Room roomDown;

        //Value for cut
        //float posY = Random.Range(0 + roomSizeY * 0.5f, room.extends.y - roomSizeY * 0.5f);
        float posY = RandomSeed.GetValue() * (room.extends.y - roomSizeY * 0.5f) + roomSizeY * 0.5f;

        //Extends
        roomDown.extends = new Vector2(room.extends.x, posY);
        roomUp.extends   = new Vector2(room.extends.x, room.extends.y - posY);

        //Center
        roomDown.center = new Vector2(room.center.x, room.center.y - room.extends.y * 0.5f + roomDown.extends.y * 0.5f);
        roomUp.center   = new Vector2(room.center.x, room.center.y + room.extends.y * 0.5f - roomUp.extends.y * 0.5f);

        //Children
        roomDown.children = new List <Room>();
        roomUp.children   = new List <Room>();

        //Add children
        room.children.Add(roomUp);
        room.children.Add(roomDown);

        roomDown.children.AddRange(CheckDivision(roomDown));
        roomUp.children.AddRange(CheckDivision(roomUp));

        return(rooms);
    }
Esempio n. 4
0
        public override IEnumerator ReceivePayload(VisualPayload payload)
        {
            foreach (var entry in Scope.GetEntries(payload.Data))
            {
                var bitstream1 = FirstStream.GetValue(entry);
                var bitstream2 = SecondStream.GetValue(entry);

                var secondStreamProportion = SecondStreamProportion.GetValue(entry);
                var randomSeed             = RandomSeed.GetValue(entry);

                var combinedStream = new RandomlyInterspersedBitstream(bitstream1,
                                                                       bitstream2, secondStreamProportion, randomSeed);

                combinedStream.AdvanceByBytes = AdvanceByBytes.GetValue(entry);

                NewBitstreamTarget.SetValue(combinedStream, entry);
            }

            var iterator = Router.TransmitAll(payload);

            while (iterator.MoveNext())
            {
                yield return(null);
            }
        }
Esempio n. 5
0
        private void Update(EvaluationContext context)
        {
            var worldToClipSpace = Matrix.Multiply(context.WorldToCamera, context.CameraToClipSpace);
            //Matrix worldToView = context.WorldToCamera * context.CameraProjection;
            //var worldToClipSpace = context.WorldToCamera
            //var viewToWorld = Matrix.Invert(worldToClipSpace);

            var brightness = Brightness.GetValue(context);

            var color             = Color.GetValue(context);
            var randomizeColor    = RandomizeColor.GetValue(context);
            var size              = Size.GetValue(context);
            var randomizeSize     = RandomizeSize.GetValue(context);
            var stretch           = Stretch.GetValue(context);
            var distanceFromLight = DistanceFromLight.GetValue(context);
            var spread            = Spread.GetValue(context);
            var randomizeSpread   = RandomizeSpread.GetValue(context);
            var positionFactor    = PositionFactor.GetValue(context);
            var randomizePosition = RandomizePosition.GetValue(context);

            var mixPointLightColor = MixPointLightColor.GetValue(context);

            var referencedLightIndex = LightIndex.GetValue(context);

            var innerFxZone      = InnerFxZone.GetValue(context);
            var edgeFxZone       = EdgeFxZone.GetValue(context);
            var zoneFxScale      = FxZoneScale.GetValue(context);
            var zoneFxBrightness = FxZoneBrightness.GetValue(context);

            var matteBoxZone = MattBoxZone.GetValue(context);

            var rand       = new Random(RandomSeed.GetValue(context));
            var fxZoneMode = (ZoneFxModes)FxZoneMode.GetValue(context);

            var rotation       = Rotation.GetValue(context);
            var rotationSpread = RotationSpread.GetValue(context);

            var rotateTowards = (Categories)RotateTowards.GetValue(context);

            int startLightIndex = 0;
            int endLightIndex   = context.PointLights.Count;

            _tempList.Clear();


            if (brightness > 0.00001f)
            {
                if (referencedLightIndex >= 0)
                {
                    startLightIndex = referencedLightIndex;
                    endLightIndex   = referencedLightIndex + 1;
                }

                var aspectRatio = (float)context.RequestedResolution.Width / (float)context.RequestedResolution.Height;

                for (int lightIndex = startLightIndex; lightIndex < endLightIndex; lightIndex++)
                {
                    var pointLight = context.PointLights.GetPointLight(lightIndex);
                    var lightPosDx = pointLight.Position.ToSharpDxVector4(1);

                    var posInViewDx = SharpDX.Vector4.Transform(lightPosDx, worldToClipSpace);
                    posInViewDx /= posInViewDx.W;

                    // Ignore light sources behind
                    var hideFactor = posInViewDx.Z < 0 ? 0 : 1;

                    posInViewDx /= posInViewDx.W;
                    var lightPosInView2D = new Vector2(posInViewDx.X, posInViewDx.Y);

                    var count = SpriteCount.GetValue(context).Clamp(0, 1000);
                    if (count != _sprites.NumElements)
                    {
                        _sprites = new StructuredList <Sprite>(count);
                    }

                    // Render Planes
                    for (var i = 0; i < count; ++i)
                    {
                        var f = count <= 1 ? 0 : ((float)i / (count - 1) - 0.5f);
                        var positionOnLine = (float)((-distanceFromLight
                                                      + f * spread * 2
                                                      + randomizeSpread * (rand.NextDouble() - 0.5) + 1));

                        Vector2 objectScreenPos = lightPosInView2D * positionOnLine * positionFactor + (new Vector2(1, 1) - positionFactor) * lightPosInView2D;

                        objectScreenPos += new Vector2((float)(randomizePosition.X * (rand.NextDouble() - 0.5)),
                                                       (float)(randomizePosition.Y * (rand.NextDouble() - 0.5)));

                        var sizeWithRandom = size * (float)(1.0 + randomizeSize * (rand.NextDouble() - 0.5)) / 0.2f;

                        var colorWithLight = new Vector4((color.X + randomizeColor.X * (float)(rand.NextDouble() - 0.5) * 4) * MathUtils.Lerp(1f, pointLight.Color.X, mixPointLightColor),
                                                         (color.Y + randomizeColor.Y * (float)(rand.NextDouble() - 0.5) * 4) * MathUtils.Lerp(1f, pointLight.Color.Y, mixPointLightColor),
                                                         (color.Z + randomizeColor.Z * (float)(rand.NextDouble() - 0.5) * 4) * MathUtils.Lerp(1f, pointLight.Color.Z, mixPointLightColor),
                                                         color.W * (1 - randomizeColor.W * (float)(rand.NextDouble() * 2)));
                        var spriteColor = Vector4.Clamp(colorWithLight, Vector4.Zero, new Vector4(100, 100, 100, 1));

                        var triggerPosition = fxZoneMode == ZoneFxModes.Lights
                                                  ? lightPosInView2D
                                                  : objectScreenPos;

                        var d          = GetDistanceToEdge(triggerPosition);
                        var cInnerZone = MathUtils.SmootherStep(innerFxZone.Y, innerFxZone.X, 1 - d);
                        var cEdgeZone  = MathUtils.SmootherStep(edgeFxZone.X, edgeFxZone.Y, 1 - d);
                        var cMatteBox  = MathUtils.SmootherStep(matteBoxZone.Y, matteBoxZone.X, 1 - d);

                        var totalTriggerAmount = (cInnerZone + cEdgeZone) * cMatteBox;

                        sizeWithRandom *= (1 + zoneFxScale * totalTriggerAmount).Clamp(0, 100);

                        var brightnessEffect = (zoneFxBrightness * totalTriggerAmount).Clamp(0, 100);
                        spriteColor.X += brightnessEffect;
                        spriteColor.Y += brightnessEffect;
                        spriteColor.Z += brightnessEffect;
                        spriteColor.W  = ((spriteColor.W + brightnessEffect)).Clamp(0, 1);

                        spriteColor.W *= cMatteBox * pointLight.Color.W;

                        // This might actually be a good idea. Maybe we should do this later..
                        // Fade with incoming alpha from FlatShaders and Materials
                        //color.W *= materialAlpha;

                        float spriteRotation = rotation;

                        switch (rotateTowards)
                        {
                        case Categories.Object:
                            break;

                        case Categories.Light:
                            spriteRotation -=
                                (float)(Math.Atan2((objectScreenPos.X - lightPosInView2D.X) * aspectRatio, objectScreenPos.Y - lightPosInView2D.Y) +
                                        MathF.PI) * (180 / MathF.PI);
                            break;

                        case Categories.ScreenCenter:
                            spriteRotation -= (float)(Math.Atan2(objectScreenPos.X, objectScreenPos.Y) + MathF.PI) * 180f / MathF.PI;
                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }

                        // // Transforom UV to pick correct texture cell
                        // if (TextureCellsRows == 0)
                        //     TextureCellsRows = 1;
                        //
                        // if (TextureCellsColumns == 0)
                        //     TextureCellsColumns = 1;

                        // int row = (int)(Math.Floor(i / TextureCellsColumns) % TextureCellsRows);
                        // int column = (int)(i % TextureCellsRows);
                        //
                        // var translationUV = new Vector3(1 / TextureCellsColumns * column, 1 / TextureCellsRows * row, 0);
                        // var rotationUV = new Quaternion();
                        // var scaleUV = new Vector3(1 / TextureCellsColumns, 1 / TextureCellsRows, 0);
                        // var pivotUV = new Vector3(0, 0, 0);
                        //
                        // var transformUV = Matrix.Transformation(pivotUV, new Quaternion(), scaleUV, pivotUV, rotationUV, translationUV);
                        // var prevTransformUV = context.TextureMatrix;
                        // context.TextureMatrix = transformUV * prevTransformUV;
                        spriteColor.W *= brightness;

                        _tempList.Add(new Sprite
                        {
                            PosInClipSpace = objectScreenPos,
                            Size           = sizeWithRandom * stretch * hideFactor,
                            Color          = spriteColor,
                            RotationDeg    = spriteRotation + f * rotationSpread * 180,
                            UvMin          = Vector2.Zero,
                            UvMax          = Vector2.One,
                        });
                    }
                }
            }
            // Copy to structured array
            if (_tempList.Count != _sprites.NumElements)
            {
                _sprites = new StructuredList <Sprite>(_tempList.Count);
            }

            for (var spriteIndex = 0; spriteIndex < _tempList.Count; spriteIndex++)
            {
                _sprites.TypedElements[spriteIndex] = _tempList[spriteIndex];
            }
            OutBuffer.Value = _sprites;
        }
        private void Update(EvaluationContext context)
        {
            var wasUpdate = false;

            var neighbourCount = NeighbourCount.GetValue(context).Clamp(1, 9);
            var stateCount     = StateCount.GetValue(context).Clamp(1, 100);
            var seed           = RandomSeed.GetValue(context);
            var lambda         = Lambda.GetValue(context);
            var isotropic      = Isotropic.GetValue(context);

            if (neighbourCount != _neighbourCount ||
                stateCount != _stateCount ||
                seed != _randomSeed ||
                Math.Abs(lambda - _lambda) > 0.001f ||
                isotropic != _isotropic)
            {
                wasUpdate       = true;
                _neighbourCount = neighbourCount;
                _stateCount     = stateCount;
                _randomSeed     = seed;
                _lambda         = lambda;
                _isotropic      = isotropic;

                _requiredBitCount = (int)Math.Ceiling(Math.Log(_stateCount, 2));
                _ruleTableLength  = 1 << (_requiredBitCount * neighbourCount);

                if (_cellBuffer.Length != _ruleTableLength)
                {
                    _cellBuffer = new Cell[_ruleTableLength];
                }

                var rand = new Random(seed);

                var countPositives = 0f;
                //_lambda = 0.49f;
                for (var ruleIndex = 0; ruleIndex < _ruleTableLength; ruleIndex++)
                {
                    var choice    = rand.NextDouble();
                    var nextState = (choice > _lambda || ruleIndex == 0)
                                   ? 0
                                   : (rand.Next(stateCount - 1) + 1);

                    if (nextState == 0)
                    {
                        countPositives++;
                    }

                    _cellBuffer[ruleIndex] = new Cell((uint)nextState);
                    _cellBuffer[FlipLookupIndex(ruleIndex)] = new Cell((uint)nextState);
                }

                // Initialize with random
                // for (var ruleIndex = 0; ruleIndex < _ruleTableLength; ruleIndex++)
                // {
                //     var nextState = rand.Next(stateCount);
                //     _cellBuffer[ruleIndex] =  new Cell((uint)nextState);
                // }

                // Flip to approach lambda
                //var countPositives = 0f;
                // int step = _ruleTableLength/6;
                // for (var ruleIndex = 0; ruleIndex < _ruleTableLength; ruleIndex+= step)
                // {
                //     var currentL = MeasureLambda();
                //     if (currentL < _lambda)
                //     {
                //         _cellBuffer[ruleIndex] =  new Cell((uint)rand.Next(stateCount-1)+1);
                //         _cellBuffer[FlipLookupIndex(ruleIndex)] =  new Cell((uint)rand.Next(stateCount-1)+1);
                //     }
                //     else
                //     {
                //         _cellBuffer[ruleIndex] =   new Cell(0);
                //         _cellBuffer[FlipLookupIndex(ruleIndex)] =  new Cell(0);
                //     }
                //     //countPositives++;
                // }


                Log.Debug($" Lambda:  {MeasureLambda()}");

                const int stride          = 4;
                var       resourceManager = ResourceManager.Instance();
                _bufferWithViews.Buffer = _buffer;
                resourceManager.SetupStructuredBuffer(_cellBuffer, stride * _cellBuffer.Length, stride, ref _buffer);
                resourceManager.CreateStructuredBufferSrv(_buffer, ref _bufferWithViews.Srv);
                resourceManager.CreateStructuredBufferUav(_buffer, UnorderedAccessViewBufferFlags.None, ref _bufferWithViews.Uav);
            }

            OutBuffer.Value   = _bufferWithViews;
            TableLength.Value = _ruleTableLength;
            WasUpdated.Value  = wasUpdate;
        }