Skip to content

Commit

Permalink
Added property (maxAnisotropy) to set the mipmapping anisotropic filt…
Browse files Browse the repository at this point in the history
…ering max number of samples, on a per texture basis. Slightly different implementations for HTML5 but all work except Flash.
  • Loading branch information
Greg209 committed Sep 30, 2014
1 parent 9596c42 commit 326b5cd
Show file tree
Hide file tree
Showing 18 changed files with 147 additions and 33 deletions.
22 changes: 22 additions & 0 deletions away3d/materials/MaterialBase.hx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/**
/**
* MaterialBase forms an abstract base class for any material.
* A material consists of several passes, each of which constitutes at least one render call. Several passes could
Expand Down Expand Up @@ -39,6 +40,7 @@ class MaterialBase extends NamedAssetBase implements IAsset {
public var smooth(get_smooth, set_smooth):Bool;
public var depthCompareMode(get_depthCompareMode, set_depthCompareMode):Context3DCompareMode;
public var repeat(get_repeat, set_repeat):Bool;
public var maxAnisotropy(get_maxAnisotropy, set_maxAnisotropy):Float;
public var bothSides(get_bothSides, set_bothSides):Bool;
public var blendMode(get_blendMode, set_blendMode):BlendMode;
public var alphaPremultiplied(get_alphaPremultiplied, set_alphaPremultiplied):Bool;
Expand Down Expand Up @@ -97,6 +99,7 @@ class MaterialBase extends NamedAssetBase implements IAsset {
private var _mipmap:Bool;
private var _smooth:Bool;
private var _repeat:Bool;
private var _maxAnisotropy:Float;
private var _depthPass:DepthMapPass;
private var _distancePass:DistanceMapPass;
private var _lightPicker:LightPickerBase;
Expand All @@ -110,6 +113,7 @@ class MaterialBase extends NamedAssetBase implements IAsset {
_blendMode = BlendMode.NORMAL;
_mipmap = true;
_smooth = true;
_maxAnisotropy = 1;
_depthCompareMode = Context3DCompareMode.LESS_EQUAL;
_owners = new Array<IMaterialOwner>();
_passes = new Array<MaterialPassBase>();
Expand Down Expand Up @@ -222,6 +226,23 @@ class MaterialBase extends NamedAssetBase implements IAsset {
return value;
}

/**
* Indicates the number of Anisotropic filtering samples to take for mipmapping
*/
public function get_maxAnisotropy():Float {
return _maxAnisotropy;
}

public function set_maxAnisotropy(value:Float):Float {
_maxAnisotropy = value;
var i:Int = 0;
while (i < _numPasses) {
_passes[i].maxAnisotropy = maxAnisotropy;
++i;
}
return maxAnisotropy;
}

/**
* Cleans up resources owned by the material, including passes. Textures are not owned by the material since they
* could be used by other materials and will not be disposed.
Expand Down Expand Up @@ -594,6 +615,7 @@ class MaterialBase extends NamedAssetBase implements IAsset {
pass.mipmap = _mipmap;
pass.smooth = _smooth;
pass.repeat = _repeat;
pass.maxAnisotropy = _maxAnisotropy;
pass.lightPicker = _lightPicker;
pass.bothSides = _bothSides;
pass.addEventListener(Event.CHANGE, onPassChange);
Expand Down
7 changes: 5 additions & 2 deletions away3d/materials/TextureMaterial.hx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package away3d.materials;
import openfl.display.BlendMode;
import openfl.geom.ColorTransform;
import away3d.textures.Texture2DBase;

class TextureMaterial extends SinglePassMaterialBase {
public var animateUVs(get_animateUVs, set_animateUVs):Bool;
public var alpha(get_alpha, set_alpha):Float;
Expand All @@ -18,13 +19,15 @@ class TextureMaterial extends SinglePassMaterialBase {
* @param smooth Indicates whether the texture should be filtered when sampled. Defaults to true.
* @param repeat Indicates whether the texture should be tiled when sampled. Defaults to true.
* @param mipmap Indicates whether or not any used textures should use mipmapping. Defaults to true.
*/
public function new(texture:Texture2DBase = null, smooth:Bool = true, repeat:Bool = false, mipmap:Bool = true) {
* @param maxAnisotropy Indicates the number of samples to use if Anisotropic mipmap filtering is applied
*/
public function new(texture:Texture2DBase = null, smooth:Bool = true, repeat:Bool = false, mipmap:Bool = true, maxAnisotropy:Float = 1) {
super();
this.texture = texture;
this.smooth = smooth;
this.repeat = repeat;
this.mipmap = mipmap;
this.maxAnisotropy = maxAnisotropy;
}

/**
Expand Down
7 changes: 4 additions & 3 deletions away3d/materials/TextureMultiPassMaterial.hx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package away3d.materials;

import away3d.textures.Texture2DBase;


class TextureMultiPassMaterial extends MultiPassMaterialBase {
public var animateUVs(get_animateUVs, set_animateUVs):Bool;
public var texture(get_texture, set_texture):Texture2DBase;
Expand All @@ -19,13 +18,15 @@ class TextureMultiPassMaterial extends MultiPassMaterialBase {
* @param smooth Indicates whether the texture should be filtered when sampled. Defaults to true.
* @param repeat Indicates whether the texture should be tiled when sampled. Defaults to true.
* @param mipmap Indicates whether or not any used textures should use mipmapping. Defaults to true.
*/
public function new(texture:Texture2DBase = null, smooth:Bool = true, repeat:Bool = false, mipmap:Bool = true) {
* @param maxAnisotropy Indicates the number of samples to use if Anisotropic mipmap filtering is applied
*/
public function new(texture:Texture2DBase = null, smooth:Bool = true, repeat:Bool = false, mipmap:Bool = true, maxAnisotropy:Float = 1) {
super();
this.texture = texture;
this.smooth = smooth;
this.repeat = repeat;
this.mipmap = mipmap;
this.maxAnisotropy = maxAnisotropy;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion away3d/materials/compilation/ShaderCompiler.hx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class ShaderCompiler {
private var _smooth:Bool;
private var _repeat:Bool;
private var _mipmap:Bool;
private var _maxAnisotropy:Float;
private var _enableLightFallOff:Bool;
private var _preserveAlpha:Bool;
private var _animateUVs:Bool;
Expand Down Expand Up @@ -230,10 +231,11 @@ class ShaderCompiler {
* @param repeat Indicates whether the texture should be tiled when sampled. Defaults to true.
* @param mipmap Indicates whether or not any used textures should use mipmapping. Defaults to true.
*/
public function setTextureSampling(smooth:Bool, repeat:Bool, mipmap:Bool):Void {
public function setTextureSampling(smooth:Bool, repeat:Bool, mipmap:Bool, maxAnisotropy:Float = 1):Void {
_smooth = smooth;
_repeat = repeat;
_mipmap = mipmap;
_maxAnisotropy = maxAnisotropy;
}

/**
Expand Down Expand Up @@ -477,6 +479,7 @@ class ShaderCompiler {
methodVO.useSmoothTextures = _smooth;
methodVO.repeatTextures = _repeat;
methodVO.useMipmapping = _mipmap;
methodVO.maxAnisotropy = _maxAnisotropy;
methodVO.useLightFallOff = _enableLightFallOff && _profile != "baselineConstrained";
methodVO.numLights = _numLights + _numLightProbes;
method.initVO(methodVO);
Expand Down
2 changes: 1 addition & 1 deletion away3d/materials/methods/BasicAmbientMethod.hx
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class BasicAmbientMethod extends ShadingMethodBase {
override public function activate(vo:MethodVO, stage3DProxy:Stage3DProxy):Void {
if (_useTexture) {
#if !flash
stage3DProxy._context3D.setSamplerStateAt(vo.texturesIndex, vo.repeatTextures ? Context3DWrapMode.REPEAT : Context3DWrapMode.CLAMP, vo.useSmoothTextures ? Context3DTextureFilter.LINEAR : Context3DTextureFilter.NEAREST, vo.useMipmapping ? Context3DMipFilter.MIPLINEAR : Context3DMipFilter.MIPNONE);
stage3DProxy._context3D.setSamplerStateAt(vo.texturesIndex, vo.repeatTextures ? Context3DWrapMode.REPEAT : Context3DWrapMode.CLAMP, vo.useSmoothTextures ? Context3DTextureFilter.LINEAR : Context3DTextureFilter.NEAREST, vo.useMipmapping ? Context3DMipFilter.MIPLINEAR : Context3DMipFilter.MIPNONE, vo.maxAnisotropy );
#end
stage3DProxy._context3D.setTextureAt(vo.texturesIndex, _texture.getTextureForStage3D(stage3DProxy));
}
Expand Down
2 changes: 1 addition & 1 deletion away3d/materials/methods/BasicDiffuseMethod.hx
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ class BasicDiffuseMethod extends LightingMethodBase {
override public function activate(vo:MethodVO, stage3DProxy:Stage3DProxy):Void {
if (_useTexture) {
#if !flash
stage3DProxy._context3D.setSamplerStateAt(vo.texturesIndex, vo.repeatTextures ? Context3DWrapMode.REPEAT : Context3DWrapMode.CLAMP, vo.useSmoothTextures ? Context3DTextureFilter.LINEAR : Context3DTextureFilter.NEAREST, vo.useMipmapping ? Context3DMipFilter.MIPLINEAR : Context3DMipFilter.MIPNONE);
stage3DProxy._context3D.setSamplerStateAt(vo.texturesIndex, vo.repeatTextures ? Context3DWrapMode.REPEAT : Context3DWrapMode.CLAMP, vo.useSmoothTextures ? Context3DTextureFilter.LINEAR : Context3DTextureFilter.NEAREST, vo.useMipmapping ? Context3DMipFilter.MIPLINEAR : Context3DMipFilter.MIPNONE, vo.maxAnisotropy );
#end
stage3DProxy._context3D.setTextureAt(vo.texturesIndex, _texture.getTextureForStage3D(stage3DProxy));
if (_alphaThreshold > 0) vo.fragmentData[vo.fragmentConstantsIndex] = _alphaThreshold;
Expand Down
2 changes: 1 addition & 1 deletion away3d/materials/methods/BasicNormalMethod.hx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class BasicNormalMethod extends ShadingMethodBase {
override public function activate(vo:MethodVO, stage3DProxy:Stage3DProxy):Void {
if (vo.texturesIndex >= 0) {
#if !flash
stage3DProxy._context3D.setSamplerStateAt(vo.texturesIndex, vo.repeatTextures ? Context3DWrapMode.REPEAT : Context3DWrapMode.CLAMP, vo.useSmoothTextures ? Context3DTextureFilter.LINEAR : Context3DTextureFilter.NEAREST, vo.useMipmapping ? Context3DMipFilter.MIPLINEAR : Context3DMipFilter.MIPNONE);
stage3DProxy._context3D.setSamplerStateAt(vo.texturesIndex, vo.repeatTextures ? Context3DWrapMode.REPEAT : Context3DWrapMode.CLAMP, vo.useSmoothTextures ? Context3DTextureFilter.LINEAR : Context3DTextureFilter.NEAREST, vo.useMipmapping ? Context3DMipFilter.MIPLINEAR : Context3DMipFilter.MIPNONE, vo.maxAnisotropy );
#end
stage3DProxy._context3D.setTextureAt(vo.texturesIndex, _texture.getTextureForStage3D(stage3DProxy));
}
Expand Down
14 changes: 7 additions & 7 deletions away3d/materials/methods/BasicSpecularMethod.hx
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,15 @@ class BasicSpecularMethod extends LightingMethodBase {

var viewDirReg:ShaderRegisterElement = _sharedRegisters.viewDirFragment;
var normalReg:ShaderRegisterElement = _sharedRegisters.normalFragment;
// blinn-phong half vector model
// blinn-phong half vector model
code += "add " + t + ", " + lightDirReg + ", " + viewDirReg + "\n" + "nrm " + t + ".xyz, " + t + "\n" + "dp3 " + t + ".w, " + normalReg + ", " + t + "\n" + "sat " + t + ".w, " + t + ".w\n";
if (_useTexture) {
// apply gloss modulation from texture
// apply gloss modulation from texture
code += "mul " + _specularTexData + ".w, " + _specularTexData + ".y, " + _specularDataRegister + ".w\n" + "pow " + t + ".w, " + t + ".w, " + _specularTexData + ".w\n";
}

else code += "pow " + t + ".w, " + t + ".w, " + _specularDataRegister + ".w\n";
// attenuate
// attenuate
if (vo.useLightFallOff) code += "mul " + t + ".w, " + t + ".w, " + lightDirReg + ".w\n";
if (_modulateMethod != null) code += _modulateMethod(vo, t, regCache, _sharedRegisters);
code += "mul " + t + ".xyz, " + lightColReg + ", " + t + ".w\n";
Expand All @@ -204,7 +204,7 @@ class BasicSpecularMethod extends LightingMethodBase {
override public function getFragmentCodePerProbe(vo:MethodVO, cubeMapReg:ShaderRegisterElement, weightRegister:String, regCache:ShaderRegisterCache):String {
var code:String = "";
var t:ShaderRegisterElement;
// write in temporary if not first light, so we can add to total diffuse colour
// write in temporary if not first light, so we can add to total diffuse colour
if (_isFirstLight) t = _totalLightColorReg
else {
t = regCache.getFreeFragmentVectorTemp();
Expand All @@ -231,7 +231,7 @@ class BasicSpecularMethod extends LightingMethodBase {
if (vo.numLights == 0) return code;
if (_shadowRegister != null) code += "mul " + _totalLightColorReg + ".xyz, " + _totalLightColorReg + ", " + _shadowRegister + ".w\n";
if (_useTexture) {
// apply strength modulation from texture
// apply strength modulation from texture
code += "mul " + _totalLightColorReg + ".xyz, " + _totalLightColorReg + ", " + _specularTexData + ".x\n";
regCache.removeFragmentTempUsage(_specularTexData);
}
Expand All @@ -244,11 +244,11 @@ class BasicSpecularMethod extends LightingMethodBase {
* @inheritDoc
*/
override public function activate(vo:MethodVO, stage3DProxy:Stage3DProxy):Void {
//var context : Context3D = stage3DProxy._context3D;
//var context : Context3D = stage3DProxy._context3D;
if (vo.numLights == 0) return;
if (_useTexture) {
#if !flash
stage3DProxy._context3D.setSamplerStateAt(vo.texturesIndex, vo.repeatTextures ? Context3DWrapMode.REPEAT : Context3DWrapMode.CLAMP, vo.useSmoothTextures ? Context3DTextureFilter.LINEAR : Context3DTextureFilter.NEAREST, vo.useMipmapping ? Context3DMipFilter.MIPLINEAR : Context3DMipFilter.MIPNONE);
stage3DProxy._context3D.setSamplerStateAt(vo.texturesIndex, vo.repeatTextures ? Context3DWrapMode.REPEAT : Context3DWrapMode.CLAMP, vo.useSmoothTextures ? Context3DTextureFilter.LINEAR : Context3DTextureFilter.NEAREST, vo.useMipmapping ? Context3DMipFilter.MIPLINEAR : Context3DMipFilter.MIPNONE, vo.maxAnisotropy );
#end
stage3DProxy._context3D.setTextureAt(vo.texturesIndex, _texture.getTextureForStage3D(stage3DProxy));
}
Expand Down
2 changes: 2 additions & 0 deletions away3d/materials/methods/MethodVO.hx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class MethodVO {
public var useMipmapping:Bool;
public var useSmoothTextures:Bool;
public var repeatTextures:Bool;
public var maxAnisotropy:Float;
// internal stuff for the material to know before assembling code
public var needsProjection:Bool;
public var needsView:Bool;
Expand Down Expand Up @@ -50,6 +51,7 @@ class MethodVO {
vertexConstantsIndex = -1;
fragmentConstantsIndex = -1;
useMipmapping = true;
maxAnisotropy = 1;
useSmoothTextures = true;
repeatTextures = false;
needsProjection = false;
Expand Down
11 changes: 10 additions & 1 deletion away3d/materials/passes/CompiledPass.hx
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ class CompiledPass extends MaterialPassBase {
_compiler.methodSetup = _methodSetup;
_compiler.diffuseLightSources = _diffuseLightSources;
_compiler.specularLightSources = _specularLightSources;
_compiler.setTextureSampling(_smooth, _repeat, _mipmap);
_compiler.setTextureSampling(_smooth, _repeat, _mipmap, _maxAnisotropy);
_compiler.setConstantDataBuffers(_vertexConstantData, _fragmentConstantData);
_compiler.animateUVs = _animateUVs;
_compiler.alphaPremultiplied = _alphaPremultiplied && _enableBlending;
Expand Down Expand Up @@ -302,6 +302,15 @@ class CompiledPass extends MaterialPassBase {
return value;
}

/**
* @inheritDoc
*/
override public function set_maxAnisotropy(value:Float):Float {
if (_maxAnisotropy == value) return value;
super.maxAnisotropy = value;
return value;
}

/**
* The normal map to modulate the direction of the surface for each texel. The default normal method expects
* tangent-space normal maps, but others could expect object-space maps.
Expand Down
18 changes: 18 additions & 0 deletions away3d/materials/passes/MaterialPassBase.hx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class MaterialPassBase extends EventDispatcher {
public var mipmap(get_mipmap, set_mipmap):Bool;
public var smooth(get_smooth, set_smooth):Bool;
public var repeat(get_repeat, set_repeat):Bool;
public var maxAnisotropy(get_maxAnisotropy, set_maxAnisotropy):Float;
public var bothSides(get_bothSides, set_bothSides):Bool;
public var depthCompareMode(get_depthCompareMode, set_depthCompareMode):Context3DCompareMode;
public var animationSet(get_animationSet, set_animationSet):IAnimationSet;
Expand Down Expand Up @@ -60,6 +61,7 @@ class MaterialPassBase extends EventDispatcher {
private var _smooth:Bool;
private var _repeat:Bool;
private var _mipmap:Bool;
private var _maxAnisotropy:Float;
private var _depthCompareMode:Context3DCompareMode;
private var _blendFactorSource:Context3DBlendFactor;
private var _blendFactorDest:Context3DBlendFactor;
Expand Down Expand Up @@ -105,6 +107,7 @@ class MaterialPassBase extends EventDispatcher {
_smooth = true;
_repeat = false;
_mipmap = true;
_maxAnisotropy = 1;
_depthCompareMode = Context3DCompareMode.LESS_EQUAL;

_blendFactorSource = Context3DBlendFactor.ONE;
Expand Down Expand Up @@ -168,6 +171,21 @@ class MaterialPassBase extends EventDispatcher {
return _mipmap;
}

/**
* Indicates the number of Anisotropic filtering samples to take for mipmapping
*/
public function get_maxAnisotropy():Float {
return _maxAnisotropy;
}

public function set_maxAnisotropy(value:Float):Float {
if (_maxAnisotropy == value)
return _maxAnisotropy;
_maxAnisotropy = value;
invalidateShaderProgram();
return maxAnisotropy;
}

/**
* Defines whether smoothing should be applied to any used textures.
*/
Expand Down
2 changes: 1 addition & 1 deletion away3d/textures/BitmapTexture.hx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class BitmapTexture extends Texture2DBase {
}

override private function uploadContent(texture:TextureBase):Void {
#if (flash || html5)
#if flash
if (_generateMipmaps) MipmapGenerator.generateMipMaps(_bitmapData, texture, _mipMapHolder, true)
else cast((texture), Texture).uploadFromBitmapData(_bitmapData, 0);
#else
Expand Down
2 changes: 1 addition & 1 deletion away3d/textures/RenderCubeTexture.hx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class RenderCubeTexture extends CubeTextureBase {
var bmd:BitmapData = new BitmapData(_width, _height, false, 0);
var i:Int = 0;
while (i < 6) {
#if (flash || html5)
#if flash
MipmapGenerator.generateMipMaps(bmd, texture, null, false, i);
#else
cast((texture), CubeTexture).uploadFromBitmapData(bmd, i, 0);
Expand Down
Loading

0 comments on commit 326b5cd

Please sign in to comment.