装载和卸载新模型

发布于 2024-10-05 09:14:39 字数 4046 浏览 8 评论 0原文

如果这个问题之前已经发布过,我很抱歉,我根本无法找到我的小问题的解决方案。


我正在使用 pv3d 为学校项目加载几个模型。这些模型通常不会做太多事情,它们只是在启动时在表盘上旋转。

在任何给定时间,仅显示两个模型。我最初的想法是利用两个全局 Collada 实例,并让一个函数调用 Collada 加载函数将新模型加载到实例中。查看 Collada 解析器,加载函数似乎附加了新模型,将现有模型保留在那里,而不是加载一组新的顶点。

很公平。此时,我决定应该从场景中删除模型,并在每次函数触发时创建新模型。

这就是我的问题所在。这可能是因为我对 AS3/pv3d 的工作原理缺乏了解,所以请耐心等待。当我从场景中删除模型并再次添加它们时,模型不会出现在场景中。但是,当我运行跟踪时,模型实例仍然可跟踪

这是供参考的代码。我省略了重复的部分。

实例是在全局级别上创建的,因为如果我在任何其他级别上创建它,onRenderTick 似乎无法引用它

public var model:Collada = new Collada();
public var model2:Collada = new Collada();

初始属性,例如 xy位置和间距在创建转盘时设置

public function setupDials():void {
   var materialList:MaterialsList = new MaterialsList();
   var bitmapFileMaterial:BitmapFileMaterial = new BitmapFileMaterial("assets/images/UV/marble.jpg");
   materialList.addMaterial(bitmapFileMaterial, "all");
   dial = new Collada("assets/Dial.dae", materialList);
   dial2 = new Collada("assets/Dial.dae", materialList);
   dial.scale = 2;
   dial.x = 400;
   dial.y = -100;
   dial.pitch(-10);
   dial2.scale = 2;
   dial2.x = -400;
   dial2.y = -100;
   dial2.pitch(-10);
   scene.addChild(dial);   
   scene.addChild(dial2);

   // run once only
   model.x = 450;
   model.y = 100;
   model.pitch(-10);
   model2.x = -450;
   model2.y = 100;
   model2.pitch(-10);
  }

设置转盘后,使用 loadAnimals() 加载模型

public function loadAnimals(param1:String):void {

   if (!first) {
    scene.removeChild(model);
    scene.removeChild(model2);
    initNewModels();
   } // end if

   first = false;
   model.addEventListener(FileLoadEvent.LOAD_COMPLETE, daeLoaded);
   model2.addEventListener(FileLoadEvent.LOAD_COMPLETE, daeLoaded);   

   if (param1 == "environment1") {
    var leopardMats:MaterialsList = new MaterialsList();
    var bitmapFileMaterial:BitmapFileMaterial = new BitmapFileMaterial(textures[0]);
    leopardMats.addMaterial(bitmapFileMaterial, "all");
    model.load("assets/Leopard.dae", leopardMats);
    model.scale = 2;

    var wolverineMats:MaterialsList = new MaterialsList();
    var bitmapFileMaterial2:BitmapFileMaterial = new BitmapFileMaterial(textures[1]);
    wolverineMats.addMaterial(bitmapFileMaterial2, "all");
    model2.load("assets/Wolverine.dae", wolverineMats);
    model2.scale = 0.7;
   }

   else if (param1 == "environment2") {
    var markhorMats:MaterialsList = new MaterialsList();
    var markhorFileMaterial:BitmapFileMaterial = new BitmapFileMaterial(textures[4]);
    markhorMats.addMaterial(markhorFileMaterial, "all");
    model.load("assets/Markhor.dae", markhorMats);
    model.scale = 2;

    var oryxMats:MaterialsList = new MaterialsList();
    var oryxFileMaterial:BitmapFileMaterial = new BitmapFileMaterial(textures[5]);
    oryxMats.addMaterial(oryxFileMaterial, "all");
    model2.load("assets/Oryx.dae", oryxMats);
    model2.scale = 10;
   }
  }

_adds加载的 daes 到场景中,不言自明

  public function daeLoaded(e:FileLoadEvent):void {
   e.target.removeEventListener(FileLoadEvent.LOAD_COMPLETE, daeLoaded);
   scene.addChild(DisplayObject3D(e.target));
  }

initNewModels() 在从场景中删除模型时调用,以添加新模型

  public function initNewModels():void {
   var model:Collada = new Collada();
   var model2:Collada = new Collada();
   model.x = 450;
   model.y = 100;
   model.pitch(-10);
   model2.x = -450;
   model2.y = 100;
   model2.pitch(-10); 
  } // end initModels function

此时,使用这段代码,发生的情况是模型最初在第一次单击时加载良好。当我尝试加载后续模型时,旋转的转盘仍然存在,但模型却无处可见。但是,当我运行跟踪并返回它们的 x、y、z 坐标时,它们是可追踪的。

我的问题是:

a) 是否有其他方法可以将模型从场景中完全删除?当我调用 scene.removeChild(model) 时,该项目不应该是可追踪的,但它确实是可追踪的。

b) 有更好的方法来解决这个问题吗?我可能不应该从 initNewModels 函数创建新模型,因为在我看来,这样做会导致其他函数无法渲染新模型(因此导致它不可见)。

我更愿意自己尝试解决这个问题,因为这似乎是一个相当简单的问题,但我的截止日期即将到来,我不应该在某件事上停留太久。我感谢所提供的任何帮助。非常感谢您看完本文!

My apologies if this question has been posted before, I am simply unable to source out the solution to my little problem.


I am using pv3d to load a couple of models for a school project. The models typically don't do much, they just spin on a dial when fired up.

