In this section we deal with how to import a W3D model into your scene and add physics to the model.
You can create complex model using 3D modeling application such as AutoDesk’s Studio Max or Maya, and then import them into Director in the W3D format (Web 3D format is used to import 3D images and text created in third-party rendering software, typically, each rendering application requires its own specific file converter to create W3D files).
Import the W3D file into you Director movie. In my example the W3D model is named as “cup.w3d”. When you have a W3D scene with more than one model, we can get the names of each model as follows.
put member("cup").model[2].name
Inorder to make a clone of the models in the W3D scene we follow five simple steps
- Create a modelresource for a proxy model (approx in the shape of the model to be cloned)
- Create the proxy model and hide it
- Create a cloned model from the W3D scene model
- Add it as a child to the proxy model
- Add the rigid body properties to the proxy model.
-- clone the cup
mr_Cup = tMember.newmodelResource("MRCup",#cylinder)
mr_Cup.topRadius = 1.8
mr_Cup.bottomRadius = 10
mr_Cup.height = 15
model_name = member("cup").model[1].name
m_Cup = tMember.newModel("MCup",mr_Cup)
m_Cup.addmodifier(#meshdeform)
m_Cup.transform.position = vector(10,20,100)
m_Cup.visibility = #none
clone_Cup = tMember.cloneModelFromCastMember("CCup", model_name,member("cup"))
clone_Cup.addmodifier(#meshdeform)
clone_Cup.transform.position = vector(0,25,100)
clone_Cup.transform.rotation = vector(0,180,0)
clone_Cup.scale(0.5, 0.5, 0.5)
tMember.model("MCup").addChild(tMember.model("CCup"), #preserveWorld)
rb_Cup = gDirPhyz.createRigidBody(m_Cup.name,m_Cup.name, #convexshape, #dynamic)
rb_Cup.mass = 2
rb_Cup.restitution = 1
rb_Cup.friction = 1
The CloneModelFromCastMember method is used to copy the model from a W3D cast member, rename it and inserts it into the 3D world. This method also copies the children of the SourceModel, as well as the model resources such as shaders and textures used by the model and its children.
model_name = member("cup").model[1].name
clone_Cup = tMember.cloneModelFromCastMember("CCup", model_name, member("cup"))
member(whichCastmember).cloneModelFromCastMember(newModelName, sourceModelName, sourceCastmember)
The addChild method adds the model as the child of the 3DWorld, and removes it from the list of children of its former parent.
tMember.model("MCup").addChild(tMember.model("CCup"), #preserveWorld)
member(whichCastmember).node(whichParentNode).addChild(member(whichCastmember).node(whichChildNode) {,#preserveWorld})
A reference is given to the camera to add itself to the list of cameras for the sprite. Valid values are #preserveWorld or #preserveParent.
#preserveParent, the parent-relative transform of the child remains unchanged and the child jumps to that transform in the space of its new parent. The child’s world transform is recalculated.
#preserveWorld, the world transform of the child remains unchanged and the child does not jump to its transform in the space of its new parent. Its parent-relative transform is recalculated.
The following example is used to clone a simple W3D scene.
Open Director, insert physics cast member (Insert-> Media Element-> Physics) and name it “physX”. Insert a 3D scenes (Insert-> Media Element-> Shockwave 3D), name the cast member as “3D world”. Insert the W3D scene into your movie and name it as “cup”.
Drag and Drop the “3D world” cast member on the stage, and then drag and drop the physics cast member on the 3D scene. Left click on the 3D scene and add the following code.
property pTimeStep
property pSubSteps
global gDirPhyz
on beginSprite me
tSprite = sprite(me.spriteNum)
tMember = tSprite.member
tMember.resetWorld()
tCamera = tMember.camera[1]
tCamera.transform.position = vector (-3, 35, 250)
pTimeStepMode = #equal
pTimeStep = 0.33
pSubSteps = 5
gDirPhyz = member("physX")
gDirPhyz.Init(member(tMember), vector(1,1,1),#equal, pTimeStep, pSubSteps )
gDirPhyz.gravity = vector(0,-10,0)
-- create the sphere
model
mr_sphere = tMember.newModelResource("Sphere", #sphere)
mr_sphere.radius
= 10
m_sphere = tMember.newModel("Sphere",
mr_sphere)
m_sphere.transform.position = vector(0,15,0)
-- add new shader and
texture to the ball model
tMember.newTexture("Texture01",#fromImageObject,member("MyImage").image)
newShader01 = tMember.newShader("newPainter01",#standard)
newShader01.texture = tMember.texture("Texture01")
tMember.shader("newPainter01").texturemode = #wrapSpherical
m_sphere.shader =
newShader01
-- create a rigid body
for the sphereModel
m_sphere.addmodifier(#meshdeform)
rb_sphere = gDirPhyz.createRigidBody(m_sphere.name,m_sphere.name,#sphere,#dynamic)
rb_sphere.restitution = 0
rb_sphere.mass = 100
-- clone the cup
mr_Cup = tMember.newmodelResource("MRCup",#cylinder)
mr_Cup.topRadius = 1.8
mr_Cup.bottomRadius = 10
mr_Cup.height = 15
model_name = member("cup").model[1].name
m_Cup = tMember.newModel("MCup",mr_Cup)
m_Cup.addmodifier(#meshdeform)
m_Cup.transform.position = vector(10,20,100)
m_Cup.visibility = #none
clone_Cup = tMember.cloneModelFromCastMember("CCup", model_name,member("cup"))
clone_Cup.addmodifier(#meshdeform)
clone_Cup.transform.position = vector(0,25,100)
clone_Cup.transform.rotation = vector(0,180,0)
clone_Cup.scale(0.5, 0.5, 0.5)
tMember.model("MCup").addChild(tMember.model("CCup"), #preserveWorld)
rb_Cup = gDirPhyz.createRigidBody(m_Cup.name,m_Cup.name, #convexshape, #dynamic)
rb_Cup.mass = 2
rb_Cup.restitution = 1
rb_Cup.friction = 1
-- clone the ground
mr_ground = tMember.newModelResource("MRGround", #box)
mr_ground.width
= 300
mr_ground.height
= 3
mr_ground.length
= 300
m_ground = tMember.newModel("MGround", mr_ground)
m_ground.transform.position = vector(0,0,0)
m_ground.addmodifier(#meshdeform)
m_ground.visibility = #none
model_name = member("cup").model[2].name
clone_Ground = tMember.cloneModelFromCastMember("CGround", model_name,member("cup"))
clone_Ground.addmodifier(#meshdeform)
clone_Ground.transform.position = vector(0,0,-150)
clone_Ground.transform.rotation = vector(0,180,0)
clone_Ground.scale(1.5, 1, 1)
tMember.model("MGround").addChild(tMember.model("CGround"), #preserveWorld)
rb_Ground = gDirPhyz.createRigidBody(m_ground.name,m_ground.name, #box,#static)
rb_Ground.mass = 100
rb_Ground.restitution = 0
-- light the scene
tMember.deleteLight(1)
tMember.newLight("Light01",#point)
tMember.light("Light01").transform.position = m_ground.transform.position
end
on endSprite me
if gDirPhyz.isInitialized then
gDirPhyz.destroy()
end if
end
on exitFrame me
rb = gDirPhyz.getrigidbody("Sphere")
if _key.keyPressed(123) then -- left button
rb.applyForce(vector(-10,0,0))
end if
if _key.keyPressed(124) then -- right button
rb.applyForce(vector(10,0,0))
end if
if _key.keyPressed(126) then -- up button
rb.applyForce(vector(0,0,-10))
end if
if _key.keyPressed(125) then -- down button
rb.applyForce(vector(0,0,10))
end if
gDirPhyz.simulate(pTimeStep, pSubSteps)
end
Feedback:
If you have any questions or comments concerning this article, please send me a message at srideviaishwariya@gmail.com
|