[B3D] 3D SkyBox

Übersicht BlitzBasic Codearchiv

Neue Antwort erstellen

Xaymar

ehemals "Cgamer"

Betreff: [B3D] 3D SkyBox

BeitragDi, März 31, 2009 15:48
Antworten mit Zitat
Benutzer-Profile anzeigen
Ich hab mir mal gedanken über die 3D Skyboxes in Hallife2(Source Engine) gemacht und auch ein nettes ergebnis bekommen.

Beispiel Code("3DSkyBox TEST.bb"):
Code: [AUSKLAPPEN]
Graphics3D 1024, 768, 0, 2
SetBuffer BackBuffer()
SeedRnd MilliSecs()

Include "3DSkyBox.bb"

Cam = CreateCamera()

SkyBoxFogRed = 0
SkyBoxFogGreen = 0
SkyBoxFogBlue = 0

Init3DSky(Cam)

PositionEntity SkyBoxStart, 0, 1000, 0

MoveEntity Cam, 0, 0, 0

For A# = 0 To 1000
   t = CreateSphere()
   PositionEntity t, Rnd(-100.0,100.0), 1000+Rnd(-100.0,100.0), Rnd(-100.0,100.0)
   ScaleEntity t, Rnd(.1, 5), Rnd(.1, 5), Rnd(.1, 5)
   EntityColor t, Rand(0,255), Rand(0,255), Rand(0,255)
   EntityAlpha t, Rnd(0.0, 1.0)
   EntityBlend t, Rand(1,3)
Next

While Not KeyHit(1)
   Cls
   
   A# = A# + .25
   
   PositionEntity Cam, Cos(A)*100, 0, Sin(A)*100
   PointEntity Cam, WorldStart
   
   RenderWorld
   
   Color 0, 0, 0
   Rect 0, 0, 300, 45
   
   Color 255, 255, 255
   Text 0, 0, EntityX(WorldStart)-EntityX(NormalCam)
   Text 0,15, EntityY(WorldStart)-EntityY(NormalCam)
   Text 0,30, EntityZ(WorldStart)-EntityZ(NormalCam)
   
   Text 100, 0, EntityX(NormalCam)-EntityX(WorldStart)
   Text 100,15, EntityY(NormalCam)-EntityY(WorldStart)
   Text 100,30, EntityZ(NormalCam)-EntityZ(WorldStart)
   
   Flip
   Update3DSky()
Wend


Hauptdatei("3DSkyBox.bb"):
Code: [AUSKLAPPEN]
;3D Skybox

;Variables (NEEDED)
Global NormalCam, SkyBoxCam, WorldStart, SkyBoxStart, SkyCube, SkyCubeTex
Global SkyBoxScale    = 16         ;16:1
Global SkyBoxTexSize  = 512         ;Resultion of Cubemap
Global SkyBoxFog      = 1         ;Fog in SkyBox
Global SkyBoxFogStart = 150         ;Fog start
Global SkyBoxFogEnd   = 200         ;Fog End
Global SkyBoxFogRed   = 255         ;Fog Color Red
Global SkyBoxFogGreen = 255         ;Fog Color Green
Global SkyBoxFogBlue  = 255         ;Fog Color Blue

;Function
Function Init3DSky(Camera)            ;Run Once
   NormalCam = Camera
   SkyBoxCam = CreateCamera()
   
   WorldStart = CreatePivot()
   SkyBoxStart = CreatePivot()
   
   SkyCubeTex = CreateTexture(SkyBoxTexSize, SkyBoxTexSize, 128+257):SetCubeMode SkyCubeTex, 3
   
   SkyCube = CreateCube()
   ScaleEntity Skycube, 100, 100, 100
   EntityTexture SkyCube, SkyCubeTex
   EntityOrder SkyCube, 1024
   EntityFX SkyCube, 1+8
   FlipMesh SkyCube
   
   Update3DSkySettings()
End Function

