Esempio n. 1
0
        private void LoadFromBitmap(string textureKey, Bitmap bitmap, TextureProperties properties = default)
        {
            using (var stream = new MemoryStream())
            {
                if (properties.Rounded)
                {
                    bitmap = bitmap.Round(this.textureRoundRatio.GetRatio(textureKey));
                }
                else if (properties.Squared)
                {
                    bitmap = bitmap.Square();
                }

                if (!properties.ColorRatio.IsZero)
                {
                    bitmap = bitmap.AdjustColor(properties.ColorRatio);
                }

                if (properties.Brightness != 0)
                {
                    bitmap = bitmap.AdjustBrightness(properties.Brightness);
                }

                bitmap.Save(stream, ImageFormat.Png);
                bitmap.Dispose();

                this.LoadFromStream(textureKey, stream);
            }
        }
Esempio n. 2
0
        public void LoadFromFile(string textureKey, string file, TextureProperties properties = default)
        {
            if (!this.loadedTextures.Add(textureKey))
            {
                return;
            }

            Task.Run(
                () =>
            {
                try
                {
                    if (!File.Exists(file))
                    {
                        Log.Warn("Texture file not found: " + file);
                        return;
                    }

                    using (var fileStream = File.OpenRead(file))
                    {
                        var resource = new ResourceFile(fileStream, Path.GetExtension(file));
                        var bitmap   = resource.GetBitmap();

                        this.LoadFromBitmap(textureKey, bitmap, properties);
                    }
                }
                catch (Exception e)
                {
                    Log.Error(e);
                }
            });
        }
 public void OnReset(object data = null)
 {
     lerpers                   = new TextureLerper[12];
     texturePagePool           = new TexturePagePool();
     textureBuffers            = new TextureBuffer[12];
     externallyUpdatedTextures = new Texture2D[12];
     for (int i = 0; i < 12; i++)
     {
         TextureProperties textureProperties = default(TextureProperties);
         textureProperties.textureFormat = TextureFormat.Alpha8;
         textureProperties.filterMode    = FilterMode.Bilinear;
         textureProperties.blend         = false;
         textureProperties.blendSpeed    = 1f;
         TextureProperties item = textureProperties;
         for (int j = 0; j < this.textureProperties.Length; j++)
         {
             if (i == (int)this.textureProperties[j].simProperty)
             {
                 item = this.textureProperties[j];
             }
         }
         Property property = (Property)i;
         item.name = property.ToString();
         if ((UnityEngine.Object)externallyUpdatedTextures[i] != (UnityEngine.Object)null)
         {
             UnityEngine.Object.Destroy(externallyUpdatedTextures[i]);
             externallyUpdatedTextures[i] = null;
         }
         Texture texture;
         if (item.updatedExternally)
         {
             externallyUpdatedTextures[i] = new Texture2D(Grid.WidthInCells, Grid.HeightInCells, TextureUtil.TextureFormatToGraphicsFormat(item.textureFormat), TextureCreationFlags.None);
             texture = externallyUpdatedTextures[i];
         }
         else
         {
             TextureBuffer[] array     = textureBuffers;
             int             num       = i;
             Property        property2 = (Property)i;
             array[num] = new TextureBuffer(property2.ToString(), Grid.WidthInCells, Grid.HeightInCells, item.textureFormat, item.filterMode, texturePagePool);
             texture    = textureBuffers[i].texture;
         }
         if (item.blend)
         {
             TextureLerper[] array2         = lerpers;
             int             num2           = i;
             Texture         target_texture = texture;
             Property        property3      = (Property)i;
             array2[num2]     = new TextureLerper(target_texture, property3.ToString(), texture.filterMode, item.textureFormat);
             lerpers[i].Speed = item.blendSpeed;
         }
         Shader.SetGlobalTexture(item.texturePropertyName = (texture.name = GetShaderPropertyName((Property)i)), texture);
         allTextureProperties.Add(item);
     }
 }
