Microsoft PowerPoint - GameProgramming7-Model [호환 모드]

Save this PDF as:
 WORD  PNG  TXT  JPG

Size: px
Start display at page:

Download "Microsoft PowerPoint - GameProgramming7-Model [호환 모드]"

Transcription

1 Overview Model Model class represents a 3D model composed of multiple ModelMesh objects which may be moved independently. ModelMesh class represents a mesh that is part of a Model Spring /16/2012 Kyoung Shin Park ModelMeshPart class represents a batch of geometry information to submit to the graphics device during rendering. Each ModelMeshPart is a subdivision of a ModelMesh object. To learn how to load the data of an.x file into a Model object and render a 3D model. Model Class Bones property Gets a collection of ModelBone objects which describe how each mesh in the Meshes collection for this model relates to its parent mesh. Meshes property Gets a collection of ModelMesh objects which compose the model. Each ModelMesh in a model may be moved independently and may be composed of multiple materials identified as ModelMeshPart objects. Model Class CopyAbsoluteBoneTransformsTo method Copies a transform of each bone in a model relative to all parent bones of the bone into a given array. When using more complicated models, which often use hierarchical structure (where mesh positions, scales, and rotations are controlled by bones ), this method ensures that any mesh is first transformed by the bone that controls it, if such a bone exists. The mesh is then transformed relative to the bone transformation.

2 SimpleModel Draw a Model Load a model using the XNA Framework Content Pipeline You need some art assets (i.e., a 3D model and an associated texture files), and extract its contents to the project Content directory. Add a model p1_wedge.fbx in the Content To load the model by using the Content Pipeline Model model = Content.Load<Model>( Models\\p1_wedge"); This function can load any of the following model formats: FBX and X. Draw a Model Render a model using the XNA Framework Content Pipeline Create a new private method called DrawModel(Model m) pi private void oidda DrawModel(Model Model(Modelm) { Matrix[] transforms = new Matrix[m.Bones.Count]; m.copyabsolutebonetransformsto(transforms); // Draw the model. A model can have multiple meshes, so loop. foreach (ModelMesh mesh in m.meshes) { foreach (BasicEffect effect in mesh.effects) { effect.enabledefaultlighting(); // world, view, projection matrix 중간생략 // Draw the mesh, using the effects set above. mesh.draw(); Draw a Model with a Custom Effect Load and render a model using a custom effect without modifying the Content Pipeline. Add a model Terrain.fbx in the Content Load the model, typically using the Content t Pipeline Model terrain = Content.Load<Model>( Terrain"); Matrix terrainworld = Matrix.Identity; Texture2D terraintex = Content.Load<Texture2D>( TerrainTex ); Load the effect, typically using the ContentManager Effect effect = Content.Load<Effect>( CustomEffect"); t t>( C t t")