Function Update3DSkySettings()
   ;ScaleEntity    SkyBoxCam,  1/SkyBoxScale,     1/SkyBoxScale, 1/SkyBoxScale
   CameraFogMode  SkyBoxCam,      SkyBoxFog
   CameraFogRange SkyBoxCam, SkyBoxFogStart,      SkyBoxFogEnd
   CameraFogColor SkyBoxCam,   SkyBoxFogRed,    SkyBoxFogGreen,   SkyboxFogBlue
   CameraRange    SkyBoxCam,             .1, 1.01*SkyBoxFogEnd
   CameraClsColor SkyBoxCam,   SkyBoxFogRed,    SkyBoxFogGreen,   SkyboxFogBlue
End Function

Function Update3DSky()
   PositionEntity SkyCube, EntityX(NormalCam), EntityY(NormalCam), EntityZ(NormalCam)
   
   HideEntity NormalCam:ShowEntity SkyBoxCam
   
   TX# = EntityX(SkyBoxStart)+((EntityX(NormalCam)-EntityX(WorldStart))/SkyBoxScale)
   TY# = EntityY(SkyBoxStart)+((EntityY(NormalCam)-EntityY(WorldStart))/SkyBoxScale)
   TZ# = EntityZ(SkyBoxStart)+((EntityZ(NormalCam)-EntityZ(WorldStart))/SkyBoxScale)
   
   PositionEntity SkyBoxCam, TX#, TY#, TZ#
   CameraViewport SkyBoxCam, 0, 0, SkyBoxTexSize, SkyBoxTexSize
   
   SetCubeFace SkyCubeTex, 0
   RotateEntity SkyBoxCam, 0, 90, 0
   RenderWorld
   CopyRect 0, 0, SkyBoxTexSize, SkyBoxTexSize, 0, 0, BB, TextureBuffer(SkyCubeTex)
   
   SetCubeFace SkyCubeTex, 1
   RotateEntity SkyBoxCam, 0, 0, 0
   RenderWorld
   CopyRect 0, 0, SkyBoxTexSize, SkyBoxTexSize, 0, 0, BB, TextureBuffer(SkyCubeTex)
   
   SetCubeFace SkyCubeTex, 2
   RotateEntity SkyBoxCam, 0, -90, 0
   RenderWorld
   CopyRect 0, 0, SkyBoxTexSize, SkyBoxTexSize, 0, 0, BB, TextureBuffer(SkyCubeTex)
   
   SetCubeFace SkyCubeTex, 3
   RotateEntity SkyBoxCam, 0, 180, 0
   RenderWorld
   CopyRect 0, 0, SkyBoxTexSize, SkyBoxTexSize, 0, 0, BB, TextureBuffer(SkyCubeTex)
   
   SetCubeFace SkyCubeTex, 4
   RotateEntity SkyBoxCam, -90, 0, 0
   RenderWorld
   CopyRect 0, 0, SkyBoxTexSize, SkyBoxTexSize, 0, 0, BB, TextureBuffer(SkyCubeTex)
   
   SetCubeFace SkyCubeTex, 5
   RotateEntity SkyBoxCam, 90, 0, 0
   RenderWorld
   CopyRect 0, 0, SkyBoxTexSize, SkyBoxTexSize, 0, 0, BB, TextureBuffer(SkyCubeTex)
   
   CameraViewport SkyBoxCam, 0, 0, 0, 0
   
   ShowEntity NormalCam:HideEntity SkyBoxCam
End Function


Funktionsbeschreibung:

Init3DSky(Camera)
Initiiert die 3D Skybox(Erstellt 2. Camera und Texturen)

Update3DSkySettings()
Zuvor eingestellte Optionen übernehmen
Hierbei sind folgende Variablen von belang:
    SkyBoxScale
    SkyBoxTexSize
    SkyBoxFog
    SkyBoxFogStart
    SkyBoxFogEnd
    SkyBoxFogRed
    SkyBoxFogGreen
    SkyBoxFogBlue


Update3DSky
Erneuert die Cubemap.
PseudoCode:
Code: [AUSKLAPPEN]
Ausblenden Level
Einblenden SkyBoxObjekte
Update3DSky
Ausblenden SkyBoxObjekte
Einblenden Level

Ende der Funktionsbeschreibung

Wo die Weltstartet und wo die Skybox startet könnt ihr mit den zwei Pivots WorldStart undSkyBoxStart festlegen

