Let there be shadow!

Shadows in Unity is something that in most cases is a given through the use of surface shaders, but sometimes you don’t want to use a surface shader for whatever reason and create your own vertex/fragment shader. The biggest advantage is that everything is in your hands now, but this is also one of the drawbacks because you now have to handle a lot of stuff that Unity conveniently handled for you in a surface shader. Among such things are support for multiple lights and shadows.

Luckily, Unity provides you the means to get this working! The catch? Documentation on this is lacking or even non-existent. I was in the same position as most people and somewhat clueless on how to get shadows in my vertex/fragment shader, I did my fair share of googling and found some clues that didn’t quit do the trick, but gave me a good impression on where to search. I also went through a compiled surface shader to see if I could figure out how they did it. All of the research combined and some trying out finally gave me the results I needed: Shadows! And now I will share it with whoever is interested.

Before I begin, I want to make note that as mentioned earlier, Unity solves a lot of cases for you when you are using surface shaders, among such things are the inner workings when you are using deferred or forward rendering. With your own vertex/fragment shaders, you will need to take that into account yourself for some cases. Truth is, I only needed to get this to work with forward rendering and only briefly tested how this works with deferred rendering and although I did not notice

By |April 16th, 2014|Categories: Game Development, Programming, Unity|7 Comments

Specialization Project: Progress!

It’s been a while since I posted an update about the progress of my specialization project. Last time I added depth of field, but more has been added in the meanwhile!

I added a particle system that handles the floating of some small debris, it’s just floating around, rotating randomly, doing it’s thing. It’s just one texture for now made by me to test it, but it will get some variation later on.

I also, or rather redid the lightshafts, the previous implementation looked nice, but due to the fact that nothing from above (not counting the occasional fish perhaps) is blocking the light source, leaving a very overexposed scene without many shafts. So instead, I did them manually. They’re simple planes with alpha. They still need to be tweaked a lot to make a nice and convincing effect, but it’s starting to look like something!

The latest thing I added are flocking (or shoaling/schooling if you want to be precise) groups of fish. The basic behavior is implemented, but they need a lot of tweaking to becoming more convincing (not to mention implementing animation for them, which will be in soon). They’re not really bound, so they still occasionally move through the ground or above the water.

Next to the above changes, I also made a distinction between upper and lower water surface, so the depth fall off (Depth of Field) is only active when you’re below the water surface and the particles stay below the water surface. Also the models have had an update!

Here’s the result so far:

By |April 26th, 2013|Categories: Game Development, General, Programming, Specialization|Comments Off on Specialization Project: Progress!

Specialization project: Depth of Field

Today I added Depth of Field to the framework.

As with the other effects, it still needs some tweaking here and there, but I think that it already gives a nice effect as it is.

By |April 10th, 2013|Categories: IGAD, Programming, Specialization|Comments Off on Specialization project: Depth of Field

Specialization project: Godrays

One step further to get to the final product. I made an implementation of God rays, or light shafts. A form of volumetric light. Implementation is done as a post process effect.

First attempt resulted in the following:

It still needs some tweaking, and the light is a overexposed, also due to the fact that there is nothing really blocking the light source and it’s a small scene. For a first attempt however, it’s looking alright in my book!

By |March 21st, 2013|Categories: Programming, Specialization|Comments Off on Specialization project: Godrays

Specialization project: Caustics

So for my specialization I decided to do something with special effects; more specifically: Underwater rendering. Underwater rendering gives me an opportunity to make different effects that makes a nice scene when combined! The first effect I did were caustics. It’s not a real-time approach, but rather a projected texture that is animated. The result is still pretty convincing:

When and if time allows it, I will also create a real-time version based on an upper surface.

By |March 10th, 2013|Categories: Specialization|1 Comment

Let’s talk physics: Elastic Collision

It has been some time since I last made a post. I have been a bit busy with college and some personal projects, but I have some time now to talk about the last subject of what I wanted to share about physics: Elastic collision.

Just like previously, let’s take a look at what Wikipedia has to say about Elastic Collision:

An elastic collision is an encounter between two bodies in which the total kinetic energy of the two bodies after the encounter is equal to their total kinetic energy before the encounter. Elastic collisions occur only if there is no net conversion of kinetic energy into other forms.

During the collision of small objects, kinetic energy is first converted to potential energy associated with a repulsive force between the particles (when the particles move against this force, i.e. the angle between the force and the relative velocity is obtuse), then this potential energy is converted back to kinetic energy (when the particles move with this force, i.e. the angle between the force and the relative velocity is acute).

To fully understand this, you must understand what kinetic energy is. I’ll let you follow the link in the Wikipedia quote if you want a broad explanation on the matter, but for the most basic explanation, you can think of it that every moving object has energy due to its motion. This energy can be transferred to another object on impact and that is exactly what we are going to do here.

To calculate the new motion of an object after collision, we will need to know at least the mass and the current velocity. Let’s initialize some spheres for our simulation:

In here I simply put some random position values

By |March 29th, 2012|Categories: Physics, Programming|Comments Off on Let’s talk physics: Elastic Collision

Let’s talk physics: Soft Body Dynamics

Welcome back to another post about physics and this time I will be talking about Soft Body Dynamics. Soft Body Dynamics can be done in several approaches, the one I am going to discuss here is the Mass-Spring approach. Just like last time I’m going to take a look at what wikipedia has to say about these 2 topics:

