If you have your own tutorials or open source and would like to share your Director knowledge please send them to info@shocksites.com. We will review them, post it and give full credit to the developer.
Current Open Source and Tutorials

Easy 3D
Provided by Adam Montandon


One of the big features of Director 8.5 is its amazing 3D ability. There are so many new features involved in 3D lingo it can be quite hard to work out what goes where, but once you get the hang of it the sky really is the limit and you’ll be knocking up Playstation quality games quite quickly. As long as you have a good understanding of lists, there is no reason why you can't pick up 3D lingo quickly. And hopefully this tutorial will help.

Here's the sort of thing you will be able to create by the end of this tutorial:


Now, topics like 3D modeling in a package like 3D Studio Max can be quite difficult to learn, and although it's preferable to use a professional package to create models and then import them in to director, you can in fact make quite interesting models using lingo alone. This is great because you don’t have to have an expensive package or spend time using it. If you know a fair deal of Lingo and you know how to use lists correctly, then 3D lingo is no problem.

Now, 3D cast members can be used in quite a different way to normal cast members. You are probably used to building things up in sprite channels, and perhaps having several objects of the screen at once. For best 3D performance you will need to run your 3D cast member "Direct to stage". And to improve performance even more, the 3D cast member should be the only thing on the stage.

This is great because it will get you into the habit of coding your games all in one frame like the experts, and it only uses one cast member in the score, so you don’t have to worry about anything in the score at all. All your 3D objects will appear within your 3D cast member. That means that instead of laying out sprite after sprite you deal with just one and only one sprite, that can display hundreds of thousands of 3D models, in fact the only limit is your machines computing power, and of course, your own imagination.

Firstly lets click on the new 3D cast member button and name the member "Scene" this is where all our action takes place. As you can see it’s blank at the moment. We will have to fill it with objects, lights and other exciting things, one step at a time.

Now we place our "Scene" into sprite channel 1 in the score. Then, in frame one, add our trusty frame script:

on exitFrame me

go to the frame

end

That’s all you need to do in the score! How about that!

Next, we will want to create a behavior for our "Scene" cast member. We will use the Beginsprite handler to initialize our lovely 3D world. The Beginsprite handler is only called once, the first time a sprite appears on stage, so we can use this to give our world all the settings it needs to be a great 3D scene.

Lets start with something simple

on beginsprite me

member("scene").resetworld()

end

Now attach your Beginsprite behavior to the "Scene" member on the stage.

The only 3d code we have used is split into 2 parts

Member("scene") --This is what we use to refer to our 3D scene.

Resetworld() -- This "cleans out" our 3d scene. 3D lingo commands stay in memory, so if we don’t reset the world our scene would be full of all the objects, textures and shaders and lights and cameras that you were using previously, so its important to get rid of them to begin with.

Next, things get a little bit complicated. A 3D object is actually comprised of a number of things, and it takes several lines of code to even make a simple box appear on the stage. However, it is just about as easy to make one box as a hundred boxes, so it takes a while to get started, but after a while it becomes incredibly fast to develop large scenes.

Lets take a look the parts we need to make a 3d Model.

  • Texture. A texture is normally a bitmap graphic taken from the cast. For example a picture of a wall or a floor.

  • Shader. A Shader is where we define how the texture will be used in a particular instance. For example, you can take several different textures and join them together, on top of each other to become one shader. You could, for example use one Texture as a base image, with a second texture (perhaps some clouds) as a reflection, and yet another texture on top of that with a lower blend, perhaps warped in a funny way. It works sort of like Photoshop’s layers for your 3d Textures, and you can build up some great effects. Or you could just simply leave your shader to be a plane texture, with no fancy effects.

  • Model Resource. This is your "blueprint" for future 3D models. This is what all your future models are based on. You can create a plane, a cube, a sphere, a cylinder or a particle effect. The idea behind this is that if you want a scene featuring thousands of boxes all the same, you only need to create one model resource of one box. Then, if you decided that your boxes aren’t tall enough, you can change the height of your model resource and every model built from that model resource gets bigger by the same amount. Clever stuff.

  • Model. Finally, the model itself. This is the individual thing in its world. The main information the model holds are its Model Resources (I.e. what shape it is) and its Shader (I.e. what colour it is)

