/// <summary> /// Begins the rendering of all the lights in the scene. /// </summary> private void BeginLights(DrawingContext3D context) { if (hasLightBegin || hasSceneBegin) { throw new InvalidOperationException("Begin cannot be called until End has been successfully called."); } hasLightBegin = true; CreateLightBuffer(); lightBuffer.Begin(); // Setup render states for light rendering // Clear specular intensity to 0 GraphicsDevice.Clear(new Color(context.ambientLightColor.X, context.ambientLightColor.Y, context.ambientLightColor.Z, 0)); // Set render state for lights GraphicsDevice.BlendState = lightBlendState; GraphicsDevice.DepthStencilState = DepthStencilState.None; // Set up textures and sampler states GraphicsDevice.Textures[0] = depthBuffer; GraphicsDevice.Textures[1] = normalBuffer; GraphicsDevice.SamplerStates[0] = GraphicsDevice.SamplerStates[1] = SamplerState.PointClamp; }
protected internal override void EndApplyLocalParameters(DrawingContext3D context) { if (lightBufferParameter != null) { context.graphics.SamplerStates[lightBufferIndex] = context.SamplerState; } }
protected internal override void BeginApplyLocalParameters(DrawingContext3D context, MaterialGroup material) { if (textureTransformParameter != null) { textureTransformParameter.SetValue(Nine.Graphics.TextureTransform.ToArray(transform)); } }
/// <summary> /// Draws a light instance for DeferredEffect. /// </summary> private void DrawLight(DrawingContext3D context, IDeferredLight light) { if (!hasLightBegin) { throw new InvalidOperationException("Begin must be called successfully before End can be called."); } var lightGeometry = light.PrepareLightGeometry(context); if (lightGeometry == null || !lightGeometry.OnAddedToView(context)) { return; } var lightMaterial = lightGeometry.Material; if (lightMaterial == null) { return; } lightMaterial.SetTexture(TextureUsage.DepthBuffer, depthBuffer); lightMaterial.SetTexture(TextureUsage.NormalMap, normalBuffer); lightMaterial.BeginApply(context); // Draw the model, using the specified effect. // Setup correct cull mode so that each pixel is rendered only once. GraphicsDevice.DepthStencilState = greaterDepth; lightGeometry.Draw(context, lightMaterial); lightMaterial.EndApply(context); }
protected internal override void ApplyGlobalParameters(DrawingContext3D context) { if (fogColorParameter != null) { fogColorParameter.SetValue(context.fogColor); } }
protected internal override void ApplyGlobalParameters(DrawingContext3D context) { if (viewProjectionParameter != null) { viewProjectionParameter.SetValue(context.matrices.ViewProjection); } }
protected internal override void BeginApplyLocalParameters(DrawingContext3D context, MaterialGroup material) { if (textureParameter != null) { textureParameter.SetValue(NormalMap); } }
/// <summary> /// Applies all the local shader parameters before drawing any primitives. /// </summary> protected internal override void BeginApplyLocalParameters(DrawingContext3D context, MaterialGroup material) { if (textureParameter != null) { textureParameter.SetValue(Texture ?? material.texture); } if (SamplerState != null) { context.graphics.SamplerStates[textureIndex] = SamplerState; } if (overlayColorParameter != null && overlayColor.HasValue) { overlayColorParameter.SetValue(overlayColor.Value); } if (diffuseColorParameter != null) { var diffuseColorWithAlpha = new Vector4(); diffuseColorWithAlpha.X = diffuseColor.X * material.alpha; diffuseColorWithAlpha.Y = diffuseColor.Y * material.alpha; diffuseColorWithAlpha.Z = diffuseColor.Z * material.alpha; diffuseColorWithAlpha.W = material.alpha; diffuseColorParameter.SetValue(diffuseColorWithAlpha); } }
/// <summary> /// Applies lights to the target effect from the drawing context. /// </summary> public static void ApplyLights(DrawingContext3D context, IEffectLights effect) { // Update ambient light color effect.AmbientLightColor = context.ambientLightColor; var light0 = context.directionalLights[0]; var light1 = context.directionalLights[1]; var light2 = context.directionalLights[2]; // Update the shader light parameters only when it changed. if (effect.DirectionalLight0.Enabled = light0.Enabled) { effect.DirectionalLight0.Direction = light0.Direction; effect.DirectionalLight0.DiffuseColor = light0.DiffuseColor; effect.DirectionalLight0.SpecularColor = light0.SpecularColor; } if (effect.DirectionalLight1.Enabled = light1.Enabled) { effect.DirectionalLight1.Direction = light1.Direction; effect.DirectionalLight1.DiffuseColor = light1.DiffuseColor; effect.DirectionalLight1.SpecularColor = light1.SpecularColor; } if (effect.DirectionalLight2.Enabled = light2.Enabled) { effect.DirectionalLight2.Direction = light2.Direction; effect.DirectionalLight2.DiffuseColor = light2.DiffuseColor; effect.DirectionalLight2.SpecularColor = light2.SpecularColor; } }
protected internal override void BeginApplyLocalParameters(DrawingContext3D context, MaterialGroup material) { if (transformParameter != null) { transformParameter.SetValue(colorMatrix); } }
protected internal override void ApplyGlobalParameters(DrawingContext3D context) { if (eyePositionParameter != null) { ambientLightColorParameter.SetValue(context.ambientLightColor); eyePositionParameter.SetValue(context.CameraPosition); } }
protected internal override void EndApplyLocalParameters(DrawingContext3D context) { if (shadowMapParameter != null) { context.graphics.Textures[shadowMapSamplerIndex] = null; context.graphics.SamplerStates[shadowMapSamplerIndex] = context.SamplerState; } }
/// <summary> /// Restores any local shader parameters changes after drawing the promitive. /// </summary> protected internal override void EndApplyLocalParameters(DrawingContext3D context) { var count = materialParts.Count; for (int i = 0; i < count; ++i) { materialParts[i].EndApplyLocalParameters(context); } }
/// <summary> /// Applies all the local shader parameters before drawing any primitives. /// </summary> protected internal override void BeginApplyLocalParameters(DrawingContext3D context, MaterialGroup material) { var count = materialParts.Count; for (int i = 0; i < count; ++i) { materialParts[i].BeginApplyLocalParameters(context, material); } }
protected internal override void ApplyGlobalParameters(DrawingContext3D context) { if (lightBufferParameter != null) { halfPixelParameter.SetValue(context.HalfPixel); lightBufferParameter.SetValue(context.textures[TextureUsage.LightBuffer]); context.graphics.SamplerStates[lightBufferIndex] = SamplerState.PointClamp; } }
/// <summary> /// Restores any local shader parameters changes after drawing the promitive. /// </summary> protected internal override void EndApplyLocalParameters(DrawingContext3D context) { if (specularColor.HasValue && specularColorParameter != null) { specularColorParameter.SetValue(Vector3.One); } if (specularPower.HasValue && specularPowerParameter != null) { specularPowerParameter.SetValue(Constants.SpecularPower); } }
/// <summary> /// Applies all the local shader parameters before drawing any primitives. /// </summary> protected internal override void BeginApplyLocalParameters(DrawingContext3D context, MaterialGroup material) { if (textureParameter != null) { textureParameter.SetValue(EmissiveMap); } if (emissiveColorParameter != null) { emissiveColorParameter.SetValue(emissiveColor); } }
/// <summary> /// Restores any local shader parameters changes after drawing the primitive. /// </summary> protected internal override void EndApplyLocalParameters(DrawingContext3D context) { if (overlayColorParameter != null && overlayColor.HasValue) { overlayColorParameter.SetValue(Constants.DiffuseColor); } if (SamplerState != null) { context.graphics.SamplerStates[textureIndex] = context.SamplerState; } }
/// <summary> /// Ends the rendering of the scene and generates DepthNormalMap. /// </summary> private void End(DrawingContext3D context) { if (!hasSceneBegin) { throw new InvalidOperationException("Begin must be called successfully before End can be called."); } Nine.Graphics.GraphicsExtensions.PopRenderTarget(context.graphics); context.textures[TextureUsage.DepthBuffer] = DepthBuffer; context.textures[TextureUsage.NormalBuffer] = NormalBuffer; GraphicsDevice.SetRenderTarget(null); hasSceneBegin = false; }
protected internal override void BeginApplyLocalParameters(DrawingContext3D context, MaterialGroup material) { if (textureParameter != null) { Vector4 detailTextureSettings = new Vector4(); detailTextureSettings.X = DetailTextureScale.X; detailTextureSettings.Y = DetailTextureScale.Y; detailTextureSettings.Z = Attenuation; detailTextureSettings.W = Distance; detailTextureSettingsParameter.SetValue(detailTextureSettings); textureParameter.SetValue(DetailTexture); } }
/// <summary> /// Applies all the local shader parameters before drawing any primitives. /// </summary> protected internal override void BeginApplyLocalParameters(DrawingContext3D context, MaterialGroup material) { if (textureParameter != null) { textureParameter.SetValue(SpecularMap); } if (specularColor.HasValue && specularColorParameter != null) { specularColorParameter.SetValue(specularColor.Value); } if (specularPower.HasValue && specularPowerParameter != null) { specularPowerParameter.SetValue(specularPower.Value); } }
protected internal override void BeginApplyLocalParameters(DrawingContext3D context, MaterialGroup material) { if (maskTextureScaleParameter != null) { maskTextureScaleParameter.SetValue(maskTextureScale); } if (maskTexture0 != null) { maskTexture0Parameter.SetValue(maskTexture0); } if (maskTexture1 != null) { maskTexture1Parameter.SetValue(maskTexture1); } }
protected internal override void ApplyGlobalParameters(DrawingContext3D context) { if (directionParameter == null) { return; } var light = context.directionalLights[lightIndex]; directionParameter.SetValue(light.Direction); diffuseColorParameter.SetValue(light.DiffuseColor); if (specularColorParameter != null) { specularColorParameter.SetValue(light.SpecularColor); } }
protected internal override void BeginApplyLocalParameters(DrawingContext3D context, MaterialGroup material) { if (worldParameter != null) { worldParameter.SetValue(material.world); } if (worldInverseTransposeParameter != null) { worldViewProjectionParameter.SetValue(material.world * context.Matrices.ViewProjection); } if (worldInverseTransposeParameter != null) { Matrix worldInverse; Matrix.Invert(ref material.world, out worldInverse); worldInverseTransposeParameter.SetValueTranspose(worldInverse); } }
/// <summary> /// Ends the rendering of lights and generates LightTexture. /// </summary> private Texture2D EndLights(DrawingContext3D context) { if (!hasLightBegin) { throw new InvalidOperationException("Begin must be called successfully before End can be called."); } lightBuffer.End(); hasLightBegin = false; // Restore render state to default GraphicsDevice.BlendState = BlendState.Opaque; GraphicsDevice.DepthStencilState = DepthStencilState.Default; GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise; GraphicsDevice.SamplerStates[0] = GraphicsDevice.SamplerStates[1] = context.SamplerState; context.textures[TextureUsage.LightBuffer] = LightBuffer; return(LightBuffer); }
/// <summary> /// Applies all the local shader parameters before drawing any primitives. /// </summary> protected internal override void BeginApplyLocalParameters(DrawingContext3D context, MaterialGroup material) { if (shadowMapParameter == null) return; var light = context.DirectionalLight; if (light.ShadowMap == null) return; var texture = light.ShadowMap.Texture; if (texture == null) return; shadowMapParameter.SetValue(texture); shadowMapSizeParameter.SetValue(new Vector2(1.0f / texture.Width, 1.0f / texture.Height)); lightViewProjectionParameter.SetValue(light.ShadowFrustum.Matrix); shadowColorParameter.SetValue(ShadowColor); context.graphics.SamplerStates[shadowMapSamplerIndex] = SamplerState.PointClamp; }
/// <summary> /// Begins the rendering of the scene using DepthNormalEffect. /// </summary> private void Begin(DrawingContext3D context) { if (hasSceneBegin || hasLightBegin) { throw new InvalidOperationException("Begin cannot be called until End has been successfully called."); } hasSceneBegin = true; CreateDepthNormalBuffers(); // Maintain render target stack Nine.Graphics.GraphicsExtensions.PushRenderTarget(context.graphics, null); renderTargetBinding[0] = new RenderTargetBinding(depthBuffer); renderTargetBinding[1] = new RenderTargetBinding(normalBuffer); GraphicsDevice.SetRenderTargets(renderTargetBinding); GraphicsDevice.BlendState = BlendState.Opaque; ClearRenderTargets(context); }
/// <summary> /// Applies all the global shader parameters before drawing any primitives. /// </summary> protected internal virtual void ApplyGlobalParameters(DrawingContext3D context) { }
/// <summary> /// Applies all the local shader parameters before drawing any primitives. /// </summary> protected internal virtual void BeginApplyLocalParameters(DrawingContext3D context, MaterialGroup material) { }
/// <summary> /// Restores any local shader parameters changes after drawing the promitive. /// </summary> protected internal virtual void EndApplyLocalParameters(DrawingContext3D context) { }