At any given time, there are only two models shown. My initial thought is to utilize two global Collada instances and have a function call the Collada load function to load the new models into the instances. Looking through the Collada parser, it would seem that the load function appends the new model, leaving the existing models up there, as opposed to loading in a new set of vertices.

Fair enough. At this point I decide that I should remove the models from the scene, and create new ones every time the function fires.

Here is where my problem lies. This probably from my lack of understanding on the workings of AS3/pv3d, so please bear with me. When I remove the models from the scene and add them again, the models do not appear within the scene. However, the model instances still remain traceable when I run a trace.

Here is the code for reference. I omitted the portions which are duplicates.

Instances are created on a global level as onRenderTick can't seem to reference it if I create it on any other level

public var model:Collada = new Collada();
public var model2:Collada = new Collada();

The initial properties such as x y position and pitch are set when the dials are created

public function setupDials():void {
   var materialList:MaterialsList = new MaterialsList();
   var bitmapFileMaterial:BitmapFileMaterial = new BitmapFileMaterial("assets/images/UV/marble.jpg");
   materialList.addMaterial(bitmapFileMaterial, "all");
   dial = new Collada("assets/Dial.dae", materialList);
   dial2 = new Collada("assets/Dial.dae", materialList);
   dial.scale = 2;
   dial.x = 400;
   dial.y = -100;
   dial.pitch(-10);
   dial2.scale = 2;
   dial2.x = -400;
   dial2.y = -100;
   dial2.pitch(-10);
   scene.addChild(dial);   
   scene.addChild(dial2);

   // run once only
   model.x = 450;
   model.y = 100;
   model.pitch(-10);
   model2.x = -450;
   model2.y = 100;
   model2.pitch(-10);
  }

After the dials are setup, the models are loaded using loadAnimals()

public function loadAnimals(param1:String):void {

   if (!first) {
    scene.removeChild(model);
    scene.removeChild(model2);
    initNewModels();
   } // end if

   first = false;
   model.addEventListener(FileLoadEvent.LOAD_COMPLETE, daeLoaded);
   model2.addEventListener(FileLoadEvent.LOAD_COMPLETE, daeLoaded);   

   if (param1 == "environment1") {
    var leopardMats:MaterialsList = new MaterialsList();
    var bitmapFileMaterial:BitmapFileMaterial = new BitmapFileMaterial(textures[0]);
    leopardMats.addMaterial(bitmapFileMaterial, "all");
    model.load("assets/Leopard.dae", leopardMats);
    model.scale = 2;

    var wolverineMats:MaterialsList = new MaterialsList();
    var bitmapFileMaterial2:BitmapFileMaterial = new BitmapFileMaterial(textures[1]);
    wolverineMats.addMaterial(bitmapFileMaterial2, "all");
    model2.load("assets/Wolverine.dae", wolverineMats);
    model2.scale = 0.7;
   }

   else if (param1 == "environment2") {
    var markhorMats:MaterialsList = new MaterialsList();
    var markhorFileMaterial:BitmapFileMaterial = new BitmapFileMaterial(textures[4]);
    markhorMats.addMaterial(markhorFileMaterial, "all");
    model.load("assets/Markhor.dae", markhorMats);
    model.scale = 2;

    var oryxMats:MaterialsList = new MaterialsList();
    var oryxFileMaterial:BitmapFileMaterial = new BitmapFileMaterial(textures[5]);
    oryxMats.addMaterial(oryxFileMaterial, "all");
    model2.load("assets/Oryx.dae", oryxMats);
    model2.scale = 10;
   }
  }

_adds the loaded daes onto the scene, self explanatory

  public function daeLoaded(e:FileLoadEvent):void {
   e.target.removeEventListener(FileLoadEvent.LOAD_COMPLETE, daeLoaded);
   scene.addChild(DisplayObject3D(e.target));
  }

initNewModels() is called upon removal of the models from the scene, to add new models

  public function initNewModels():void {
   var model:Collada = new Collada();
   var model2:Collada = new Collada();
   model.x = 450;
   model.y = 100;
   model.pitch(-10);
   model2.x = -450;
   model2.y = 100;
   model2.pitch(-10); 
  } // end initModels function

At this juncture, using this code, what happens is that the models initially load fine on first click. When I try to load subsequent models, the spinning dials remain, but the models are nowhere to be seen. However, they are traceable when I run a trace, returning me their x, y, z coordinates.

My questions are:

a) Is there another way to completely remove the model from the scene? The item should not be traceable when I call for scene.removeChild(model), but yet it is.

b) Is there a better way to approach this? I probably should not be creating the new models from the initNewModels function, as it seems to me that doing it this way would cause the other functions to be unable to render the new models (hence causing it to be not visible).

I would have preferred to try and figure this out on my own as it seems to be a rather simple issue, but my deadline is approaching and I should not dwell on something too long. I appreciate any help rendered. Much thanks for looking this through!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开半夏魅人心 2024-10-12 09:14:39

initNewModels() 函数中,您重新声明 modelmodel2 变量,这样您就不会使用全局范围的 Collada 实例,而是使用新的函数范围变量。

对于你正在做的事情,你真的应该看看 AS3 中的面向对象编程,因为你正在重复很多代码。您应该能够通过一些简单的 OOP 以及 XML 文件(或用于配置的数组)变得更加多产并避免此类错误!

希望它有帮助...

In the initNewModels() function you are re-declaring model and model2 variables so you are not working with the global-scope Collada instances but with new function-scope variables.

For what you are doing you should really give a look to Object Oriented Programming in AS3 because you are duplicating a lot of code. You should be able to be more prolific and avoid these kind of errors with some simple OOP and maybe an XML file (or an array for configuration)!

Hope it helps...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文