3D Models Adding Behaviors to 3D Models

Posted: May 13, 2011 |Comments: 0 |

The movie above is scarcely spectacular. If you click on the rotating Box model, the background color of the 3D sprite changes, and a message is displayed telling you which face of which mesh has been clicked 3D Models. Wow.

The big deal is that the code that does this is attached to the Box model itself, in the same way that a behavior is attached to a 2D sprite.

If you thought that you can't attach behaviors directly to models, then you are right. Almost. What you can do is add script instances to the userData list which is a property of every 3D node.

Behaviors have four main advantages over the script-instance-in-a-userData-list approach:

  1. You can drop a behavior directly onto a sprite
  2. You can set the parameters of the behavior without writing any code
  3. The built-in spriteNum property allows you to obtain a pointer to the sprite the behavior is attached to
  4. Director dispatches mouse and keyboard events automatically

You'll see how to work around (or to live with) these limitations in what follows.

Adding a behavior to the userData list

Here's the handler that creates the Box model, and which attaches an instance of the script("Model Instance") to the userData list. As you can see, adding the instance requires only a couple of code. If parameters were required, they could be set in the on new() handler, or in a separate on init() handler if you prefer to work that way. If you are prepared to get your braincells dirty by working in 3D Lingo, the loss of drag and drop functionality and the joys of thegetPropertyDescriptionList() handler are not going to upset you.

on beginSprite(me)
vScene = sprite(me.spriteNum).member
vScene.resetWorld()

