Cel-Shading in Blitz3D

Blitz3D Forums/Blitz3D Programming/Cel-Shading in Blitz3D

Gun Ecstasy(Posted 2004) [#1]
Is this possible?


Dock(Posted 2004) [#2]
Yep, and have been used numerous times in blitz 3D :)

http://www.blitzbasic.com/Community/posts.php?topic=30801

I'm curious as to how they would be implemented more accurately with cube-mapping though, since we now have that and people are familiar with it.


sswift(Posted 2004) [#3]
Cube mapping, unlike sphere mapping is a mapping which depends on world space, rather than camera space. So as the camera moves around you view the cube mapping form different angles, whereas you do not view a sphere map from different angles. The sphere map thus appears to move with the camera.

As the sphere map contains your light source, whose direction is implicitly encoded into the texture, if it moves with the camera, then the light source appears to move with the camera.

Cube mapping does not have this problem. If the left face of the cube map has the light source, then when the camera moves around, the light source will always appear to come from the west, regardless of which direction the camera looks at the object from.

You can generate a cube map by making a vertical 1D texture of light from light to dark, with different bands of color rather than a smooth gradient and then applying this to a sphere mesh rotated so that it's north pole points down the negative Z axis. Then you flip it inside out, and make it fullbright. Then you rotate the entity to aim your light source, just as you would a directional light, scale it up, and place your camera inside the sphere facing down positive Z, and render a cube map from inside.

That cube map now will look like cel shading when applied to an object, with a light source coming a directional light.

Making point light sources is more diffiuclt and requires re-rendering the map for every object as it changes position relative to the light.


ashmantle(Posted 2004) [#4]
Please, please, please don't just make another "black-outline-and-therefore-cel-shading" game..


Warren(Posted 2004) [#5]
I think he can do whatever he wants if he actually ships.


sswift(Posted 2004) [#6]
Cel Shading has nothing to do with Cel Outlining. Shading means changing brightness.


Dock(Posted 2004) [#7]
It's the shading which I'm most interested in capturing.

Thanks for the detailed explanation Shawn, that definitely helps a lot and I'll be sure to give that a shot with my next project. Is there any examples of this already? It makes sense that the cube map would be limited to directional lights, but I can definitely cope with that restriction. Thanks again. ^_^


Gun Ecstasy(Posted 2004) [#8]
That's pretty clever or an outline. Load the mesh with textures, then load the mesh again without textures, change color to black, flip, the scale. Pretty damn clever.

But an outline has nothing to do with Cel-Shading. Cel-Shading requires vast knowledge of normals, lighting, shading, and some other stuff.


JaviCervera(Posted 2004) [#9]
You don't have to scale the mesh, it will look ugly. You have to EXTRUDE the mesh, which is move every vertex in the direction it is facing. If i remeber right, there was a code snippet to do it.


jhocking(Posted 2004) [#10]
Thanks for the explanation sswift. I have no immediate plans to do a game with cel shading, but I think I'll try what you described to play with cel shading using cube mapping.


Gun Ecstasy(Posted 2004) [#11]
"You don't have to scale the mesh, it will look ugly. You have to EXTRUDE the mesh, which is move every vertex in the direction it is facing. If i remeber right, there was a code snippet to do it."

Would that not be exactly the same as scaling? If that's not what blitz's scaling does then there must be something wrong with it.


Warren(Posted 2004) [#12]
If the mesh is convex, scaling is the same as extruding.

Otherwise, no.


jhocking(Posted 2004) [#13]
Extruding is not the same thing as scaling. It's complicated to explain the difference (they are similar) so just trust us all that he is correct, what you would want to do is extrude the mesh and not simply scale it.

Oh, and thus Blitz's scaling works perfectly, it's just that scaling is not what you'd want in this situation.


Warren(Posted 2004) [#14]
Sorry, to amend my post ... If the mesh is convex and the origin is in the exact center, scaling is the same as extruding.

Any other situation, and they are very different things.


AntonyWells(Posted 2004) [#15]
Not that I've really ever tried cel shading, but if you want to extude a tri in the direction it's facing, no matter what type of mesh it is, do this.

function extrude(mesh,extrude#=1)
local vp[3]
     for j=0 to countSurfaces(mesh)
          srf=getSurface(mesh,j)
          for k=0 to countTriangles(srf)-1
               v0=triangleVertex(srf,k,0)
               v1=trianglevertex(srf,k,1)
               v2=trianglevertex(Srf,k,2)
               triNrom(vertexX(srf,v0),vertexy(srf,v0),vertexz(srf,v0),vertexx(srf,v1),vertexy(srf,v1),vertexz(srf,v1),vertexz(srf,v2),vertexy(srf,v2),vertexnz(srf,v2))
               for v=0 to 2
                   vert=triangleVertex(srf,v)
                   vertexcoords srf,vertexX(srf,vert)+triNX*extrude,vertexY(srf,vert)+triNY*extrude,vertexZ(srf,vert)+triNZ*extrude
               next
          next
     next
endfunction

global triNX,triNY,triNZ
Function TriNorm(x1#, y1#, z1#, x2#, y2#, z2#, x3#, y3#, z3#)
    ux#= x1# - x2#
    uy#= y1# - y2#
    uz#= z1# - z2#
    vx#= x3# - x2#
    vy#= y3# - y2#
    vz#= z3# - z2#	
    nx#= (uy# * vz#) - (vy# * uz#)
    ny#= (uz# * vx#) - (vz# * ux#)  
    nz#= (ux# * vy#) - (vx# * uy#)
    nl#= Sqr((nx*nx) + (ny*ny) + (nz*nz))  
    If NormLen > 0
		nx = nx/NL 
            ny = ny/NL
            nz = nz/NL
	Else
		nx = 0 : ny = 0 : nz = 1
	EndIf
	TriNX = nx
	TriNY = ny
	TriNZ = nz
End Function


extrude =scale of extrud..err...extrudation..(cough). Go well below 1 or you'll just create one funky looking mesh.



(Not tested..so use at your own caution...;)