/// <summary> /// Lazily computes derived parameter values immediately before applying the effect. /// </summary> protected internal override void OnApply() { // Recompute the world+view+projection matrix or fog vector? dirtyFlags = EffectHelpers.SetWorldViewProjAndFog(dirtyFlags, ref world, ref view, ref projection, ref worldView, fogEnabled, fogStart, fogEnd, worldViewProjParam, fogVectorParam); // Recompute the diffuse/alpha material color parameter? if ((dirtyFlags & EffectDirtyFlags.MaterialColor) != 0) { diffuseColorParam.SetValue(new Vector4(diffuseColor * alpha, alpha)); dirtyFlags &= ~EffectDirtyFlags.MaterialColor; } // Recompute the shader index? if ((dirtyFlags & EffectDirtyFlags.ShaderIndex) != 0) { int shaderIndex = 0; if (!fogEnabled) { shaderIndex += 1; } if (vertexColorEnabled) { shaderIndex += 2; } dirtyFlags &= ~EffectDirtyFlags.ShaderIndex; CurrentTechnique = Techniques[shaderIndex]; } }
protected internal override bool OnApply() { this.dirtyFlags = EffectHelpers.SetWorldViewProjAndFog(this.dirtyFlags, ref this.world, ref this.view, ref this.projection, ref this.worldView, this.fogEnabled, this.fogStart, this.fogEnd, this.worldViewProjParam, this.fogVectorParam); if ((this.dirtyFlags & EffectDirtyFlags.MaterialColor) != ~EffectDirtyFlags.All) { this.diffuseColorParam.SetValue(new Vector4(this.diffuseColor * this.alpha, this.alpha)); this.dirtyFlags &= ~EffectDirtyFlags.MaterialColor; } if ((this.dirtyFlags & EffectDirtyFlags.ShaderIndex) != ~EffectDirtyFlags.All) { int num = 0; if (!this.fogEnabled) { ++num; } if (this.vertexColorEnabled) { num += 2; } this.dirtyFlags &= ~EffectDirtyFlags.ShaderIndex; if (this._shaderIndex != num) { this._shaderIndex = num; this.CurrentTechnique = this.Techniques[this._shaderIndex]; return(true); } } return(false); }
protected internal override bool OnApply() { this.dirtyFlags = EffectHelpers.SetWorldViewProjAndFog(this.dirtyFlags, ref this.world, ref this.view, ref this.projection, ref this.worldView, this.fogEnabled, this.fogStart, this.fogEnd, this.worldViewProjParam, this.fogVectorParam); if ((this.dirtyFlags & EffectDirtyFlags.MaterialColor) != ~EffectDirtyFlags.All) { EffectHelpers.SetMaterialColor(this.lightingEnabled, this.alpha, ref this.diffuseColor, ref this.emissiveColor, ref this.ambientLightColor, this.diffuseColorParam, this.emissiveColorParam); this.dirtyFlags &= ~EffectDirtyFlags.MaterialColor; } if (this.lightingEnabled) { this.dirtyFlags = EffectHelpers.SetLightingMatrices(this.dirtyFlags, ref this.world, ref this.view, this.worldParam, this.worldInverseTransposeParam, this.eyePositionParam); bool flag = !this.light1.Enabled && !this.light2.Enabled; if (this.oneLight != flag) { this.oneLight = flag; this.dirtyFlags |= EffectDirtyFlags.ShaderIndex; } } if ((this.dirtyFlags & EffectDirtyFlags.ShaderIndex) != ~EffectDirtyFlags.All) { int num = 0; if (!this.fogEnabled) { ++num; } if (this.vertexColorEnabled) { num += 2; } if (this.textureEnabled) { num += 4; } if (this.lightingEnabled) { if (this.preferPerPixelLighting) { num += 24; } else if (this.oneLight) { num += 16; } else { num += 8; } } this.dirtyFlags &= ~EffectDirtyFlags.ShaderIndex; if (this._shaderIndex != num) { this._shaderIndex = num; this.CurrentTechnique = this.Techniques[this._shaderIndex]; return(true); } } return(false); }
/// <summary> /// Lazily computes derived parameter values immediately before applying the effect. /// </summary> protected internal override void OnApply() { // Recompute the world+view+projection matrix or fog vector? dirtyFlags = EffectHelpers.SetWorldViewProjAndFog(dirtyFlags, ref world, ref view, ref projection, ref worldView, fogEnabled, fogStart, fogEnd, worldViewProjParam, fogVectorParam); // Recompute the world inverse transpose and eye position? dirtyFlags = EffectHelpers.SetLightingMatrices(dirtyFlags, ref world, ref view, worldParam, worldInverseTransposeParam, eyePositionParam); // Recompute the diffuse/emissive/alpha material color parameters? if ((dirtyFlags & EffectDirtyFlags.MaterialColor) != 0) { EffectHelpers.SetMaterialColor(true, alpha, ref diffuseColor, ref emissiveColor, ref ambientLightColor, diffuseColorParam, emissiveColorParam); dirtyFlags &= ~EffectDirtyFlags.MaterialColor; } // Check if we can use the only-bother-with-the-first-light shader optimization. bool newOneLight = !light1.Enabled && !light2.Enabled; if (oneLight != newOneLight) { oneLight = newOneLight; dirtyFlags |= EffectDirtyFlags.ShaderIndex; } // Recompute the shader index? if ((dirtyFlags & EffectDirtyFlags.ShaderIndex) != 0) { int shaderIndex = 0; if (!fogEnabled) { shaderIndex += 1; } if (weightsPerVertex == 2) { shaderIndex += 2; } else if (weightsPerVertex == 4) { shaderIndex += 4; } if (preferPerPixelLighting) { shaderIndex += 12; } else if (oneLight) { shaderIndex += 6; } shaderIndexParam.SetValue(shaderIndex); dirtyFlags &= ~EffectDirtyFlags.ShaderIndex; } }
/// <summary> /// Lazily computes derived parameter values immediately before applying the effect. /// </summary> internal override bool OnApply() { // Recompute the world+view+projection matrix or fog vector? dirtyFlags = EffectHelpers.SetWorldViewProjAndFog(dirtyFlags, ref world, ref view, ref projection, ref worldView, ref worldViewProj); if (texture != null) { device.Textures[0] = texture; } if (dirtyFlags != 0) { XnaToUnity.Matrix(worldViewProj, out worldViewProj_Property.Value); worldViewProj_Property.Value.m23 = 0; worldViewProj_Property.ApplyToMaterial(material); } return(false); }
/// <summary> /// Lazily computes derived parameter values immediately before applying the effect. /// </summary> protected internal override void OnApply() { // Recompute the world+view+projection matrix or fog vector? dirtyFlags = EffectHelpers.SetWorldViewProjAndFog(dirtyFlags, ref world, ref view, ref projection, ref worldView, fogEnabled, fogStart, fogEnd, worldViewProjParam, fogVectorParam); // Recompute the diffuse/alpha material color parameter? if ((dirtyFlags & EffectDirtyFlags.MaterialColor) != 0) { diffuseColorParam.SetValue(new Vector4(diffuseColor * alpha, alpha)); dirtyFlags &= ~EffectDirtyFlags.MaterialColor; } // Recompute the alpha test settings? if ((dirtyFlags & EffectDirtyFlags.AlphaTest) != 0) { Vector4 alphaTest = new Vector4(); bool eqNe = false; // Convert reference alpha from 8 bit integer to 0-1 float format. float reference = (float)referenceAlpha / 255f; // Comparison tolerance of half the 8 bit integer precision. const float threshold = 0.5f / 255f; switch (alphaFunction) { case CompareFunction.Less: // Shader will evaluate: clip((a < x) ? z : w) alphaTest.X = reference - threshold; alphaTest.Z = 1; alphaTest.W = -1; break; case CompareFunction.LessEqual: // Shader will evaluate: clip((a < x) ? z : w) alphaTest.X = reference + threshold; alphaTest.Z = 1; alphaTest.W = -1; break; case CompareFunction.GreaterEqual: // Shader will evaluate: clip((a < x) ? z : w) alphaTest.X = reference - threshold; alphaTest.Z = -1; alphaTest.W = 1; break; case CompareFunction.Greater: // Shader will evaluate: clip((a < x) ? z : w) alphaTest.X = reference + threshold; alphaTest.Z = -1; alphaTest.W = 1; break; case CompareFunction.Equal: // Shader will evaluate: clip((abs(a - x) < Y) ? z : w) alphaTest.X = reference; alphaTest.Y = threshold; alphaTest.Z = 1; alphaTest.W = -1; eqNe = true; break; case CompareFunction.NotEqual: // Shader will evaluate: clip((abs(a - x) < Y) ? z : w) alphaTest.X = reference; alphaTest.Y = threshold; alphaTest.Z = -1; alphaTest.W = 1; eqNe = true; break; case CompareFunction.Never: // Shader will evaluate: clip((a < x) ? z : w) alphaTest.Z = -1; alphaTest.W = -1; break; case CompareFunction.Always: default: // Shader will evaluate: clip((a < x) ? z : w) alphaTest.Z = 1; alphaTest.W = 1; break; } alphaTestParam.SetValue(alphaTest); dirtyFlags &= ~EffectDirtyFlags.AlphaTest; // If we changed between less/greater vs. equal/notequal // compare modes, we must also update the shader index. if (isEqNe != eqNe) { isEqNe = eqNe; dirtyFlags |= EffectDirtyFlags.ShaderIndex; } } // Recompute the shader index? if ((dirtyFlags & EffectDirtyFlags.ShaderIndex) != 0) { int shaderIndex = 0; if (!fogEnabled) { shaderIndex += 1; } if (vertexColorEnabled) { shaderIndex += 2; } if (isEqNe) { shaderIndex += 4; } shaderIndexParam.SetValue(shaderIndex); dirtyFlags &= ~EffectDirtyFlags.ShaderIndex; } }
/// <summary> /// Lazily computes derived parameter values immediately before applying the effect. /// </summary> protected internal override bool OnApply() { // Recompute the world+view+projection matrix or fog vector? dirtyFlags = EffectHelpers.SetWorldViewProjAndFog(dirtyFlags, ref world, ref view, ref projection, ref worldView, fogEnabled, fogStart, fogEnd, worldViewProjParam, fogVectorParam); // Recompute the world inverse transpose and eye position? dirtyFlags = EffectHelpers.SetLightingMatrices(dirtyFlags, ref world, ref view, worldParam, worldInverseTransposeParam, eyePositionParam); // Recompute the diffuse/emissive/alpha material color parameters? if ((dirtyFlags & EffectDirtyFlags.MaterialColor) != 0) { EffectHelpers.SetMaterialColor(true, alpha, ref diffuseColor, ref emissiveColor, ref ambientLightColor, diffuseColorParam, emissiveColorParam); dirtyFlags &= ~EffectDirtyFlags.MaterialColor; } // Check if we can use the only-bother-with-the-first-light shader optimization. bool newOneLight = !light1.Enabled && !light2.Enabled; if (oneLight != newOneLight) { oneLight = newOneLight; dirtyFlags |= EffectDirtyFlags.ShaderIndex; } // Recompute the shader index? if ((dirtyFlags & EffectDirtyFlags.ShaderIndex) != 0) { int shaderIndex = 0; if (!fogEnabled) { shaderIndex += 1; } if (fresnelEnabled) { shaderIndex += 2; } if (specularEnabled) { shaderIndex += 4; } if (oneLight) { shaderIndex += 8; } dirtyFlags &= ~EffectDirtyFlags.ShaderIndex; if (_shaderIndex != shaderIndex) { _shaderIndex = shaderIndex; CurrentTechnique = Techniques[_shaderIndex]; return(true); } } return(false); }
protected internal override bool OnApply() { this.dirtyFlags = EffectHelpers.SetWorldViewProjAndFog(this.dirtyFlags, ref this.world, ref this.view, ref this.projection, ref this.worldView, this.fogEnabled, this.fogStart, this.fogEnd, this.worldViewProjParam, this.fogVectorParam); if ((this.dirtyFlags & EffectDirtyFlags.MaterialColor) != ~EffectDirtyFlags.All) { this.diffuseColorParam.SetValue(new Vector4(this.diffuseColor * this.alpha, this.alpha)); this.dirtyFlags &= ~EffectDirtyFlags.MaterialColor; } if ((this.dirtyFlags & EffectDirtyFlags.AlphaTest) != ~EffectDirtyFlags.All) { Vector4 vector4 = new Vector4(); bool flag = false; float num = (float)this.referenceAlpha / (float)byte.MaxValue; switch (this.alphaFunction) { case CompareFunction.Never: vector4.Z = -1f; vector4.W = -1f; break; case CompareFunction.Less: vector4.X = num - 0.001960784f; vector4.Z = 1f; vector4.W = -1f; break; case CompareFunction.LessEqual: vector4.X = num + 0.001960784f; vector4.Z = 1f; vector4.W = -1f; break; case CompareFunction.Equal: vector4.X = num; vector4.Y = 0.001960784f; vector4.Z = 1f; vector4.W = -1f; flag = true; break; case CompareFunction.GreaterEqual: vector4.X = num - 0.001960784f; vector4.Z = -1f; vector4.W = 1f; break; case CompareFunction.Greater: vector4.X = num + 0.001960784f; vector4.Z = -1f; vector4.W = 1f; break; case CompareFunction.NotEqual: vector4.X = num; vector4.Y = 0.001960784f; vector4.Z = -1f; vector4.W = 1f; flag = true; break; default: vector4.Z = 1f; vector4.W = 1f; break; } this.alphaTestParam.SetValue(vector4); this.dirtyFlags &= ~EffectDirtyFlags.AlphaTest; if (this.isEqNe != flag) { this.isEqNe = flag; this.dirtyFlags |= EffectDirtyFlags.ShaderIndex; } } if ((this.dirtyFlags & EffectDirtyFlags.ShaderIndex) != ~EffectDirtyFlags.All) { int num = 0; if (!this.fogEnabled) { ++num; } if (this.vertexColorEnabled) { num += 2; } if (this.isEqNe) { num += 4; } this.dirtyFlags &= ~EffectDirtyFlags.ShaderIndex; if (this._shaderIndex != num) { this._shaderIndex = num; this.CurrentTechnique = this.Techniques[this._shaderIndex]; return(true); } } return(false); }
/// <summary> /// Lazily computes derived parameter values immediately before applying the effect. /// </summary> protected internal override void OnApply() { // Recompute the shader index? // if ((dirtyFlags & EffectDirtyFlags.ShaderIndex) != 0) { int shaderIndex = 0; if (!fogEnabled) { shaderIndex += 1; } if (vertexColorEnabled) { shaderIndex += 2; } if (textureEnabled) { shaderIndex += 4; } if (lightingEnabled) { if (preferPerPixelLighting) { shaderIndex += 24; } else if (oneLight) { shaderIndex += 16; } else { shaderIndex += 8; } } // // //shaderIndexParam.SetValue (shaderIndex); // // dirtyFlags &= ~EffectDirtyFlags.ShaderIndex; if (oldIndex != shaderIndex) { int vertexShader = VSArray[VSIndices[shaderIndex]]; int fragmentShader = PSArray[PSIndices[shaderIndex]]; UpdateTechnique("BasicEffect", "", vertexShader, fragmentShader); oldIndex = shaderIndex; // Update here } // } // These are the states that work GLStateManager.Projection(Projection); GLStateManager.WorldView(world, view); // Override this for now for testing purposes dirtyFlags |= EffectDirtyFlags.World | EffectDirtyFlags.WorldViewProj; dirtyFlags |= EffectDirtyFlags.WorldViewProj | EffectDirtyFlags.EyePosition; dirtyFlags &= ~EffectDirtyFlags.FogEnable; // turn off fog for now dirtyFlags |= EffectDirtyFlags.MaterialColor; GLStateManager.Textures2D(TextureEnabled); GLStateManager.ColorArray(VertexColorEnabled); // Recompute the world+view+projection matrix or fog vector? dirtyFlags = EffectHelpers.SetWorldViewProjAndFog(dirtyFlags, ref world, ref view, ref projection, ref worldView, fogEnabled, fogStart, fogEnd, worldViewProjParam, fogVectorParam); // Recompute the diffuse/emissive/alpha material color parameters? if ((dirtyFlags & EffectDirtyFlags.MaterialColor) != 0) { EffectHelpers.SetMaterialColor(lightingEnabled, alpha, ref diffuseColor, ref emissiveColor, ref ambientLightColor, diffuseColorParam, emissiveColorParam); dirtyFlags &= ~EffectDirtyFlags.MaterialColor; } if (TextureEnabled) { _texture.Apply(); textureParam.SetValue(_texture); //System.Console.WriteLine("Texture set"); } // // if (lightingEnabled) { // // Recompute the world inverse transpose and eye position? // dirtyFlags = EffectHelpers.SetLightingMatrices (dirtyFlags, ref world, ref view, worldParam, worldInverseTransposeParam, eyePositionParam); // // // Check if we can use the only-bother-with-the-first-light shader optimization. // bool newOneLight = !light1.Enabled && !light2.Enabled; // // if (oneLight != newOneLight) { // oneLight = newOneLight; // dirtyFlags |= EffectDirtyFlags.ShaderIndex; // } // } }