Need help with simple 3D shading

For my game, I’m using this code (taken from the 3d labs) to set up my meshes.

http://pastebin.com/4faU34hi

I don’t need anything complicated with reflections and all that, I just need to know how I can change the tint of each side individually.

Here is an example that I pulled off of google images

http://www.bittbox.com/wp-content/uploads/2007/02/illustrator_3d_11.png

The code DOESN’T need to be in “shader format”, I just need to know how to change each side’s color (fill) individually.

Any help is appreciated!

Usually you have to define the ‘normal’ vector to each tile, V.
Then you have to define an illumination vector V0.
These vectors should be nromalized.
Then you compute for each tile the dot product dp = V0:dot(V).
If dp <0 then dp =0.
Then you change the color of you vertext simply multiplying the colors by dp.
Your object will be correctly shaded.

@Jmv38 - I’ve been looking for the simplest possible example of this, do you have one?

I’ll check what i have.

@Jmv38 - I can calculate normals, it’s creating the diffuse effect in the fragment shader I’m trying to achieve simply

In my ADS lighting class http://twolivesleft.com/Codea/Talk/discussion/2379/ads-lighting-class#Item_1 if you look in the gist at the shaders down the bottom there are examples.

I probably do a few things more complicated than needed, but lines 251-253 is the diffuse bit of the non-textured example:

 float fDiffuseIntensity = max( c_zero, dot( curNormal, vLightDirection ));
 
    lowp vec4 vDiffuseColor = curCol * lightColor * fDiffuseIntensity * vDiffuseMaterial;

If your light is just white you can omit the lightColor. And curNormal and vLightDirection need to be in the same orientation. I have some complexity to this because I transform light normalised against each triangle and the normal for a flat triangle is then always 90 degrees, but you can instead transform your normals to the same space as the light instead.

@spacemonkey- what do you mean by “curNormal and vLightDirection need to be in the same orientation”?

Just to make things clear, I can do the math, I just need to know how to change the colors of the sides individually. I don’t need the light source or anything else, just how I change each side. Thanks for all the help!

In that case, make an image with all the different images you will need for the various sides, next to each other in the same image. Make this the mesh texture, then map the texture for each side to the part you want to use for it.

@1980geeksquad (pffffew! You should use a shorter alias, this is an ipad keyboard… ;-). ) To change the color of a mesh vertex, check the mesh.color(i) function. This is used to put shadows to a mesh.

The shade needs to be able to change. I’m not just mapping out a house or something, just trying to add a nice effect as the cubes are rotating. Sorry if I’m not clear enough.

The idea is that you have the camera as the light source, and the sides of the block get darker as their angle to the camera increases (like the picture I posted in the first comment).

I just need to know how to change the fill (or tint) during gameplay. Sorry if I’m not clear.

Check my post, simultaneous with yours!

How will that help me change the color of each vertex? Sorry, Im kinda new at this.

Have checked this function in codea built in doc?

If you know the maths, that should be enough?

I got it now! Thank you for the help

I was totally going to dig out my shader demo from my graphics class 6 years ago… except now I don’t remember where all those files are. =)

Glad you got it. Shaders are awesome, once you wrap your mind around the concept.

Great… It worked when I did two sides but once I added the third side, it said that the color was out of bounds for the first one… I tried removing the third one, and it is still “out of bounds”.

for i=1,6 do
    ms:color(i,0,0,255,255)
end

This is what I did… Any help?

Never mind. I got rid of ms:setColors. That was the problem

I got the colors to change, but they don’t update as the variables change. They are set in place when the mesh is created. Is there a way that I can re-call just the color part so the colors update?