Ejemplo n.º 1
0
        public Entity Build()
        {
            if (_colorSource == null)
            {
                _colorSource = new PlainColorSource(1, 1, 1);
            }


            var EntityManager = World.DefaultGameObjectInjectionWorld.EntityManager;

            //  Color[] textureColors = _sourceTexture.GetPixels();



            //    _cubes = new Entity[width, height];
            Parent     parent     = Parent.Create();
            ParentId   parentId   = parent.ParentId();
            ParentView parentView = new ParentView
            {
                Id     = parent.Id,
                Width  = _width,
                Height = _height
            };

            var renderData = new RenderData
            {
                Mesh     = _mesh,
                Material = _material
            };


            EntityArchetype archetype = EntityManager.CreateArchetype(
                typeof(ParentId),
                typeof(RenderData),
                typeof(WorldTransform),
                typeof(ParentViewId),
                typeof(PixelCube),
                typeof(Position),
                typeof(Rotation),
                typeof(Scale),
                typeof(RenderColor),
                typeof(MoveTo),
                typeof(RotateTo),
                typeof(MouseInteractive),
                typeof(Prefab)
                );


            var prefab = EntityManager.CreateEntity(archetype);

            EntityManager.SetSharedComponentData(prefab, renderData);
            EntityManager.SetSharedComponentData(prefab, parentView.ParentViewId());

            Profiler.BeginSample("PaintViewBuilder.CreateEntities");
            var count    = _width * _height;
            var entities = new NativeArray <Entity>(count, Allocator.Temp);

            EntityManager.Instantiate(prefab, entities);
            Profiler.EndSample();


            Profiler.BeginSample("PaintViewBuilder.PrepareData");


            var colors     = new NativeArray <RenderColor>(count, Allocator.TempJob);
            var pixelCubes = new NativeArray <PixelCube>(count, Allocator.TempJob);
            var positions  = new NativeArray <Position>(count, Allocator.TempJob);
            var rotations  = new NativeArray <Rotation>(count, Allocator.TempJob);
            var scales     = new NativeArray <Scale>(count, Allocator.TempJob);
            var parentIds  = new NativeArray <ParentId>(count, Allocator.TempJob);

            for (int x = 0; x < _width; ++x)
            {
                for (int y = 0; y < _height; ++y)
                {
                    var index = y * _width + x;

                    parentIds[index] = parentId;
                    colors[index]    = new RenderColor {
                        Value = _colorSource.getColor(index)
                    };
                    pixelCubes[index] = new PixelCube {
                        Position = new int2(x, y)
                    };
                    positions[index] = new Position {
                        Value = new float3(x, y, 0)
                    };
                    rotations[index] = new Rotation {
                        Value = quaternion.identity
                    };
                    scales[index] = new Scale {
                        Value = new float3(1, 1, 1)
                    };
                }
            }

            Profiler.EndSample();



            Profiler.BeginSample("PaintViewBuilder.SetData");

            var query = EntityManager.CreateEntityQuery(
                typeof(ParentViewId),
                typeof(PixelCube),
                typeof(Position),
                typeof(Rotation),
                typeof(Scale),
                typeof(RenderColor),
                typeof(ParentId));

            query.SetSharedComponentFilter(parentView.ParentViewId());
            query.CopyFromComponentDataArray(parentIds);
            query.CopyFromComponentDataArray(pixelCubes);
            query.CopyFromComponentDataArray(colors);
            query.CopyFromComponentDataArray(positions);
            query.CopyFromComponentDataArray(rotations);
            query.CopyFromComponentDataArray(scales);


            entities.Dispose();
            colors.Dispose();
            pixelCubes.Dispose();
            positions.Dispose();
            rotations.Dispose();
            scales.Dispose();
            parentIds.Dispose();

            Profiler.EndSample();



            Profiler.BeginSample("PaintEditor.CreateParent");
            var entity = EntityBuilder.Create()
                         .Add(parent)
                         .Add(new Position {
                Value = _position
            })
                         .Add(new Rotation {
                Value = _rotation
            })
                         .Add(new Scale {
                Value = _scale
            })
                         .Add(new ParentView {
                Width = _width, Height = _height, Id = parentId.Value
            })
                         .Build();

            Profiler.EndSample();

            return(entity);
        }
Ejemplo n.º 2
0
        void Start()
        {
            Profiler.BeginSample("PaintView.Start");
            rotationSpeed = Random.Range(-50, 50);

            _cubes = new Entity[width, height];

            var parent = Parent.Create();

            _parentId   = parent.ParentId();
            _parentView = new ParentView
            {
                Id     = parent.Id,
                Width  = width,
                Height = height
            };

            var renderData = new RenderData
            {
                Mesh     = mesh,
                Material = material
            };

            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    var color = Color.magenta;
                    if (Random.Range(1, 100) > 50)
                    {
                        if (Random.Range(1, 100) > 50)
                        {
                            color = Color.green;
                        }
                        else
                        {
                            color = Color.yellow;
                        }
                    }
                    else
                    {
                        if (Random.Range(1, 100) > 50)
                        {
                            color = Color.red;
                        }
                        else
                        {
                            color = Color.blue;
                        }
                    }


                    _cubes[i, j] = EntityBuilder.Create()
                                   .Add(_parentId)
                                   .AddShared(renderData)
                                   .AddShared(_parentView.ParentViewId())
                                   .Add(new PixelCube {
                        Position = new int2(i, j)
                    })
                                   .Add(new Position {
                        Value = new float3(i - width / 2, j - height / 2, 0)
                    })
                                   .Add(new Rotation {
                        Value = quaternion.Euler(0, 0, 0)
                    })
                                   .Add(new Scale {
                        Value = new float3(1, 1, 1)
                    })
                                   .Add(new RenderColor {
                        Value = color
                    })
                                   .Add(new MoveTo())
                                   .Build();
                }
            }

            //creates parent entity
            _entity = EntityBuilder.Create()
                      .Add(parent)
                      .Add(new Position {
                Value = transform.position
            })
                      .Add(new Rotation {
                Value = transform.rotation
            })
                      .Add(new Scale {
                Value = transform.localScale
            })
                      .Add(new ParentView {
                Width = width, Height = height, Id = _parentId.Value
            })
                      .Build();

            Profiler.EndSample();
        }