Skip to content

Commit

Permalink
Re-worked the anisotropic filtering to fall in line with the AS3 Cont…
Browse files Browse the repository at this point in the history
…ext3DFilterType enum and introduced new TextureMaterial.anisotropy property to control the level.
  • Loading branch information
Greg209 committed Oct 1, 2014
1 parent 326b5cd commit 58a2064
Show file tree
Hide file tree
Showing 21 changed files with 382 additions and 220 deletions.
21 changes: 11 additions & 10 deletions away3d/materials/MaterialBase.hx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import away3d.materials.lightpickers.LightPickerBase;
import away3d.materials.passes.DepthMapPass;
import away3d.materials.passes.DistanceMapPass;
import away3d.materials.passes.MaterialPassBase;
import away3d.textures.Anisotropy;
import openfl.display.BlendMode;
import openfl.display3D.Context3D;
import openfl.display3D.Context3DCompareMode;
Expand All @@ -40,7 +41,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 anisotropy(get_anisotropy, set_anisotropy):Anisotropy;
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 @@ -99,7 +100,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 _anisotropy:Anisotropy;
private var _depthPass:DepthMapPass;
private var _distancePass:DistanceMapPass;
private var _lightPicker:LightPickerBase;
Expand All @@ -113,7 +114,7 @@ class MaterialBase extends NamedAssetBase implements IAsset {
_blendMode = BlendMode.NORMAL;
_mipmap = true;
_smooth = true;
_maxAnisotropy = 1;
_anisotropy = Anisotropy.ANISOTROPIC2X;
_depthCompareMode = Context3DCompareMode.LESS_EQUAL;
_owners = new Array<IMaterialOwner>();
_passes = new Array<MaterialPassBase>();
Expand Down Expand Up @@ -229,18 +230,18 @@ class MaterialBase extends NamedAssetBase implements IAsset {
/**
* Indicates the number of Anisotropic filtering samples to take for mipmapping
*/
public function get_maxAnisotropy():Float {
return _maxAnisotropy;
public function get_anisotropy():Anisotropy {
return _anisotropy;
}

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

/**
Expand Down Expand Up @@ -615,7 +616,7 @@ class MaterialBase extends NamedAssetBase implements IAsset {
pass.mipmap = _mipmap;
pass.smooth = _smooth;
pass.repeat = _repeat;
pass.maxAnisotropy = _maxAnisotropy;
pass.anisotropy = _anisotropy;
pass.lightPicker = _lightPicker;
pass.bothSides = _bothSides;
pass.addEventListener(Event.CHANGE, onPassChange);
Expand Down
7 changes: 4 additions & 3 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;
import away3d.textures.Anisotropy;

class TextureMaterial extends SinglePassMaterialBase {
public var animateUVs(get_animateUVs, set_animateUVs):Bool;
Expand All @@ -19,15 +20,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.
* @param maxAnisotropy Indicates the number of samples to use if Anisotropic mipmap filtering is applied
* @param anisotropy 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) {
public function new(texture:Texture2DBase = null, smooth:Bool = true, repeat:Bool = false, mipmap:Bool = true, anisotropy:Anisotropy = null ) {
super();
this.texture = texture;
this.smooth = smooth;
this.repeat = repeat;
this.mipmap = mipmap;
this.maxAnisotropy = maxAnisotropy;
this.anisotropy = (anisotropy == null ? Anisotropy.ANISOTROPIC2X : anisotropy);
}

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

import away3d.textures.Texture2DBase;
import away3d.textures.Anisotropy;

class TextureMultiPassMaterial extends MultiPassMaterialBase {
public var animateUVs(get_animateUVs, set_animateUVs):Bool;
Expand All @@ -18,15 +19,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.
* @param maxAnisotropy Indicates the number of samples to use if Anisotropic mipmap filtering is applied
* @param anisotropy 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) {
public function new(texture:Texture2DBase = null, smooth:Bool = true, repeat:Bool = false, mipmap:Bool = true, anisotropy:Anisotropy = null) {
super();
this.texture = texture;
this.smooth = smooth;
this.repeat = repeat;
this.mipmap = mipmap;
this.maxAnisotropy = maxAnisotropy;
this.anisotropy = (anisotropy==null ? Anisotropy.ANISOTROPIC2X : anisotropy);
}

/**
Expand Down
9 changes: 5 additions & 4 deletions away3d/materials/compilation/ShaderCompiler.hx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import away3d.materials.methods.MethodVO;
import away3d.materials.methods.MethodVOSet;
import away3d.materials.methods.ShaderMethodSetup;
import away3d.materials.methods.ShadingMethodBase;
import away3d.textures.Anisotropy;

import openfl.Vector;

Expand Down Expand Up @@ -66,7 +67,7 @@ class ShaderCompiler {
private var _smooth:Bool;
private var _repeat:Bool;
private var _mipmap:Bool;
private var _maxAnisotropy:Float;
private var _anisotropy:Anisotropy;
private var _enableLightFallOff:Bool;
private var _preserveAlpha:Bool;
private var _animateUVs:Bool;
Expand Down Expand Up @@ -231,11 +232,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, maxAnisotropy:Float = 1):Void {
public function setTextureSampling(smooth:Bool, repeat:Bool, mipmap:Bool, anisotropy:Anisotropy ):Void {
_smooth = smooth;
_repeat = repeat;
_mipmap = mipmap;
_maxAnisotropy = maxAnisotropy;
_anisotropy = anisotropy;
}

/**
Expand Down Expand Up @@ -479,7 +480,7 @@ class ShaderCompiler {
methodVO.useSmoothTextures = _smooth;
methodVO.repeatTextures = _repeat;
methodVO.useMipmapping = _mipmap;
methodVO.maxAnisotropy = _maxAnisotropy;
methodVO.anisotropy = _anisotropy;
methodVO.useLightFallOff = _enableLightFallOff && _profile != "baselineConstrained";
methodVO.numLights = _numLights + _numLightProbes;
method.initVO(methodVO);
Expand Down
5 changes: 4 additions & 1 deletion away3d/materials/methods/BasicAmbientMethod.hx
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,10 @@ 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, vo.maxAnisotropy );
stage3DProxy._context3D.setSamplerStateAt(
vo.texturesIndex, vo.repeatTextures ? Context3DWrapMode.REPEAT : Context3DWrapMode.CLAMP,
getSmoothingFilter(vo.useSmoothTextures, vo.anisotropy),
vo.useMipmapping ? Context3DMipFilter.MIPLINEAR : Context3DMipFilter.MIPNONE );
#end
stage3DProxy._context3D.setTextureAt(vo.texturesIndex, _texture.getTextureForStage3D(stage3DProxy));
}
Expand Down
5 changes: 4 additions & 1 deletion away3d/materials/methods/BasicDiffuseMethod.hx
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,10 @@ 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, vo.maxAnisotropy );
stage3DProxy._context3D.setSamplerStateAt(
vo.texturesIndex, vo.repeatTextures ? Context3DWrapMode.REPEAT : Context3DWrapMode.CLAMP,
getSmoothingFilter(vo.useSmoothTextures, vo.anisotropy),
vo.useMipmapping ? Context3DMipFilter.MIPLINEAR : Context3DMipFilter.MIPNONE );
#end
stage3DProxy._context3D.setTextureAt(vo.texturesIndex, _texture.getTextureForStage3D(stage3DProxy));
if (_alphaThreshold > 0) vo.fragmentData[vo.fragmentConstantsIndex] = _alphaThreshold;
Expand Down
5 changes: 4 additions & 1 deletion away3d/materials/methods/BasicNormalMethod.hx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ 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, vo.maxAnisotropy );
stage3DProxy._context3D.setSamplerStateAt(
vo.texturesIndex, vo.repeatTextures ? Context3DWrapMode.REPEAT : Context3DWrapMode.CLAMP,
getSmoothingFilter(vo.useSmoothTextures, vo.anisotropy),
vo.useMipmapping ? Context3DMipFilter.MIPLINEAR : Context3DMipFilter.MIPNONE );
#end
stage3DProxy._context3D.setTextureAt(vo.texturesIndex, _texture.getTextureForStage3D(stage3DProxy));
}
Expand Down
5 changes: 4 additions & 1 deletion away3d/materials/methods/BasicSpecularMethod.hx
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,10 @@ class BasicSpecularMethod extends LightingMethodBase {
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, vo.maxAnisotropy );
stage3DProxy._context3D.setSamplerStateAt(
vo.texturesIndex, vo.repeatTextures ? Context3DWrapMode.REPEAT : Context3DWrapMode.CLAMP,
getSmoothingFilter(vo.useSmoothTextures, vo.anisotropy),
vo.useMipmapping ? Context3DMipFilter.MIPLINEAR : Context3DMipFilter.MIPNONE );
#end
stage3DProxy._context3D.setTextureAt(vo.texturesIndex, _texture.getTextureForStage3D(stage3DProxy));
}
Expand Down
5 changes: 3 additions & 2 deletions away3d/materials/methods/MethodVO.hx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package away3d.materials.methods;

import openfl.Vector;
import away3d.textures.Anisotropy;

class MethodVO {

Expand All @@ -23,7 +24,7 @@ class MethodVO {
public var useMipmapping:Bool;
public var useSmoothTextures:Bool;
public var repeatTextures:Bool;
public var maxAnisotropy:Float;
public var anisotropy:Anisotropy;
// internal stuff for the material to know before assembling code
public var needsProjection:Bool;
public var needsView:Bool;
Expand Down Expand Up @@ -51,7 +52,7 @@ class MethodVO {
vertexConstantsIndex = -1;
fragmentConstantsIndex = -1;
useMipmapping = true;
maxAnisotropy = 1;
anisotropy = Anisotropy.ANISOTROPIC2X;
useSmoothTextures = true;
repeatTextures = false;
needsProjection = false;
Expand Down
25 changes: 24 additions & 1 deletion away3d/materials/methods/ShadingMethodBase.hx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package away3d.materials.methods;

import away3d.events.ShadingMethodEvent;
import away3d.events.ShadingMethodEvent;
import openfl.display3D.Context3DTextureFormat;
import away3d.textures.TextureProxyBase;
import away3d.materials.compilation.ShaderRegisterElement;
import away3d.cameras.Camera3D;
Expand All @@ -16,6 +15,10 @@ import away3d.materials.compilation.ShaderRegisterCache;
import away3d.library.assets.NamedAssetBase;
import away3d.materials.compilation.ShaderRegisterData;
import away3d.materials.passes.MaterialPassBase;
import away3d.textures.Anisotropy;

import openfl.display3D.Context3DTextureFormat;
import openfl.display3D.Context3DTextureFilter;

class ShadingMethodBase extends NamedAssetBase {
public var sharedRegisters(get_sharedRegisters, set_sharedRegisters):ShaderRegisterData;
Expand Down Expand Up @@ -203,5 +206,25 @@ class ShadingMethodBase extends NamedAssetBase {
*/
public function copyFrom(method:ShadingMethodBase):Void {
}

/*
* Set the smoothing dependent on smooth property and anisotropy property from the VO
*/
private function getSmoothingFilter(smooth:Bool, anisotropy:Anisotropy) {
#if flash
return smooth ? Context3DTextureFilter.LINEAR : Context3DTextureFilter.NEAREST;
#else
if (smooth) {
switch (anisotropy) {
case Anisotropy.ANISOTROPIC2X : return Context3DTextureFilter.ANISOTROPIC2X;
case Anisotropy.ANISOTROPIC4X : return Context3DTextureFilter.ANISOTROPIC4X;
case Anisotropy.ANISOTROPIC8X : return Context3DTextureFilter.ANISOTROPIC8X;
case Anisotropy.ANISOTROPIC16X : return Context3DTextureFilter.ANISOTROPIC16X;
case Anisotropy.NONE : return Context3DTextureFilter.LINEAR;
}
} else
return Context3DTextureFilter.NEAREST;
#end
}
}

9 changes: 5 additions & 4 deletions away3d/materials/passes/CompiledPass.hx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import away3d.materials.methods.MethodVOSet;
import away3d.materials.methods.ShaderMethodSetup;
import away3d.materials.methods.ShadowMapMethodBase;
import away3d.textures.Texture2DBase;
import away3d.textures.Anisotropy;
import openfl.display3D.Context3D;
import openfl.display3D.Context3DProgramType;
import openfl.geom.Matrix;
Expand Down Expand Up @@ -209,7 +210,7 @@ class CompiledPass extends MaterialPassBase {
_compiler.methodSetup = _methodSetup;
_compiler.diffuseLightSources = _diffuseLightSources;
_compiler.specularLightSources = _specularLightSources;
_compiler.setTextureSampling(_smooth, _repeat, _mipmap, _maxAnisotropy);
_compiler.setTextureSampling(_smooth, _repeat, _mipmap, _anisotropy);
_compiler.setConstantDataBuffers(_vertexConstantData, _fragmentConstantData);
_compiler.animateUVs = _animateUVs;
_compiler.alphaPremultiplied = _alphaPremultiplied && _enableBlending;
Expand Down Expand Up @@ -305,9 +306,9 @@ class CompiledPass extends MaterialPassBase {
/**
* @inheritDoc
*/
override public function set_maxAnisotropy(value:Float):Float {
if (_maxAnisotropy == value) return value;
super.maxAnisotropy = value;
override public function set_anisotropy(value:Anisotropy):Anisotropy {
if (_anisotropy == value) return value;
super.anisotropy = value;
return value;
}

Expand Down
Loading

0 comments on commit 58a2064

Please sign in to comment.