Esempio n. 4
0
        /// <summary>
        /// Performs texture properties setting
        /// </summary>
        /// <param name="control"></param>
        /// <param name="name"></param>
        private void OnTexturePropertiesChanged(TextureProperties control, TextureProperties.PropertyName name)
        {
            if (CurrentSolidManipulationMode == SolidManipulationMode.Vertex)
            {
                return;
            }

            // collect solid and group map objects
            CustomOperation setSolidPropertiesOperation = new CustomOperation();

            setSolidPropertiesOperation.OnMapObjectGroup = group =>
                                                           group.MapObjectList.ForEach(m => m.PerformOperation(setSolidPropertiesOperation));

            setSolidPropertiesOperation.OnSolid = solid =>
            {
                solid.Faces.Where(f => f.Selected).ToList().ForEach(face =>
                {
                    switch (name)
                    {
                    case TextureProperties.PropertyName.UScale:
                        face.TextureMapping.UScale = control.UScale;
                        break;

                    case TextureProperties.PropertyName.VScale:
                        face.TextureMapping.VScale = control.VScale;
                        break;

                    case TextureProperties.PropertyName.UShift:
                        face.TextureMapping.UShift = control.UShift;
                        break;

                    case TextureProperties.PropertyName.VShift:
                        face.TextureMapping.VShift = control.VShift;
                        break;

                    case TextureProperties.PropertyName.TextureLock:
                        face.TextureMapping.TextureLocked = control.TextureLocked;
                        break;
                    }

                    // Rotation is handled differently
                    if (name == TextureProperties.PropertyName.Rotation)
                    {
                        solid.SetTextureRotationForFace(face, control.Rotation);
                    }

                    solid.CalculateTextureCoordinatesForFace(face);
                });
            };
            Selection.PerformOperation(setSolidPropertiesOperation);

            SceneDocument.IsDirty = true;

            RenderViewports();
        }
 private void OnShadersReloaded()
 {
     for (int i = 0; i < 12; i++)
     {
         TextureLerper textureLerper = lerpers[i];
         if (textureLerper != null)
         {
             TextureProperties textureProperties = allTextureProperties[i];
             Shader.SetGlobalTexture(textureProperties.texturePropertyName, textureLerper.Update());
         }
     }
 }
Esempio n. 6
0
        private Texture CreateTexture(int width, int height, float scale)
        {
            int legalWidth  = NumberUtil.NextPowerOfTwo(width * scale);
            int legalHeight = NumberUtil.NextPowerOfTwo(height * scale);

            TextureProperties texProps = new TextureProperties
            {
                TextureFormat      = TextureFormat.Rgba8888,
                Scale              = scale,
                Width              = legalWidth,
                Height             = legalHeight,
                NumMipmaps         = 0,
                PremultipliedAlpha = true
            };

            return(new GLTexture(IntPtr.Zero, texProps));
        }
 private void LateUpdate()
 {
     if (Grid.IsInitialized())
     {
         Shader.SetGlobalVector(WorldSizeID, new Vector4((float)Grid.WidthInCells, (float)Grid.HeightInCells, 1f / (float)Grid.WidthInCells, 1f / (float)Grid.HeightInCells));
         Shader.SetGlobalVector(PropTexWsToCsID, new Vector4(0f, 0f, 1f, 1f));
         Shader.SetGlobalVector(PropTexCsToWsID, new Vector4(0f, 0f, 1f, 1f));
         Shader.SetGlobalFloat(TopBorderHeightID, (float)Grid.TopBorderHeight);
         GetVisibleCellRange(out int x, out int y, out int x2, out int y2);
         Shader.SetGlobalFloat(FogOfWarScaleID, FogOfWarScale);
         int num = NextPropertyIdx++ % allTextureProperties.Count;
         TextureProperties textureProperties = allTextureProperties[num];
         while (textureProperties.updateEveryFrame)
         {
             num = NextPropertyIdx++ % allTextureProperties.Count;
             textureProperties = allTextureProperties[num];
         }
         for (int i = 0; i < allTextureProperties.Count; i++)
         {
             TextureProperties p = allTextureProperties[i];
             if (num == i || p.updateEveryFrame || GameUtil.IsCapturingTimeLapse())
             {
                 UpdateProperty(ref p, x, y, x2, y2);
             }
         }
         for (int j = 0; j < 12; j++)
         {
             TextureLerper textureLerper = lerpers[j];
             if (textureLerper != null)
             {
                 if (Time.timeScale == 0f)
                 {
                     textureLerper.LongUpdate(Time.unscaledDeltaTime);
                 }
                 TextureProperties textureProperties2 = allTextureProperties[j];
                 Shader.SetGlobalTexture(textureProperties2.texturePropertyName, textureLerper.Update());
             }
         }
     }
 }
Esempio n. 8
0
        public void LoadFromDota(string textureKey, string file, TextureProperties properties = default)
        {
            if (!this.loadedTextures.Add(textureKey))
            {
                return;
            }

            Task.Run(
                async() =>
            {
                try
                {
                    await this.vpkReaderTask.ConfigureAwait(false);

                    var bitmap = this.vpkBrowser.GetBitmap(file);
                    if (bitmap == null)
                    {
                        Log.Warn("Texture file not found: " + file);
                        return;
                    }

                    if (properties.Sliced)
                    {
                        for (var i = 0; i <= 100; i++)
                        {
                            this.LoadFromBitmap(textureKey + i, bitmap.Pie(i), properties);
                        }
                    }
                    else
                    {
                        this.LoadFromBitmap(textureKey, bitmap, properties);
                    }
                }
                catch (Exception e)
                {
                    Log.Error(e);
                }
            });
        }
