[Closed] Interactive bitmaps
Here are some Toys that I have writen in the last few days,
just to improve my skilles :D. The basic idea is based on Bobo’s tutorial
about the basic maxScript renderer combined with some of my ideas about
a joystick gui.
If you have any ideas of other cool stuff that we can do with this simple
engen, you are wellcom to post them here.
I would also like to here any ideas about how to improve the engen and make it faster.
Cheers :buttrock:
Edit:
(04.07.2008) Added New code on [page 2]( http://forums.cgsociety.org/showthread.php?f=98&t=639039&page=2&pp=15) - Spline.
(05.07.2008) Added new feature to Spline demo - 'Bezier / Bezier Corner' Handles.
(06.07.2008) Improved ‘Draw Dot’ function makes it possible to control spline thickness.
(03.07.2008) Added New code on [page 2]( http://forums.cgsociety.org/showthread.php?f=98&t=639039&page=2&pp=15) - Bezier Curve with the Draw Edge technique.
[img] http://img70.imageshack.us/img70/7307/bitmaptest8jc5.jpg [/img]
(03.07.2008) Added New code on [page 2]( http://forums.cgsociety.org/showthread.php?f=98&t=639039&page=2&pp=15) - Draw Edge.
[img] http://img67.imageshack.us/img67/3986/bitmaptest7ve6.jpg [/img]
(16.06.2008) Added New code on [page 2]( http://forums.cgsociety.org/showthread.php?f=98&t=639039&page=2&pp=15) - Bezier Curve.
[img] http://img355.imageshack.us/img355/7579/test06hz6.jpg [/img]
Test #1:
try destroyDialog bitmapTest catch ()
rollout bitmapTest "Bitmap test 1"
(
-- Local Variable Declerations
------------------------------------------
local dim = 100
local jsPos = [dim/2,dim/2]
local mousePressed = false
-- User Interface
------------------------------------------
bitmap bmGraph "" width:dim height:dim
spinner spRadius "Radius:" range:[0.1,1000,10] align:#center fieldwidth:40
spinner spPower "Power:" range:[0,10,10] align:#center fieldwidth:40 scale:0.01
-- Functions
------------------------------------------
fn mainFunc x y =
(
local dist = distance jsPos [x,y]
if dist > spRadius.value do dist = spRadius.value
dist ^ spPower.value / spRadius.value ^ spPower.value
)
fn updateGraph =
(
local newBitmap = bitmap dim dim color:white
for y = 1 to dim - 1 do (
local pixelArr = #()
for x = 1 to dim - 1 do (
local pixelColor = white
case of (
default: (pixelColor *= mainFunc x y)
(y == dim/2): (pixelColor *= if mainFunc x y > 0.7 then 0.7 else mainFunc x y)
(x == dim/2): (pixelColor *= if mainFunc x y > 0.7 then 0.7 else mainFunc x y)
) -- end case
append pixelArr pixelColor
) -- end x loop
setPixels newBitmap [1,y] pixelArr
) -- end y loop
bmGraph.bitmap = newBitmap
) -- end updateGraph fn
fn updateJS pos force:false =
(
if mousePressed or force do (
jsPos = pos - bmGraph.pos
updateGraph()
)
)
fn openDialog =
(
createDialog bitmapTest width:(dim + 10)
)
fn init =
(
updateGraph()
)
fn done =
(
-- cleanup code
gc light:true
)
-- Event Handlers
------------------------------------------
on spRadius changed val do updateGraph()
on spPower changed val do updateGraph()
on bitmapTest mousemove pos do updateJS pos
on bitmapTest lbuttondown pos do mousePressed = true
on bitmapTest lbuttonup pos do mousePressed = false
on bitmapTest rbuttonup pos do updateJS ([dim/2,dim/2] + bmGraph.pos) force:true
on bitmapTest open do init()
on bitmapTest close do done()
) -- end of rollout
bitmapTest.openDialog()
Hold down left mouse button and drag around inside the bitmap to move the circle
Test #2:
try destroyDialog bitmapTest catch ()
rollout bitmapTest "Bitmap test 2"
(
-- Local Variable Declerations
------------------------------------------
local dim = 100
local jsPos = [dim/2,dim/2]
local mousePressed = false
local color1 = black
local color2 = green
local color3 = white
local color4
-- User Interface
------------------------------------------
bitmap bmGraph "" width:dim height:dim
-- Functions
------------------------------------------
fn mainFunc x y =
(
x -= dim / 2
y = dim / 2 - y
local a = jsPos.x - dim / 2
local b = dim / 2 - jsPos.y
local f = (b / (1 + ((x / a) ^ 2)))
if y == f then color4 = color3 else color4 = color2
if b >= 0 then (
case of (
(f < y): 1
(y / f < 0): 1
default: (1 - y / f)
)
) else (
case of (
(f > y): 1
(y / f < 0): 1
default: (1 - y / f)
)
)
)
fn updateGraph =
(
local newBitmap = bitmap dim dim color:color1
for y = 1 to dim do (
local pixelArr = #()
for x = 1 to dim - 1 do (
local pixelColor
case of (
default: (pixelColor = color1 * (mainFunc x y) + color4 * (1 - (mainFunc x y)))
(y == dim/2): (pixelColor = white * 0.5)
(x == dim/2): (pixelColor = white * 0.5)
) -- end case
append pixelArr pixelColor
) -- end x loop
setPixels newBitmap [1,y] pixelArr
) -- end y loop
bmGraph.bitmap = newBitmap
) -- end updateGraph fn
fn updateJS pos force:false =
(
if mousePressed or force do (
jsPos = pos - bmGraph.pos
updateGraph()
)
)
fn openDialog =
(
createDialog bitmapTest width:(dim + 10) height:(dim + 10)
)
fn init =
(
updateGraph()
)
fn done =
(
-- cleanup code
gc light:true
)
-- Event Handlers
------------------------------------------
on bitmapTest mousemove pos do updateJS pos
on bitmapTest lbuttondown pos do mousePressed = true
on bitmapTest lbuttonup pos do mousePressed = false
on bitmapTest rbuttonup pos do updateJS ([dim/2,dim/2] + bmGraph.pos) force:true
on bitmapTest open do init()
on bitmapTest close do done()
) -- end of rollout
bitmapTest.openDialog()
Hold down left mouse button and drag inside the bitmap to see the graph update
Test #3:
try destroyDialog sphereTest catch ()
rollout sphereTest "Sphere Test"
(
-- Local Variable Declerations
------------------------------------------
local dim = 100
local jsPos = [dim/2,dim/2]
local mousePressed = false
-- User Interface
------------------------------------------
bitmap bmGraph "" width:dim height:dim
spinner spRadius "Radius:" range:[0,1000,40] align:#right fieldWidth:40
spinner spZ "Z:" range:[-(dim/2),(dim/2),10] align:#right fieldWidth:40
spinner spMult "Multiply:" range:[0,10,1] align:#right fieldWidth:40
spinner spSteps "Steps:" range:[1,6,1] align:#right fieldWidth:40 type:#integer
-- Functions
------------------------------------------
fn mainFunc x y =
(
local dist = distance [dim/2,dim/2] [x,y]
local z = spRadius.value ^ 2 - dist ^ 2
z = if z < 0.0 then 0.0 else sqrt z
local v1 = [dim/2,dim/2,0] - [jsPos.x,jsPos.y,spZ.value]
local v2 = [dim/2,dim/2,0] - [x,y,z]
local exp = dot (normalize v1) (normalize v2) * spMult.value
if exp < 0.0 then exp = 0.0
if exp > 1.0 then exp = 1.0
if dist < spRadius.value - 2 then exp else (
if dist > spRadius.value + 2 then 1.0 else (
local val = 0.0
for a = 1 to spSteps.value do (
for b = 1 to spSteps.value do (
dist = distance [dim/2,dim/2] [x + (1.0 / a) - 0.5 , y + (1.0 / b) - 0.5]
val += (if dist <= spRadius.value then exp else 1.0) / (spSteps.value ^ 2)
)
)
val
)
)
)
fn updateGraph =
(
local newBitmap = bitmap dim dim color:white
for y = 1 to dim - 1 do (
local pixelArr = #()
for x = 1 to dim - 1 do (
local pixelColor = white
case of (
default: (pixelColor *= mainFunc x y)
(y == jsPos.y): (pixelColor = red)
(x == jsPos.x): (pixelColor = red)
) -- end case
append pixelArr pixelColor
) -- end x loop
setPixels newBitmap [1,y] pixelArr
) -- end y loop
bmGraph.bitmap = newBitmap
) -- end updateGraph fn
fn updateJS pos force:false =
(
if mousePressed or force do (
jsPos = pos - bmGraph.pos
updateGraph()
)
)
fn openDialog =
(
createDialog sphereTest width:(dim + 10)
)
fn init =
(
updateGraph()
)
fn done =
(
-- cleanup code
gc light:true
)
-- Event Handlers
------------------------------------------
on spRadius changed val do updateGraph()
on spZ changed val do updateGraph()
on spMult changed val do updateGraph()
on spSteps changed val do updateGraph()
on sphereTest mousemove pos do updateJS pos
on sphereTest lbuttondown pos do mousePressed = true
on sphereTest lbuttonup pos do mousePressed = false
on sphereTest rbuttonup pos do updateJS ([dim/2,dim/2] + bmGraph.pos) force:true
on sphereTest open do init()
on sphereTest close do done()
) -- end of rollout
sphereTest.openDialog()
Hold down left mouse button and drag around inside the bitmap to light the sphere
from other angles.
Test #4:
try destroyDialog bitmapTest catch ()
rollout bitmapTest "Bitmap test 4"
(
-- Local Variable Declerations
------------------------------------------
local dim = 100
local randArr = #()
local colorArr = #()
local lastPt = 0
local mousePressed = false
-- User Interface
------------------------------------------
bitmap bmGraph "" width:dim height:dim
spinner spNumPts "Num Pts:" range:[1,100,5] type:#integer align:#right fieldwidth:40
spinner spRadius "Radius:" range:[0,10,2.5] align:#right fieldwidth:40
spinner spLines "Lines:" range:[0,50,0] align:#right fieldwidth:40
-- Functions
------------------------------------------
fn mainFunc x y =
(
local dist1 = 999999
local dist2 = undefined
local n = 0
for i = 1 to randArr.count do (
local newDist = distance [x,y] randArr[i]
if newDist <= dist1 do (
dist2 = dist1
dist1 = newDist
n = i
)
)
if dist2 != undefined and abs (dist1 - dist2) < spLines.value then black else (
if dist1 > spRadius.value then colorArr[n] else red
)
)
fn updateGraph =
(
local newBitmap = bitmap dim dim color:white
for y = 1 to dim do (
local pixelArr = #()
for x = 1 to dim - 1 do (
local pixelColor = mainFunc x y
append pixelArr pixelColor
) -- end x loop
setPixels newBitmap [1,y] pixelArr
) -- end y loop
bmGraph.bitmap = newBitmap
) -- end updateGraph fn
fn initRand =
(
randArr = #()
colorArr = #()
for i = 1 to spNumPts.value do (
append randArr [random 0 dim,random 0 dim]
append colorArr (random (color 50 50 50) (color 200 200 200))
)
updateGraph()
)
fn getPt pos =
(
pos = pos - bmGraph.pos
local lastDist = 999999
lastPt = 0
for i = 1 to randArr.count do (
local newDist = distance [pos.x,pos.y] randArr[i]
if newDist < lastDist do (
lastDist = newDist
lastPt = i
)
)
mousePressed = true
)
fn updatePt pos =
(
pos = pos - bmGraph.pos
if mousePressed and lastPt > 0 then (
randArr[lastPt] = pos
updateGraph()
)
)
fn resizeDialog pos =
(
local oldDim = dim
dim = if bitmapTest.width < bitmapTest.height then bitmapTest.width - 20 else bitmapTest.height - 20
bmGraph.width = dim
bmGraph.height = dim
for i = 1 to randArr.count do (
randArr[i] *= dim / oldDim
)
updateGraph()
spLines.pos = [spLines.pos.x,pos.y - 20]
spRadius.pos = [spRadius.pos.x,spLines.pos.y - 20]
spNumPts.pos = [spNumPts.pos.x,spRadius.pos.y - 20]
)
fn openDialog =
(
createDialog bitmapTest width:(dim + 20) style:#(#style_titlebar, #style_border, #style_sysmenu,#style_resizing)
)
fn init =
(
initRand()
)
fn done =
(
-- cleanup code
gc light:true
)
-- Event Handlers
------------------------------------------
on spRadius changed val do updateGraph()
on spLines changed val do updateGraph()
on spNumPts changed val do initRand()
on bitmapTest lbuttondown pos do getPt pos
on bitmapTest lbuttonup pos do mousePressed = false
on bitmapTest mousemove pos do updatePt pos
on bitmapTest resized pos do resizeDialog pos
on bitmapTest open do init()
on bitmapTest close do done()
) -- end of rollout
bitmapTest.openDialog()
Hold down left mouse button and drag around inside the bitmap to see what happens..
Test #5:
try destroyDialog bitmapTest catch ()
rollout bitmapTest "Bitmap test 5"
(
-- Local Variable Declerations
------------------------------------------
local dim = 100.0
local jsPos = [dim / 2 , dim / 2]
local mousePressed = false
-- User Interface
------------------------------------------
bitmap bmGraph "" width:dim height:dim
spinner spGrid "Grid:" range:[0,20,8] align:#right fieldwidth:40
spinner spRadius "Radius:" range:[-100,100,15] align:#right fieldwidth:40
spinner spMulti "Multiply:" range:[-100,100,1] align:#right fieldwidth:40
spinner spAspect "Aspect:" range:[0,1,0.3] align:#right fieldwidth:40 scale:0.01
-- Functions
------------------------------------------
fn mainFunc x y =
(
fn modFunc i x y =
(
local p = [x,y] - [dim,dim] / 2 - ([x,y] - jsPos) / (1 + ((length ([x,y] - jsPos)) / spRadius.value) ^ spMulti.value)
local m = mod (abs(p[i])) (dim / spGrid.value)
m >= ((dim / spGrid.value) * (1 - spAspect.value) / 2) and m < ((dim / spGrid.value) * (1 + spAspect.value) / 2)
) -- end fn
if distance jsPos [x,y] < 2.5 then (
red
) else (
if modFunc 1 x y then (
white
) else (
if modFunc 2 x y then (
white
) else (
black
) -- end y
) -- end x
) -- end Red
)
fn updateGraph =
(
local newBitmap = bitmap dim dim color:white
for y = 1 to dim do (
local pixelArr = #()
for x = 1 to dim - 1 do (
local pixelColor = mainFunc x y
append pixelArr pixelColor
) -- end x loop
setPixels newBitmap [1,y] pixelArr
) -- end y loop
bmGraph.bitmap = newBitmap
) -- end updateGraph fn
fn updateJS pos =
(
if mousePressed do (
jsPos = pos - bmGraph.pos
updateGraph()
)
)
fn openDialog =
(
createDialog bitmapTest width:(dim + 20) style:#(#style_titlebar, #style_border, #style_sysmenu,#style_resizing)
)
fn init =
(
updateGraph()
)
fn done =
(
-- cleanup code
gc light:true
)
-- Event Handlers
------------------------------------------
on spRadius changed val do updateGraph()
on spAspect changed val do updateGraph()
on spGrid changed val do updateGraph()
on spMulti changed val do updateGraph()
on bitmapTest lbuttondown pos do mousePressed = true
on bitmapTest lbuttonup pos do mousePressed = false
on bitmapTest mousemove pos do updateJS pos
on bitmapTest open do init()
on bitmapTest close do done()
) -- end of rollout
bitmapTest.openDialog()
You know what to do…
Nice, thanks for posting!
I especially like the delaunay demo and the bell curve graph thing.
– MartinB
Hey, that’s really cool!
Maybe next time post a small screenshot that links directly to the script. I think I’d be more likely to play then.