Painting Objects.
Have you ever wanted to 'paint' an object in DBPro? Bullet holes? Footprints? Shadows? Too difficult? Here's what you've been looking for! Less than 3 Lines of code (and a demo)! Paint any object any time. Make a few easy mods to the function for different types of decals. And all using DBPro 5.8 commads.
20 line code
`This program demonstrates how to "paint" a 3d object.
`This is usefull for footsteps or bulletholes, or other "painted" effects.
`The concept can even be used for shadows - with some modifications.
`This is the set-up line.
`1 Line:
Global PaintObj as DWord:PaintObj=101:Set Image ColorKey 0,0,254:Bullet_Hole(1)
`Demo
Sync On: Sync Rate 60: Autocam Off
`Make our test object to draw on. This would typically be a .x level or similar object.
`For this demo, I'm putting a cube and a sphere together.
Make Object Cube 1,50
Make Mesh From Object 1,1
Delete Object 1
Make Object Sphere 1,75,50,50
Add Limb 1,1,1
Offset Limb 1,0,25,0,0
Offset Limb 1,1,-25,0,0
`Set Object Emissive 1,Rgb(50,50,50)
Set Object Cull 1,0
Ghost Object On 1 `Ghost object 1 for a pretty neat effect.
`Place our demo object out a bit.
Position Object 1,0,0,120
`Make a dummy (placeholder) object. This will save a lot of math, but will also slow things down a bit.
Make Object Sphere 2,2,6,6
Hide object 2
Do
Move_Camera()
If MouseClick()=1 Then Paint(1)
Sync
Loop
`This function simply orbits the camera around the object's center. It's not important to the "painting".
Function Move_Camera()
Position Object 2,0,0,120
Point Object 2,Camera Position X(),Camera Position Y(),Camera POsition Z()
Turn Object Left 2,RightKey()-LeftKey()
Move Object 2,120
Position Camera Object Position X(2),Object Position Y(2),Object POsition Z(2)
Point Camera 0,0,120
EndFunction
`This function creates a demo-decal image for the paint function.
Function Bullet_Hole(ImageID)
If Bitmap Exist(1):Delete Bitmap 1:Endif
Create Bitmap 1,128,128
Ink Rgb(0,0,254),0
Box 0,0,127,127
For i = 0 to 15
Ink Rgb(i*5,i*5,i*5),0
ang=rnd(360)
If i>7 then i=i+1
Circle 30,30,i
Next i
Get Image ImageID,0,0,60,60,0
Set Current Bitmap 0
Delete Bitmap 1
EndFunction
`This is the Paint Function. It creates a "decal" using Image 1 over the object.
`2 Lines:
Function Paint(TargetID):o1=Pick Object(Mousex()-5,MouseY()-5,TargetID,TargetID):d1#=Get Pick Distance()-.1:Pick Screen Mousex()-5,Mousey()-5,d1#:x1#=Get Pick Vector X()+ Camera Position X():y1#=Get Pick Vector Y()+ Camera Position Y():z1#=Get Pick Vector Z()+ Camera Position Z():o2=Pick Object(Mousex()+7,MouseY(),TargetID,TargetID):d2#=Get Pick Distance()-.1:Pick Screen Mousex()+7,Mousey(),d2#:x2#=Get Pick Vector X()+ Camera Position X():y2#=Get Pick Vector Y()+ Camera Position Y():z2#=Get Pick Vector Z()+ Camera Position Z():o3=Pick Object(Mousex(),MouseY()+7,TargetID,TargetID):If o1=0 or o2=0 or o3=0 :ExitFunction:Endif:d3#=Get Pick Distance()-.1:Pick Screen Mousex(),Mousey()+7,d3#:
If abs(d1#-d2#)>6 or abs(d1#-d3#)>6 or abs(d2#-d3#)>6:ExitFunction:Endif:x3#=Get Pick Vector X()+ Camera Position X():y3#=Get Pick Vector Y()+ Camera Position Y():z3#=Get Pick Vector Z()+ Camera Position Z():If Object Exist(PaintObj):Delete Object PaintObj:Endif:Make Object Triangle PaintObj,x1#,y1#,z1#,x2#,y2#,z2#,x3#,y3#,z3#:Set Object Cull PaintObj,0:Texture Object PaintObj,1:Set Object Emissive PaintObj,Rgb(64,64,64):Set Object transparency PaintObj,1:PaintObj=PaintObj+1: If PaintObj>400:PaintObj=101:Endif:EndFunction
Extended Code:
`This program demonstrates how to "paint" a 3d object.
`This is usefull for footsteps or bulletholes, or other "painted" effects.
`The concept can even be used for shadows - with some modifications.
Global PaintObj as DWord
PaintObj=101
Set Image ColorKey 0,0,254
Bullet_Hole(1)
Sync On: Sync Rate 60: Autocam Off
`Make our test object to draw on. This would typically be a .x level or similar object.
`For this demo, I'm putting a cube and a sphere together.
Make Object Cube 1,50
Make Mesh From Object 1,1
Delete Object 1
Make Object Sphere 1,75,50,50
Add Limb 1,1,1
Offset Limb 1,0,25,0,0
Offset Limb 1,1,-25,0,0
`Set Object Emissive 1,Rgb(50,50,50)
Set Object Cull 1,0
Ghost Object On 1 `Ghost object 1 for a pretty neat effect.
`Place our demo object out a bit.
Position Object 1,0,0,120
`Make a dummy (placeholder) object. This will save a lot of math, but will also slow things down a bit.
Make Object Sphere 2,2,6,6
Hide object 2
Do
Move_Camera()
If MouseClick()=1 Then Paint(1)
Sync
Loop
`This function simply orbits the camera around the object's center. It's not important to the "painting".
Function Move_Camera()
Position Object 2,0,0,120
Point Object 2,Camera Position X(),Camera Position Y(),Camera POsition Z()
Turn Object Left 2,RightKey()-LeftKey()
Move Object 2,120
Position Camera Object Position X(2),Object Position Y(2),Object POsition Z(2)
Point Camera 0,0,120
EndFunction
`This function creates a demo-decal image for the paint function.
Function Bullet_Hole(ImageID)
If Bitmap Exist(1):Delete Bitmap 1:Endif
Create Bitmap 1,128,128
Ink Rgb(0,0,254),0
Box 0,0,127,127
For i = 0 to 15
Ink Rgb(i*5,i*5,i*5),0
ang=rnd(360)
If i>7 then i=i+1
Circle 30,30,i
Next i
Get Image ImageID,0,0,60,60,0
Set Current Bitmap 0
Delete Bitmap 1
EndFunction
`This is the Paint Function. It creates a "decal" using Image 1 over the object.
Function Paint(TargetID)
o1=Pick Object(Mousex()-5,MouseY()-5,TargetID,TargetID)
If o1=0 :ExitFunction:Endif
d1#=Get Pick Distance()-.1
Pick Screen Mousex()-5,Mousey()-5,d1#
x1#=Get Pick Vector X()+ Camera Position X()
y1#=Get Pick Vector Y()+ Camera Position Y()
z1#=Get Pick Vector Z()+ Camera Position Z()
o2=Pick Object(Mousex()+7,MouseY(),TargetID,TargetID)
If o2=0 :ExitFunction:Endif
d2#=Get Pick Distance()-.1
Pick Screen Mousex()+7,Mousey(),d2#
x2#=Get Pick Vector X()+ Camera Position X()
y2#=Get Pick Vector Y()+ Camera Position Y()
z2#=Get Pick Vector Z()+ Camera Position Z()
o3=Pick Object(Mousex(),MouseY()+7,TargetID,TargetID)
If o3=0 :ExitFunction:Endif
d3#=Get Pick Distance()-.1
Pick Screen Mousex(),Mousey()+7,d3#
If abs(d1#-d2#)>6 or abs(d1#-d3#)>6 or abs(d2#-d3#)>6:ExitFunction:Endif
x3#=Get Pick Vector X()+ Camera Position X()
y3#=Get Pick Vector Y()+ Camera Position Y()
z3#=Get Pick Vector Z()+ Camera Position Z()
If Object Exist(PaintObj):Delete Object PaintObj:Endif
Make Object Triangle PaintObj,x1#,y1#,z1#,x2#,y2#,z2#,x3#,y3#,z3#
Set Object Cull PaintObj,0
Texture Object PaintObj,1
Set Object Emissive PaintObj,Rgb(64,64,64)
Set Object transparency PaintObj,1
PaintObj=PaintObj+1: If PaintObj>400:PaintObj=101:Endif
EndFunction
"Droids don't rip your arms off when they lose." -H. Solo
REALITY II