实现效果

 

主要代码

 

  1 package
2 {
3 import com.adobe.utils.AGALMiniAssembler;
4 import com.adobe.utils.PerspectiveMatrix3D;
5
6 import flash.display.Bitmap;
7 import flash.display.BitmapData;
8 import flash.display.Sprite;
9 import flash.display.Stage3D;
10 import flash.display3D.Context3D;
11 import flash.display3D.Context3DProgramType;
12 import flash.display3D.Context3DTextureFormat;
13 import flash.display3D.Context3DVertexBufferFormat;
14 import flash.display3D.IndexBuffer3D;
15 import flash.display3D.Program3D;
16 import flash.display3D.VertexBuffer3D;
17 import flash.display3D.textures.Texture;
18 import flash.events.Event;
19 import flash.geom.Matrix;
20 import flash.geom.Matrix3D;
21 import flash.geom.Vector3D;
22
23 /**
24 * @author smartqi
25 * @E-mail: [email=408176274@qq.com]408176274@qq.com[/email]
26 * 创建时间:2013-6-29 上午9:36:36
27 *
28 */
29 public class TextureTest extends Sprite
30 {
31
32 private var context:Context3D;
33 private var vertexBuff:VertexBuffer3D;
34 private var indexBuff:IndexBuffer3D;
35 private var vertexData:Vector.<Number>;
36 private var indexData:Vector.<uint>;
37 private var shaderProgram:Program3D;
38 private var perspectiveMatrix:PerspectiveMatrix3D;
39 private var i:int;
40 private var sign:int = 1;
41 private const angleGap:Number = 20;
42 private var angle:Number = 0;
43 private var modelMatrix:Matrix3D;
44 private var viewMatrix:Matrix3D;
45 private var finalMatrix:Matrix3D;
46 private var texture:Texture;
47 [Embed (source = "texture.jpg")]
48 private var textureClass:Class;
49
50
51 public function TextureTest()
52 {
53 var stage3d:Stage3D = stage.stage3Ds[0];
54 stage3d.addEventListener(Event.CONTEXT3D_CREATE,onContextCreate);
55 stage3d.requestContext3D();
56 }
57
58 private function onContextCreate(e:Event):void{
59 context = (e.target as Stage3D).context3D;
60 if(context == null) return;
61 context.enableErrorChecking = true; //允许进行错误检测,release版本应设置
62 context.configureBackBuffer(500,500,0); //设置显示区域的大小
63 setupVertexBuff(); //设置顶点缓冲
64 setupTexture(); //设置纹理缓冲
65 setupShaderProgram(); //设置shander
66 setupPerspectiveMatrix(); //设置投影矩阵
67 initMatrix();
68 addEventListener(Event.ENTER_FRAME,onEnterFrame);
69 }
70
71 private function setupVertexBuff():void{
72 vertexData = Vector.<Number>([
73 // x y z r g b u v
74 40, 40, -40, 1, 0, 0, 0, 0,
75 40, -40, -40, 0, 1, 0, 0, 1,
76 -40, -40, -40, 0, 0, 1, 1, 1,
77 -40, 40, -40, 1, 1, 1, 1, 0
78 ]);
79
80 indexData = Vector.<uint>([0,1,2,0,2,3]);
81 vertexBuff = context.createVertexBuffer(4,vertexData.length/4);
82 vertexBuff.uploadFromVector(vertexData,0,4);
83 indexBuff = context.createIndexBuffer(6);
84 indexBuff.uploadFromVector(indexData,0,6);
85 context.setVertexBufferAt(0,vertexBuff,0,Context3DVertexBufferFormat.FLOAT_3);
86 context.setVertexBufferAt(1,vertexBuff,3,Context3DVertexBufferFormat.FLOAT_3);
87 context.setVertexBufferAt(2,vertexBuff,6,Context3DVertexBufferFormat.FLOAT_2);
88 }
89
90 private function setupTexture():void{
91 var bitmap:Bitmap = new textureClass();
92 texture = context.createTexture(512,512,Context3DTextureFormat.BGRA,true);
93 uploadTextureWithMipmaps(texture,bitmap.bitmapData);
94 context.setTextureAt(0,texture);
95 }
96
97 public function uploadTextureWithMipmaps(dest:Texture, src:BitmapData):void
98 {
99 var ws:int = src.width;
100 var hs:int = src.height;
101 var level:int = 0;
102 var tmp:BitmapData;
103 var transform:Matrix = new Matrix();
104 tmp = new BitmapData(src.width, src.height, true, 0x00000000);
105 while ( ws >= 1 && hs >= 1 )
106 {
107 tmp.draw(src, transform, null, null, null, true);
108 dest.uploadFromBitmapData(tmp, level); //上传不同层次的纹理,满足miplinear映射的需要
109 transform.scale(0.5, 0.5);
110 level++;
111 ws >>= 1;
112 hs >>= 1;
113 if (hs && ws)
114 {
115 tmp.dispose();
116 tmp = new BitmapData(ws, hs, true, 0x00000000);
117 }
118 }
119 tmp.dispose();
120 }
121
122 private function setupShaderProgram():void{
123 var vertexProgram:AGALMiniAssembler = new AGALMiniAssembler();
124 vertexProgram.assemble(Context3DProgramType.VERTEX,
125 "m44 op,va0,vc0\n" +
126 "mov v1,va1\n" +
127 "mov v2,va2\n");
128 var fragmentProgram:AGALMiniAssembler = new AGALMiniAssembler();
129 fragmentProgram.assemble(Context3DProgramType.FRAGMENT,
130 "tex ft0, v2, fs0 <2d,repeat,miplinear>\n" +
131 "mov oc,ft0\n");
132 shaderProgram = context.createProgram();
133 shaderProgram.upload(vertexProgram.agalcode,fragmentProgram.agalcode);
134 context.setProgram(shaderProgram);
135 }
136
137 private function setupPerspectiveMatrix():void{
138 perspectiveMatrix = new PerspectiveMatrix3D();
139 perspectiveMatrix.perspectiveFieldOfViewRH(Math.PI*90/180,1,1,1000); //注意这里的角度使用的是弧度
140 }
141
142 private function initMatrix():void{
143 modelMatrix = new Matrix3D();
144 viewMatrix = new Matrix3D();
145 finalMatrix = new Matrix3D();
146 }
147
148 private function onEnterFrame(e:Event):void{
149 context.clear(0,0,0);
150 angle += angleGap;
151 modelMatrix.identity();
152 modelMatrix.prependRotation(angle,Vector3D.Z_AXIS); //绕着Z轴旋转物体,注意这里的角度使用的是角度
153 if(i>40){
154 sign = -1;
155 }
156 if(i<0){
157 sign = 1;
158 }
159 i += sign;
160 viewMatrix.identity();
161 viewMatrix.prependTranslation(0,0,-50 + i); //将相机向后移,使物体看起来变小了,将相机向前移,使物体看起来变大
162 finalMatrix.identity();
163 finalMatrix.append(modelMatrix);
164 finalMatrix.append(viewMatrix);
165 finalMatrix.append(perspectiveMatrix);
166 context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX,0,finalMatrix,true); //上传最终的顶点转换矩阵,注意这里最后一个参数为true
167 context.drawTriangles(indexBuff,0,2);
168 context.present();
169 }
170 }
171 }

 