Esempio n. 9
0
        public void LoadFromResource(string textureKey, string file, TextureProperties properties = default)
        {
            if (!this.loadedTextures.Add(textureKey))
            {
                return;
            }

            var assembly = Assembly.GetCallingAssembly();

            Task.Run(
                () =>
            {
                try
                {
                    var resourceFile = Array.Find(assembly.GetManifestResourceNames(), x => x.EndsWith(file));
                    if (resourceFile == null)
                    {
                        Log.Warn("Texture file not found: " + file);
                        return;
                    }

                    var resourceStream = assembly.GetManifestResourceStream(resourceFile);
                    if (resourceStream == null)
                    {
                        return;
                    }

                    using (resourceStream)
                    {
                        this.LoadFromBitmap(textureKey, new Bitmap(resourceStream), properties);
                    }
                }
                catch (Exception e)
                {
                    Log.Error(e);
                }
            });
        }
    private void UpdateProperty(ref TextureProperties p, int x0, int y0, int x1, int y1)
    {
        if (!Game.Instance.IsLoading())
        {
            int simProperty = (int)p.simProperty;
            if (!p.updatedExternally)
            {
                TextureRegion texture_region = textureBuffers[simProperty].Lock(x0, y0, x1 - x0 + 1, y1 - y0 + 1);
                switch (p.simProperty)
                {
                case Property.StateChange:
                    UpdateTextureThreaded(texture_region, x0, y0, x1, y1, UpdateStateChange);
                    break;

                case Property.GasPressure:
                    UpdateTextureThreaded(texture_region, x0, y0, x1, y1, UpdatePressure);
                    break;

                case Property.GasColour:
                    UpdateTextureThreaded(texture_region, x0, y0, x1, y1, UpdateGasColour);
                    break;

                case Property.GasDanger:
                    UpdateTextureThreaded(texture_region, x0, y0, x1, y1, UpdateDanger);
                    break;

                case Property.FogOfWar:
                    UpdateTextureThreaded(texture_region, x0, y0, x1, y1, UpdateFogOfWar);
                    break;

                case Property.SolidDigAmount:
                    UpdateTextureThreaded(texture_region, x0, y0, x1, y1, UpdateSolidDigAmount);
                    break;

                case Property.SolidLiquidGasMass:
                    UpdateTextureThreaded(texture_region, x0, y0, x1, y1, UpdateSolidLiquidGasMass);
                    break;

                case Property.WorldLight:
                    UpdateTextureThreaded(texture_region, x0, y0, x1, y1, UpdateWorldLight);
                    break;

                case Property.Temperature:
                    UpdateTextureThreaded(texture_region, x0, y0, x1, y1, UpdateTemperature);
                    break;
                }
                texture_region.Unlock();
            }
            else
            {
                switch (p.simProperty)
                {
                case Property.Flow:
                    externallyUpdatedTextures[simProperty].LoadRawTextureData(externalFlowTex, 8 * Grid.WidthInCells * Grid.HeightInCells);
                    break;

                case Property.Liquid:
                    externallyUpdatedTextures[simProperty].LoadRawTextureData(externalLiquidTex, 4 * Grid.WidthInCells * Grid.HeightInCells);
                    break;

                case Property.ExposedToSunlight:
                    externallyUpdatedTextures[simProperty].LoadRawTextureData(externalExposedToSunlight, Grid.WidthInCells * Grid.HeightInCells);
                    break;
                }
                externallyUpdatedTextures[simProperty].Apply();
            }
        }
    }
            protected override void OnSceneLoad(SceneOperationKind operation, ReadOnlyDictionary <int, ObjectCtrlInfo> loadedItems)
            {
                var data = GetExtendedData();

                if (operation == SceneOperationKind.Clear || operation == SceneOperationKind.Load)
                {
                    RendererPropertyList.Clear();
                    MaterialFloatPropertyList.Clear();
                    MaterialColorPropertyList.Clear();
                    MaterialTexturePropertyList.Clear();
                    TextureDictionary.Clear();
                }

                if (data == null)
                {
                    return;
                }

                var importDictionary = new Dictionary <int, int>();

                if (operation == SceneOperationKind.Load)
                {
                    if (data.data.TryGetValue(nameof(TextureDictionary), out var texDic) && texDic != null)
                    {
                        TextureDictionary = MessagePackSerializer.Deserialize <Dictionary <int, byte[]> >((byte[])texDic);
                    }
                }
                else if (operation == SceneOperationKind.Import)
                {
                    if (data.data.TryGetValue(nameof(TextureDictionary), out var texDic) && texDic != null)
                    {
                        Dictionary <int, byte[]> importTextureDictionary = MessagePackSerializer.Deserialize <Dictionary <int, byte[]> >((byte[])texDic);

                        foreach (var x in importTextureDictionary)
                        {
                            importDictionary[x.Key] = SetAndGetTextureID(x.Value);
                        }
                    }
                }
                else if (operation == SceneOperationKind.Clear)
                {
                    return;
                }

                if (data.data.TryGetValue(nameof(RendererPropertyList), out var rendererProperties) && rendererProperties != null)
                {
                    var loadedRendererProperties = MessagePackSerializer.Deserialize <List <RendererProperty> >((byte[])rendererProperties);

                    foreach (var loadedRendererProperty in loadedRendererProperties)
                    {
                        if (loadedItems.TryGetValue(loadedRendererProperty.ID, out ObjectCtrlInfo objectCtrlInfo) && objectCtrlInfo is OCIItem ociItem)
                        {
                            foreach (var renderer in ociItem.objectItem.GetComponentsInChildren <Renderer>())
                            {
                                if (renderer.NameFormatted() == loadedRendererProperty.RendererName)
                                {
                                    RendererPropertyList.Add(new RendererProperty(GetObjectID(objectCtrlInfo), loadedRendererProperty.RendererName, loadedRendererProperty.Property, loadedRendererProperty.Value, loadedRendererProperty.ValueOriginal));
                                    SetRendererProperty(renderer, loadedRendererProperty.Property, int.Parse(loadedRendererProperty.Value));
                                }
                            }
                        }
                    }
                }

                if (data.data.TryGetValue(nameof(MaterialFloatPropertyList), out var materialFloatProperties) && materialFloatProperties != null)
                {
                    var loadedMaterialFloatProperties = MessagePackSerializer.Deserialize <List <MaterialFloatProperty> >((byte[])materialFloatProperties);

                    foreach (var loadedMaterialFloatProperty in loadedMaterialFloatProperties)
                    {
                        if (loadedItems.TryGetValue(loadedMaterialFloatProperty.ID, out ObjectCtrlInfo objectCtrlInfo) && objectCtrlInfo is OCIItem ociItem)
                        {
                            foreach (var rend in ociItem.objectItem.GetComponentsInChildren <Renderer>())
                            {
                                foreach (var mat in rend.materials)
                                {
                                    if (mat.NameFormatted() == loadedMaterialFloatProperty.MaterialName && mat.HasProperty($"_{loadedMaterialFloatProperty.Property}") && FloatProperties.Contains(loadedMaterialFloatProperty.Property))
                                    {
                                        var valueOriginal = mat.GetFloat($"_{loadedMaterialFloatProperty.Property}").ToString();
                                        MaterialFloatPropertyList.Add(new MaterialFloatProperty(GetObjectID(objectCtrlInfo), loadedMaterialFloatProperty.MaterialName, loadedMaterialFloatProperty.Property, loadedMaterialFloatProperty.Value, valueOriginal));
                                        SetFloatProperty(ociItem.objectItem, mat, loadedMaterialFloatProperty.Property, loadedMaterialFloatProperty.Value, ObjectType.StudioItem);
                                    }
                                }
                            }
                        }
                    }
                }

                if (data.data.TryGetValue(nameof(MaterialColorPropertyList), out var materialColorProperties) && materialColorProperties != null)
                {
                    var loadedMaterialColorProperties = MessagePackSerializer.Deserialize <List <MaterialColorProperty> >((byte[])materialColorProperties);

                    foreach (var loadedMaterialColorProperty in loadedMaterialColorProperties)
                    {
                        if (loadedItems.TryGetValue(loadedMaterialColorProperty.ID, out ObjectCtrlInfo objectCtrlInfo) && objectCtrlInfo is OCIItem ociItem)
                        {
                            foreach (var rend in ociItem.objectItem.GetComponentsInChildren <Renderer>())
                            {
                                foreach (var mat in rend.materials)
                                {
                                    if (mat.NameFormatted() == loadedMaterialColorProperty.MaterialName && mat.HasProperty($"_{loadedMaterialColorProperty.Property}") && ColorProperties.Contains(loadedMaterialColorProperty.Property))
                                    {
                                        var valueOriginal = mat.GetColor($"_{loadedMaterialColorProperty.Property}");
                                        MaterialColorPropertyList.Add(new MaterialColorProperty(GetObjectID(objectCtrlInfo), loadedMaterialColorProperty.MaterialName, loadedMaterialColorProperty.Property, loadedMaterialColorProperty.Value, valueOriginal));
                                        SetColorProperty(ociItem.objectItem, mat, loadedMaterialColorProperty.Property, loadedMaterialColorProperty.Value, ObjectType.StudioItem);
                                    }
                                }
                            }
                        }
                    }
                }

                if (data.data.TryGetValue(nameof(MaterialTexturePropertyList), out var materialTextureProperties) && materialTextureProperties != null)
                {
                    var loadedMaterialTextureProperties = MessagePackSerializer.Deserialize <List <MaterialTextureProperty> >((byte[])materialTextureProperties);

                    foreach (var loadedMaterialTextureProperty in loadedMaterialTextureProperties)
                    {
                        if (loadedItems.TryGetValue(loadedMaterialTextureProperty.ID, out ObjectCtrlInfo objectCtrlInfo) && objectCtrlInfo is OCIItem ociItem)
                        {
                            foreach (var rend in ociItem.objectItem.GetComponentsInChildren <Renderer>())
                            {
                                foreach (var mat in rend.materials)
                                {
                                    if (mat.NameFormatted() == loadedMaterialTextureProperty.MaterialName && mat.HasProperty($"_{loadedMaterialTextureProperty.Property}") && TextureProperties.Contains(loadedMaterialTextureProperty.Property))
                                    {
                                        int texID = operation == SceneOperationKind.Import ? importDictionary[loadedMaterialTextureProperty.TexID] : loadedMaterialTextureProperty.TexID;
                                        MaterialTextureProperty newTextureProperty = new MaterialTextureProperty(GetObjectID(objectCtrlInfo), loadedMaterialTextureProperty.MaterialName, loadedMaterialTextureProperty.Property, texID);
                                        MaterialTexturePropertyList.Add(newTextureProperty);
                                        SetTextureProperty(ociItem.objectItem, mat, newTextureProperty.Property, newTextureProperty.Texture, ObjectType.StudioItem);
                                    }
                                }
                            }
                        }
                    }
                }
            }
