Skip to content

Instantly share code, notes, and snippets.

@ifree
Created October 31, 2012 02:23
Show Gist options
  • Save ifree/3984438 to your computer and use it in GitHub Desktop.
Save ifree/3984438 to your computer and use it in GitHub Desktop.
starling custom display object not work
package com.ifree.common.test
{
import com.adobe.utils.AGALMiniAssembler;
import flash.display3D.Context3D;
import flash.display3D.Context3DProgramType;
import flash.display3D.Context3DVertexBufferFormat;
import flash.display3D.IndexBuffer3D;
import flash.display3D.VertexBuffer3D;
import flash.display3D.textures.Texture;
import flash.geom.Matrix;
import flash.geom.Rectangle;
import starling.core.RenderSupport;
import starling.core.Starling;
import starling.display.DisplayObject;
import starling.errors.MissingContextError;
import starling.events.Event;
/**
* ...
* @author ifree
*/
public class Cave extends DisplayObject
{
private static var PROGRAM_NAME:String = "cave";
// helper objects (to avoid temporary objects)
private static var sHelperMatrix:Matrix = new Matrix();
private static var sRenderAlpha:Vector.<Number> = new <Number>[1.0, 1.0, 1.0, 1.0];
//material
private var vertices:Vector.<Number>;
private var uvs:Vector.<Number> ;
private var indices:Vector.<uint> ;
protected var vertexBuffer:VertexBuffer3D;
protected var colorBuffer:VertexBuffer3D;
protected var normalBuffer:VertexBuffer3D;
protected var uvBuffer:VertexBuffer3D;
protected var indexBuffer:IndexBuffer3D;
private static var vertexShader:String =
"m44 op, va0, vc0\n" +
"mov v0.xy, va1.xy\n" +
"mov v0.zw, va0.zw";
private static var fragmentShader:String =
"tex ft1, v0, fs0 <2d,linear,nomip>\n" +
"mov oc, ft1";
private var mTexture:Texture;
public function Cave(texture:Texture)
{
mTexture=texture;
vertices = Vector.<Number>([-0.2,-9.1,-0.8,0.3,9.2,0.9,5.4,-7.8,-2.4,5.5,-8.1,0.6,4.1,-8.4,3.3,1.5,-8.4,4.9,8.0,-0.9,-8.6,9.9,4.6,-2.4,10.0,4.1,2.8,7.6,3.7,7.4,3.1,3.6,10.1,-1.9,-7.3,-6.3,1.2,-7.3,-6.4,3.8,-7.6,-5.0,9.5,-3.8,-3.8,9.8,-5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0.649755,0.0719255,0.319063,0.41742,0.344397,0.255846,0.377448,0.179146,0.387316,0.100763,0.377556,0.0150489,0.848348,0.269238,0.773812,0.300987,0.699865,0.28775,0.534682,0.318699,0.528126,0.198263,0.532866,0.111775,0.554235,0.0233535,0.0810332,0.29004,0.173996,0.326621,0.26872,0.307559,0.274553,0.194672,0.298233,0.152912,0.940121,0.371831,0.777663,0.436092,0.626791,0.401898,0.411831,0.306328,0.449486,0.195403,0.459207,0.104084,0.465587,0.00527865,0.303297,0.103339,0.287524,0.052627,0.128835,0.20822,0.180531,0.228152,0.232632,0.222302,0.0340293,0.499998,0.186919,0.459514,0.994721,0.430666,0.187318,0.505693,0.332289,0.448449,0.873853,0.408809,0.0969606,0.534548,0.113598,0.468689,0.236608,0.453739,0.273213,0.477887,0.284755,0.437543,0.349039,0.395431,0.401823,0.397688,0.557253,0.389892,0.637839,0.441058,0.681075,0.418773,0.701492,0.468279,0.833041,0.476613,0.0685763,0.328878,0.0979602,0.366627,0.183995,0.377056,0.225723,0.313931,0.235994,0.367251,0.198708,0.870523,0.237423,0.34019,0.183786,0.33368,0.219777,0.789052,0.111588,0.364165,0.00182043,0.871685,0.191428,0.36496,0.182912,0.929338,0.224193,0.320031,0.364287,0.790868,0.232201,0.360586,0.309682,0.934234,0.229305,0.340448,0.351604,0.870348,0.00527866,0.438342,0.0717758,0.444994,0.054603,0.375215,0.0658184,0.390204,0.523592,0.324464,0.824164,0.785412,0.774591,0.451281,0.307724,0.706818,0.615116,0.409951,0.591224,0.698313,0.415409,0.311631,0.99818,0.594046,0.0292105,0.512251,0.00182042,0.53046,0.981009,0.455105,0.0189269,0.647621,0.188802,0.51155,0.31241,0.602579,0.335009,0.451385,0.618851,0.614326,0.877022,0.424119,0.143196,0.731291,0.0993983,0.538774,0.101257,0.634986,0.117552,0.475566,0.15704,0.509538,0.239952,0.462727,0.428467,0.497914,0.274839,0.480457,0.482573,0.608933,0.289525,0.445172,0.54605,0.508504,0.357935,0.402441,0.745481,0.549587,0.407746,0.399809,0.792191,0.679107,0.546943,0.393602,0.73585,0.658051,0.635007,0.445613,0.54907,0.611103,0.675319,0.428438,0.483567,0.700477,0.696177,0.475669,0.430414,0.606985,0.83506,0.486609,0.222874,0.616321,0.00923084,0.424229,0.367928,0.874186,0.0674109,0.437542,0.424257,0.949233,0.0487461,0.380758,0.409258,0.789052,0.0579562,0.393797,0.432682,0.828372,0.64849,0.244806,0.625601,0.186647,0.626234,0.127472,0.319063,0.41742,0.344397,0.255846,0.377448,0.179146,0.387316,0.100763,0.773812,0.300987,0.699865,0.28775,0.534682,0.318699,0.528126,0.198263,0.532866,0.111775,0.173996,0.326621,0.26872,0.307559,0.274553,0.194672,0.298233,0.152912,0.777663,0.436092,0.626791,0.401898,0.411831,0.306328,0.449486,0.195403,0.459207,0.104084,0.303297,0.103339,0.180531,0.228152,0.232632,0.222302,0.186919,0.459514,0.187318,0.505693,0.332289,0.448449,0.873853,0.408809,0.0969606,0.534548,0.113598,0.468689,0.236608,0.453739,0.273213,0.477887,0.284755,0.437543,0.349039,0.395431,0.401823,0.397688,0.557253,0.389892,0.637839,0.441058,0.681075,0.418773,0.701492,0.468279,0.833041,0.476613,0.0979602,0.366627,0.183995,0.377056,0.225723,0.313931,0.235994,0.367251,0.198708,0.870523,0.237423,0.34019,0.183786,0.33368,0.219777,0.789052,0.111588,0.364165,0.00182043,0.871685,0.191428,0.36496,0.182912,0.929338,0.224193,0.320031,0.364287,0.790868,0.232201,0.360586,0.309682,0.934234,0.229305,0.340448,0.351604,0.870348,0.0717758,0.444994,0.0658184,0.390204,0.523592,0.324464,0.824164,0.785412,0.774591,0.451281,0.307724,0.706818,0.615116,0.409951,0.591224,0.698313,0.415409,0.311631,0.99818,0.594046,0.188802,0.51155,0.31241,0.602579,0.335009,0.451385,0.618851,0.614326,0.877022,0.424119,0.143196,0.731291,0.0993983,0.538774,0.101257,0.634986,0.117552,0.475566,0.15704,0.509538,0.239952,0.462727,0.428467,0.497914,0.274839,0.480457,0.482573,0.608933,0.289525,0.445172,0.54605,0.508504,0.357935,0.402441,0.745481,0.549587,0.407746,0.399809,0.792191,0.679107,0.546943,0.393602,0.73585,0.658051,0.635007,0.445613,0.54907,0.611103,0.675319,0.428438,0.483567,0.700477,0.696177,0.475669,0.430414,0.606985,0.83506,0.486609,0.222874,0.616321,0.0674109,0.437542,0.424257,0.949233,0.0579562,0.393797,0.432682,0.828372,]);
indices = Vector.<uint>([15,14,2,3,15,2,16,15,3,4,16,3,17,16,4,5,17,4,22,21,7,8,22,7,30,22,8,9,30,8,31,30,9,10,31,9,33,32,18,19,33,18,34,33,19,56,34,19,21,34,20,7,21,20,40,23,11,12,40,11,50,24,12,13,50,12,14,25,13,2,14,13,39,6,45,28,27,15,16,28,15,29,28,16,17,29,16,3,2,0,1,21,22,4,3,0,1,22,30,5,4,0,1,30,31,13,25,50,50,25,49,20,57,6,46,20,6,8,7,26,27,8,26,9,8,27,28,9,27,10,9,28,29,10,28,12,11,0,1,32,33,13,12,0,1,33,34,2,13,0,1,34,21,42,36,55,54,42,55,7,20,46,26,7,46,27,26,14,15,27,14,24,50,51,46,6,39,25,14,48,37,23,40,12,24,40,19,18,53,54,19,53,42,35,41,38,36,42,56,20,34,43,36,38,26,46,47,40,24,52,59,20,56,45,43,44,123,119,121,125,123,121,59,57,20,65,58,63,55,36,43,57,55,43,71,58,65,69,71,65,57,43,45,6,57,45,58,61,63,67,61,58,71,67,58,64,62,54,55,64,54,68,64,55,57,68,55,70,68,57,59,70,57,62,60,19,54,62,19,66,70,59,56,66,59,60,66,56,19,60,56,75,73,42,54,75,42,74,75,54,53,74,54,73,72,35,42,73,35,95,85,87,93,95,87,77,83,107,117,97,95,93,117,95,79,89,97,117,79,97,107,105,109,77,107,109,115,99,89,79,115,89,101,99,115,113,101,115,111,103,101,113,111,101,109,105,91,81,109,91,81,91,103,111,81,103,92,86,37,40,92,37,116,92,40,52,116,40,78,116,52,24,78,52,114,78,24,51,114,24,112,114,51,50,112,51,110,112,50,49,110,50,80,110,49,25,80,49,108,80,25,48,108,25,76,108,48,14,76,48,82,76,14,26,82,14,106,82,26,47,106,26,104,106,47,46,104,47,90,104,46,39,90,46,102,90,39,45,102,39,100,102,45,44,100,45,98,100,44,43,98,44,88,98,43,38,88,43,96,88,38,42,96,38,94,96,42,41,94,42,84,94,41,35,84,41,124,120,73,75,124,73,122,124,75,74,122,75,120,118,72,73,120,72,136,127,126,135,136,126,137,128,127,136,137,127,17,5,128,137,17,128,141,131,130,140,141,130,147,132,131,141,147,131,31,10,132,147,31,132,148,138,18,32,148,18,149,168,138,148,149,138,140,130,139,149,140,139,153,133,11,23,153,11,163,134,133,142,163,133,135,126,134,143,135,134,129,152,158,146,137,136,145,146,136,29,17,137,146,29,137,126,127,0,140,1,141,127,128,0,141,1,147,128,5,0,147,1,31,143,134,163,143,163,162,139,159,129,169,139,129,131,145,144,130,131,144,132,146,145,131,132,145,10,29,146,132,10,146,11,133,0,32,1,148,133,134,0,148,1,149,134,126,0,149,1,140,155,166,167,150,155,167,130,144,159,139,130,159,145,136,135,144,145,135,163,142,164,129,159,152,135,143,161,23,37,153,142,133,153,138,166,53,18,138,53,35,155,154,150,151,155,139,168,149,150,156,151,159,144,160,142,153,165,139,171,168,156,158,157,123,227,225,119,123,225,169,171,139,170,177,175,167,169,156,150,167,156,183,181,177,170,183,177,169,129,158,156,169,158,173,170,175,179,183,170,173,179,170,176,167,166,174,176,166,180,169,167,176,180,167,182,171,169,180,182,169,174,166,138,172,174,138,178,168,171,182,178,171,172,138,168,178,172,168,185,166,155,184,185,155,74,53,166,185,74,166,184,155,35,72,184,35,201,199,87,85,201,87,193,187,213,223,199,201,203,223,201,189,223,203,195,189,203,213,187,215,211,213,215,221,189,195,205,221,195,207,219,221,205,207,221,217,219,207,209,217,207,215,191,197,211,215,197,191,217,209,197,191,209,198,153,37,86,198,37,222,165,153,198,222,153,188,142,165,222,188,165,220,164,142,188,220,142,218,163,164,220,218,164,216,162,163,218,216,163,190,143,162,216,190,162,214,161,143,190,214,143,186,135,161,214,186,161,192,144,135,186,192,135,212,160,144,192,212,144,210,159,160,212,210,160,196,152,159,210,196,159,208,158,152,196,208,152,206,157,158,208,206,158,204,156,157,206,204,157,194,151,156,204,194,156,202,155,151,194,202,151,200,154,155,202,200,155,84,35,154,200,84,154,226,185,184,224,226,184,122,74,185,226,122,185,224,184,72,118,224,72,]);
init();
// handle lost context
Starling.current.addEventListener(Event.CONTEXT3D_CREATE, onContextCreated);
}
private function init():void
{
registerPrograms();
initBuffer();
if (mTexture!=null) {
Starling.context.setTextureAt(0,mTexture);
}
}
private function initBuffer(recreat:Boolean=true):void{
var context:Context3D = Starling.context;
if(recreat||this.vertexBuffer==null){
this.vertexBuffer=context.createVertexBuffer(this.vertices.length/3,3);
vertexBuffer.uploadFromVector(vertices,0,this.vertices.length/3);
}
if(recreat||this.uvBuffer==null){
this.uvBuffer=context.createVertexBuffer(this.uvs.length/2,2);
uvBuffer.uploadFromVector(uvs,0,this.uvs.length/2);
}
if(recreat||this.indexBuffer==null){
this.indexBuffer=context.createIndexBuffer(indices.length);
indexBuffer.uploadFromVector(indices,0,indices.length);
}
context.setVertexBufferAt(0,vertexBuffer,0,Context3DVertexBufferFormat.FLOAT_3);
context.setVertexBufferAt(1,uvBuffer,0,Context3DVertexBufferFormat.FLOAT_2);
}
/** Disposes all resources of the display object. */
public override function dispose():void
{
Starling.current.removeEventListener(Event.CONTEXT3D_CREATE, onContextCreated);
vertexBuffer.dispose();
uvBuffer.dispose();
indexBuffer.dispose();
//texture?
super.dispose();
}
private function onContextCreated(event:Event):void
{
// the old context was lost, so we create new buffers and shaders.
init();
}
/** Creates vertex and fragment programs from assembly. */
private static function registerPrograms():void
{
var target:Starling = Starling.current;
if (target.hasProgram(PROGRAM_NAME)) return; // already registered
// va0 -> position
// va1 -> color
// vc0 -> mvpMatrix (4 vectors, vc0 - vc3)
// vc4 -> alpha
var vertexProgramAssembler:AGALMiniAssembler = new AGALMiniAssembler();
vertexProgramAssembler.assemble(Context3DProgramType.VERTEX, vertexShader);
var fragmentProgramAssembler:AGALMiniAssembler = new AGALMiniAssembler();
fragmentProgramAssembler.assemble(Context3DProgramType.FRAGMENT, fragmentShader);
target.registerProgram(PROGRAM_NAME, vertexProgramAssembler.agalcode,
fragmentProgramAssembler.agalcode);
}
/** Renders the object with the help of a 'support' object and with the accumulated alpha
* of its parent object. */
public override function render(support:RenderSupport, alpha:Number):void
{
// always call this method when you write custom rendering code!
// it causes all previously batched quads/images to render.
support.finishQuadBatch();
sRenderAlpha[0] = sRenderAlpha[1] = sRenderAlpha[2] = 1.0;
sRenderAlpha[3] = alpha * this.alpha;
var context:Context3D = Starling.context;
if (context == null) throw new MissingContextError();
// apply the current blendmode
support.applyBlendMode(false);
// activate program (shader) and set the required buffers / constants
context.setProgram(Starling.current.getProgram(PROGRAM_NAME));
initBuffer(false);
context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 0, support.mvpMatrix3D, true);
context.setProgramConstantsFromVector(Context3DProgramType.VERTEX, 4, sRenderAlpha, 1);
// finally: draw the object!
context.drawTriangles(indexBuffer,0,indices.length);
// reset buffers
context.setVertexBufferAt(0, null);
context.setVertexBufferAt(1, null);
}
/** Returns a rectangle that completely encloses the object as it appears in another
* coordinate system. */
public override function getBounds(targetSpace:DisplayObject, resultRect:Rectangle=null):Rectangle
{
if (resultRect == null) resultRect = new Rectangle();
var transformationMatrix:Matrix = targetSpace == this ?
null : getTransformationMatrix(targetSpace, sHelperMatrix);
return resultRect;// mVertexData.getBounds(transformationMatrix, 0, -1, resultRect);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment