Jump to content

  • Log In with Google      Sign In   
  • Create Account


Unity raytracer

Posted by , 26 March 2014 - - - - - - · 743 views

Unity raytracer Messing about with Unity, wrote a little raytracer - quite easy as Unity does all the hard work!

Have a bash... stick a few spheres, planes, lights in a scene then stick this script on an object:
using UnityEngine;
using System.Collections;

public class Tracer : MonoBehaviour
	public Color AmbientLight;
	[Range(0.0f, 1.0f)]
	public float Reflectivity;

	private Texture2D Texture;
	private Light[] Lights;

	void Start()
		float ratio = Screen.height / (float)Screen.width;
		Texture = new Texture2D(256, (int)(ratio*256));

		Lights = GameObject.FindObjectsOfType<Light>();
	void OnGUI()
		GUILayout.BeginArea(new Rect(16, 8, 256, 64));
		if (GUILayout.Button("Trace"))

		GUI.DrawTexture(new Rect(16, 48, Texture.width, Texture.height), Texture);

	private void DoTrace()
		float dx = 1/(float)(Texture.width);
		float dy = 1/(float)(Texture.height);
		Vector3 vec = new Vector3();
		for (int y=0 ; y<Texture.height ; y++)
			vec.y = y * dy;
			for (int x=0 ; x<Texture.width ; x++)
				vec.x = x * dx;
				Ray ray = Camera.main.ViewportPointToRay(vec);
				RaycastHit hit;
				if (Physics.Raycast(ray, out hit))
					Color clr = AmbientLight;
					foreach (var l in Lights)
						clr += ColorAtPoint(hit.point, hit.normal);

						if (Reflectivity > 0.0f)
							var refl = Vector3.Reflect(ray.direction, hit.normal);
							RaycastHit hit2;
							if (Physics.Raycast(hit.point, refl, out hit2))
								clr += ColorAtPoint(hit2.point, hit2.normal) * Reflectivity;
					Texture.SetPixel(x, y, clr);
					Texture.SetPixel(x, y, Color.black);

	private Color ColorAtPoint(Vector3 aPos, Vector3 aNormal)
		Color clr = new Color();
		foreach (var l in Lights)
			if (!l.gameObject.activeInHierarchy)

			var dirToLight = (l.transform.position - aPos).normalized;
			float NdotL = Vector3.Dot(aNormal, dirToLight);
			if (NdotL > 0)
				if (!Physics.Raycast(aPos, dirToLight))
					float dist = Vector3.Distance(aPos, l.transform.position);
					float v = (dist/l.range)+1;
					float intensity = l.intensity / (v * v);
					clr += l.color * NdotL * intensity;
		return clr;

Once is happenstance...

Posted by , 28 June 2013 - - - - - - · 813 views
Chaos, Remakes, Unity, Nostalgia
Twice is coincidence, so Mr Fleming wrote.

I love Chaos. The game that is - that old Speccy turn-based peculiarity.

Mr Gollop is one of my programming heroes. His work probably influenced me more than any other to take up programming; games programming in particular. I used to dream up great new ideas that I'd add to Chaos when I became a programmer. One of the first 'proper' programs I wrote allowed you to hack into Chaos and change the names, graphics and attributes of the creatures and spells. 'Death to hackers' indeed ;) Speaking of which, the Rebelstar series wasn't too bad either - that game (along with Laser Squad and XCom after it) sealed my adoration of the TBS genre for good. I remember nearly killing myself racing round my paper round, desperate to get home and play it yet again.

So to the point. Whenever I get a new device nowadays, one of the first things I do is try and get Chaos running on it somehow. I've just installed a version on my Android phone in fact.
So a few months ago I came to thinking... there's nothing new been added to the game by the 'remakes', why don't I follow my dream of years ago and do a remake myself to take advantage of the new mobile revolution? It surely can't go as badly as my last attempt at a remake can it now? It's been almost 30 years since the original Chaos after all, surely Mr Gollop has moved on...


Somebody above is telling me to stop attempting remakes. I'll just leave this here...

Posted Image

Well well well...

Posted by , 08 October 2012 - - - - - - · 836 views

The official Carrier Command remake is finally released. It took them 6+ years with a smallish team, and the results... well let's see.

'Vulcan is now an enemy island!'

Posted by , 09 May 2009 - - - - - - · 2,755 views

The Carrier 2 remake is now dead. Gone. Buried.
With the intended release of an official Carrier Command game from the new IP holders, I can't really continue with my friendly remake.

I do intend to take what I have, hopefully get more people on it, bypass any IP issues and get something released. Eventually.
And with the planned modding ability, it's not beyond imagining that someone could come up with a mod that resembles a certain other Carrier commanding game ;)

Carrier2 has served me exceptionally well in it's original purpose: a way of learning 3d graphics and games-related programming in general. Looking back at the first screenshots I have to have a little chuckle :)
I think I opened it up publically far too early as you all know - it's taken quite some time, though as games programmers know all too well, once a game is 90% finished, you still have the other 90% to get done...
I've been astounded by the enthusiasm people still have for the game, and the support I've had from literally hundreds of people all over the world.

