Snippets

John Martini gravitation maxscript

Created by John Martini
/* scene setup */
clearlistener()
delete objects

target = convertToMesh (geosphere radius:30 wirecolor:[40,40,40] segs:5)

lite_pt = point size:2 wirecolor:yellow
lite = omnilight parent:lite_pt pos:[0,0,200]
lite_falloff = 30

pin = box length:4 height:4 width:4 widthsegs:1 heightsegs:1 lengthsegs:1
pin_min_height = 4
pin_max_height = 30

-- animate light distance variation 
lite_offset = 20
lite.showtrajectory = true
with animate on for t = animationrange.start to animationrange.end by 10 do
(
	at time t (lite.pos.z = 60 + random -lite_offset lite_offset)
)

-- animate rotate of lite's parent pt
with animate on for t = animationrange.start to animationrange.end by 10 do
(
	at time t (rotate lite_pt (eulerAngles (random -360 360) (random -360 360) (random -360 360)))
)

fn GetVectorsAngle v1 v2 =
(
	local d = dot (normalize v1) (normalize v2)
	d = amax d -1.0
	d = amin d 1.0
	acos d
)

fn remap_value val from1 to1 from2 to2 =
(
	return ((val - from1) / (to1 - from1) * (to2 - from2) + from2)
)


fn clamp minVal maxVal val =
(
	if val < minVal then val = minVal
	else if val > maxVal then val = maxVal
	return val
)

fn place_pins pinObj surfaceObj influenceObj =
(
	local vertCount = surfaceObj.numverts
	for i = 1 to vertCount do
	(
		local vertFaces = meshop.getFacesUsingVert surfaceObj i

		-- calculate normal
		local tNormal = [0,0,0]
		for f in vertFaces do tNormal += getFaceNormal surfaceObj f
		local vec = normalize(tNormal / vertFaces.numberSet)

		local vPos = getVert surfaceObj i
		local thePin = copy pinObj
		local theTM = matrixFromNormal vec

		thePin.transform = theTM
		thePin.pos = vPos
		thePin.wirecolor = black
		thePin.material = StandardMaterial()

		with animate on for t = animationrange.start to animationrange.end by 1 do
		(
			at time t
			(
				-- user ang between pin and lite to influence height
				local ang = GetVectorsAngle vec influenceObj.pos
				local mult = remap_value ang 0 50 1.0 0.0
				mult = clamp 0 1.0 mult
				val = mult * pin_max_height
				thePin.height = val + pin_min_height

				col = black * (1 - mult) + red * mult
		 		thePin.material.diffuse = col
	 		)
 		)
	)
)


place_pins pin target lite











-- fn setupDistanceColorController mat source target maxdistance:100 = 
-- ( 
-- 	c = mat.diffuse.controller = point3_script()
-- 	c.AddConstant "maxdistance" maxdistance
-- 	c.AddObject "source" (NodeTransformMonitor node:source)
-- 	c.AddObject "target" (NodeTransformMonitor node:target)

--  	str =  "if isvalidnode source and isvalidnode target then\n"
--  	str += "(\n"
--  	str += " c = amin 1.0 ((distance source target)/maxdistance)\n"
-- 	str += " \n"
--  	str += ")\n"
-- 	str += "else [0,0,0]"
-- 	print str

--  	c.SetExpression str
--   	c
-- )

-- (
-- 	delete objects
-- 	mat = standard name:"Distance Meter Material"
-- 	source = box name:"source" isselected:on material:mat
-- 	target = box name:"target" wirecolor:green
-- 	setupDistanceColorController mat source target
-- )

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.