现在的位置: 首页 > 综合 > 正文

Away3d 4材质学习笔记

2013年10月12日 ⁄ 综合 ⁄ 共 5628字 ⁄ 字号 评论关闭
/**
 * @see http://away3d.com/tutorials/Globe_Materials_Tutorial#conclusion
 * Away3d 4材质学习笔记,记下重要的知识点,方便以后查询
 */

BitmapTexture最终由GPU渲染,Away3d会把它上传到GPU,BitmapTexture在TextureMaterial中使用

var moonSurfaceTexture:BitmapTexture = Cast.bitmapTexture(MoonSurfaceDiffuse);
var moonSurfaceMaterial:TextureMaterial = new TextureMaterial(moonSurfaceTexture);

**Geometry是缓冲容器类,记录了大量的三角形
var moonSurfaceGeometry:SphereGeometry = new SphereGeometry(50,100,50);

*Geometry与material结合就形成了mesh,mesh为实际渲染所有的功能,geometry决定物体的形状,material决定物体的shader,mesh也包装了其他一些信息比如transform信息,这样我们才能在场景中放置,缩放和旋转物体。

var moonSurfaceMesh:Mesh = new Mesh(moonSurfaceGeometry,moonSurfaceMaterial);

另一个拥有transform信息的是ObjectContainer3D,

_moon = new ObjectContainer3D();
_moon.rotationY = rand(0,360);
_view.scene.addChild(_moon);
moonSurfaceMesh.x = 1000;
_moon.addChild(moonSurfaceMesh);

Sprite3D 比Mesh要轻量级,Sprite3D 只包含material与transform,不包含geometry,其geometry实质是一个对着相机的平面,所以非常适合用来显示远处的物体或者粒子,Sprite3D与Mesh相比较,三角形要少得多

设置材质的alphaBlending = true混合来使用带有透明材质
var bitmapData:BitmapData = blackToTransparent(Cast.bitmapData(StarTexture));
var starMaterial:TextureMaterial = new TextureMaterial(new BitmapTexture(bitmapData));
starMaterial.alphaBlending = true;