There are of course, lots of other elements of the 3d world, but we are just looking at the basics for now. So, now we have an understanding of how it all goes together lets make a box with 2 textures (Base and sky) 1 shader 1 model resource and 1 model.

First, open the paint window and make a "base" texture. Just paint something interesting, keep it small and keep it square. Perhaps scrawl your name graffiti style. Name your graphic "Base".

Now, open the paint window again and paint some fluffy blue clouds on a light blue background. Call this graphic "Sky".

That’s it.

Now, before we begin coding, its important to remember that Director, being director, tries to do a lot of stuff for you without you asking. It actually creates one texture (called DefaultTexture) one shader (called DeafultShader) and one model resource (called Defaultmodel) These are all numbered [1] in their lists, so we are going to skip around them, and begin making new textures shaders and model resources in list position [2]. Ok?

Lets code!

This goes in our Beginsprite handler after the resetworld code.

Member("scene").newtexture("BaseTx") --Makes a new texture in memory called BaseTX its good to put TX after your textures name so you don’t get it mixed up.

Member("scene").texture[2].Member = Member("Base") -- This applies your cast member "Base" to the second texture list position (because defaultTexture takes up the number 1 spot)

Member("scene").newtexture("SkyTx") -- The same again for the sky, making a new texture in memory called SkyTX

Member("scene").texture[3].Member = Member("sky") -- Now assigning the third position in the texture list to your cast member Sky graphic.

That’s our textures! Now, lets combine them to make a shader, we’ll use the BaseTX as a base and the SkyTX as something to reflect.

Member("scene").newshader("ourshader",#standard) -- This creates a new Standard shader called "ourshader". Everything needs its name. You could give it something more descriptive if you like.

Member("scene").shader[2].textureList[1] = Member("scene").texture[2] -- Ok, here’s where it gets a bit crazy, Ourshader is shader number [2] because of directors defaultshader taking the number 1 spot. Texturelist[1] is the bottom of the photoshop-style stack of layers. So texturelist[1] of shader[2] is assigned texture[2] (that’s the baseTX)!

Member("scene").shader[2].textureList[2] = Member("scene").texture[3] -- Now, still working with Shader[2] (Ourshade) we move to the second layer of the Photoshop style layer levels Texturelist[2] and its assigned the sky ( Texture[3] )

Member("scene").shader[2].textureModeList[2] = #reflection --Now, we want the sky to be reflected off of our model so we change shader[2] (ourshade) texturemodelist property to Reflection. Note that Texturemodelist is at position [2] because that’s where the sky is in our Photoshop layer style position.

I told you you needed to know your lists!

Thankfully that’s Ourshade taken care of, and once you create it, you can use it again and again so it may appear long winded but it comes into its own on larger projects.

Next on our list is the modelResource

Member("scene").newModelResource("BoxRes",#box) --Just one easy line makes a new modelresource called BoxRes. You only need to do this once, no matter how many boxes you want.

Now, lets make our model and join it all together.

Member("scene").newmodel("OurBox", Member("scene").modelResource("BoxRes")) --This creates in memory a new model called Ourbox based on the modelresource BoxRes

Member("scene").model[1].shaderList = Member("scene").shader[2]

--this applies our shader (Shader[2]) to model[1]. Model[1] is OurBox, the box we just made.

That’s it! Compile the code and run it! You actually got a 3D Box on the screen. Ok, so it doesn’t look all that exciting until you learn how to move it.

You can experiment in the message window by typing:

Member("scene").model[1].rotate (45,45,45)

The Rotate command rotates something by an increment of the number shown in the order (X, Y, Z) So, if you type member("scene").model[1].rotate (0,1,2) it will rotate 0 degrees in the X, 1 degree in the Y and 2 degrees in the z.

Play around with rotating it at small increments like member("scene").model[1].rotate (1,1,1)

If you want to see how effective the reflections are, try changing your exitframe script to

On exitframe me

member("scene").model[1].rotate (1,1,1)

go to the frame

end

Now, THAT’S what I’m talking about.

Well done.