同人星球BBS -> 游戏制作 -> [Blitz3D]卡通渲染效果:Toon Shading [打印本页] 登录 -> 注册 -> 回复主题 -> 发表主题

djgcn 07-01-19 14:37

你需要Blitz3D(1.80以上版本)編譯運行,請關閉DEBUG
範例與源碼見附件,方向鍵改變視角。

原理:改變模型為黑色,改變向量方向並且反轉,中度增加運算負擔
作者:Rob Pearmain
授權:Creative Comon License
   作者不對任何風險提供擔保

Copy code
; ------------------------------------------------------------------
; Toon5
; ------------------------------------------------------------------
; Example of Cel Rendering in Blitz3D


;   by R.Pearmain (06/11/2001) [url]www.peargames.com[/url]

;   Help/Contributions from :

;   RobinS
;   John Blythe

; ------------------------------------------------------------------

; ------------------------------------------------------------------
; Game's frames-per-second setting
; ------------------------------------------------------------------

Global gameFPS = 120

; ------------------------------------------------------------------
; Open 3D display mode
; ------------------------------------------------------------------

Graphics3D 640, 480, 16, 2

; ------------------------------------------------------------------
; Use double buffered animation (automatic, but I prefer this!)
; ------------------------------------------------------------------

SetBuffer BackBuffer ()

; ------------------------------------------------------------------
; Single camera setup
; ------------------------------------------------------------------

cam = CreateCamera ()
CameraClsColor cam,255,255,0
CameraViewport cam, 0, 0, GraphicsWidth (), GraphicsHeight ()

; ------------------------------------------------------------------
; General setup
; ------------------------------------------------------------------

; Load and arrange objects, textures, etc here...

; Quick example (just delete this)...

Global oMario = LoadAnimMesh("mariorun.x")
Texture=LoadTexture ("gradient.bmp",64)
ScaleEntity oMario,.2,.2,.2
RotateEntity oMario,0,180,0
settexture(oMario,Texture)

Global oMarioShadow = LoadAnimMesh("mariorun.x",oMario)
setflip(oMarioShadow)

PositionEntity oMario, 0, -2, 5

; ------------------------------------------------------------------
; Frame limiting code setup
; ------------------------------------------------------------------

framePeriod = 1000 / gameFPS
frameTime = MilliSecs () - framePeriod

Animate oMario,1,.1
Animate oMarioShadow,1,.1

nFPSCounter = 0
nFPS = 0

nCurrentTime = MilliSecs()

Repeat

  nFPSCounter = nFPSCounter+1
  If nCurrentTime + 1000 < MilliSecs()
    nFPS = nFPSCounter
    nFPSCounter=0
    nCurrentTime = MilliSecs()
  EndIf
  ; --------------------------------------------------------------
  ; Frame limiting
  ; --------------------------------------------------------------
 
  Repeat
    frameElapsed = MilliSecs () - frameTime
  Until frameElapsed
 
  frameTicks = frameElapsed / framePeriod
 
  frameTween# = Float (frameElapsed Mod framePeriod) / Float (framePeriod)
 
  ; --------------------------------------------------------------
  ; Update game and world state
  ; --------------------------------------------------------------
 
  For frameLimit = 1 To frameTicks
 
    If frameLimit = frameTicks Then CaptureWorld
        frameTime = frameTime + framePeriod
       
        UpdateGame ()
    UpdateWorld
 
  Next
 
  ; --------------------------------------------------------------
  ; **** Wireframe for DEBUG only -- remove before release! ****
  ; --------------------------------------------------------------
 
  If KeyHit (17): w = 1 - w: WireFrame w: EndIf ; Press 'W'
 
  ; --------------------------------------------------------------
  ; Draw 3D world
  ; --------------------------------------------------------------
 
  RenderWorld frameTween
 
  ; --------------------------------------------------------------
  ; Show result
  ; --------------------------------------------------------------
  Color 0,0,0
  Text 0,0,"Toon 5 Demo by Rob Pearmain ([url]www.peargames.com[/url])"
  Text 0,16,"based on theory by J. Blythe and Robin S"
  Text 0,32,"use Cursor keys to move"
  Text 0,64,"Frames PS : " + nFPS
Flip

Until KeyHit (1)

End

; ------------------------------------------------------------------
; Game update routine, called from frame limiting code
; ------------------------------------------------------------------

Function UpdateGame ()

  ; Get keypresses, move entities, etc
 
  ; EXAMPLE CODE -- REMOVE! Uses cursors...
  If KeyDown (203) TurnEntity oMario, 0, 2, 0
  If KeyDown (205) TurnEntity oMario, 0, -2, 0
  If KeyDown(200) TurnEntity oMario,2,0,0
  If KeyDown(208) TurnEntity oMario,-2,0,0
 
End Function

; ------------------------------------------------------------------
; Color the mesh black, expand all vertices along normal and then flip
; ------------------------------------------------------------------
Function setflip(model)
 
  ;   Black
  EntityColor model,0,0,0

  ;   Move each vertex along normal (Thanks RobinS)
  surfcnt=CountSurfaces(model)
  If surfcnt > 0
    For scnt=1 To surfcnt
        surf=GetSurface(model,scnt)
        For index=0 To CountVertices(surf)-1

            oldX# = VertexX( surf, index )
            NX# = VertexNX( surf, index )
            newX# = oldX + NX/10
            oldY# = VertexY( surf, index )
            NY# = VertexNY( surf, index )
            newY# = oldY + NY/10
            oldZ# = VertexZ( surf, index )
            NZ# = VertexNZ( surf, index )
            newZ# = oldZ + NZ   /10
            VertexCoords surf, index, newX,newY,newZ
        Next
    Next

  EndIf
   
  ;   Flip Mesh
 
  FlipMesh model

  ;   Repeat as needed
  For a=1 To CountChildren(model)
    l=GetChild(model,a)
    setflip(l)

  Next
End Function


; ------------------------------------------------------------------
; Set the texture on all meshes (for animated meshes)
; ------------------------------------------------------------------

Function settexture(model,texture)
    EntityTexture model, texture
    ;   Full Brightness
    EntityFX model,1
    For a=1 To CountChildren(model)
        l=GetChild(model,a)
        settexture(l,texture)
    Next
End Function


查看完整版本: [-- [Blitz3D]卡通渲染效果:Toon Shading --] [-- top --]

Powered by PHPWind v5.3 Code © 2003-05 PHPWind
Time 0.041901 second(s),query:3 Gzip enabled

You can contact us