Old-timers check your inbox... old timers I may have forgotten, send me a mail!


Posted by , 10 March 2009 - - - - - - · 844 views

Gratuitous C2 screenie that I quite liked, showing the enigmatic Walrus.

You can just see how the procedural placement of the structure foundations allows the terrain to poke through and look silly sometimes ;)
You may also notice how the framerate has halved due to the FBO stuff. Still investigating that one :( Suspect maybe the render buffer texture format...


Posted by , 10 March 2009 - - - - - - · 544 views

OK so Carrier2 doesn't like ATi gfx cards. Or the other way round. Either way I'm in the process of rebuilding my spare PC with a rather ageing but still robust Radeon 9800 Pro :)

In other news, I picked up a little 'medieval' RPG-style game called 'Mount & Blade' on steam some weeks ago. This project started out as a man & wife team editing a 3D demo. Several years later (with a few more members added) this small team, unconstrained by publishers and commercial needs, has created something that IMO surpasses some commercial games in parts (the brilliant combat system being the main one).
At first I was disappointed that there was no continuous world 'map', but after a while this really didn't matter to me. It actually reminds me of Lords of Midnight mixed with Elite on horseback - simple on the surface but quite deep too.
I'm from the school that regards complex games like Total War etc as superior, and yet I've played this little gem for much longer than those already (hey, I need *some* time off C2 hackage). Helping that, it feels like you can dip in & out of the game for a short play and still make meaningful progress. I just hope I can emulate this kind of approachable gameplay in Carrier...
Anyway I recommend you give the demo a try if you value gameplay and want to support a small indie dev team.

Into the shadows...

Posted by , 01 March 2009 - - - - - - · 526 views

So while I wait for a physics fix-up, I thought I'd take a look at that scary omnipresent todo-list item casting a dark shadow on my thoughts - erm... shadows. As is becoming usual with these game dev things, getting an initial implementation working wasn't nearly as hard as I thought it would be (umm standing on the shoulders of giants & all that). It's just that they're, well, ugly as sin really, with the requisite 'surface acne' and blocky low-res shimmery-ness close up. Using a huge shadow map texture (1024-2048 square!) close to the camera helps somewhat, though I've not done anything too clever with the light view and frustum yet (enter cascade shadow maps or similar).
Also, using glCopyYadder to capture the scene depth buffer was being limited by the app window size, so I've been forced to make the jump to using frame buffer objects too - delving into a rather arcane and temperamental part of OpenGL (boy is DirectX looking (slightly) more attractive nowadays, especially with the Longs Peak debacle. /me remembers back to my first blog comment...)
Getting the shadows integrated (I have to sample the shadow texture in each shader) also revealed errors in the ambient lighting calculations in the terrain and mesh shaders, which was nice.

Anyway, nuff yacking, here's how shadows change the look of the old gfx... (yep: could do with some textures about now I think). I'm now looking at overcoming the various idiosynchrasies as we speak. Ysaneya's journal has been quite helpful here describing his journey through the same territory.

Vegetative vagiaries

Posted by , 27 November 2008 - - - - - - · 845 views

Argh - I is demoted! Lost my GDNet+ status due to my paypal details becoming obsolete. Was a shock to see a blue avatar icon next to my name and be unable to post here! Fixed now :)

Had a bit of a bad time with my chosen Lua binding lib LuaPlus recently - it seems I can't trap any meaningful error output when calling Lua functions from C++. This makes development a nightmare, and almost impossible for 3rd parties to write mods. Plus it looks like no-one uses LuaPlus any more as it's too old/unsupported, so I may have to migrate to a different solution there :(

Added level-of-detail facilities to the model & rendering system. Not essential at the moment, but will be once the world/model detail ramps up, and I want to increase the island mesh resolution and size too at some point (bigger, more detailed islands :)).
Model meshes tagged in Blender as a LOD level are auto-extracted and used when rendering at a distance.
Also added support for specifying mesh 'render flags' in the modeller: i.e. setting polygons to be two-sided, alpha-tested and unlit.
To test both of these new additions I added the first vegetation to the world - a nice fir tree in keeping with the original's unusual planting scheme.

Ah refactorisation. The structure hierarchy has had yet another revamp, and this time I’m happy with it at last! I'd strayed from the path a bit regarding the cells as a different class, so now it's back in the heirarchy deriving from a Structure and all is well. Now the basic objects/structures in the world are fully recursive, so I could build a cell base, build a structure on it, and build a defense turret on that, etc. The individual cells are like mini ACCB's and build their own supporting structures at the site. All these spawn/build instructions and the ACCB deployment have been moved over to lua so are fully customisable.
Work has been done on the structure placement process too, as it was simply some basic suitability checks on land height/slope. Now I need to evaluate a 'placement score' for a given position, maybe relaxing the score requirements after some searching so we can guarantee that essential structures always get placed somewhere. Might have to alter the island geometry in real-time to ensure gameplay requirements are met though - will have to see how it goes.

So yes, gameplay progression has been somewhat stalled, but hope to get back on track...

October 2016 »

23242526 27 2829

Recent Entries

Recent Comments

Recent Entries

Recent Comments

Latest Visitors