参考:http://www.baidu.com/s?wd=%E5%A4%9A%E5%B1%82%E7%BA%B9%E7%90%86%E6%98%A0%E5%B0%84&pn=40&tn=baiduhome_pg&ie=utf-8&rsv_page=1

 

Glow Map (发光纹理)

Dark Map(暗纹理)

Decal Map(贴花纹理)

Detail Map(细节纹理)

Gloss Map(高光纹理)

Bump Map(凹凸纹理)

Normal Map(法线纹理)

Parallax Map(四叉纹理)

Shader Map(着色纹理)

http://www.cnblogs.com/arun/articles/1966255.html

参考文献

http://wenku.baidu.com/view/b27454d7360cba1aa811da02.html

http://shiba.hpe.sh.cn/jiaoyanzu/WULI/showArticle.aspx?articleId=376&classId=4

http://blog.csdn.net/huangzhipeng/article/details/7957233

Flash3D学习计划(四)——学习纹理相关知识,载入一张纹理,并应用于前面的矩形;并学习多层纹理映射相关知识,尝试dark map, glow map的更多相关文章

  1. [转]Android系统Surface机制的SurfaceFlinger服务简要介绍和学习计划

    转自:Android系统Surface机制的SurfaceFlinger服务简要介绍和学习计划 前面我们从Android应用程序与SurfaceFlinger服务的关系出发,从侧面简单学习了Surfa ...

  2. Flash 3D学习计划

    1.理解并记住3D渲染的一般管线流程(一天). 2.理解世界,取景,投影变换,并理解投影坐标系(一天). 3.学习VB,IB相关,理解三角形顶点顺序:在屏幕上显示2D矩形,并实现缩放,平移,旋转(三天 ...

  3. 官网实例详解-目录和实例简介-keras学习笔记四

    官网实例详解-目录和实例简介-keras学习笔记四 2018-06-11 10:36:18 wyx100 阅读数 4193更多 分类专栏: 人工智能 python 深度学习 keras   版权声明: ...

  4. Python学习报告及后续学习计划

    第一次有学习Python的想法是源于寒假在家的时候,高中同学问我是否学了Python(用于深度学习),当时就到b站收藏了黑马最新的教学视频,但是"收藏过等于我看了",后续就是过完年 ...

  5. 酒旗少年狂暖风,至0基本的前端开发project教师们学习计划

    酒旗风暖少年狂,为0基础前端开发project师做学习计划 夜幕降暂时.走到一张废弃已久的书桌前,打开台灯,看到书桌上已经布满灰尘,而桌上的那盆羸弱的文竹已经枝繁叶茂.我擦干净了桌面,坐了下来,把买回 ...

  6. openresty 学习笔记四:连接mysql和进行相关操作

    openresty 学习笔记四:连接mysql和进行相关操作 毕竟redis是作为缓存,供程序的快速读写,虽然reidis也可以做持久化保存,但还是需要一个做数据存储的数据库.比如首次查询数据在red ...

  7. Java知识图谱(附:阿里Java学习计划)

    摘要:     本文主要描绘了Java基础学习过程,给出Java知识结构图,以及阿里Java岗学习计划,对Java学习爱好者.准备及将要从事Java开发方面的同学大有裨益. 温馨提示:     由于C ...

  8. [转] Android资源管理框架(Asset Manager)简要介绍和学习计划

    转自:http://blog.csdn.net/luoshengyang/article/details/8738877 Android应用程序主要由两部分内容组成:代码和资源.资源主要就是指那些与U ...

  9. Android资源管理框架(Asset Manager)简要介绍和学习计划

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8738877 Android应用程序主要由两部分 ...

随机推荐

  1. 哈希URAL 1941 - Scary Martian Word

    A - Scary Martian Word Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I ...

  2. 当网卡收到一个包的目的地址是本主机其他接口的IP时.2

    arp包进入主机后要经过的过滤是:rp_filter rp_filter会过滤网段 所以说不要在进行arp_ignore测试的时候把rp_filter设置成2, 此时就不会对源地址进行路由的检查了 然 ...

  3. 【bzoj2969】矩形粉刷 期望

    题目描述 为了庆祝新的一年到来,小M决定要粉刷一个大木板.大木板实际上是一个W*H的方阵.小M得到了一个神奇的工具,这个工具只需要指定方阵中两个格子,就可以把这两格子为对角的,平行于木板边界的一个子矩 ...

  4. 自动驾驶缺人才?听听David Silver怎么说!

    如今自动驾驶在全球范围内的发展势头愈发“凶猛”,该领域人才也一度被视为“香饽饽”. 即使在美国,自动驾驶工程师的起薪也已经突破了25万美元,我国‘“开价”之高更是令人咋舌. 人才.人才.还是人才!重要 ...

  5. [01]关于TDD、BDD和DDD的一些看法

    在实际的项目中,我们可能随时面对各种不同的需求,它的各个方面的要素决定了我们所采用的开发模式. 比如,它的复杂度如何?所有的需求是否足够清晰?开发人员对相关的业务是否足够了解?项目的工期是否合理?种种 ...

  6. 读入输出优化_C++

    当我们考试时遇到大量的读入或者输出时,这些代码会耗费许多运行程序的时间,导致TL 本来 log2n 的算法因为读入被卡成线性的就太不划算了,所以我们这里要采用读入输出优化 getchar 和 putc ...

  7. vue 项目 webstrom IDE格式化代码规则遵循eslint设置

    首先vue-cli生成了一个项目,开启了eslint的检测, 但是根据webstorm的快捷格式化代码 ctrl+alt+L会造成eslint报错. 解决办法一: 编辑器打开文件 首先,在编辑器里面要 ...

  8. 《Linux命令、编辑器与shell编程》第三版 学习笔记---003 使用multibootusb

    1.下载文件https://codeload.github.com/mbusb/multibootusb-8.9.0.tar.gz,使用命令: tar xvf multibootusb-8.9.0.t ...

  9. 嵌入式Linux支持LCD console【转】

    转自:http://blog.sina.com.cn/s/blog_664c545f0100v9zl.html 转载:http://www.mculee.cn/post/48.html [1]LCD ...

  10. UVA 10359 Tiling

    考虑最左边一列和最左边两列分别可以一个纵方块:2个横方块+2*2: 则f[i]=f[i-1]+2f[i-2]; #include <map> #include <set> #i ...