3 Draw a Model with a Custom Effect Iterate through each ModelMeshPart in the model, and assign the effect to the Effect property of the ModelMeshPart public static void RemapModel(Model model, Effect effect) { foreach (ModelMesh mesh in model.meshes) { foreach (ModelMeshPart part in mesh.meshparts) { part.effect = effect; Draw a model foreach (ModelMesh mesh in terrain.meshes) { foreach (Effect effect in mesh.effects) { mesh.draw(); Make a Model Move Using Input Connect Xbox360 controller or Use keyboard Create variables to turn and move the model Take input from the user to control the model Make a Model Move Using Input HandleInput private void CheckKeyboardInput(GameTime gametime) { // 중간생략 if (currentkeyboardstate.iskeydown(keys.left)) n(ke s modelrotation += elapsedtime * 3.0f; else if (currentkeyboardstate.iskeydown(keys.right)) modelrotation -= elapsedtime * 3.0f; else if (currentkeyboardstate.iskeydown(keys.up)) { modelvelocity = Vector3.Forward * 3.0f; modelposition += modelvelocity; else if (currentkeyboardstate.iskeydown(keys.down)) { modelvelocity = Vector3.Backward * 3.0f; modelposition += modelvelocity; Draw Models Load and render various models models[0] = Content.Load<Model>( airplane2"); models[1] = Content.Load<Model>( starfish"); models[2] = Content.Load<Model>( cactus");

4 Skinned Model with Skeletal Animation XNA Skinned Model with Skeletal Animation Character animation is widely used in video games. Skinned mesh with skeleton animation is usually used for character animation. This example shows how to create a skinned mesh content processor to import a skinned model through the content pipeline, establish skeletal hierarchy, store and use animation information, blend animation s transformations, and play animations. A skinned model has a skeleton. This skeleton has a number of bones starting from a root. SkinningSample SkinnedModel Sample SkinningSample SkinnedModel AnimationClip, AnimationPlayer, Keyframe, SkinningData

5 SkinningSample SkinningSample SkinnedModelPipeline SkinnedModelProcessor SkinningSample SkinningSample Load dude.fbx Skinned Model with Skeletal Animation Compile SkinningSample_4_0 to get SkinnedModel.dll & SkinnedModelPipeline.dll at SkinningSample_4_0\SkinnedModel\bin\x86\Debug g SkinningSample_4_0\SkinnedModelPipeline\bin\x86\Debug Copy them into content s SkinnedModel directory

6 Skinned Model with Skeletal Animation Add SkinnedModel.dll into the project as a reference Skinned Model with Skeletal Animation Add SkinnedModelPipeline.dll into the content project as a reference Skinned Model with Skeletal Animation Your solution explorer should be like this, showing SkinnedModel.dll & SkinnedModelPipeline Skinned Model with Skeletal Animation Specify SkinnedModelProcessor for dude.fbx Content Processor

7 Skinned Model with Skeletal Animation Skinned Model with Skeletal Animation Set a break point and check the Tag attribute of model Skinned Model with Skeletal Animation SkinnedModel(dude.fbx) & Model(p1_wedge.fbx) Creating a Skinned Creating a Skinned Model in 3DS MAX

8 Create a Skinned Model 3DS Max 에 3D model 을불러온다 Create a Skinned Model All 을 Bone 으로선택한다. - 모델링에서 Bone 만선택이가능하다. F3 을눌러 Wireframe 로바꿔주면, 모델링안의 Biped 가보인다. Create a Skinned Model Create a Skinned Model Time configuration 을누른다 가운데있는다이아몬드형태의도형을더블클릭하여 Biped 전체를선택한다. Animation에서 End Time에원하는프레임 ( 시간 ) 만큼입력해준다. (30fps = 1 초 ) 화면아래쪽의타임라인이 90fps로변경된걸확인할수있다.

9 Create a Skinned Model 전체가선택된상태에서 Auto Key 를눌러활성화시킨후 Time Slider 를 0fps로이동시킨다. Create a Skinned Model 설정이완료되면 0 번과 90 번프레임에표시가되는것을확인할수있다. 앞에서전체선택한 Biped에대한표시이다. (Time Slider) 가장위에있는 Biped 를더블클릭할경우아래 Biped 까지선택이된다. Time Slider 를우클릭하면, 왼쪽과같은화면이나오는데 Destination Time 에첫프레임인 0 과마지막프레임인 90 을입력해준다. - 각각해줘야한다. - 처음과마지막을같은동작으로해줘야자연스럽게움직이는화면처럼보인다. Create a Skinned Model Create a Skinned Model 단축키 Q - 선택 단축키 W - 이동 Timeline 에서움직임이들어갈프레임으로위치를옮긴후 Biped를선택, 움직임을주면자동으로프레임에표시가된다. 단축키 E - 회전 단축키 R - 크기조절 움직임을확인하는방법은버튼을누르면재생이된다. < 단축키 > / : 재생, 정지 < : 이전프레임 > : 다음프레임

10 Tip 모델링에 Texture 가적용되지않았을경우 M 을눌러 Material Editor 창을연다. Tip 중간에 Diffuse 옆 M 이라고써있는버튼을클릭한다. 모델링에 Texture 가적용된모습. Bitmap 옆에 Texture 의경로가있는데버튼을누른후적용하고자하는 Texture 를찾아선택해준다. Tip Tip 각진모델링을부드럽게해주기위해서는 smooth 를해줘야하는데오른쪽 Toolbar 에서두번째메뉴를선택, 아래 Polygon 을선택한다. Polygon 전체가선택된상태에서숫자를누르면 smooth 가적용이된다.

11 Tip Key Info 에서 Set Planted Key 를누르면설정한지점아래로내려가지않는다. Export the Model into.x File 모델링에움직임을넣어주는작업이끝났으면파일을 export 해야한다. File -> export 를선택한다. 파일창이열리면파일형식을 Panda DirectX (*.X) 로선택한후원하는위치에저장을한다. ( 한글이들어간폴더는되도록피한다.) 발모두에 Set Planted Key를적용한상태. 몸전체를내려도지면아래로발이내려가지않고다리를굽힌모습처럼된다. 적용을하지않은경우몸을내리면지면아래로내려간다. Export the Model into.x File 저장을누르면옵션창이나오는데왼쪽과같이 Bones에체크를해야한다. 하지않을경우애니메이션이없는모델링만추출이된다. Export the Model into.x File Textures &.fx files 탭에서는 Texture의파일형식변경이가능하다. Animation 탭에서는이름을기억하고있어야한다. XNA SkinningSample에서불러올때사용한이름을써야하기때문이다. Start, End는시작프레임과끝나는프레임을정해준다. 설정이끝난후확인을누르면저장된위치에.X 파일과 Texture 파일이저장된다. Edit 에서수정이가능하다.

12 Export the Model into.x File Using XNA SkinningSample Export 한.x 파일은 DirectX Viewer 로확인이가능하다. 모델링에 Texture가적용되지않을경우 Texture의이름이바뀌었거나위치가잘못되었기때문에위치설정을다시해줘야한다. 앞에서 export 한.x 파일을 Content 에서불러온다. polar_bear.x 파일을선택한후속성에서 Content Processor을 SkinnedModelProcessor을선택한다. Using XNA SkinningSample SkinningSample.cs 에서수정할부분 protected override void LoadContent() { // Load the model. currentmodel = Content.Load<Model>("polar_bear\\polar_bear"); d l l b // Load Content에서북극곰모델링의위치를지정해준다. Using XNA SkinningSample 실행을하면움직이는북극곰을확인할수있다. if (skinningdata == null) throw new InvalidOperationException ("This model does not contain a SkinningData tag."); // Create an animation i player, and start decoding di an animation i clip. animationplayer = new AnimationPlayer(skinningData); AnimationClip clip = skinningdata.animationclips["anim-1"]; 1]; // 앞에서.x파일을 export할때사용한이름으로수정한다. (Take 001->Anim-1) animationplayer.startclip(clip);

13 Kinect XNA Skinned Model Skinned Model + Kinect