---
title: "[转] MoleHill操作AGAL的Helper类之一"
author: "AS3"
date: "2011-04-29T01:46:38Z"
tags: ["C", "F#", "Flash", "VB", "VC++"]
categories: ["编程语言"]
slug: "molehill-agal-helper"
draft: false
lastmod: "2011-04-29T01:46:38Z"
id: 1022718
url: "https://as3.iteye.com/blog/1022718"
---

本帖最后由 sleep2death 于 2011-4-20 16:49 编辑 应7yue大大的要求,贴写小东西. \[code="java"] package shader { import flash.display3D.\*; import flash.display3D.textures.\*; import flash.geom.Matrix3D; import com.adobe.utils.\*; public class AGALHelper { public static var context : Context3D; public static var program : Program3D; public static var vaNames : Vector. = new Vector.(8); // MAX 8 public static var fsNames : Vector. = new Vector.(8); // MAX 8 public static var vcNames : Vector. = new Vector.(128); // MAX 128 public static var fcNames : Vector. = new Vector.(28); // MAX 28 public static var vtNames : Vector. = new Vector.(8); // MAX 8 public static var ftNames : Vector. = new Vector.(8); // MAX 8 public static var vNames : Vector. = new Vector.(8); // MAX 8 public static const FLOAT : uint = 0; public static const FLOAT2 : uint = 1; public static const FLOAT3 : uint = 2; public static const FLOAT4 : uint = 3; public static const VERTEX : uint = 0; public static const FRAMENT : uint = 1; public static function getAttributeIndex(name : String) : int { var insertIndex : int = vaNames.indexOf(name); if(insertIndex &lt; 0) { insertIndex = vaNames.indexOf(null); vaNames\[insertIndex] = name; trace("assign " + name + " to va" + insertIndex); } return insertIndex; } public static function setAttribute(name : String, buffer : VertexBuffer3D, offset : uint = 0, format : String = "float4") : void { context.setVertexBufferAt(getAttributeIndex(name), buffer, offset, format); } public static function getVertexConstantIndex(name : String) : int { var insertIndex : int = vcNames.indexOf(name); if(insertIndex &lt; 0) { insertIndex = vcNames.indexOf(null); vcNames\[insertIndex] = name; trace("assign " + name + " to vc" + insertIndex); } return insertIndex; } public static function setVertexConstant(name : String, value : \*) : void { if(value is Vector.){ context.setProgramConstantsFromVector(Context3DProgramType.VERTEX, getVertexConstantIndex(name), value); }else if(value is Matrix3D){ context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, getVertexConstantIndex(name), value, true); } } public static function getFragmentConstantIndex(name : String) : int { var insertIndex : int = fcNames.indexOf(name); if(insertIndex &lt; 0) { insertIndex = fcNames.indexOf(null); fcNames\[insertIndex] = name; trace("assign " + name + " to fc" + insertIndex); } return insertIndex; } public static function setFragmentConstant(name : String, value : \*) : void { if(value is Vector.){ context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT, getVertexConstantIndex(name), value); }else if(value is Matrix3D){ context.setProgramConstantsFromMatrix(Context3DProgramType.FRAGMENT, getVertexConstantIndex(name), value, true); } } public static function getSampleIndex(name : String) : int { var insertIndex : int = fsNames.indexOf(name); if(insertIndex &lt; 0) { insertIndex = fsNames.indexOf(null); fsNames\[insertIndex] = name; trace("assign " + name + " to fs" + insertIndex); } return insertIndex; } public static function setSample(name : String, value : Texture) : void { context.setTextureAt(getSampleIndex(name), value); } public static function getIndexByName(vf : String, type : String, name : String) : uint { var first : String = type == "v" ? "v" : vf + type; var names : Vector. = AGALHelper\[first + "Names"]; var insertIndex : int = names.indexOf(type + "\_" + name); if(insertIndex &lt; 0) { insertIndex = names.indexOf(null); names\[insertIndex] = type + "\_" + name; trace("assign " + type + "\_" + name + " to " + first + insertIndex); } return insertIndex; } public static var v\_code : String = new String(); public static function addVertexCode(op\_code : String, out : String, in1 : String ,in2 : String = null, extra : String = null) : void { if(op\_code == "len"){ ExtraCode\_len(out, in1); }else{ v\_code += op\_code + " " + parseParam(out, "v") + ", " + parseParam(in1, "v"); if(in2 != null) v\_code += " " + parseParam(in2, "v"); if(extra != null) v\_code +=" " + extra; v\_code += " \\n"; } } public static var f\_code : String = new String(); public static function addFragmentCode(op\_code : String, out : String, in1 : String ,in2 : String = null, extra : String = null) : void { f\_code += op\_code + " " + parseParam(out, "f") + ", " + parseParam(in1, "f"); if(in2 != null) f\_code += " " + parseParam(in2, "f"); if(extra != null) f\_code +=" " + extra; f\_code += " \\n"; } private static var v\_assembler : AGALMiniAssembler = new AGALMiniAssembler(); private static var f\_assembler : AGALMiniAssembler = new AGALMiniAssembler(); public static function uploadCode() : void { v\_assembler.assemble(Context3DProgramType.VERTEX, v\_code); f\_assembler.assemble( Context3DProgramType.FRAGMENT, f\_code); if(!program){ program = context.createProgram(); context.setProgram(program); } program.upload(v\_assembler.agalcode, f\_assembler.agalcode); } public static function printCode() : void { trace("======== VERTEX ========"); trace(v\_code); trace("======== FRAGMENT ========"); trace(f\_code); } private static function parseParam( code : String, vf : String ) : String { var prop : Array = code.split("."); var prop\_str : String; if(prop.length &gt; 1) { code = prop\[0]; prop\_str = "."+prop\[1]; }else{ prop\_str = ""; } var codes : Array = code.split("\_"); if(codes.length &gt; 1) return (codes\[0] == "v" ?"" : vf) + codes\[0] + getIndexByName(vf, codes\[0], codes\[1]) + prop\_str; return code + prop\_str; } private static function ExtraCode\_len(out : String, in1 : String) : void { addVertexCode("dp3", out, in1, in1); addVertexCode("sqr", out, out); } } }\[/code] 如何使用呢? 先用helper设置一些属性: AGALHelper.setVertexConstant("c\_a1", Vector.(\[1.025])); AGALHelper.setAttribute("a\_vb", vertexBuffer)); 然后写汇编 AGALHelper.addVertexCode("mov", "t\_vb", "a\_vb"); AGALHelper.addVertexCode("mov", "t\_uvb", "a\_uvb"); AGALHelper.addVertexCode("mov", "t\_nb", "a\_nb"); AGALHelper.addVertexCode("sub", "t\_v3Ray", "t\_vb", "c\_v3CameraPos.xyz"); AGALHelper.addVertexCode("len", "t\_fFar.xyz", "t\_v3Ray"); AGALHelper.addVertexCode("m44", "op", "t\_vb", "c\_proj"); AGALHelper.addVertexCode("mov", "v\_uvb", "t\_uvb"); AGALHelper.addVertexCode("mov", "v\_nb", "t\_nb"); AGALHelper.addFragmentCode("mov", "t\_uvb", "v\_uvb"); AGALHelper.addFragmentCode("tex", "t\_day", "t\_uvb", "s\_day", "&lt;2d, clamp, linear&gt;"); AGALHelper.addFragmentCode("mov", "oc", "t\_day"); AGALHelper.printCode(); AGALHelper.uploadCode(); 好处是你再也不要去记ft0, fc1到底是啥了...另外可以新增一些方法,比如length等等