Esempio n. 12
0
        private void OnTexturePropertiesJustifyClicked(TextureProperties textureProperties, TextureProperties.JustifyType justifyType)
        {
            if (CurrentSolidManipulationMode == SolidManipulationMode.Vertex)
            {
                return;
            }

            // collect solid and group map objects
            CustomOperation setSolidPropertiesOperation = new CustomOperation();

            setSolidPropertiesOperation.OnMapObjectGroup = group =>
                                                           group.MapObjectList.ForEach(m => m.PerformOperation(setSolidPropertiesOperation));
            AABB extends = new AABB();

            // create extends for all selected faces if treat as one is true
            if (textureProperties.TreatAsOne)
            {
                CustomOperation createExtendsOperation = new CustomOperation();
                createExtendsOperation.OnMapObjectGroup = group =>
                                                          group.MapObjectList.ForEach(m => m.PerformOperation(createExtendsOperation));
                createExtendsOperation.OnSolid = solid =>
                {
                    solid.Faces.Where(f => f.Selected).ToList()
                    .ForEach(face =>
                    {
                        IEnumerable <Vector3> points = solid.GetVerticesForFace(face).Select(v => v.Position);
                        extends.Grow(points);
                    });
                };

                Selection.PerformOperation(createExtendsOperation);
            }

            setSolidPropertiesOperation.OnSolid = solid =>
            {
                solid.Faces.Where(f => f.Selected).ToList().ForEach(face =>
                {
                    // create extends for this face if Treat as one is false
                    if (!textureProperties.TreatAsOne)
                    {
                        extends.Reset();
                        extends.Grow(solid.GetVerticesForFace(face).Select(v => v.Position));
                    }

                    switch (justifyType)
                    {
                    case TextureProperties.JustifyType.Fit:
                        solid.FitTextureToExtendsForFace(extends, face);
                        break;

                    case TextureProperties.JustifyType.Top:
                    case TextureProperties.JustifyType.Bottom:
                    case TextureProperties.JustifyType.Left:
                    case TextureProperties.JustifyType.Right:
                    case TextureProperties.JustifyType.Center:
                        solid.AlignTextureToExtendsForFace(extends, face, justifyType);
                        break;
                    }

                    solid.CalculateTextureCoordinatesForFace(face, true);
                });
            };
            Selection.PerformOperation(setSolidPropertiesOperation);

            SceneDocument.IsDirty = true;
            UpdateUserInterface();

            RenderViewports();
        }