About Soft Body Dynamics:

Soft body dynamics is a field of computer graphics that focuses on visually realistic physical simulations of the motion and properties of deformable objects (or soft bodies). The applications are mostly in video games and film. Unlike in simulation of rigid bodies, the shape of soft bodies can change, meaning that the relative distance of two points on the object is not fixed. While the relative distances of points are not fixed, the body is expected to retain its shape to some degree (unlike a fluid). The scope of soft body dynamics is quite broad, including simulation of soft organic materials such as muscle, fat, hair and vegetation, as well as other deformable materials such as clothing and fabric. Generally, these methods only provide visually plausible emulations rather than accurate scientific/engineering simulations, though there is some crossover with scientific methods, particularly in the case of finite element simulations. Several physics engines currently provide software for soft-body simulation.

About Mass-Springs:

In this approach, the body is modeled as a set of point masses (nodes) connected by ideal weightless elastic springs obeying some variant of Hooke’s law. The nodes may either derive from the edges of a two-dimensional polygonal mesh representation of the surface of the object, or from a three-dimensional network of nodes

By |October 19th, 2011|Categories: Physics, Programming|2 Comments

Let’s talk physics: Orbital Motion

Welcome back to a new post about physics. Last time I talked about Ballistic Motion in it’s simplest form: Dropping a ball under the influence of gravity. This time I will explain Orbital motion or more specifically, the n-body problem. Just like last time, let’s take a look at what wikipedia has to say about it:

About orbit:

In physics, an orbit is the gravitationally curved path of an object around a point in space, for example the orbit of a planet around the center of a star system, such as the Solar System. Orbits of planets are typically elliptical.

Current understanding of the mechanics of orbital motion is based on Albert Einstein‘s general theory of relativity, which accounts for gravity as due to curvature of space-time, with orbits following geodesics. For ease of calculation, relativity is commonly approximated by the force-based theory of universal gravitationbased on Kepler’s laws of planetary motion

About the n-body problem:

The n-body problem is the problem of predicting the motion of a group of celestial objects that interact with each other gravitationally. Solving this problem has been motivated by the need to understand the motion of the Sun, planets and the visible stars. Its first complete mathematical formulation appeared in Isaac Newton‘s Principia (the n-body problem in general relativity is considerably more difficult).[citation needed] Since gravity was responsible for the motion of planets and stars, Newton had to express gravitational interactions in terms of differential equations. Newton proved in thePrincipia that a spherically-symmetric body can be modelled as a point mass.

So what we’ll

By |October 6th, 2011|Categories: Physics, Programming|2 Comments

Let’s talk physics: Ballistic Motion

Welcome to the first actual post on physics and in this part I’ll be talking about Ballistic Motion. Just like I did, you might think to yourself: “What is Ballistic Motion?” Let’s take a look at what Wikipedia has to say.

Ballistics (gr. βάλλειν (‘ba’llein’), “throw”) is the science of mechanics that deals with the flight, behavior, and effects of projectiles, especially bulletsgravity bombsrockets, or the like; the science or art of designing and accelerating projectiles so as to achieve a desired performance.

ballistic body is a body which is free to move, behave, and be modified in appearance, contour, or texture by ambient conditions, substances, or forces, as by the pressure of gases in a gun, by rifling in a barrel, by gravity, by temperature, or by air particles. A ballistic missile is a missile only guided during the relatively brief initial powered phase of flight, whose course is subsequently governed by the laws of classical mechanics.

If you’re not familiar with physics, this might be some information to swallow, but let’s narrow it down to what I’ll be discussing here: How can we make an object (in this case, a ball) fall and bounce on the ground. That narrows it down quite a bit doesn’t it? So what you will see here will be 2 different implementations of a ball that falls down to the ground under the influence of gravity and bounces when it hits the ground. These 2 different implementations will be the Euler Integrator and the Perfect Integrator.

Euler Integrator

So we want to make the ball bounce in the most simplest way, but still convincing. First let’s define some things

By |October 2nd, 2011|Categories: Physics, Programming|1 Comment

Let’s talk physics: Introduction

This will be the start of a 7 part blog entry (this one excluded) and as you might have guessed, I’ll be talking about physics here. Physics is part of the lecture I am following at my college (applied to game programming, more or less).

Before I start I must warn you that everything I write in the following physics related posts is, in most cases, new for me in a sense that I never really dug deep in physics before. So if you want to take my implementations and they seem incorrect, please don’t hate me as I’m just beginning to grasp the concept of physics.

As I said, this will be a 7 part entry and will contain the following topics:

  • Ballistic Motion
  • Orbital Motion
  • Soft Body Dynamics
  • Elastic Collisions
  • Rigid Body Mechanics: Rotational Physics
  • Rigid Body Mechanics: Collision Response
  • Rigid Body Mechanics: Friction

I will go through the basics and probably not all that deep, but it’s a start.

Before anyone asks, the framework for the rendering part I will be using is Ogre3D and next to my teacher’s lectures and slides, my main reference will be Game Physics, second edition by David H. Eberly and of course, the internet.

By |October 1st, 2011|Categories: Programming|Comments Off on Let’s talk physics: Introduction