Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
SkyBoxPlugin.cs
Go to the documentation of this file.
1 // Copyright (c) 2011 Silicon Studio
2 
3 using System;
4 using System.Collections.Generic;
5 
6 using SiliconStudio.Paradox.DataModel;
7 using SiliconStudio.Paradox.Effects.Modules;
8 using SiliconStudio.Paradox.Graphics;
9 using SiliconStudio.Core;
10 using SiliconStudio.Core.Mathematics;
11 using SiliconStudio.Paradox.Shaders;
12 
14 
15 namespace SiliconStudio.Paradox.Effects
16 {
17  /// <summary>
18  /// Posteffect manager.
19  /// </summary>
21  {
22  public SkyBoxPlugin() : this(null)
23  {
24  }
25 
26  public SkyBoxPlugin(string name) : this(name, null)
27  {
28  }
29 
30  public SkyBoxPlugin(string name, ShaderSource skyBoxComposition)
31  : base(name)
32  {
33  Texture = null;
34  SkyBoxColor = skyBoxComposition;
35  }
36 
37  public MainPlugin MainPlugin { get; set; }
38 
39  public RenderTargetsPlugin MainTargetPlugin { get; set; }
40 
41  internal EffectOld skyboxEffect;
42 
43  public Texture2D Texture
44  {
45  get
46  {
47  return Parameters.Get(TexturingKeys.Texture0);
48  }
49  set
50  {
51  Parameters.Set(TexturingKeys.Texture0, value);
52  }
53  }
54 
55  public ShaderSource SkyBoxColor { get; set; }
56 
57  public override void Load()
58  {
59  base.Load();
60 
61  skyboxEffect = this.EffectSystemOld.BuildEffect("Skybox")
62  .Using(new StateShaderPlugin() { RenderPassPlugin = this, UseDepthStencilState = true })
63  .Using(new BasicShaderPlugin(
64  new ShaderMixinSource() {
65  Mixins = new List<ShaderClassSource>() { new ShaderClassSource("SkyBox")},
66  Compositions = new Dictionary<string, ShaderSource>() { {"color", SkyBoxColor}}
67  }) { RenderPassPlugin = this })
68  .InstantiatePermutation();
69 
70  if (OfflineCompilation)
71  return;
72 
73  Parameters.AddSources(MainPlugin.ViewParameters);
74 
75  var zBackgroundValue = MainTargetPlugin.ClearDepth;
76  // Generates a quad for post effect rendering (should be utility function)
77  var vertices = new[]
78  {
79  -1.0f, 1.0f, zBackgroundValue, 1.0f,
80  1.0f, 1.0f, zBackgroundValue, 1.0f,
81  -1.0f, -1.0f, zBackgroundValue, 1.0f,
82  1.0f, -1.0f, zBackgroundValue, 1.0f,
83  };
84 
85  Parameters.RegisterParameter(EffectPlugin.DepthStencilStateKey);
86  Parameters.Set(TexturingKeys.Sampler, GraphicsDevice.SamplerStates.LinearWrap);
87 
88  // Use the quad for this effectMesh
89  var quadData = new Mesh();
90  quadData.Draw = new MeshDraw
91  {
92  DrawCount = 4,
93  PrimitiveType = PrimitiveType.TriangleStrip,
94  VertexBuffers = new[]
95  {
97  }
98  };
99 
100  RenderPass.StartPass += (context) =>
101  {
102  // Setup the Viewport
103  context.GraphicsDevice.SetViewport(MainTargetPlugin.Viewport);
104 
105  // Setup the depth stencil and main render target.
106  context.GraphicsDevice.SetRenderTarget(MainTargetPlugin.DepthStencil, MainTargetPlugin.RenderTarget);
107  };
108 
109  RenderPass.EndPass += (context) => context.GraphicsDevice.UnsetRenderTargets();
110 
111  var skyboxMesh = new EffectMesh(skyboxEffect, quadData).KeepAliveBy(this);
112  // If the main target plugin is not clearing anything, we assume that this is the job of the skybox plugin
113  if (!MainTargetPlugin.EnableClearTarget && !MainTargetPlugin.EnableClearDepth)
114  {
115  var description = new DepthStencilStateDescription().Default();
116  description.DepthBufferFunction = CompareFunction.Always;
117  var alwaysWrite = DepthStencilState.New(GraphicsDevice, description);
118  skyboxMesh.Parameters.Set(EffectPlugin.DepthStencilStateKey, alwaysWrite);
119  }
120  else
121  {
122  skyboxMesh.Parameters.Set(EffectPlugin.DepthStencilStateKey, MainTargetPlugin.DepthStencilState);
123  }
124 
125  skyboxMesh.Parameters.AddSources(this.Parameters);
126  RenderSystem.GlobalMeshes.AddMesh(skyboxMesh);
127  }
128  }
129 }
The layout of a vertex buffer with a set of VertexElement.
A mixin performing a combination of ShaderClassSource and other mixins.
Basic shader plugin built directly from shader source file.
SiliconStudio.Paradox.Graphics.Buffer Buffer
Definition: SkyBoxPlugin.cs:13
All-in-One Buffer class linked SharpDX.Direct3D11.Buffer.
Performs primitive-based rendering, creates resources, handles system-level variables, adjusts gamma ramp levels, and creates shaders. See The+GraphicsDevice+class to learn more about the class.
static Buffer New(GraphicsDevice device, BufferDescription description, PixelFormat viewFormat=PixelFormat.None)
Creates a new Buffer instance.
Definition: Buffer.cs:343
Represents a four dimensional mathematical vector.
Definition: Vector4.cs:42
Plugin used for the main rendering view.
Definition: MainPlugin.cs:15
static VertexElement Position(PixelFormat format, int offsetInBytes=AppendAligned)
Declares a VertexElement with the semantic "POSITION".
A Texture 2D frontend to SharpDX.Direct3D11.Texture2D.
Definition: Texture2D.cs:37
SkyBoxPlugin(string name, ShaderSource skyBoxComposition)
Definition: SkyBoxPlugin.cs:30
Level10 render pass using a depth buffer and a render target.
PrimitiveType
Defines how vertex data is ordered.
A description of a single element for the input-assembler stage. This structure is related to Direct3...
Base class for texture resources.
Definition: Texture.cs:38
Binding structure that specifies a vertex buffer and other per-vertex parameters (such as offset and ...