Tuesday, 25 February 2014

Unity3D: Scrolling 2D Background

I came across a Facebook Unity3D user group post where someone was having an issue with a scrolling 2D background. The OP was doing the scrolling programmatically and was ending up having a gap appear in the background. I did exactly the same thing a while back, but found that a better way of doing this in Unity3D was to use Animators and Animation Clips.
So, here is a tutorial on how to do this, Ill start off creating a 2D project in Unity3D
image
We are now going to create a Texture\Backgrounds folder
image
I have a number of background textures, that I am going to use:
BGForeGround_1_1BGForeGround_1_2BGForeGround_1_3
But, for now we will work with the first texture, we will get to the others in a little while. First thing we need to do is make sure that out images are imported as sprites.
image
Next thing we are going to do is set up the sprite on the screen in relation to the camera, so just drag and drop the first image into the scene.
image
Now, we are going to set the textures position to 0,0,1 and stretch it to fit the full view of the camera, so we will scale it to 1.2, 2.4,1
image
Now, make a copy of it, and paste it into the scene, line it up with the edge of the first texture by setting the position to 19.125,0,0
image
Create an empty GameObject, name it Background and in the Hierarchy drag the two images we have just set up in the scene into it.
image
In our container GameObject, add an Animator component, so Add Component, Miscilanious and select Animator.
image
We are now going to create our Animator, first create a folder called Animation
image
In here we will add another two folders Clips and Controllers
image
Now create an Animator Controller
image
Name it BackgroundAnimation, double click it and we will see the Animator window. Now in clips, create a new Animation.
image
Name is BGScroller. Add this Animation to the BackgroundAnimation Animator Controller by dragging and dropping it in and set the speed to .125
image
image
Now, in our Background GameObject (the one that is holding our sprites) set the Animator to use the new BackgroundAnimator we just set up.
image
With the Background GameObject selected open up the Animation Window
image
Select Add Curve, select Transform and Position
image
Expand this out, to show the elements of the position element, drag the far right set of dots out to the 10 second mark and set the x element to -19.125.
image
Now run it, it will now smoothly scroll our textures from right to left :D, BUT, after 10 seconds it stops (if played at the speed of 1) :S not what we want, so, go into the BGScroller Animation and set the Loop Time to true.
image
Run it now, and it will scroll on and on, looping lovely with no issue :)

Parallax

So, what if we want some parallax scrolling background?
Well, we just do what we did before, but we will create a different Animator Controller for each layer. First thing ill do is set the first to background z positions to 0, but set their container, Background", z to 1, this was we can set the layer order.
So, for the next layer, we add the next texture like before.
image
As before, create an empty GameObject to put them in and call it Background2 and add an Animator component to it.
image
Now in the controller folder create a new Animator Controller and call it Backgrouhd2Animation, drag and drop the same Animation clip into it, BGScroller, but we will set the speed to .25 and then set Background2’s Animator to the new controller Background2Animation.
Run it and the two sets of backgrounds scroll at different speeds :D
Now do the same for the another set of backgrounds, this time set the animation speed to .5, then we will create another empty GameObject called Backgrounds, and put all three of our container GameObjects in it.
image
So, this is what it will look like
You can get the project for this here

Tuesday, 11 February 2014

Direct3D Rendering Cookbook By Justin Stenning

Packt Publishing have asked me to review another book, the Direct3d Rendering Cookbook

I didn’t know where to post this, my many blogs are either XNA (C#), DX11 C++ or Unity3D, or a combination of the three lol, anyway, decided as this is my most recent blog, to post it here, hope it’s not too off topic :P

7101OT_Direct3D Rendering Cookbook

One of the first things I noticed and liked as a .NET developer is that it’s for C# developers using SharpDX. I have never used SharpDX before, have played a little with SlimDX when I was helping out on the ST: Excalubur project (which you should check out, the guys are doing an amazing job with it, check out the Youtube channel too)

Chapter 1

The first chapter lays out the fundamental’s for getting up and running, like most books of this ilk as well as going into the graphics pipeline, and it’s pretty in depth I have to say. The chapter then covers creating your first project using the SharpDX library with great detail and a fair bit of depth.

Chapter 2

Goes into great detail again on setting up your code to render 3D, again, this is quite in depth and a lot of info is given. What I do find odd is that the creation of the renderer code is described as a recipe, I know there are a number of ways to do this, but I would not have had this as a recipe as it’s pretty fundamental to you rendering full stop, it’s something you should have in place, and this chapter should really just be a way of doing it. To me, its more of a preparation step than a recipe…None the less, there is great detail again here.

Chapter 3

We are then given recipes on rendering meshes, we start of creating the mesh in code, then look into some simple lighting techniques and texturing and thankfully (as I think this tends to get missed out a fait bit) it gives and example of loading a mesh from file, and what I do like is that Blender3D is used :D Great stuff..

Chapter 4

Then moves on to skinned meshes, again, this is a great chapter and full of detail :)

Chapter 5

Looks at Hardware Tessellation, some great detail here too, the diagrams are great at for describing the pipeline. It goes on to describe tessellating bicubic Bezier surfaces, refining a mesh with Phong tessellation and loads more.

Chapter 6

Covers normal and displacement mapping, and again, this goes into masses and masses of detail, with some great screen shots

Chapter 7

We take a break from geometry and move on to image processing. There is some awesome stuff in here too, from desaturation, contrast and brightness to implementing box blur, gaussian blur and Sobel edge detection and luminance histograms.

Chapter 8

Covers using a physics engine, and again, I am glad to see BulletSharp being used as I have used it in my own engines, including my XNA Illuminati deferred lighting engine. This chapter also covers rendering waves (personally prefer this being done as a post process) and instanced particles, again another topic close to my heart as again I use them in my own engine.

Chapter 9

Now goes even deeper with rendering on multiple threads and deferred contexts, now, due to me coming from a DX9 background, this was very interesting.

Chapter 10

Moves onto Deferred Rendering, another topic I love having implemented it in XNA :)  Again, masses and masses of detail here, this book has really got me wanting to start writing my own engine again lol

Chapter 11

Covers integrating Direct3D into Windows 8.1 and the use of XAML, this chapter will be of more use to me once I finally get a decent development machine. Hoping to sort that out some time this year :) Again though, lots and lost of details in here.

In Summary…

I loved this book, even if you are not a C# developer, you can port/include these techniques to/in your C++ engine. If you are just starting out, this may not be the book for you, if you have been working with earlier versions of DX and looking for an interesting read, then I don’t think you can go wrong with this book.

I have reviewed a few books by Packt Publishing, and I have to say, they have tended to turn up short, however, the last two books I have reviewed have been truly excellent, keep up the good work :) Check out my last review here.

Where can I get this book?

here.