Import DAE and add external file as material


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  1. package
  2. {
  3. import flash.display.Bitmap;
  4. import flash.display.BitmapData;
  5. import flash.display.Sprite;
  6. import flash.events.Event;
  7. import flash.geom.Rectangle;
  8.  
  9. import org.papervision3d.events.FileLoadEvent;
  10. import org.papervision3d.lights.PointLight3D;
  11. import org.papervision3d.materials.BitmapColorMaterial;
  12. import org.papervision3d.materials.BitmapFileMaterial;
  13. import org.papervision3d.materials.BitmapMaterial;
  14. import org.papervision3d.materials.ColorMaterial;
  15. import org.papervision3d.materials.WireframeMaterial;
  16. import org.papervision3d.materials.shadematerials.GouraudMaterial;
  17. import org.papervision3d.materials.shadematerials.PhongMaterial;
  18. import org.papervision3d.materials.shaders.GouraudShader;
  19. import org.papervision3d.materials.shaders.PhongShader;
  20. import org.papervision3d.materials.shaders.ShadedMaterial;
  21. import org.papervision3d.materials.utils.MaterialsList;
  22. import org.papervision3d.objects.parsers.DAE;
  23. import org.papervision3d.objects.primitives.Cube;
  24. import org.papervision3d.objects.primitives.PaperPlane;
  25. import org.papervision3d.objects.primitives.Plane;
  26. import org.papervision3d.view.BasicView;
  27.  
  28. public class ShirtModelView extends BasicView
  29. {
  30. private var model:DAE;
  31. private var light:PointLight3D;
  32. private var angle:Number = 0;
  33. private var distance:Number = 250;
  34. private var speed:Number = 0.2;
  35. private var rangeX:Number = 0.02;
  36. private var rangeY:Number = 0.02;
  37. private var rangeZ:Number = 0.05;
  38. private var targetRotationX:Number = 0;
  39.  
  40. [Embed(source="uv-map-MEDIUM.png")]
  41. [Bindable]
  42. public var uvMap:Class;
  43.  
  44. protected var cube:Cube;
  45.  
  46. //-------------------------------------------------------------------------------
  47. public function ShirtModelView() {
  48.  
  49. super(0,0,true);
  50.  
  51. loadModel("t-shirt_with_material_and_uv.dae");
  52.  
  53. //_camera.zoom = 1000 / _camera.focus + 1;
  54.  
  55. //camera.far = camera.far *4;
  56.  
  57. //camera.useCulling = false;
  58.  
  59. light = new PointLight3D();
  60. light.z = -200;
  61. light.y = 300;
  62.  
  63. scene.addChild(light);
  64.  
  65. /*var cubeMaterials:MaterialsList = new MaterialsList();
  66. var colorMaterial:ColorMaterial = new ColorMaterial(0xff0000,0.5);
  67. colorMaterial.doubleSided = true;
  68.  
  69. cubeMaterials.addMaterial(colorMaterial,"all");
  70.  
  71. cube = new Cube(cubeMaterials,500,500,500);
  72.  
  73.  
  74. scene.addChild(cube);
  75.  
  76. camera.target = cube;*/
  77.  
  78. startRendering();
  79. }
  80.  
  81. //-------------------------------------------------------------------------------
  82. private function loadModel(path:String) : void
  83. {
  84. model = new DAE(false,"model");
  85.  
  86. var mlist:MaterialsList = new MaterialsList();
  87.  
  88. var bitmapMaterial:BitmapMaterial = new BitmapMaterial(Bitmap(new uvMap()).bitmapData);
  89. bitmapMaterial.smooth = true;
  90.  
  91. var shader:GouraudShader = new GouraudShader(light,0xffffff,0x000000,10);
  92. var shadedMaterial:ShadedMaterial = new ShadedMaterial(bitmapMaterial,shader);
  93.  
  94. //var shader:PhongShader = new PhongShader(light,0xffffff,0x333333,25);
  95. //var shadedMaterial:ShadedMaterial = new ShadedMaterial(bitmapMaterial,shader);
  96.  
  97.  
  98. mlist.addMaterial(shadedMaterial,"teamkit-uv-map_jp");
  99.  
  100. //*
  101. model.load(path,mlist);
  102. model.addEventListener(FileLoadEvent.LOAD_COMPLETE, modelLoaded);
  103. //model.culled = false;
  104. //*/
  105. }
  106.  
  107. //-------------------------------------------------------------------------------
  108. private function modelLoaded(event : FileLoadEvent) : void
  109. {
  110. trace("materials:"+model.materials);
  111.  
  112. light.lookAt(model);
  113.  
  114. model.scaleX = model.scaleY = model.scaleZ = 5;
  115.  
  116. /*
  117. var bmpData:BitmapData = model.getMaterialByName("teamkit-uv-map_jp").bitmap;
  118. bmpData.fillRect(new Rectangle(10,10,200,200),0xff0000);
  119. */
  120.  
  121. scene.addChild(model);
  122. camera.target = model;
  123. }
  124. //-------------------------------------------------------------------------------
  125. override protected function onRenderTick(e:Event=null):void
  126. {
  127. model.yaw(0.1);
  128. //cube.yaw(0.5);
  129. //*
  130. camera.x += (mouseX - (stage.stageWidth * 0.5) - (camera.x * rangeX)) * speed;
  131. camera.y += (mouseY- (stage.stageHeight * 0.5) - (camera.y * rangeY)) * speed;
  132. camera.z += ((-mouseY * 2) - camera.z ) * rangeZ;
  133.  
  134. camera.moveForward(camera.distanceTo(model) - distance);
  135. //*/
  136. super.onRenderTick(e);
  137. }
  138. }
  139. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.