Lots of remarks to read.
Use the left/right arrows to orbit the camera around the object.
Use the left mouse button to "paint".
Comments welcome.
Enjoy.
Demonstrates the very basics of how to do bullet holes or footprints.
`This snippet 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.
Sync On: Sync Rate 60: Autocam Off `Standard set up
Global PaintObj as DWord
PaintObj=101 `PaintObj will cycle through the paint "spots"
`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
`Hide object 1 `Hide object 1 for a pretty neat effect.
`Place our demo object out a ways.
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
`Our main loop:
`First we move the camera in an orbit around our object.
`Second, if the left mouse button is clicked, we "paint" the object.
Do
Move_Camera()
If MouseClick()=1 Then Paint()
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 is the Paint Function.
Function Paint()
`First, get a point in 3D space that is in line with the mouse cursor.
`There are lots of ways to do this, but I went with Pick Screen.
`Pick Screen Returns 3d Coordinates based on a 2D screen position + a distance.
`The distance in this case is 250. I chose 250 since it went well beyond the center of our object.
`Any distance beyond the object will do.
Pick Screen Mousex(),MouseY(),250
`When we use Pick Screen, it returns the 3D coordinates in the Pick Vector. Don't let the term 'Vector' scare you.
`Basically, it's not much different than a variable - in this case it holds our 3D coordinates relative to the camera.
`Since the coordinates are relative to the camera, we have to add the Camera Position to the "Vector" positions.
x1#=Get Pick Vector X()+ Camera Position X()
y1#=Get Pick Vector Y()+ Camera Position Y()
z1#=Get Pick Vector Z()+ Camera Position Z()
`There is also a Get Pick Distance() function which also returns the distance from the Camera. In this case,
`it's not much use since we supplied the distance in the Pick Screen command.
`There are other uses for Get Pick Distance(), but we won't cover them here.
`Here we use the Pick screen again to get 3D Coordinates that are much closer to the camera (closer than our demo object)
Pick Screen Mousex(),MouseY(),20
x2#=Get Pick Vector X()+ Camera Position X()
y2#=Get Pick Vector Y()+ Camera Position Y()
z2#=Get Pick Vector Z()+ Camera Position Z()
`And here is how we get the coordinates for the edge of the sphere.
`We atart with Interect Object, which basically draws a line between 2 points and gets the distance along that line
`to the first point of contact with the "target" object (in this case Object 1).
d#=Intersect Object(1,x2#,y2#,z2#,x1#,y1#,z1#)
if d#>0 `If d#=0 then the line does not intersct the object, so we only want if d#>0.
`So now we know that the edge of our demo object is d# distance from x2#,y2#,z2#. Where is that? Let's find out.
`Using our "dummy" object - Object 2, we place it at x2#,y2#,z2#
Position Object 2,x2#,y2#,z2#
`Then we point our object 2 right along our line - right to x1#,y1#,z1#
Point Object 2,x1#,y1#,z1#
`Last, we move our object 2 a distance of d# - the distance along the x2-x1 line to the edge of the object.
Move Object 2,d#
`This next line allows us to recycle the "paint" objects. This would be like old bullet holes or footprints fading away.
If Object Exist(PaintObj) Then Delete Object PaintObj
`Next, we make our paint object. Instead of a footprint or a bullet hole, I used triangles for the demo.
Make Object Triangle PaintObj,0,0,0,.5,1,0,1,0,0
`Get the coordinates of our placeholder object.
x#=Object Position X(2)
y#=Object Position Y(2)
z#=Object Position Z(2)
`And place the "paint" right there.
Position Object PaintObj,x#,y#,z#
`Now the "paint" isn't in the best place or at the best angle just yet.
`For this simple demo, we will simply face the paint towrds the center of our demo object
`and move it away from the center a little bit. This way we can be sure it's 'outside' the demo object.
`For a .x level, this will be a lot more complicated to achieve, but is not going to be part of this demo.
Point Object PaintObj,Object Position X(1),Object Position Y(1),Object POsition Z(1)
Move Object PaintObj,-.5
`Finally, some visual settings for our paint.
Set Object Cull PaintObj,0
Color Object PaintObj,rgb(255,0,0)
`Increase our PaintObj variable so we use a new object next time. Also set the cycle back value.
PaintObj=PaintObj+1: If PaintObj>400 then PaintObj=101
Endif
EndFunction
"Droids don't rip your arms off when they lose." -H. Solo
REALITY II