Sunday, July 23, 2017

Change material of a primitive object on Papervision object using BitmapFileMaterial

Change Material of a Primitive Object on Papervision3D Using BitmapFileMaterial

When working with Papervision3D, applying custom materials (textures) to 3D objects is a common requirement. One such approach is using the BitmapFileMaterial class. To avoid runtime issues with asset paths, it's best to embed both the 3D model and texture images directly into your ActionScript project.

Step 1: Embed Model and Texture Assets

Use the [Embed] directive to load both your DAE (Collada) model and the associated texture bitmap:

[Embed(source = "assets/Guitar.dae", mimeType = "application/octet-stream")]
private var LoadGuitar:Class;

[Embed(source = "assets/Guitar/guitarTexture.png", mimeType = "application/octet-stream")]
private var LoadGuitarTexture:Class;

By embedding assets this way, you eliminate file path issues and ensure that assets are bundled within the SWF, making your 3D application more portable and reliable.

Next Steps

Once you have the model and texture embedded, you can proceed to:

  • Instantiate the Collada object from the LoadGuitar class
  • Create a BitmapFileMaterial using LoadGuitarTexture
  • Apply the material to the Collada mesh or any primitive you create

Example

Here is a simple example of applying the material to a 3D object:

// Create material from embedded bitmap
var texture:Bitmap = new LoadGuitarTexture() as Bitmap;
var material:BitmapFileMaterial = new BitmapFileMaterial(texture.bitmapData);
material.smooth = true;

// Load the model
var guitarModel:Collada = new Collada(new LoadGuitar(), material);

// Add to scene
scene.addChild(guitarModel);

Conclusion

Using embedded assets with BitmapFileMaterial ensures efficient and portable material application in Papervision3D. This method works great for offline builds or asset-heavy 3D Flash experiences where external loading may cause delays or complications.