A3D加载材质和纹理
“材质”(Material)指的其实是颜色,准确的说,指的是物体表面对射到表面上的色光的RGB分量的反射率。通常材质都包括环境光、漫射光、镜面光和自发光等成分,指的就是对不同的光线,不同颜色分量的反射程度。
“纹理”(texture)指的是位图,把一张图贴到一个表面上去,实际是摹拟了自然事物的漫射材质。因为材质一般只对顶点指定,你不可能对这个平面上的每个像素都指定一种材质。纹理其实就是起这个作用,相当于对这个平面上的每个像素都指定了不同的漫射材质。
原文地址:http://wiki.alternativaplatform.com/Alternativa_8_for_Dummies_Part_II
A3D加载纹理前提是纹理图片大小必须是2的次幂(size 32 px, 64px, 128, 256, 512...), A3D的纹理使用Bitmapdata
1:直接使用常规加载方法(Embed或Loader)加载,然后创建
BitmapTextureResource对象,然后调用BitmapTextureResource.upload()方法将资源上传到GPU
2:使用A3D的TexturesLoader.loadResource(resource:ExternalTextureResource, createTexture3D:Boolean = true, needBitmapData:Boolean = true):void
resource:ExternalTextureResource
createTexture3D:Boolean (default = true) — 是否创建外部贴图资源。
needBitmapData:Boolean (default = true) — 是否保存贴图的位图数据。
或者loadResource(resource:ExternalTextureResource, createTexture3D:Boolean = true, needBitmapData:Boolean = true):void来加载单个纹理
官方示例:
var textureResources:Vector.<ExternalTextureResource> = new Vector.<ExternalTextureResource>(); //vector of ExternalTextureResource
var metalTexture:ExternalTextureResource = new ExternalTextureResource("metal01.jpg"); //create ExternalTextureResource
var electroTexture:ExternalTextureResource = new ExternalTextureResource("electro14.jpg"); //
var sackTexture:ExternalTextureResource = new ExternalTextureResource("sack1.jpg"); //
textureResources.push(metalTexture); //add all ExternalTextureResource in vector
textureResources.push(electroTexture);
textureResources.push(sackTexture);
var textureLoader:TexturesLoader = new TexturesLoader(stage3D.context3D); //create TexturesLoader
textureLoader.loadResources(textureResources, true, false); //and load the texture
如果我们希望取得加载完成后的Bitmapdata实例,可以为TexturesLoader添加Event.COMPLETE事件监听,官方示例:
var textureLoader:TexturesLoader = new TexturesLoader(stage3D.context3D); //create TexturesLoader
textureLoader.loadResources(textureResources, true, true); //load. Here the third parameter must be necessarily true
//True - means that we can get access to the loaded texture bitmapData
textureLoader.addEventListener(Event.COMPLETE, onTextures); //Listen to the end of loading all the textures
private function onTextures(event:Event):void {
var e:TexturesLoaderEvent = TexturesLoaderEvent(event); //Event to give a TexturesLoaderEvent
var bitmapDataVector:Vector.<BitmapData> = Vector.<BitmapData>(e.getBitmapDatas()); //by getBitmapDatas () class TexturesLoaderEvent obtain and record the vector bitmapData
var bitmapResource:BitmapTextureResource = new BitmapTextureResource(bitmapDataVector[0]); //I need say first texture, and its load
bitmapResource.upload(stage3D.context3D); //in the context of our stage
box.setMaterialToAllSurfaces(new TextureMaterial(bitmapResource, 1));// and "paint the" box
}
材质:
A3D支持6种材质: FillMaterial, TextureMaterial, LightMapMaterial, StandartMaterial, VertexLightTextureMaterial, ParserMaterial~~~一个一个来。
1: FillMaterial
简单来说就是“纯色”。构造函数就2个参数,颜色和透明度~~很简单。
2: TextureMaterial
允许填充到纹理模型,不过不支持光照。 这个材质也比较简单,以下是构造函数:
TextureMaterial(diffuseMap:TextureResource = null, opacityMap:TextureResource = null, alpha:Number = 1)
diffuseMap:TextureResource (default = null) — 漫反射贴图资源
opacityMap:TextureResource (default = null) — 透明度贴图资源
alpha:Number (default = 1) - 透明度
3: LightMapMaterial
光线映射材质,此材质支持。

左图未漫反射材质,右为高光材质
box.setMaterialToAllSurfaces(new LightMapMaterial(texture, texture2));
运行效果:

4:StandardMaterial
标准材质,支持漫反射贴图、法线贴图、高光贴图、光泽度贴图和光泽度、高光强度设置
var fileTexture:Vector.<ExternalTextureResource> = new Vector.<ExternalTextureResource>();
var diffuse:ExternalTextureResource = new ExternalTextureResource("Commando.jpg"); //diffuse texture
var normal:ExternalTextureResource = new ExternalTextureResource("Commando_n.jpg"); //NormalMap
var specular:ExternalTextureResource = new ExternalTextureResource("Commando_spec.jpg"); //SpecularMap
var opacity:ExternalTextureResource = new ExternalTextureResource("Commando_o.jpg"); //OpacityMap
fileTexture.push(diffuse); //add to the vector
fileTexture.push(normal);
fileTexture.push(specular);
fileTexture.push(opacity);
var textureLoader:TexturesLoader = new TexturesLoader(stage3D.context3D); //load
textureLoader.loadResources(fileTexture, true, false);
mesh.setMaterialToAllSurfaces(new StandardMaterial(diffuse, normal, specular));
5.VertexLightTextureMaterial
支持动态灯光。如果没有光源,将看不到设置了该材质的 Surface 对象。可填充 Surface 骨骼数少于 35 的 Skin 对象。使用 Skin.divide() 方法可将 Skin 分割为多个带较少骨骼的 Surface 对象。要让贴图正确显示,几何体顶点的 UV 坐标和法线十分重要。
var texture:BitmapTextureResource = new BitmapTextureResource(new DiffTexture().bitmapData);
box.setMaterialToAllSurfaces(new VertexLightTextureMaterial(texture));

6.ParserMaterial
模型文件中设置给对象的默认材质。该材质只可以使用一个通道(漫反射或者高光等)。可填充 Surface 骨骼数少于 41 的 Skin 对象。使用 Skin.divide() 方法可将 Skin 分割为多个带较少骨骼的 Surface 对象。
var textures:Vector.<ExternalTextureResource> = new Vector.<ExternalTextureResource>();
for (var i:int = 0; i < mesh.numSurfaces; i++){
var surface:Surface = mesh.getSurface(i);
var material:ParserMaterial = surface.material as ParserMaterial;
material.renderChannel = "transparent"; //will render the channel "transparent"
if (material != null){
var transparent:TextureResource = material.textures["transparent"]; //create a material
if (transparent != null){
textures.push(transparent);
surface.material = new TextureMaterial(transparent);
}
}
}

浙公网安备 33010602011771号