📄 pv3dcolladascene.as.svn-base
字号:
StageTools.stage = stage;
super.init3D();
initScene();
}
/**
* @private
*/
protected function initScene():void
{
}
/**
* @private
* Creates the 3 main types of materials and loads external materials.
*
* Note that smooth has been pulled. It was causing rte's about smooth not being a property of the materials. Will have to fix later.
*/
protected function createMaterials():void
{
if(debug) log.debug("extMaterials", extMaterials.dataProvider ? extMaterials.dataProvider.length : 0);
var loadCollada:Boolean = false;
if(extMaterials.dataProvider.length > 0)
{
// reset the materials que
materialsQue = new Dictionary();
var clrMat:ColorMaterial = new ColorMaterial(0x00ff00, .75);
for(var i:Number=0;i<extMaterials.dataProvider.length;i++)
{
// materials are in library with linkage
var materialsListItem:MaterialsListItem = MaterialsListItem(extMaterials.dataProvider[i]);
var mov:MovieMaterial;
switch(materialsListItem.materialType.toLowerCase())
{
case "bitmapassetmaterial":
if(isLivePreview)
{
materialsList.addMaterial(clrMat, materialsListItem.materialName);
}else
{
var bam:BitmapAssetMaterial = materialsListItem.precisionMaterial ? new PreciseBitmapAssetMaterial(materialsListItem.materialLocation) : new BitmapAssetMaterial(materialsListItem.materialLocation);;
loadCollada = true;
bam.oneSide = materialsListItem.singleSided;
if( materialsListItem.interactive ) bam.interactive = materialsListItem.interactive;
if( materialsListItem.smooth ) bam.smooth = materialsListItem.smooth;
if(materialsListItem.precisionMaterial)
{
var pbam:PreciseBitmapAssetMaterial = PreciseBitmapAssetMaterial(bam);
pbam.precision = materialsListItem.precision
pbam.minimumRenderSize = materialsListItem.minimumRenderSize;
}
materialsList.addMaterial(bam, materialsListItem.materialName);
}
if(!checkForFileLoads(extMaterials)) loadCollada = true;
break;
case "bitmapfilematerial":
var fileLocation:String = isLivePreview ? _localPath + materialsListItem.materialLocation : materialsListItem.materialLocation;
fileLocation = fileLocation.split("\\").join("/");
if(debug) log.debug("File to load", fileLocation);
var bm:BitmapFileMaterial = materialsListItem.precisionMaterial ? new PreciseBitmapFileMaterial("") : new BitmapFileMaterial("");
bm.addEventListener(FileLoadEvent.LOAD_COMPLETE, handleBitmapFileLoadComplete);
materialsQue[bm] = false;
// setting the texture property actually causes the load of the file
bm.texture = fileLocation;
if( materialsListItem.interactive ) bm.interactive = materialsListItem.interactive;
if( materialsListItem.smooth ) bm.smooth = materialsListItem.smooth;
// because we didn't set the URL through the constructor, we have to set it manually if we want it back in the event thats disatched
bm.url = fileLocation;
bm.oneSide = materialsListItem.singleSided;
if(materialsListItem.precisionMaterial)
{
var pbm:PreciseBitmapFileMaterial = PreciseBitmapFileMaterial(bm);
pbm.precision = materialsListItem.precision
pbm.minimumRenderSize = materialsListItem.minimumRenderSize;
}
materialsList.addMaterial(bm, materialsListItem.materialName);
break;
case "movieassetmaterial":
if(isLivePreview)
{
materialsList.addMaterial(clrMat, materialsListItem.materialName);
}else
{
mov = materialsListItem.precisionMaterial ? new PreciseMovieAssetMaterial(materialsListItem.materialLocation, materialsListItem.transparent) : new MovieAssetMaterial(materialsListItem.materialLocation, materialsListItem.transparent);
if(materialsListItem.animated) mov.animated = true;
mov.oneSide = materialsListItem.singleSided;
if( materialsListItem.interactive ) mov.interactive = materialsListItem.interactive;
if( materialsListItem.smooth ) mov.smooth = materialsListItem.smooth;
if(materialsListItem.precisionMaterial)
{
var pmov:PreciseMovieAssetMaterial = PreciseMovieAssetMaterial(mov);
pmov.precision = materialsListItem.precision
pmov.minimumRenderSize = materialsListItem.minimumRenderSize;
}
materialsList.addMaterial(mov, materialsListItem.materialName);
}
if(!checkForFileLoads(extMaterials)) loadCollada = true;
break;
case "moviematerial":
if(isLivePreview)
{
materialsList.addMaterial(clrMat, materialsListItem.materialName);
}else
{
var movieClipReference:Sprite = StageTools.buildObjectFromString(materialsListItem.materialLocation) as Sprite;
if( !movieClipReference )
{
trace("please privide a valid MovieClip or sprite instance");
log.error("please privide a valid MovieClip or sprite instance");
break;
}
mov = materialsListItem.precisionMaterial ? new PreciseMovieMaterial(movieClipReference, materialsListItem.transparent) : new MovieMaterial(movieClipReference, materialsListItem.transparent);
if(materialsListItem.animated) mov.animated = true;
mov.oneSide = materialsListItem.singleSided;
if( materialsListItem.interactive ) mov.interactive = materialsListItem.interactive;
if( materialsListItem.smooth ) mov.smooth = materialsListItem.smooth;
if(materialsListItem.precisionMaterial)
{
var pmm:PreciseMovieMaterial = PreciseMovieMaterial(mov);
pmm.precision = materialsListItem.precision
pmm.minimumRenderSize = materialsListItem.minimumRenderSize;
}
materialsList.addMaterial(mov, materialsListItem.materialName);
}
if(!checkForFileLoads(extMaterials)) loadCollada = true;
break;
}
}
}else
{
if(debug) log.debug("*************************** NO MATERIALS TO LOAD***************");
loadCollada = true;
}
if(loadCollada) createColladaScene();
}
/**
* @private
* Checks the load que to make sure all files are loaded before loading the collada scene
*/
private function checkForFileLoads(obj:SimpleDataProvider):Boolean
{
for(var i:Number=0;i<obj.dataProvider.length;i++)
{
var materialsListItem:MaterialsListItem = MaterialsListItem(extMaterials.dataProvider[i]);
if(debug) log.debug("@@@@@@@@@@@@@@@@ checkForFileLoads", materialsListItem.materialType.toLowerCase())
if(materialsListItem.materialType.toLowerCase() == "file") return true;
}
return false;
}
/**
* @private
* When an external file is completely loaded, we receive this event and check to see if all the files have been loaded before loading the collada scene
*/
protected function handleBitmapFileLoadComplete(e:FileLoadEvent):void
{
if(debug) log.debug("%%%%%% handleBitmapFileLoadComplete", e.file);
materialsQue[e.target] = true;
var bm:BitmapFileMaterial = BitmapFileMaterial(e.target);
bm.removeEventListener(FileLoadEvent.LOAD_COMPLETE, handleBitmapFileLoadComplete);
if(collada != null && materialsList.numMaterials > 0) collada.materials = materialsList;
if(colladaFile.length > 0 && checkLoadedQue())
{
if(debug) log.debug("should load collada after getting all bitmaps");
createColladaScene();
}
}
/**
* @private
*/
protected function checkLoadedQue():Boolean
{
for each(var items:Object in materialsQue)
{
if(!items) return false;
}
return true;
}
/**
* @private
*/
override protected function stageResizeHandler(e:Event):void
{
if(!resizeWithStage) return;
if(debug) log.debug("stageResize");
resizeStage();
}
/**
* @private
* Creates the Collada object and loads the file
*/
protected function createColladaScene():void
{
if(colladaFile.length == 0) return;
if(debug) log.debug("createColladaScene", colladaFile, scene == null);
if(collada != null) scene.removeChild(collada);
var fileLocation:String = isLivePreview ? _localPath + colladaFile : colladaFile;
if(debug) log.debug("fileLocation for collada", fileLocation);
if( collada && collada.container ) collada.container.graphics.clear();
collada = new Collada(fileLocation, materialsList, sceneScale,{localPath:localPath});
scene.addChild(collada);
collada.addEventListener(FileLoadEvent.LOAD_COMPLETE, handleLoadComplete);
//collada.addEventListener(FileLoadEvent.COLLADA_MATERIALS_DONE, handleLoadComplete);
collada.addEventListener(FileLoadEvent.LOAD_PROGRESS, handleLoadProgress);
collada.addEventListener(FileLoadEvent.LOAD_ERROR, handleLoadError);
collada.addEventListener(FileLoadEvent.SECURITY_LOAD_ERROR, handleLoadError);
}
/**
* @private
* Called when Collada file is completely rendered
*/
protected function handleLoadComplete(e:FileLoadEvent):void
{
if(debug) log.debug("handleLoadComplete - Collada", e.file);
// remove listeners
collada.removeEventListener(FileLoadEvent.LOAD_COMPLETE, handleLoadComplete);
collada.removeEventListener(FileLoadEvent.LOAD_PROGRESS, handleLoadProgress);
collada.removeEventListener(FileLoadEvent.LOAD_ERROR, handleLoadError);
collada.removeEventListener(FileLoadEvent.SECURITY_LOAD_ERROR, handleLoadError);
finalizeColladaLoad();
}
/**
* @private
* Called when Collada progress event is dispatched
*/
protected function handleLoadProgress(e:FileLoadEvent):void
{
dispatchEvent(new FileLoadEvent(SCENE_LOAD_PROGRESS, e.file, e.bytesLoaded, e.bytesTotal));
}
/**
* @private
* Called when Collada has an error with loading the DAE file specified
*/
protected function handleLoadError(e:FileLoadEvent):void
{
dispatchEvent(new FileLoadEvent(SCENE_LOAD_ERROR, e.file, 0, 0, e.message));
}
/**
* @private
* Called after Collada file is loaded completely. sets the rotation, and updates the scene.
*/
protected function finalizeColladaLoad():void
{
collada.rotationX = rotationList.pitch;
collada.rotationY = rotationList.yaw;
collada.rotationZ = rotationList.roll;
updateScene();
dispatchEvent(new FileLoadEvent(SCENE_COMPLETE));
// set to false so no unnecessary redraws occur
rebuildCollada = false;
if(sceneRotation && !isLivePreview)
{
ObjectController.getInstance().registerStage(this.stage);
ObjectController.getInstance().registerControlObject(collada);
}
// for debugging
if(debug) showChildren();
}
/**
* @private
* Just for testing to see the children of the collada object
*/
private function showChildren():void
{
for each(var item:Object in collada.children) if(debug) trace("collada children: ", item.name);
}
/**
* @private
*/
override protected function handleTimerUpdate(e:TimerEvent):void
{
updateScene();
}
/**
* @private
* Used for matching the materials list that comes in when an update occurs on the component at Designtime. A brand new version is sent with every change to the
* component at design time, so we have to crawl through and verify if it's been changed to really know if we need to re-render the collada scene.
*/
private function checkMaterialListsMatch(obj_0:SimpleDataProvider, obj_1:SimpleDataProvider):Boolean
{
if(obj_0.dataProvider.length != obj_1.dataProvider.length) return false;
for(var i:Number=0;i<obj_0.dataProvider.length;i++)
{
// materials are in library with linkage
var m0:MaterialsListItem = MaterialsListItem(obj_0.dataProvider[i]);
var m1:MaterialsListItem = MaterialsListItem(obj_1.dataProvider[i]);
var props:Array = PropertyTools.getProperties(m0);
for (var ii:Number=0;i<props.length;i++)
{
if(debug) log.debug("compare", m0[props[i].name] + ", " + m1[props[i].name]);
if(m0[props[i].name] != m1[props[i].name]) return false;
}
}
return true;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -