Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added classic TextureMaterial options and mipmapping settings, BitmapTextureResourceLite #45

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
65 changes: 46 additions & 19 deletions src/alternativa/engine3d/materials/TextureMaterial.as
Expand Up @@ -46,33 +46,52 @@ package alternativa.engine3d.materials {
private var cachedContext3D:Context3D;
private var programsCache:Dictionary;

static alternativa3d const diffuseProcedures:Object = { };
static alternativa3d const diffuseOpacityProcedures:Object = { };

private var samplerOptions:String;

/**
* @private
* Procedure for diffuse map with alpha channel
*/
static alternativa3d const getDiffuseProcedure:Procedure = new Procedure([
"#v0=vUV",
"#s0=sDiffuse",
"#c0=cThresholdAlpha",
"tex t0, v0, s0 <2d, linear,repeat, miplinear>",
"mul t0.w, t0.w, c0.w",
"mov o0, t0"
], "getDiffuseProcedure");
private function get getDiffuseProcedure ():Procedure {
var procedure:Procedure = diffuseProcedures [samplerOptions];
if (procedure == null) {
procedure = new Procedure([
"#v0=vUV",
"#s0=sDiffuse",
"#c0=cThresholdAlpha",
"tex t0, v0, s0 <2d, " + samplerOptions + ">",
"mul t0.w, t0.w, c0.w",
"mov o0, t0"
], "getDiffuseProcedure");
diffuseProcedures [samplerOptions] = procedure;
}
return procedure;
}

/**
* @private
* Procedure for diffuse with opacity map.
*/
static alternativa3d const getDiffuseOpacityProcedure:Procedure = new Procedure([
"#v0=vUV",
"#s0=sDiffuse",
"#s1=sOpacity",
"#c0=cThresholdAlpha",
"tex t0, v0, s0 <2d, linear,repeat, miplinear>",
"tex t1, v0, s1 <2d, linear,repeat, miplinear>",
"mul t0.w, t1.x, c0.w",
"mov o0, t0"
], "getDiffuseOpacityProcedure");
private function get getDiffuseOpacityProcedure ():Procedure {
var procedure:Procedure = diffuseOpacityProcedures [samplerOptions];
if (procedure == null) {
procedure = new Procedure([
"#v0=vUV",
"#s0=sDiffuse",
"#s1=sOpacity",
"#c0=cThresholdAlpha",
"tex t0, v0, s0 <2d, " + samplerOptions + ">",
"tex t1, v0, s1 <2d, " + samplerOptions + ">",
"mul t0.w, t1.x, c0.w",
"mov o0, t0"
], "getDiffuseOpacityProcedure");
diffuseOpacityProcedures [samplerOptions] = procedure;
}
return procedure;
}

/**
* @private
Expand Down Expand Up @@ -140,12 +159,20 @@ package alternativa.engine3d.materials {
* Creates a new TextureMaterial instance.
*
* @param diffuseMap Diffuse map.
* @param opacityMap Opacity map.
* @param alpha Transparency.
* @param smooth Texture smoothing while scaling.
* @param repeat Texture repeat.
* @param mipmap Mipmap (0=disable, 1=nearest, 2=linear).
*/
public function TextureMaterial(diffuseMap:TextureResource = null, opacityMap:TextureResource = null, alpha:Number = 1) {
public function TextureMaterial(diffuseMap:TextureResource = null, opacityMap:TextureResource = null, alpha:Number = 1,
smooth:Boolean = true, repeat:Boolean = true, mipmap:uint = 2) {

this.diffuseMap = diffuseMap;
this.opacityMap = opacityMap;
this.alpha = alpha;
this.samplerOptions = (smooth ? "linear" : "nearest") + "," + (repeat ? "repeat" : "clamp") + "," +
["nomip", "mipnearest", "miplinear"][mipmap];
}

/**
Expand Down
52 changes: 52 additions & 0 deletions src/alternativa/engine3d/resources/BitmapTextureResourceLite.as
@@ -0,0 +1,52 @@
package alternativa.engine3d.resources {
import flash.display.BitmapData;
import flash.display3D.Context3D;
import flash.display3D.Context3DTextureFormat;
import flash.display3D.textures.Texture;
import alternativa.engine3d.alternativa3d;
import flash.geom.Matrix;
use namespace alternativa3d;
/**
* Use this when you need to upload the texture frequently.
* Does not create any mip levels other than 0.
* Size of texture must be power of two (e.g., 256х256, 128*512, 256* 32).
* @see alternativa.engine3d.resources.TextureResource
* @see alternativa.engine3d.resources.BitmapTextureResource
*/
public class BitmapTextureResourceLite extends TextureResource {
/**
* BitmapData object.
*/
public var data:BitmapData;

/**
* Uploads textures from <code>BitmapData</code> to GPU.
*/
public function BitmapTextureResourceLite (data:BitmapData) {
this.data = data;
}

/**
* @inheritDoc
*/
override public function upload(context3D:Context3D):void {
if (_texture != null) _texture.dispose();
if (data != null) {
_texture = context3D.createTexture (data.width, data.height, Context3DTextureFormat.BGRA, false);
(_texture as Texture).uploadFromBitmapData (data, 0);
} else {
_texture = null;
throw new Error("Cannot upload without data");
}
}

/**
* Reuploads the data to the same texture.
*/
public function reupload ():void {
if (_texture && data) {
(_texture as Texture).uploadFromBitmapData (data, 0);
}
}
}
}