Olá pessoal, Muitos de vocês já se perguntaram como trocar o material de um objeto sua durante sua execução, o material de um plane, cylinder ou até mesmo a face de um cubo. Pois bem para fazermos esta mágica utilizamos o método replaceMaterialByName.
Este método consiste em substituir a instância de material que possui o nome indicado.
objeto.replaceMaterialbyName(material:Material,name:String);
Uma das divergências deste método é que o nome deste método deveria se chamar replaceMaterialByInstancia. Para vocês entenderem melhor este problema vou descrever um exemplo com um cubo.
blue = new ColorMaterial(0x0000FF); red = new ColorMaterial(0xFF0000); lista = new MaterialsList() lista.addMaterial(blue, 'front'); lista.addMaterial(blue, 'back'); lista.addMaterial(blue, 'top'); lista.addMaterial(blue, 'bottom'); lista.addMaterial(blue, 'left'); lista.addMaterial(blue, 'right'); cube = new Cube(lista,100,100,100); cubo.replaceMaterialByName(red, 'right');
Teoricamente este código deveria criar um cubo com todas as faces ‘Azul’ e ao final ele colocaria a face da direita com a cor ‘Vermelho’. Porém ele no final deixa todas as faces ‘vermelho’, pois a instancia de material do lado direito ‘right’ é igual a instancia de material utilizado para as outras faces. Para que não aconteça isso é recomendado que cada face tenha sua própria instancia. Agora vamos ao código de nosso tutorial.
Tutorial replaceMaterialbyName
Neste tutorial decidi brincar um pouco mais. Vamos utilizar um pouco de ReflectionView e o componente ColorPicker (componente do próprio Flash)
Criaremos 3 ColorPicker que será responsável por 2 lados diferentes do cubo e um botão para resetar para o estado utilizando os MovieMaterial.
Logica:
package { import fl.controls.ColorPicker; import fl.events.ColorPickerEvent; import flash.display.SimpleButton; import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import org.papervision3d.core.effects.view.ReflectionView; import org.papervision3d.materials.ColorMaterial; import org.papervision3d.materials.MovieAssetMaterial; import org.papervision3d.materials.utils.MaterialsList; import org.papervision3d.objects.primitives.Cube; public class ppv_changeMaterial extends ReflectionView { public var matsmile:MovieAssetMaterial; public var matface:MovieAssetMaterial; public var matLeftRight:MovieAssetMaterial; public var cubo:Cube; public var lista:MaterialsList; public var color1:ColorPicker; public var color2:ColorPicker; public var color3:ColorPicker; public var reset:SimpleButton; public function ppv_changeMaterial() { setChildIndex(viewportReflection, 1); viewportReflection.alpha = 0.8; matsmile = new MovieAssetMaterial("smile"); matface = new MovieAssetMaterial("face"); matLeftRight = new MovieAssetMaterial("face"); lista = new MaterialsList() lista.addMaterial(matface, 'front'); lista.addMaterial(matsmile, 'back'); lista.addMaterial(new ColorMaterial(0xF9EAAD), 'top'); lista.addMaterial(new ColorMaterial(0x000000), 'bottom'); lista.addMaterial(matLeftRight, 'left'); lista.addMaterial(matLeftRight, 'right'); cubo = new Cube(lista, 200, 200, 250); scene.addChild(cubo); cubo.y = 150; color1 = new ColorPicker(); color1.x = 20; color1.y = 100; color2 = new ColorPicker(); color2.x = 20; color2.y = 140; color3 = new ColorPicker(); color3.x = 20; color3.y = 180; reset = new Reset(); reset.x = 20; reset.y = 220; addChild(reset); addChild(color1); addChild(color2); addChild(color3); color1.addEventListener(ColorPickerEvent.CHANGE, changeLeftRight); color2.addEventListener(ColorPickerEvent.CHANGE, changeTopBottom); color3.addEventListener(ColorPickerEvent.CHANGE, changeFrontBack); reset.addEventListener(MouseEvent.CLICK, clickReset); addEventListener(Event.ENTER_FRAME, onEnterFrame); } private function changeLeftRight(e:ColorPickerEvent) { cubo.replaceMaterialByName(new ColorMaterial(e.target.selectedColor), 'left'); cubo.replaceMaterialByName(new ColorMaterial(e.target.selectedColor), 'right'); } private function changeTopBottom(e:ColorPickerEvent) { cubo.replaceMaterialByName(new ColorMaterial(e.target.selectedColor), 'top'); cubo.replaceMaterialByName(new ColorMaterial(e.target.selectedColor), 'bottom'); } private function changeFrontBack(e:ColorPickerEvent) { cubo.replaceMaterialByName(new ColorMaterial(e.target.selectedColor), 'front'); cubo.replaceMaterialByName(new ColorMaterial(e.target.selectedColor), 'back'); } public function clickReset(e:MouseEvent):void { cubo.replaceMaterialByName(matface, 'front'); cubo.replaceMaterialByName(matsmile, 'back'); cubo.replaceMaterialByName(new ColorMaterial(0xF9EAAD), 'top'); cubo.replaceMaterialByName(new ColorMaterial(0x000000), 'bottom'); cubo.replaceMaterialByName(matface, 'left'); cubo.replaceMaterialByName(matface, 'right'); } private function onEnterFrame(e:Event):void { singleRender(); camera.orbit(Math.abs(mouseY+1 / 2), Math.abs(mouseX+1)); } } }
Pode ter certeza que este método será muito útil em diversas de suas aplicações, até a próxima.