var cubeTexture:BitmapCubeTexture = new BitmapCubeTexture(Cast.bitmapData(PosX),Cast.bitmapData(NegX),Cast.bitmapData(PosY),Cast.bitmapData(NegY),Cast.bitmapData(PosZ),Cast.bitmapData(NegZ),
var skyBox:SkyBox = new SkyBox(cubeTexture);
_view.scene.addChild(skyBox);

材质有一个lightPicker属性,能够与对应的灯光绑定起来
lightPicker是一个对象用于编组和收集场景中的灯光。
我感觉 它的使用就像flash里面的滤镜数组一样DisplayObject.filters = [blurFilter,glowFilter...];
Here, we’ve touched on the light source’s ambient, diffuse and specular values and the materials’ gloss values. The ambient property of the light illuminates objects from no specific origin or direction, simulating a scene’s random reflectance with no specifically
directed shading. In this case, we’ve set it to 1 so that the dark side of our celestial bodies are not completely in darkness. The diffuse property affects the amount in which incident light on the geometry’s normals illuminates it. We’ve set it to 2 to exaggerate
the difference between the lit areas and the ones in darkness. The specular property of the light affects how much the reflecting highlight (depending on the incident direction of the light), the surface normals and the view angle light the surface. Finally,
the gloss property of the material determines how spread out the specular highlight is.

//讲了为什么material与灯光都有ambient,diffuse和specular属性,因为这些属性要与shader相结合,例如相同的灯光打上去,不同的物体其环境反射(ambient)必定不同
One thing to note and that is sometimes confusing is that materials, as lights, share some of the ambient, diffuse and specular properties. These values get combined in the shader so you can have, for instance, different ambient illumination on a set of objects
affected by the same light source.

法线贴图,高光贴图和环境贴图
 a normal map, a specular map and an ambient map 

 //对高光贴图的解释
  Lets start by the specular map, which is the easiest to understand. This map simply acts as a sort of mask for the specular highlight of the material. Wherever the map is white, the specular
  highlight is allowed to exist, wherever it is black, it is not.Values in between attenuate the highlight
//环境贴图解释
 The ambient map is used as a sort of diffuse map wherever no light reaches the geometry.
//法线贴图解释
The normal map is a little more involved and produces the illusion of having a higher poly count on our model using a technique known as normal mapping.

TextureMaterial能够更好地使用这几个类
earthMaterial.normalMap = Cast.bitmapTexture(EarthSurfaceNormals);
earthMaterial.specularMap = Cast.bitmapTexture(EarthSurfaceSpecular);
earthMaterial.ambientTexture = Cast.bitmapTexture(EarthSurfaceNight);

FresnelSpecularMethod菲涅尔镜面法
菲涅尔镜面法的相关解释
 If the viewer is looking down on the surface producing the specular highlights, they’re weak. If instead he is aligned with the surface and looking at it edge on, the highlights are strong. Imagine yourself looking at a pool of water on a sunny day. If you
stand by the pool and look straight down, you will see little light reflected on the surface and you will be able to see the bottom of the pool. If instead you’re in the pool and looking at the water with the sun in the background, you will see little of the
pool’s bottom and see much more light directly reflected from the sun on the surface of the water. 

var earthFresnelSpecularMethod:FresnelSpecualrMethod = new FresnelSpecualrMethod(true);
earthFresnelSpecularMethod.fresnelPower = 1;
earthFresnelSpecularMethod.normalReflectance = .1;
earthFresnelSpecularMethod.shadingModel = SpecularShadingModel.PHONG;
//...
earthMaterial.specularMethod = earthFresnelSpecualrMethod;

var bitmapData:BitmapData = blackToTransparent( Cast.bitmapData( EarthSkyDiffuse ) );
var earthCloudMaterial:TextureMaterial = new TextureMaterial( new BitmapTexture( bitmapData ) );
earthCloudMaterial.alphaBlending = true;
earthCloudMaterial.lightPicker = _lightPicker;
//ignore specular highlights on the clouds by setting its specular property to zero.
earthCloudMaterial.specular = 0;

//翻转一个球体,其他两个轴也可以翻转
earthAtmosphere.scaleX = -1;

使用CompositeDiffuseMethod这个对象及其功能,它暴露了一些shader代码,所以我们可以修改它,
修改代码
private function modulateDiffuseMethod( vo:MethodVO, t:ShaderRegisterElement, regCache:ShaderRegisterCache ):String { 
 var viewDirFragmentReg:ShaderRegisterElement = _atmosphereDiffuseMethod.viewDirFragmentReg;
 var normalFragmentReg:ShaderRegisterElement = _atmosphereDiffuseMethod.normalFragmentReg;
 var temp:ShaderRegisterElement = regCache.getFreeFragmentSingleTemp();
 regCache.addFragmentTempUsages( temp, 1 );
 var code:String = "dp3 " + temp + ", " + viewDirFragmentReg + ".xyz, " + normalFragmentReg + ".xyz\n" +
 "mul " + temp + ", " + temp + ", " + temp + "\n" +   
 "mul " + t + ".w, " + t + ".w, " + temp + "\n";
 regCache.removeFragmentTempUsage( temp );
 return code;
}

//使用光晕滤镜
_bloomFilter = new BloomFilter3D( 2, 2, 0.5, 0, 4 );
_view.filters3d = [ _bloomFilter ];

//3D 与 2D坐标转换
 The most important part of this method is the usage of view.project( vector:Vector3D ). This method takes a scene position in 3D space and transforms it to 2D screen space.
 
//phong shading 术语解释

http://en.wikipedia.org/wiki/Phong_shading

//GLSL链接网址

http://nehe.gamedev.net/article/glsl_an_introduction/25007/

抱歉!评论已关闭.