The documentation will be available in the next few days. It will contain both tutorials and technical articles. But to help you getting started, I want to share a few examples.
Use a shader on the whole scene:
viewport.defaultEffect = new SinglePassRenderingEffect(new MyCoolShader())
Use a shader on a specific sub-scene:
var es : EffectGroup = new EffectGroup(new SinglePassRenderingEffect(new MyCoolShader()));
es.addChild(... // add anything you want to display with this shader
_scene.addChild(es);
If you want to see the output AGAL assembly code compiled by the JIT compiler, you can use the following debug option:
Minko.debugLevel = DebugLevel.SHADER_AGAL;
_viewport = new SinglePassPostProcessingEffect(new MyCoolPostProcessingShader());
public class SimpleTextureShader extends ActionScriptShader
{
// returns the vertex position in clipspace (normalized screenspace [-1 .. 1])
override protected function getOutputPosition() : SValue
{
return vertexClipspacePosition;
}
override protected function getOutputColor() : SValue
{
// we have to use "interpolate" for every value coming from the vertex shader
var uv : SValue = interpolate(vertexUV);
return sampleTexture(BasicStyle.DIFFUSE, uv);
}
}
public class SimpleShader extends ActionScriptShader
{
override protected function getOutputPosition() : SValue
{
return vertexClipspacePosition;
}
// returns red
override protected function getOutputColor() : SValue
{
return float4(1., 0., 0., 1.);
}
}
public class SimpleTextureShader extends ActionScriptShader
{
private var _offset : Vector4 = null;
private var _multiplier : Vector4 = null;
public function ColorTransformShader(offset : Vector4, multiplier : Vector4)
{
_offset = offset;
_multiplier = multiplier;
}
override protected function getOutputPosition() : SValue
{
return vertexClipspacePosition;
}
override protected function getOutputColor() : SValue
{
// we have to use "interpolate" for every value coming from the vertex shader
var uv : SValue = interpolate(vertexUV);
var diffuse : SValue = sampleTexture(BasicStyle.DIFFUSE, uv);
diffuse = multiply(diffuse, multiplier);
diffuse = add(diffuse, _offset);
return diffuse;
}
}
public class LightCubeShader extends ActionScriptShader
{
override protected function getOutputColor() : SValue
{
var pointLights : WorldDataList = getWorldDataList(LightData);
var numLights : int = pointLights.length;
var illumination : SValue = float3(0., 0., 0.);
for (var lightIndex : int = 0; lightIndex < numLights; ++lightIndex)
{
var lightPosition : SValue = getWorldParameter(3, LightData, LightData.LOCAL_POSITION, lightIndex);
var lightDiffuse : SValue = getWorldParameter(3, LightData, LightData.PREMULTIPLIED_DIFFUSE_COLOR, lightIndex);
var squareLocalDist : SValue = getWorldParameter(1, LightData, LightData.SQUARE_LOCAL_DISTANCE, lightIndex);
var lightToPoint : SValue = subtract(interpolate(vertexPosition), lightPosition);
var lightDirection : SValue = normalize(lightToPoint);
var lambertFactor : SValue = saturate(interpolate(vertexNormal).dotProduct3(lightDirection));
// hacky hacky...
var attenuation : SValue = saturate(multiply(squareLocalDist,
reciprocal(dotProduct3(lightToPoint, lightToPoint))));
// here goes the hack again...
lightDiffuse.scaleBy(attenuation)
.scaleBy(.8);
illumination.incrementBy(lightDiffuse);
}
var uv : SValue = interpolate(vertexUV);
var diffuse : SValue = sampleTexture(BasicStyle.DIFFUSE, uv);
illumination = power(illumination, 1.2);
return float4(multiply(diffuse.rgb, illumination), diffuse.a);
}
override protected function getOutputPosition() : SValue
{
return vertexClipspacePosition;
}
override public function getDataHash(styleData : StyleData,
transformData : TransformData,
worldData : Dictionary) : String
{
return (worldData[LightData] as WorldDataList).length.toString(16);
}
}
public class GreyscalePostProcessingShader extends PostProcessingActionScriptShader
{
override protected function getFinalColor(outputColor : SValue) : SValue
{
return float4(
float3(divide(add(outputColor.r, outputColor.g, outputColor.b), 3.)),
1.0
);
}
}
// Tangent, Binormal, Normal - float3
// vNormal - float3 too.
// Transform the surface normal into world space (in order to compute reflection
// vector to perform environment map look-up):
float3x3 mTangentToWorld = transpose( float3x3( Tangent, Binormal, Normal ) );
float3 vNormalWorld = normalize( mul( mTangentToWorld, vNormal ));
how to define a 3x3 matrix by three float3 vectors and multiply this matrix on float3
float3x3 mTangentToWorld = transpose( float3x3( Tangent, Binormal, Normal ) );
float3 vNormalWorld = normalize( mul( mTangentToWorld, vNormal ));
// compute the vertex bitangent (aka binormal)
var vertexBitangent : SValue = cross(vertexNormal, vertexTangent);
// tangent space to local space
var a : SValue = float3(vertexTangent.x, vertexBitangent.x, vertexNormal.x);
var b : SValue = float3(vertexTangent.y, vertexBitangent.y, vertexNormal.y);
var c : SValue = float3(vertexTangent.z, vertexBitangent.z, vertexNormal.z);
var localNormal = float3(
dotProduct3(normal, a),
dotProduct3(normal, b),
dotProduct3(normal, c)
);
localNormal = normalize(localNormal);
private function toTangentSpace(vector : SValue) : SValue
{
var vertexBitangent : SValue = cross(vertexNormal, vertexTangent);
return float3(
dotProduct3(vector, vertexTangent),
dotProduct3(vector, vertexBitangent),
dotProduct3(vector, vertexNormal)
);
}
pos = size * (gl_Vertex.x * view_inverse_matrix[0]
+ gl_Vertex.y * view_inverse_matrix[1]).xyz;
// other calculataions
gl_Position = gl_ModelViewProjectionMatrix * vec4(pos,1.0);
How to make quad aligned to screen?
pos = size * (gl_Vertex.x * view_inverse_matrix[0]
+ gl_Vertex.y * view_inverse_matrix[1]).xyz;
// other calculataions
gl_Position = gl_ModelViewProjectionMatrix * vec4(pos,1.0);
override protected function getOutputPosition() : SValue
{
var p : SValue = getVertexAttribute(PARTICLE_DATA);
var xy : SValue = p.xy;
var id : SValue = p.z;
// get the center of the quad in view space
var center : SValue = multiply4x4(float4(0, 0, 0, 1), localToViewMatrix);
// get the size of the quad
var scale : SValue = multiply4x4(float4(1, 1, 0, 1), localToViewMatrix);
var offset : SValue = float4(multiply(xy, scale.xy), 0., 0.);
return multiply4x4(add(center, offset), projectionMatrix);
}
"Bad AGAL source operands. Both are constants (this must be precomputed) at token 1 of vertex program."
var center : SValue = multiply4x4(float4(0, 0, 0, 1), copy(localToViewMatrix));
public interface IParticleSystem
{
function getPosition(id : SValue, time : SValue) : SValue;
function getRotation(id : SValue, time : SValue) : SValue;
function getAlpha(id : SValue, time : SValue) : SValue;
function getColor(id : SValue, time : SValue) : SValue;
}
As you suggested the copy() instruction did solved the compile error.
if Minko 2.0 is in the works I will just sit tight and try again when its released
dp4 v0.x, va0, vc0
dp4 v0.y, va0, vc1
mov v0.zw, va0.zw
uvTransform[0] = matrix.a; uvTransform[1] = matrix.b; uvTransform[2] = 0; uvTransformData[3] = matrix.tx;
uvTransform[4] = matrix.c; uvTransform[5] = matrix.d; uvTransform[6] = 0; uvTransformData[7] = matrix.ty;
It looks like you're new here. If you want to get involved, click one of these buttons!