Mfg
Warbseite
 

martin_moehler

BeitragDi, März 31, 2009 21:59
Antworten mit Zitat
Benutzer-Profile anzeigen
Echt tolle sache. Wenn man dann statt der Kreise Wolken oder so etwas zeichnet. Schicke Sache! Probier ich unbedingt gleich aus!
Intel Core 2 quad 2.3 Ghz, Nvidea GeForce GT 120, 4GB Arbeitsspeicher, Vista 64 bit

Xaymar

ehemals "Cgamer"

BeitragDi, März 31, 2009 22:07
Antworten mit Zitat
Benutzer-Profile anzeigen
Noch eine kleine Anmerkung: In der 3D Skybox muss alles um SkyBoxScale verkleinert sein(1/SkyBoxScale)
[Edit]Sofern man die zeile(Scaleentity SkyBoxCamera, 1/SkyBoxscale, 1/SkyBoxscale, 1/SkyBoxscale) entkommentiert
Warbseite

Xaymar

ehemals "Cgamer"

BeitragMi, Apr 01, 2009 16:17
Antworten mit Zitat
Benutzer-Profile anzeigen
Nen kleines Speed update:
Code: [AUSKLAPPEN]
;3D Skybox

;Variables (NEEDED)
Global NormalCam, SkyBoxCam, WorldStart, SkyBoxStart, SkyCube, SkyCubeTex
Global SkyBoxScale    = 4         ;16:1
Global SkyBoxTexSize  = 512         ;Resultion of Cubemap
Global SkyBoxFog      = 1         ;Fog in SkyBox
Global SkyBoxFogStart = 150         ;Fog start
Global SkyBoxFogEnd   = 200         ;Fog End
Global SkyBoxFogRed   = 255         ;Fog Color Red
Global SkyBoxFogGreen = 255         ;Fog Color Green
Global SkyBoxFogBlue  = 255         ;Fog Color Blue

;For Speed Reasons
Global LCamX# = -1, LCamY# = -1, LCamZ# = -1

;Function
Function Init3DSky(Camera)            ;Run Once
   NormalCam = Camera
   SkyBoxCam = CreateCamera()
   
   WorldStart = CreatePivot()
   SkyBoxStart = CreatePivot()
   
   SkyCubeTex = CreateTexture(SkyBoxTexSize, SkyBoxTexSize, 128+257):SetCubeMode SkyCubeTex, 3
   
   SkyCube = CreateCube()
   ScaleEntity Skycube, 4, 4, 4
   EntityTexture SkyCube, SkyCubeTex
   EntityOrder SkyCube, 1024
   EntityFX SkyCube, 1+8
   FlipMesh SkyCube
   
   Update3DSkySettings()
End Function

Function Update3DSkySettings()
;   ScaleEntity    SkyBoxCam,  1.0/SkyBoxScale,     1.0/SkyBoxScale, SkyBoxScale
   CameraFogMode  SkyBoxCam,      SkyBoxFog
   CameraFogRange SkyBoxCam, SkyBoxFogStart,      SkyBoxFogEnd
   CameraFogColor SkyBoxCam,   SkyBoxFogRed,    SkyBoxFogGreen,   SkyboxFogBlue
   CameraRange    SkyBoxCam,             .1, 1.01*SkyBoxFogEnd
   CameraClsColor SkyBoxCam,   SkyBoxFogRed,    SkyBoxFogGreen,   SkyboxFogBlue
End Function

Function Update3DSky(CheckIfRendered=1)
   If (LCamX# <> EntityX(NormalCam) Or LCamY# <> EntityY(NormalCam) Or LCamZ# <> EntityZ(NormalCam))
      LCamX# = EntityX(NormalCam):LCamY# = EntityY(NormalCam):LCamZ# = EntityZ(NormalCam)
      PositionEntity SkyCube, EntityX(NormalCam), EntityY(NormalCam), EntityZ(NormalCam)
      
      HideEntity NormalCam:ShowEntity SkyBoxCam
      
      TX# = EntityX(SkyBoxStart)+((EntityX(NormalCam)-EntityX(WorldStart))/SkyBoxScale)
      TY# = EntityY(SkyBoxStart)+((EntityY(NormalCam)-EntityY(WorldStart))/SkyBoxScale)
      TZ# = EntityZ(SkyBoxStart)+((EntityZ(NormalCam)-EntityZ(WorldStart))/SkyBoxScale)
      
      PositionEntity SkyBoxCam, TX#, TY#, TZ#
      CameraViewport SkyBoxCam, 0, 0, SkyBoxTexSize, SkyBoxTexSize
      
      SetCubeFace SkyCubeTex, 0
      RotateEntity SkyBoxCam, 0, 90, 0
      RenderWorld
      CopyRect 0, 0, SkyBoxTexSize, SkyBoxTexSize, 0, 0, BB, TextureBuffer(SkyCubeTex)
      
      SetCubeFace SkyCubeTex, 1
      RotateEntity SkyBoxCam, 0, 0, 0
      RenderWorld
      CopyRect 0, 0, SkyBoxTexSize, SkyBoxTexSize, 0, 0, BB, TextureBuffer(SkyCubeTex)
      
      SetCubeFace SkyCubeTex, 2
      RotateEntity SkyBoxCam, 0, -90, 0
      RenderWorld
      CopyRect 0, 0, SkyBoxTexSize, SkyBoxTexSize, 0, 0, BB, TextureBuffer(SkyCubeTex)
      
      SetCubeFace SkyCubeTex, 3
      RotateEntity SkyBoxCam, 0, 180, 0
      RenderWorld
      CopyRect 0, 0, SkyBoxTexSize, SkyBoxTexSize, 0, 0, BB, TextureBuffer(SkyCubeTex)
      
      SetCubeFace SkyCubeTex, 4
      RotateEntity SkyBoxCam, -90, 0, 0
      RenderWorld
      CopyRect 0, 0, SkyBoxTexSize, SkyBoxTexSize, 0, 0, BB, TextureBuffer(SkyCubeTex)
      
      SetCubeFace SkyCubeTex, 5
      RotateEntity SkyBoxCam, 90, 0, 0
      RenderWorld
      CopyRect 0, 0, SkyBoxTexSize, SkyBoxTexSize, 0, 0, BB, TextureBuffer(SkyCubeTex)
      
      CameraViewport SkyBoxCam, 0, 0, 0, 0
      
      ShowEntity NormalCam:HideEntity SkyBoxCam
   EndIf
   If CheckIfRendered = 0
      LCamX# = -EntityX(NormalCam):LCamY# = -EntityY(NormalCam):LCamZ# = -EntityZ(NormalCam)
   EndIf
End Function
Warbseite

Xaymar

ehemals "Cgamer"

BeitragSo, Sep 27, 2009 18:02
Antworten mit Zitat
Benutzer-Profile anzeigen
Der Letzte Post liegt mehr als ein halbes Jahr zurück und warum ein neues Thema aufmachen wenns doch schon da ist.

Habe die 3D Skybox mal geupdatet:
Arrow 1 Rendervorgang statt 6
Arrow EntitySkyBox(Entity, ScaleX, ScaleY, ScaleZ) skaliert das entity in skybox größe

3D Sky Flat 2.bb
BlitzBasic: [AUSKLAPPEN]
;3D Skybox

;Variables (NEEDED)
Global NormalCam, SkyBoxCam, WorldStart, SkyBoxStart, SkyCube, SkyCubeTex
Global SkyBoxScale = 4 ;16:1
Global SkyBoxTexSize = 512 ;Resultion of Cubemap
Global SkyBoxFog = 1 ;Fog in SkyBox
Global SkyBoxFogStart = 150 ;Fog start
Global SkyBoxFogEnd = 200 ;Fog End
Global SkyBoxFogRed = 255 ;Fog Color Red
Global SkyBoxFogGreen = 255 ;Fog Color Green
Global SkyBoxFogBlue = 255 ;Fog Color Blue
Global LastRenderedFace = 1 ;Speed it up:D

;For Speed Reasons
Global LCamX# = -1, LCamY# = -1, LCamZ# = -1

;Function
Function Init3DSky(Camera) ;Run Once
NormalCam = Camera
SkyBoxCam = CreateCamera()

WorldStart = CreatePivot()
SkyBoxStart = CreatePivot()

SkyCubeTex = CreateTexture(SkyBoxTexSize, SkyBoxTexSize, 257)

SkyCube = CreateCube(Camera)
ScaleEntity SkyCube, 4, 4, 4
EntityTexture SkyCube, SkyCubeTex
EntityOrder SkyCube, 1024
EntityFX SkyCube, 1+8
FlipMesh SkyCube

Update3DSkySettings()
End Function

Function Update3DSkySettings()
ScaleEntity SkyBoxCam, 1.0/SkyBoxScale, 1.0/SkyBoxScale, 1.0/SkyBoxScale
CameraFogMode SkyBoxCam, SkyBoxFog
CameraFogRange SkyBoxCam, SkyBoxFogStart/SkyBoxScale , SkyBoxFogEnd/SkyBoxScale
CameraFogColor SkyBoxCam, SkyBoxFogRed , SkyBoxFogGreen , SkyBoxFogBlue
CameraRange SkyBoxCam, 1 , SkyBoxFogEnd*2
CameraClsColor SkyBoxCam, SkyBoxFogRed , SkyBoxFogGreen , SkyBoxFogBlue
End Function

Function EntitySkyBox(ent, XS#, YS#, ZS#)
ScaleEntity ent, XS/SkyBoxScale, YS/SkyBoxScale, ZS/SkyBoxScale
EntityAutoFade ent, SkyBoxFogEnd/SkyBoxScale*0.75, SkyBoxFogEnd/SkyBoxScale*0.875
End Function

Function Update3DSky()
HideEntity NormalCam:ShowEntity SkyBoxCam

TX# = EntityX(SkyBoxStart)+((EntityX(WorldStart)-EntityX(NormalCam))/SkyBoxScale)
TY# = EntityY(SkyBoxStart)+((EntityY(WorldStart)+EntityY(NormalCam))/SkyBoxScale)
TZ# = EntityZ(SkyBoxStart)+((EntityZ(WorldStart)+EntityZ(NormalCam))/SkyBoxScale)

PositionEntity SkyBoxCam, TX#, TY#, TZ#
CameraViewport SkyBoxCam, 0, 0, SkyBoxTexSize, SkyBoxTexSize

RotateEntity SkyBoxCam, EntityPitch(NormalCam), -EntityYaw(NormalCam), EntityRoll(NormalCam)
RenderWorld
CopyRect 0, 0, SkyBoxTexSize, SkyBoxTexSize, 0, 0, BB, TextureBuffer(SkyCubeTex)

CameraViewport SkyBoxCam, 0, 0, 0, 0

ShowEntity NormalCam:HideEntity SkyBoxCam
End Function


3D Skybox Test.bb
BlitzBasic: [AUSKLAPPEN]
Graphics3D 1280, 1024, 32, 1
SetBuffer BackBuffer()
SeedRnd MilliSecs()

;Include "3DSkyBox.bb"
Include "3D Sky Flat 2.bb"

Cam = CreateCamera():CameraRange Cam, 1, 100
AntiAlias 1
Dither 1

SkyBoxTexSize = 1024
SkyBoxFogStart = 256
SkyBoxFogEnd = 512
SkyBoxFogRed = 50
SkyBoxFogGreen = 70
SkyBoxFogBlue = 153
SkyBoxFog = 1

Init3DSky(Cam)

PositionEntity SkyBoxStart, 0, 1000, 0

MoveEntity Cam, 0, 0, 0

For A# = 0 To 1000
t = CreateCube()
PositionEntity t, Rnd(-100.0,100.0), 1000+Rnd(-100.0,100.0), Rnd(-100.0,100.0)
EntityAlpha t, 1
EntityBlend t, 1
EntityColor t, Rand(255), Rand(255), Rand(255)
EntitySkybox t, 2.5, 2.5, 2.5
Next

FreeEntity t

WireFrame 0

While Not KeyHit(1)
MYS = MouseYSpeed()
MXS = MouseXSpeed()
Cls

MoveEntity Cam, (KeyDown(32)-KeyDown(30))*.25, 0, (KeyDown(17)-KeyDown(31))*.25
RotateEntity Cam, EntityPitch(Cam)+MYS, EntityYaw(Cam)-MXS, 0

WireFrame KeyDown(18)

RenderWorld

Color 0, 0, 0
Rect 0, 0, 350, 45

Color 255, 255, 255
Text 0, 0, EntityX(WorldStart)-EntityX(NormalCam)
Text 0,15, EntityY(WorldStart)-EntityY(NormalCam)
Text 0,30, EntityZ(WorldStart)-EntityZ(NormalCam)

Text 150, 0, EntityX(NormalCam)-EntityX(WorldStart)
Text 150,15, EntityY(NormalCam)-EntityY(WorldStart)
Text 150,30, EntityZ(NormalCam)-EntityZ(WorldStart)

Text 0, 400, FPS()
Text 0, 415, msecr
Flip
frm = (frm +1) Mod 6
If frm = 5 MoveMouse GraphicsWidth()/2, GraphicsHeight()/2

msecr = MilliSecs()
Update3DSky()
msecr = MilliSecs()-msecr
Wend

Global FPS, FPS_temp, FPS_time
Function FPS()
ctime = MilliSecs()
FPS_temp = FPS_temp + 1
If ctime - FPS_time > 500 Then
FPS = FPS_temp * 2
FPS_temp = 0
FPS_time = ctime
EndIf
Return FPS
End Function

Function AddMeshToMesh(Mesh, Mesh2, R=255, G=255, B=255, A#=1.0)
Surf = GetSurface(Mesh2, 1)

X# = EntityX(Mesh2)-EntityX(Mesh)
Y# = EntityY(Mesh2)-EntityY(Mesh)
Z# = EntityZ(Mesh2)-EntityZ(Mesh)

SurfC = CountSurfaces(Mesh)
For A = 1 To SurfC
Surf2 = GetSurface(Mesh, A)
TriC = CountTriangles(Surf2)
For A = 0 To TriC-1
V = TriangleVertex(Surf2, A, 0)
V0 = AddVertex(Surf, X#+VertexX(Surf2, V), Y#+VertexY(Surf2, V), Z#+VertexZ(Surf2, V), VertexU(Surf2, V), VertexV(Surf2, V))
V = TriangleVertex(Surf2, A, 1)
V1 = AddVertex(Surf, X#+VertexX(Surf2, V), Y#+VertexY(Surf2, V), Z#+VertexZ(Surf2, V), VertexU(Surf2, V), VertexV(Surf2, V))
V = TriangleVertex(Surf2, A, 2)
V2 = AddVertex(Surf, X#+VertexX(Surf2, V), Y#+VertexY(Surf2, V), Z#+VertexZ(Surf2, V), VertexU(Surf2, V), VertexV(Surf2, V))
VertexColor Surf, V0, R, G, B, A#
VertexColor Surf, V1, R, G, B, A#
VertexColor Surf, V2, R, G, B, A#
Next
Next
End Function

Function EntityScaleX#(Entity)
Vx# = GetMatElement(Entity, 0, 0)
Vy# = GetMatElement(Entity, 0, 1)
Vz# = GetMatElement(Entity, 0, 2)
Scale# = Sqr(Vx#*Vx# + Vy#*Vy# + Vz#*Vz#)

Return Scale#
End Function

Function EntityScaleY#(Entity)
Vx# = GetMatElement(Entity, 1, 0)
Vy# = GetMatElement(Entity, 1, 1)
Vz# = GetMatElement(Entity, 1, 2)
Scale# = Sqr(Vx#*Vx# + Vy#*Vy# + Vz#*Vz#)

Return Scale#
End Function

Function EntityScaleZ#(Entity)
Vx# = GetMatElement(Entity, 2, 0)
Vy# = GetMatElement(Entity, 2, 1)
Vz# = GetMatElement(Entity, 2, 2)
Scale# = Sqr(Vx#*Vx# + Vy#*Vy# + Vz#*Vz#)

Return Scale#
End Function


Viel Spaß damit:)
Warbseite

Neue Antwort erstellen


Übersicht BlitzBasic Codearchiv

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group