-- Create a box model
vResource = vScene.newModelResource("Box", #box)
pModel = vScene.newModel("Box", vResource)

-- Add a "behavior" to the model
vInstance = script("Model Instance").new()
pModel.userData.addProp(#instance, vInstance)
end beginSprite

Dispatching events 3D Models

Attaching an instance to the userData list for a model not in itself enough to trigger event handlers when the user clicks on the model. You need to add a behavior to the 3D sprite that dispatches events to the appropriate userData list. You'll find a complete Pick Action behavior in the download source movie. Here's a simplified which deals only with mouseUp events:

on mouseUp(me)
vSprite = sprite(me.spriteNum)
vLeft = vSprite.left
vTop = vSprite.top

-- Determine which 3D model is under the mouse
tSpriteLoc = point(the mouseH - vLeft, the mouseV - vTop)
tModelData = vSprite.camera.modelsUnderLoc(tSpriteLoc, 1, #detailed)

if not tModelData.count then
-- There are no models under the mouse
exit
end if

-- Retrieve the userData list for the model under the mouse
tModelData = tModelData.getLast() -- only 1 model is asked for
tModel = tModelData.model
tUserData = tModel.userData

-- Call the userData list, sending a pointer to this sprite and
-- the picking data. NOTE: since D8.5 it is possible to "call" a
-- property list.

call(anEvent, tUserData, vSprite, tModelData)
end mouseUp

The Pick Action behavior in the download files is more sophisticated, and sends#mouseDown, #mouseUp, #rightMouseDown and #rightMouseUp events, as required.

Obtaining a pointer to the sprite

Here's a simplifed version of the "behavior" that is added to the Box model'suserData list:

on mouseUp(me, a3DSprite, aModelData)

vMember = a3DSprite.member
vColor = rgb(random(5)-1, random(5)-1, random(5)-1) * 51
vMember.bgColor = vColor
end mouseUp

The me handler points to the "behavior" itself, but you can't get at the sprite by usingsprite(me.spriteNum), so the sprite has to be passed in as a separate parameter. The picking details for the model are passed in as a third parameter. 3D Models This will be a property list similar to the following:

[#model: model("Box"),
#distance: 148.3333,
#isectPosition: vector( 23.0958, 6.8110, -12.9864 ),
#isectNormal: vector( 0.9848, 0.0000, -0.1736 ),
#meshID: 2,
#faceID: 1,
#vertices: [vector( 20.2790, 25.0000, -28.9614 ),
vector( 28.9614, -25.0000, 20.2790 ),
vector( 20.2790, -25.0000, -28.9614 )],
#uvCoord: [#u: 0.3244,
#v: 0.0394]]

Whether you need some or all of this data, the Pick Action behavior has already prepared it for you.

Questions and Answers

Ask
200 Characters left
Rate this Article
  • 1
  • 2
  • 3
  • 4
  • 5
  • 0 vote(s)
    Feedback
    Print
    Re-Publish
    Source:  http://www.articlesbase.com/art-articles/3d-models-adding-behaviors-to-3d-models-4763003.html

    Article Tags:

    3d free models

    ,

    3d models 3d model

    ,

    download free 3ds

    ,

    model free 3ds

    ,

    3d max model

    ,

    free maya model

    ,

    free 3d models

    ,

    download 3d models

    ,

    top 3d models

    Many industries use 3D character design programs to build avatars, create 3D creatures and buildings and to put extra features to their work. For example, architects use 3D character design programs to design....

    By: mahnoorl Arts & Entertainment> Artl Jan 24, 2011

    Modern mass media is no longer as conventional as one may think. In the new age, modern mass media personnel may prefer the use of computer aided graphics in lieu of using physical products in the scenes they wish to show.

    By: all3dmodell Arts & Entertainment> Artl Dec 24, 2010

    The 3D industry is one of the booming career and education domains in our contemporary society. In fact owing to the sweeping development mainly in technology 3D has become the in-thing and core driving concept in...

    By: all3dmodell Arts & Entertainment> Artl Oct 27, 2010 lViews: 105

    3D ANIMATIONS In 2d animation 50% of work is done by the artist himself. Computer comes to the scene in the last sections only. But in 3d the artist becomes a technician. I.e. the all work of the artist is done by 3d software's....

    By: all3dmodell Arts & Entertainment> Artl Aug 25, 2010

    Many of the animation tools that professional production companies use for film applications are available to the general public; however, because the software and equipment

    By: mahnoorl Arts & Entertainment> Artl Jan 24, 2011

    BEAUTIFUL Animations With Minimum Effort! Producing 3D Animations & Graphics has never been THIS EASY The Complete 3D Creation Software Suite & IllusionMage Video Training Package "Easily The Most Powerful 3D Creation Software On The Planet!" Illusion Mage Get it now! http://bit.ly/clUCcW

    By: nourl Advertising> Graphic Designl Nov 19, 2010 lViews: 326

    This article is about learning. The way of learning how to use these powerful tools to dominate or just have fun online.

    By: thomasl Arts & Entertainment> Artl May 25, 2012

    Painting for sale and Art reproduction are in demand from different occasion period. Besides that there is a special time and appeal ,to network with all of them. There is no preplanned to obtain them:- On line or form a shop . You have to be keep in touch with some pointers that can directly lead you there and must provide the relevant material for which you are searching.

    By: robertmoore1l Arts & Entertainment> Artl May 23, 2012
    Ton Pascal

    "Leda And The Swan", by Leonardo da Vinci, is one of history's great art losses. From the surviving copies we can see its beauty, the masterly composition, and the complex emblematic symbolism. Leonardo went beyond the sexual act portrayed until then about this mythic story. The great popularity of this subject in the sixteenth century was due to the fact that it was then more acceptable to depict a woman having an act of copulation with an animal than with a man.

    By: Ton Pascall Arts & Entertainment> Artl May 23, 2012

    Art Reproduction tend to be terrific concept for decorating as they'll immediately provide beauty, add color and can enhance the actual overlook associated with home, office or even library whilst admire the very best quality duplication of unique artwork.

    By: architecturepaintingsl Arts & Entertainment> Artl May 23, 2012

    Oil paintings comes in all shapes and sizes and generally based on a variety of themes that appeal to all tastes. For oil painting, different painting materials are used like canvas and cardboard. Original oil paintings are most valuable art among other existing art, out of them some are valued at millions of dollars.

    By: robertmoore1l Arts & Entertainment> Artl May 22, 2012

    (I-Newswire) August 8, 2009 - Compressed 3D Models Still Look Great with VizUp 3! VizUp unveils a new polygon reduction system that enables anyone to reduce the number of polygons in a complex 3D model up to 90% without much effect on its visual quality

    By: karinl Arts & Entertainment> Artl Apr 02, 2011

    A swift and fast demonstration to a string about images for 2D as well as 3D artwork to be able to create a powerful illusion regarding motion is usually understood seeing that animation. The skill of creating moving items with the effective use of computers has the name computer animation and is also sometimes called computer-generated symbolism or computer-generated image resolution (CGI). A movie has become the forms about presenting toon, as can also be a video program and all kinds of other

    By: karinl Arts & Entertainment> Artl Apr 02, 2011

    I have just began to study Photoshop. Now I have created a 3D model by 3D tool and now I want to paint on the 3D model. I know that we can use 3D tool to realize, but I don't know how to operate. Any help is appreciated.

    By: karinl Arts & Entertainment> Artl Apr 02, 2011

    Human Anatomy 3D Models are comprised of many organs and body parts. These computer graphics are used everyday to bring thousands of animations to life. They are also used to demonstrate the dynamics of breakthrough surgery or to generate educational renders to show doctors about how to use a medical device.

    By: karinl Arts & Entertainment> Artl Apr 02, 2011 lViews: 138

    Discuss this Article

    Author Box
    Articles Categories
    All Categories
    Quantcast