Thursday, 30 July 2015

Launched new website for game developers. Quality materials & Textures.

I launched recently new website - http://www.gamesmaterials.com which main purpose is to store high quality materials ready to be used in Unity game engine and UDK and Cry Engine in the future.

At the moment there are 14 free and premium materials but I have already created over 50 materials that can be purchased from Unity asset store in one bundle : http://u3d.as/gq7

Any feedback and suggestion will be much appreciated. I will introduce very competitive subscription payment in the future and more free stuff as well as full projects.

Some finished examples:

- simple low poly cylinder with cobble material (4 textures - diffuse, occlusion, normal and height)




- Plane with window blocks material


- Plane with Destroyed concrete bunker material





Wednesday, 29 July 2015

Exporting HTML to PDF in ASP.NET MVC C#

In this tutorial we are going to set up dev environment to use html to pdf converter and convert previously saved wikipedia page to pdf.

1. Download and install application from http://wkhtmltopdf.org/ - FREE to use
2. Create new Asp.NET MVC application.
3. Add location to exe file in your web config file(this is installation path for previously installed wkhtmltopdf) - something like that :

 <add key="HtmlToPdfPath" value="C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe"/>


4. Download  https://en.wikipedia.org/wiki/CERN page and save it in My documents folder under PdfExample folder.



5. Create new api controller and call it HtmlPdf.
6. Alter Get(int value) web api method :

public string Get(string name)
        {
            string path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "PdfExample");

            if (File.Exists(string.Format("{0}\\{1}", path, name)))
            {
                try
                {
                    string nameWithoutExtension = name.Replace(".html", string.Empty);
                    var psi = new ProcessStartInfo(System.Configuration.ConfigurationManager.AppSettings["HtmlToPdfPath"], string.Format("{0} {1}.pdf", name, nameWithoutExtension));
                    psi.WorkingDirectory = path + "/";

                    var proc = Process.Start(psi);
                    proc.WaitForExit();

                    return "completed";
                }
                catch (Exception ex)
                {
                    return ex.InnerException.ToString();
                }
                
            }
            else
            {
                return "Failed to convert";
            }


        }
}

7. Run your application and open url:

http://localhost:yourport/api/htmlpdf?name=CERN.html

8. In your folder PdfExample you can see pdf version of Cern wikipedia html website.


Wednesday, 22 July 2015

Rendering Grid, Canvas into image in WPF application

In tutorial regarding populating images from local storage in wpf application we displayed images in grid. In this tutorial I want to demonstrate how to easily convert part of grid, canvas to bitmap and save it as jpeg image.

1. Modify XAML code from the previous tutorial. It should look like that.
 <Grid x:Name="MainFrame" ShowGridLines="True">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition Height="40"/>
        </Grid.RowDefinitions>
      
        <Grid x:Name="Photo1" Grid.Row="0" Margin="10" Grid.RowSpan="1"/>
        <Grid x:Name="Photo2" Grid.Row="1" Margin="10" Grid.RowSpan="1"/>
        <Grid x:Name="Photo3" Grid.Row="2" Margin="10" Grid.RowSpan="1"/>


          <Grid x:Name="PrintFooter" HorizontalAlignment="Right" Height="75" VerticalAlignment="Bottom" Width="100" Grid.Row="3">
            <Button Content="Print"  VerticalAlignment="Bottom" Margin="10" Width="75" Click="Button_Click_1" />
        </Grid>
       
    </Grid>

2. Write a method for Button_Click_1 - you can rename the component and method.


private void Button_Click_1(object sender, RoutedEventArgs e)
        {

// Hide print footer

            PrintFooter.Visibility = Visibility.Hidden;
            MemoryStream memstream = GenerateImage(MainFrame, (int)MainFrame.ActualWidth, (int)MainFrame.ActualHeight);


            // save Image

            using (FileStream fstream = File.OpenWrite(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), string.Format("RemotePhoto\\{0}.{1}", Guid.NewGuid(), ImageFormat.Jpeg))))
            {
                memstream.WriteTo(fstream);
                fstream.Flush();
                fstream.Close();
            }
// Show print footer

            PrintFooter.Visibility = Visibility.Visible;
        }

public MemoryStream GenerateImage(Visual visual, int width, int height)
        {
            BitmapEncoder encoder = new JpegBitmapEncoder();
           
            if (encoder == null) return null;

            RenderTargetBitmap rtb = this.RenderVisualToBitmap(visual, width, height);
            MemoryStream file = new MemoryStream();
            encoder.Frames.Add(BitmapFrame.Create(rtb));
            encoder.Save(file);

            return file;
        }

public RenderTargetBitmap RenderVisualToBitmap(Visual visual, int width, int height)
        {
            RenderTargetBitmap rtb = new RenderTargetBitmap
                (width, height, 96, 96, PixelFormats.Default);
            rtb.Render(visual);
           
            BitmapSource bsource = rtb;
            
            return rtb;
        }



GenerateImage method will accept Visual component which in our case is grid. Using RenderVisualToBitmap method we are going to create RenderTargetBitmap object from Visual component.

New image should appear in your pictures under RemotePhoto folder, you can of course change the location as next print you make will include the image from the results which might look odd.






Thursday, 16 July 2015

Realtime Material Previewer - Unity 3d - Available Now For FREE



Sample materials used from http://u3d.as/gq7
Get single game materials - http://www.gamesmaterials.com
Get Free Material Previewer for Unity 5 - http://u3d.as/hZh

Free tool to allow user to preview materials in realtime in Unity 3d game engine.

Just drag and drop created materials into script and they will automatically appear in the preview dropdown list.

This tool allows you to change light intensity, day & night transition speed, normal, occlusion, specularity and height volume.

You can tweak X & Y tiling as well.






Wednesday, 15 July 2015

Populating images from local storage into WPF application

1. Create new WPF Application.
2. In your main window xaml add 3 grids, your code should looks like this:

<Grid x:Name="MainFrame" ShowGridLines="True">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
      
        <Grid x:Name="Photo1" Grid.Row="0" Margin="10" Grid.RowSpan="1"/>
        <Grid x:Name="Photo2" Grid.Row="1" Margin="10" Grid.RowSpan="1"/>
        <Grid x:Name="Photo3" Grid.Row="2" Margin="10" Grid.RowSpan="1"/>
</Grid>

3. Open code behind the file and create new function:

private void PutImagesIntoFrame()
        {
            // Get last 3 images saved

            string path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "RemotePhoto");

            var directory = new DirectoryInfo(path);

            int numberOfImages = 3;

            List<fileinfo> images = directory.GetFiles("*.jpg").OrderByDescending(m => m.LastWriteTime).Take(numberOfImages).ToList();

            List<imagebrush> imageBrushes = new List<imagebrush>();

            if (images.Count == numberOfImages)
            {
                foreach (FileInfo item in images)
                {
                    ImageBrush imageBrush = new ImageBrush();
                    Image image = new Image();
                    image.Source = new BitmapImage(
                      new Uri(item.FullName));

                    imageBrushes.Add(imageBrush);
                    imageBrush.ImageSource = image.Source;

                }
            }

            // Add image brushes to individual grids

            Photo1.Background = imageBrushes[0];
            Photo2.Background = imageBrushes[1];
            Photo3.Background = imageBrushes[2];

        }
4. Add some pictures to My Pictures folder inside RemotePhoto Folder.
5. Your application should looks like this.