/// <summary> /// Custom Effects Manager for 3D Model /// </summary> public CustomEffectsManager() { var customMesh = new TechniqueDescription(CustomShaderNames.CustomShader) { InputLayoutDescription = new InputLayoutDescription(DefaultVSShaderByteCodes.VSMeshDefault, DefaultInputLayout.VSInput), PassDescriptions = new[] { new ShaderPassDescription(DefaultPassNames.Default) { ShaderList = new[] { DefaultVSShaderDescriptions.VSMeshDefault, CustomPSShaderDescription.PSCustomMesh }, BlendStateDescription = DefaultBlendStateDescriptions.BSAlphaBlend, DepthStencilStateDescription = DefaultDepthStencilDescriptions.DSSDepthLess }, new ShaderPassDescription(DefaultPassNames.OITPass) { ShaderList = new[] { DefaultVSShaderDescriptions.VSMeshDefault, DefaultPSShaderDescriptions.PSMeshBlinnPhongOIT }, BlendStateDescription = DefaultBlendStateDescriptions.BSOITBlend, DepthStencilStateDescription = DefaultDepthStencilDescriptions.DSSLessNoWrite }, } }; AddTechnique(customMesh); }
public static string FindTechniqueAnnotationString(Effect effect, EffectHandle handle, string name) { if (effect == null) { throw new ArgumentNullException(nameof(effect)); } if (handle == null) { throw new ArgumentNullException(nameof(handle)); } if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException(nameof(name)); } TechniqueDescription paramDesc = effect.GetTechniqueDescription(handle); for (int i = 0; i < paramDesc.Annotations; i++) { EffectHandle annotationHandle = effect.GetAnnotation(handle, i); if (annotationHandle != null) { ParameterDescription annotationDesc = effect.GetParameterDescription(annotationHandle); if (annotationDesc.Type == ParameterType.String && StringUtils.EqualsIgnoreCase(annotationDesc.Name, name)) { return(effect.GetString(annotationHandle)); } } } return(null); }
/// <summary> /// <see cref="IEffectsManager.AddTechnique(TechniqueDescription)"/> /// </summary> /// <param name="description"></param> public void AddTechnique(TechniqueDescription description) { if (techniqueDict.ContainsKey(description.Name)) { throw new ArgumentException($"Technique { description.Name } already exists."); } techniqueDict.Add(description.Name, new Lazy <IRenderTechnique>(() => { return(Initialized ? Collect(new Technique(description, Device, this)) : null); }, true)); }
internal ShaderTechnique(DeviceContext context, Shader shader, EffectHandle techniqueHandle) { TechniqueDescription desc = shader.Effect.GetTechniqueDescription(techniqueHandle); Handle = techniqueHandle; PassCount = desc.Passes; Name = desc.Name; Passes = new ShaderPassCollection(context, shader, this); }
/// <summary> /// Render the frame /// </summary> protected void RenderScene() { if (needsResizing) { needsResizing = false; renderTargetView.Dispose(); SwapChainDescription sd = swapChain.Description; swapChain.ResizeBuffers(sd.BufferCount, (uint)host.ActualWidth, (uint)host.ActualHeight, sd.BufferDescription.Format, sd.Options); SetViews(); // Update the projection matrix projectionMatrix = DXUtil.Camera.MatrixPerspectiveFovLH((float)Math.PI * 0.25f, ((float)host.ActualWidth / (float)host.ActualHeight), 0.5f, 100.0f); projectionVariable.Matrix = projectionMatrix; } Matrix4x4F worldMatrix; currentTime = (Environment.TickCount - startTime) / 1000.0f; //WPF transforms used here use degrees as opposed to D3DX which uses radians in the native tutorial //360 degrees == 2 * Math.PI //world matrix rotates the first cube by t degrees RotateTransform3D rotateTransform = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 1, 0), currentTime * 30)); worldMatrix = rotateTransform.Value.ToMatrix4x4F(); // Modify the color meshColor.X = ((float)Math.Sin(currentTime * 1.0f) + 1.0f) * 0.5f; meshColor.Y = ((float)Math.Cos(currentTime * 3.0f) + 1.0f) * 0.5f; meshColor.Z = ((float)Math.Sin(currentTime * 5.0f) + 1.0f) * 0.5f; // Clear the backbuffer device.ClearRenderTargetView(renderTargetView, backColor); // // Update variables that change once per frame // worldVariable.Matrix = worldMatrix; meshColorVariable.FloatVector = meshColor; // // Render the cube // TechniqueDescription techDesc = technique.Description; for (uint p = 0; p < techDesc.Passes; ++p) { technique.GetPassByIndex(p).Apply(); device.DrawIndexed(36, 0, 0); } swapChain.Present(0, PresentOptions.None); }
private void LoadCustomTechniqueDescriptions() { var dataSampling = new TechniqueDescription(CustomShaderNames.DataSampling) { InputLayoutDescription = new InputLayoutDescription(CustomVSShaderDescription.VSMeshDataSamplerByteCode, DefaultInputLayout.VSInput), PassDescriptions = new[] { new ShaderPassDescription(DefaultPassNames.ColorStripe1D) { ShaderList = new[] { CustomVSShaderDescription.VSDataSampling, //DefaultVSShaderDescriptions.VSMeshDefault, CustomPSShaderDescription.PSDataSampling }, BlendStateDescription = DefaultBlendStateDescriptions.BSAlphaBlend, DepthStencilStateDescription = DefaultDepthStencilDescriptions.DSSDepthLess }, new ShaderPassDescription(DefaultPassNames.Wireframe) { ShaderList = new[] { CustomVSShaderDescription.VSDataSampling, DefaultPSShaderDescriptions.PSMeshWireframe }, BlendStateDescription = DefaultBlendStateDescriptions.BSAlphaBlend, DepthStencilStateDescription = DefaultDepthStencilDescriptions.DSSDepthLess } } }; var noiseMesh = new TechniqueDescription(CustomShaderNames.NoiseMesh) { InputLayoutDescription = new InputLayoutDescription(DefaultVSShaderByteCodes.VSMeshDefault, DefaultInputLayout.VSInput), PassDescriptions = new[] { new ShaderPassDescription(DefaultPassNames.Default) { ShaderList = new[] { DefaultVSShaderDescriptions.VSMeshDefault, CustomPSShaderDescription.PSNoiseMesh }, BlendStateDescription = DefaultBlendStateDescriptions.BSAlphaBlend, DepthStencilStateDescription = DefaultDepthStencilDescriptions.DSSDepthLess } } }; AddTechnique(dataSampling); AddTechnique(noiseMesh); }
/// <summary> /// /// </summary> /// <param name="description"></param> /// <param name="device"></param> /// <param name="manager"></param> public Technique(TechniqueDescription description, Device device, IEffectsManager manager) { Name = description.Name; EffectsManager = manager; Layout = manager.ShaderManager.RegisterInputLayout(description.InputLayoutDescription); if (description.PassDescriptions != null) { foreach (var desc in description.PassDescriptions) { var pass = new Lazy <ShaderPass>(() => { return(Collect(new ShaderPass(desc, manager))); }, true); passDict.Add(desc.Name, pass); passList.Add(pass); } } }
private void RenderFlag(float t, double a, int shells) { TechniqueDescription techDesc = effects.Technique.Description; for (int x = -shells; x <= shells; x++) { for (int z = -shells; z <= shells; z++) { float height = ((float)Math.Sin(0.5 * (x + 4 * t)) + (float)Math.Cos(0.25 * (z + 2 * t))); Vector4F vBaseColor = new Vector4F(0.0f, 0.0f, 0.0f, 1.0f); if (x < 0 && z > 0) { vBaseColor.X = 0.75f + 0.125f * height; //red } else if (x > 0 && z > 0) { vBaseColor.Y = 0.75f + 0.125f * height; //green } else if (x < 0 && z < 0) { vBaseColor.Z = 0.75f + 0.125f * height; //blue } else if (x > 0 && z < 0) {//yellow vBaseColor.X = 0.75f + 0.125f * height; vBaseColor.Y = 0.75f + 0.125f * height; } else { continue; } effects.BaseColor = vBaseColor; float yScale = 5f + 0.5f * height; effects.WorldMatrix = MatrixMath.MatrixScale(0.35f, yScale, 0.35f) * MatrixMath.MatrixTranslate(x, yScale - 10, z); for (uint p = 0; p < techDesc.Passes; ++p) { effects.Technique.GetPassByIndex(p).Apply(); device.DrawIndexed(36, 0, 0); } } } }
public void DumpTechnique(EffectHandle tech, int index) { TechniqueDescription td = effect.GetTechniqueDescription(tech); Console.WriteLine("{0}\t{1}", index, td.Name); int nannotation = td.Annotations; if (nannotation > 0) { Console.WriteLine("\tAnnotations:"); for (int i = 0; i < nannotation; i++) { EffectHandle annotation = effect.GetAnnotation(tech, i); ParameterDescription ad = effect.GetParameterDescription(annotation); Console.WriteLine("\t{0,3} {1} {2}", i, ad.Type, ad.Name); } } }
/// <summary> /// Render the frame /// </summary> protected void RenderScene() { if (needsResizing) { needsResizing = false; renderTargetView.Dispose(); SwapChainDescription sd = swapChain.Description; swapChain.ResizeBuffers(sd.BufferCount, (uint)directControl.ClientSize.Width, (uint)directControl.ClientSize.Height, sd.BufferDescription.Format, sd.Options); SetViews(); // Update the projection matrix projectionMatrix = DXUtil.Camera.MatrixPerspectiveFovLH((float)Math.PI * 0.5f, ((float)directControl.ClientSize.Width / (float)directControl.ClientSize.Height), 0.1f, 100.0f); projectionVariable.Matrix = projectionMatrix; } t = (Environment.TickCount - dwTimeStart) / 50; // Clear the backbuffer device.ClearRenderTargetView(renderTargetView, backColor); // Rotate the cube RotateTransform3D rt = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 1, 0), t)); worldMatrix = rt.Value.ToMatrix4x4F(); // // Update variables that change once per frame // worldVariable.Matrix = worldMatrix; // // Render the cube // TechniqueDescription techDesc = technique.Description; for (uint p = 0; p < techDesc.Passes; ++p) { technique.GetPassByIndex(p).Apply(); device.DrawIndexed(36, 0, 0); } swapChain.Present(0, PresentOptions.None); }
/// <summary> /// Render the frame /// </summary> protected void RenderScene() { Matrix4x4F worldMatrix; currentTime = (Environment.TickCount - dwTimeStart) / 1000.0f; //WPF transforms used here use degrees as opposed to D3DX which uses radians in the native tutorial //360 degrees == 2 * Math.PI //world matrix rotates the first cube by t degrees RotateTransform3D rt1 = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 1, 0), currentTime * 30)); worldMatrix = rt1.Value.ToMatrix4x4F(); // Modify the color meshColor.X = ((float)Math.Sin(currentTime * 1.0f) + 1.0f) * 0.5f; meshColor.Y = ((float)Math.Cos(currentTime * 3.0f) + 1.0f) * 0.5f; meshColor.Z = ((float)Math.Sin(currentTime * 5.0f) + 1.0f) * 0.5f; // Clear the backbuffer device.ClearRenderTargetView(renderTargetView, backColor); // // Update variables that change once per frame // worldVariable.Matrix = worldMatrix; meshColorVariable.FloatVector = meshColor; // // Render the cube // TechniqueDescription techDesc = technique.Description; for (uint p = 0; p < techDesc.Passes; ++p) { technique.GetPassByIndex(p).Apply(); device.DrawIndexed(36, 0, 0); } swapChain.Present(0, PresentOptions.None); }
protected void AddDynamoTechniques() { var dynamoCustomMeshTech = new TechniqueDescription(DynamoMeshShaderName) { InputLayoutDescription = new InputLayoutDescription(DynamoMeshRenderVertexShaderDescription.VSMeshDataSamplerByteCode, DefaultInputLayout.VSInput), PassDescriptions = new[] { new ShaderPassDescription(DefaultPassNames.Default) { ShaderList = new ShaderDescription[] { DynamoMeshRenderVertexShaderDescription.VertexShaderDynamoMeshDescription, DynamoMeshRenderPixelShaderDescription.PixelShaderDynamoMeshDescription, }, BlendStateDescription = DefaultBlendStateDescriptions.BSAlphaBlend, DepthStencilStateDescription = DefaultDepthStencilDescriptions.DSSDepthLess }, } }; AddTechnique(dynamoCustomMeshTech); }
/// <summary> /// /// </summary> /// <param name="description"></param> /// <param name="device"></param> /// <param name="manager"></param> public Technique(TechniqueDescription description, Device device, IEffectsManager manager) { Description = description; Name = description.Name; EffectsManager = manager; if (description.InputLayoutDescription != null && description.PassDescriptions != null) { if (description.PassDescriptions != null) { foreach (var desc in description.PassDescriptions) { if (desc.InputLayoutDescription == null) { desc.InputLayoutDescription = description.InputLayoutDescription; } var pass = new Lazy<ShaderPass>(() => { return new ShaderPass(desc, manager); }, true); passDict.Add(desc.Name, pass); passList.Add(pass); } } } }
static ImGuiNode() { RenderTechnique = new TechniqueDescription(ImGuiRenderTechnique) { InputLayoutDescription = new InputLayoutDescription(DefaultVSShaderByteCodes.VSSprite2D, VSInputImGui2D), PassDescriptions = new[] { new ShaderPassDescription(DefaultPassNames.Default) { ShaderList = new[] { DefaultVSShaderDescriptions.VSSprite2D, DefaultPSShaderDescriptions.PSSprite2D, }, Topology = PrimitiveTopology.TriangleList, BlendStateDescription = DefaultBlendStateDescriptions.BSAlphaBlend, DepthStencilStateDescription = DefaultDepthStencilDescriptions.DSSNoDepthNoStencil, RasterStateDescription = DefaultRasterDescriptions.RSSpriteCW, } } }; }
/// <summary> /// Render the frame /// </summary> protected void RenderScene() { if (needsResizing) { needsResizing = false; renderTargetView.Dispose(); SwapChainDescription sd = swapChain.Description; swapChain.ResizeBuffers(sd.BufferCount, (uint)directControl.ClientSize.Width, (uint)directControl.ClientSize.Height, sd.BufferDescription.Format, sd.Options); SetViews(); } // Just clear the backbuffer device.ClearRenderTargetView(renderTargetView, backColor); TechniqueDescription techDesc = technique.Description; for (uint p = 0; p < techDesc.Passes; ++p) { technique.GetPassByIndex(p).Apply(); device.Draw(3, 0); } swapChain.Present(0, PresentOptions.None); }
/// <summary> /// Render the frame /// </summary> protected void RenderScene() { if (needsResizing) { needsResizing = false; renderTargetView.Dispose(); SwapChainDescription sd = swapChain.Description; swapChain.ResizeBuffers(sd.BufferCount, (uint)directControl.ClientSize.Width, (uint)directControl.ClientSize.Height, sd.BufferDescription.Format, sd.Options); SetViews(); // Update the projection matrix projectionMatrix = Microsoft.WindowsAPICodePack.DirectX.DirectXUtilities.Camera.MatrixPerspectiveFovLH((float)Math.PI * 0.25f, ((float)directControl.ClientSize.Width / (float)directControl.ClientSize.Height), 0.5f, 100.0f); projectionVariable.Matrix = projectionMatrix; } Matrix4x4F worldMatrix; t = (Environment.TickCount - dwTimeStart) / 50.0f; //WPF transforms used here use degrees as opposed to D3DX which uses radians in the native tutorial //360 degrees == 2 * Math.PI //world matrix rotates the first cube by t degrees RotateTransform3D rt1 = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 1, 0), t)); worldMatrix = rt1.Value.ToMatrix4x4F(); //Setup our lighting parameters Vector4F[] vLightDirs = { new Vector4F(-0.577f, 0.577f, -0.577f, 1.0f), new Vector4F(0.0f, 0.0f, -1.0f, 1.0f) }; Vector4F[] vLightColors = { new Vector4F(0.5f, 0.5f, 0.5f, 1.0f), new Vector4F(0.5f, 0.0f, 0.0f, 1.0f) }; //rotate the second light around the origin //create a rotation matrix RotateTransform3D rt2 = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 2, 0), -t)); //rotate vLightDirs[1] vector using the rotation matrix Vector3D vDir = new Vector3D(vLightDirs[1].X, vLightDirs[1].Y, vLightDirs[1].Z); vDir = rt2.Transform(vDir); vLightDirs[1].X = (float)vDir.X; vLightDirs[1].Y = (float)vDir.Y; vLightDirs[1].Z = (float)vDir.Z; // Clear the backbuffer device.ClearRenderTargetView(renderTargetView, backColor); // Clear the depth buffer to 1.0 (max depth) device.ClearDepthStencilView(depthStencilView, ClearOptions.Depth, 1.0f, (byte)0); // // Update variables that change once per frame // worldVariable.Matrix = worldMatrix; lightDirVariable.SetFloatVectorArray(vLightDirs); lightColorVariable.SetFloatVectorArray(vLightColors); // // Render the cube // TechniqueDescription techDesc = technique.Description; for (uint p = 0; p < techDesc.Passes; ++p) { technique.GetPassByIndex(p).Apply(); device.DrawIndexed(36, 0, 0); } // // Render each light // TechniqueDescription techLightDesc = techniqueLight.Description; for (int m = 0; m < 2; m++) { Vector3F vLightPos = new Vector3F(vLightDirs[m].X * 5, vLightDirs[m].Y * 5, vLightDirs[m].Z * 5); Transform3DGroup tg = new Transform3DGroup(); tg.Children.Add(new ScaleTransform3D(0.2, 0.2, 0.2)); tg.Children.Add(new TranslateTransform3D(vLightPos.X, vLightPos.Y, vLightPos.Z)); worldVariable.Matrix = tg.Value.ToMatrix4x4F(); outputColorVariable.FloatVector = new Vector4F(vLightColors[m].X, vLightColors[m].Y, vLightColors[m].Z, vLightColors[m].W); for (uint p = 0; p < techLightDesc.Passes; ++p) { techniqueLight.GetPassByIndex(p).Apply(); device.DrawIndexed(36, 0, 0); } } swapChain.Present(0, PresentOptions.None); }
/// <summary> /// Custom Effects Manager for 3D Model /// </summary> public CustomEffectsManager() { var customMesh = new TechniqueDescription(CustomShaderNames.CustomShader) { InputLayoutDescription = new InputLayoutDescription(DefaultVSShaderByteCodes.VSMeshDefault, DefaultInputLayout.VSInput), PassDescriptions = new[] { new ShaderPassDescription(DefaultPassNames.Default) { ShaderList = new[] { DefaultVSShaderDescriptions.VSMeshDefault, CustomPSShaderDescription.PSCustomMesh }, BlendStateDescription = DefaultBlendStateDescriptions.BSAlphaBlend, DepthStencilStateDescription = DefaultDepthStencilDescriptions.DSSDepthLess }, new ShaderPassDescription(DefaultPassNames.PBR) { ShaderList = new[] { DefaultVSShaderDescriptions.VSMeshDefault, DefaultPSShaderDescriptions.PSMeshPBR }, BlendStateDescription = DefaultBlendStateDescriptions.BSAlphaBlend, DepthStencilStateDescription = DefaultDepthStencilDescriptions.DSSDepthLessEqual }, new ShaderPassDescription(DefaultPassNames.Colors) { ShaderList = new[] { DefaultVSShaderDescriptions.VSMeshDefault, DefaultPSShaderDescriptions.PSMeshVertColor }, BlendStateDescription = DefaultBlendStateDescriptions.BSAlphaBlend, DepthStencilStateDescription = DefaultDepthStencilDescriptions.DSSDepthLessEqual }, new ShaderPassDescription(DefaultPassNames.Normals) { ShaderList = new[] { DefaultVSShaderDescriptions.VSMeshDefault, DefaultPSShaderDescriptions.PSMeshVertNormal }, BlendStateDescription = DefaultBlendStateDescriptions.BSAlphaBlend, DepthStencilStateDescription = DefaultDepthStencilDescriptions.DSSDepthLessEqual }, new ShaderPassDescription(DefaultPassNames.Positions) { ShaderList = new[] { DefaultVSShaderDescriptions.VSMeshDefault, DefaultPSShaderDescriptions.PSMeshVertPosition }, BlendStateDescription = DefaultBlendStateDescriptions.BSSourceAlways, DepthStencilStateDescription = DefaultDepthStencilDescriptions.DSSDepthLess }, new ShaderPassDescription(DefaultPassNames.Diffuse) { ShaderList = new[] { DefaultVSShaderDescriptions.VSMeshDefault, DefaultPSShaderDescriptions.PSMeshDiffuseMap }, BlendStateDescription = DefaultBlendStateDescriptions.BSAlphaBlend, DepthStencilStateDescription = DefaultDepthStencilDescriptions.DSSDepthLessEqual }, new ShaderPassDescription(DefaultPassNames.ColorStripe1D) { ShaderList = new[] { DefaultVSShaderDescriptions.VSMeshDefault, DefaultPSShaderDescriptions.PSMeshColorStripe }, BlendStateDescription = DefaultBlendStateDescriptions.BSAlphaBlend, DepthStencilStateDescription = DefaultDepthStencilDescriptions.DSSDepthLessEqual }, new ShaderPassDescription(DefaultPassNames.ViewCube) { ShaderList = new[] { DefaultVSShaderDescriptions.VSMeshDefault, DefaultPSShaderDescriptions.PSMeshViewCube }, BlendStateDescription = DefaultBlendStateDescriptions.BSSourceAlways, DepthStencilStateDescription = DefaultDepthStencilDescriptions.DSSDepthLess }, new ShaderPassDescription(DefaultPassNames.NormalVector) { ShaderList = new[] { DefaultVSShaderDescriptions.VSMeshDefault, DefaultGSShaderDescriptions.GSMeshNormalVector, DefaultPSShaderDescriptions.PSLineColor }, BlendStateDescription = DefaultBlendStateDescriptions.BSSourceAlways, DepthStencilStateDescription = DefaultDepthStencilDescriptions.DSSDepthLess, Topology = PrimitiveTopology.PointList }, new ShaderPassDescription(DefaultPassNames.OITPass) { ShaderList = new[] { DefaultVSShaderDescriptions.VSMeshDefault, DefaultPSShaderDescriptions.PSMeshBlinnPhongOIT }, BlendStateDescription = DefaultBlendStateDescriptions.BSOITBlend, DepthStencilStateDescription = DefaultDepthStencilDescriptions.DSSLessNoWrite }, new ShaderPassDescription(DefaultPassNames.PBROITPass) { ShaderList = new[] { DefaultVSShaderDescriptions.VSMeshDefault, DefaultPSShaderDescriptions.PSMeshPBROIT }, BlendStateDescription = DefaultBlendStateDescriptions.BSOITBlend, DepthStencilStateDescription = DefaultDepthStencilDescriptions.DSSLessNoWrite }, new ShaderPassDescription(DefaultPassNames.DiffuseOIT) { ShaderList = new[] { DefaultVSShaderDescriptions.VSMeshDefault, DefaultPSShaderDescriptions.PSMeshDiffuseMapOIT }, BlendStateDescription = DefaultBlendStateDescriptions.BSOITBlend, DepthStencilStateDescription = DefaultDepthStencilDescriptions.DSSLessNoWrite }, new ShaderPassDescription(DefaultPassNames.PreComputeMeshBoneSkinned) { ShaderList = new[] { DefaultVSShaderDescriptions.VSMeshBoneSkinnedBasic, DefaultGSShaderDescriptions.GSMeshBoneSkinnedOut }, BlendStateDescription = DefaultBlendStateDescriptions.NoBlend, DepthStencilStateDescription = DefaultDepthStencilDescriptions.DSSNoDepthNoStencil, Topology = PrimitiveTopology.PointList, InputLayoutDescription = new InputLayoutDescription(DefaultVSShaderByteCodes.VSMeshBoneSkinningBasic, DefaultInputLayout.VSInputBoneSkinnedBasic), }, new ShaderPassDescription(DefaultPassNames.DepthPrepass) { ShaderList = new[] { DefaultVSShaderDescriptions.VSMeshDepth, DefaultPSShaderDescriptions.PSDepthStencilOnly }, BlendStateDescription = DefaultBlendStateDescriptions.NoBlend, DepthStencilStateDescription = DefaultDepthStencilDescriptions.DSSDepthLess }, new ShaderPassDescription(DefaultPassNames.MeshSSAOPass) { ShaderList = new[] { DefaultVSShaderDescriptions.VSMeshSSAO, DefaultPSShaderDescriptions.PSSSAOP1 }, BlendStateDescription = DefaultBlendStateDescriptions.BSSourceAlways, DepthStencilStateDescription = DefaultDepthStencilDescriptions.DSSDepthLessEqual }, new ShaderPassDescription(DefaultPassNames.MeshTriTessellation) { ShaderList = new[] { DefaultVSShaderDescriptions.VSMeshTessellation, DefaultHullShaderDescriptions.HSMeshTessellation, DefaultDomainShaderDescriptions.DSMeshTessellation, DefaultPSShaderDescriptions.PSMeshBlinnPhong }, BlendStateDescription = DefaultBlendStateDescriptions.BSAlphaBlend, DepthStencilStateDescription = DefaultDepthStencilDescriptions.DSSDepthLessEqual, Topology = PrimitiveTopology.PatchListWith3ControlPoints }, new ShaderPassDescription(DefaultPassNames.MeshTriTessellationOIT) { ShaderList = new[] { DefaultVSShaderDescriptions.VSMeshTessellation, DefaultHullShaderDescriptions.HSMeshTessellation, DefaultDomainShaderDescriptions.DSMeshTessellation, DefaultPSShaderDescriptions.PSMeshBlinnPhongOIT }, BlendStateDescription = DefaultBlendStateDescriptions.BSOITBlend, DepthStencilStateDescription = DefaultDepthStencilDescriptions.DSSLessNoWrite, Topology = PrimitiveTopology.PatchListWith3ControlPoints }, new ShaderPassDescription(DefaultPassNames.MeshPBRTriTessellation) { ShaderList = new[] { DefaultVSShaderDescriptions.VSMeshTessellation, DefaultHullShaderDescriptions.HSMeshTessellation, DefaultDomainShaderDescriptions.DSMeshTessellation, DefaultPSShaderDescriptions.PSMeshPBR }, BlendStateDescription = DefaultBlendStateDescriptions.BSAlphaBlend, DepthStencilStateDescription = DefaultDepthStencilDescriptions.DSSDepthLessEqual, Topology = PrimitiveTopology.PatchListWith3ControlPoints }, new ShaderPassDescription(DefaultPassNames.MeshPBRTriTessellationOIT) { ShaderList = new[] { DefaultVSShaderDescriptions.VSMeshTessellation, DefaultHullShaderDescriptions.HSMeshTessellation, DefaultDomainShaderDescriptions.DSMeshTessellation, DefaultPSShaderDescriptions.PSMeshPBROIT }, BlendStateDescription = DefaultBlendStateDescriptions.BSOITBlend, DepthStencilStateDescription = DefaultDepthStencilDescriptions.DSSLessNoWrite, Topology = PrimitiveTopology.PatchListWith3ControlPoints }, new ShaderPassDescription(DefaultPassNames.MeshOutline) { ShaderList = new[] { DefaultVSShaderDescriptions.VSMeshDefault, DefaultPSShaderDescriptions.PSMeshXRay }, BlendStateDescription = DefaultBlendStateDescriptions.BSOverlayBlending, DepthStencilStateDescription = DefaultDepthStencilDescriptions.DSSLessEqualNoWrite }, new ShaderPassDescription(DefaultPassNames.ShadowPass) { ShaderList = new[] { DefaultVSShaderDescriptions.VSMeshShadow, DefaultPSShaderDescriptions.PSShadow }, BlendStateDescription = DefaultBlendStateDescriptions.NoBlend, DepthStencilStateDescription = DefaultDepthStencilDescriptions.DSSDepthLess }, new ShaderPassDescription(DefaultPassNames.Wireframe) { ShaderList = new[] { DefaultVSShaderDescriptions.VSMeshWireframe, DefaultPSShaderDescriptions.PSMeshWireframe }, BlendStateDescription = DefaultBlendStateDescriptions.BSSourceAlways, DepthStencilStateDescription = DefaultDepthStencilDescriptions.DSSLessEqualNoWrite, Topology = PrimitiveTopology.TriangleList }, new ShaderPassDescription(DefaultPassNames.WireframeOITPass) { ShaderList = new[] { DefaultVSShaderDescriptions.VSMeshWireframe, DefaultPSShaderDescriptions.PSMeshWireframeOIT }, BlendStateDescription = DefaultBlendStateDescriptions.BSOITBlend, DepthStencilStateDescription = DefaultDepthStencilDescriptions.DSSLessEqualNoWrite, Topology = PrimitiveTopology.TriangleList }, new ShaderPassDescription(DefaultPassNames.EffectOutlineP1) { ShaderList = new[] { DefaultVSShaderDescriptions.VSMeshWireframe, DefaultPSShaderDescriptions.PSMeshOutlineQuadStencil }, BlendStateDescription = DefaultBlendStateDescriptions.BSSourceAlways, DepthStencilStateDescription = DefaultDepthStencilDescriptions.DSSMeshOutlineP1, StencilRef = 1 }, new ShaderPassDescription(DefaultPassNames.EffectMeshXRayP1) { ShaderList = new[] { DefaultVSShaderDescriptions.VSMeshWireframe, DefaultPSShaderDescriptions.PSDepthStencilOnly }, BlendStateDescription = DefaultBlendStateDescriptions.NoBlend, DepthStencilStateDescription = DefaultDepthStencilDescriptions.DSSEffectMeshXRayP1, }, new ShaderPassDescription(DefaultPassNames.EffectMeshXRayP2) { ShaderList = new[] { DefaultVSShaderDescriptions.VSMeshDefault, DefaultPSShaderDescriptions.PSEffectMeshXRay }, BlendStateDescription = DefaultBlendStateDescriptions.BSOverlayBlending, DepthStencilStateDescription = DefaultDepthStencilDescriptions.DSSEffectMeshXRayP2, StencilRef = 1 }, new ShaderPassDescription(DefaultPassNames.EffectMeshXRayGridP1) { ShaderList = new[] { DefaultVSShaderDescriptions.VSMeshWireframe, DefaultPSShaderDescriptions.PSDepthStencilOnly }, BlendStateDescription = DefaultBlendStateDescriptions.NoBlend, DepthStencilStateDescription = DefaultDepthStencilDescriptions.DSSEffectMeshXRayGridP1, StencilRef = 1 }, new ShaderPassDescription(DefaultPassNames.EffectMeshXRayGridP2) { ShaderList = new[] { DefaultVSShaderDescriptions.VSMeshWireframe, DefaultPSShaderDescriptions.PSDepthStencilOnly }, BlendStateDescription = DefaultBlendStateDescriptions.NoBlend, DepthStencilStateDescription = DefaultDepthStencilDescriptions.DSSEffectMeshXRayGridP2, StencilRef = 1 }, new ShaderPassDescription(DefaultPassNames.EffectMeshXRayGridP3) { ShaderList = new[] { DefaultVSShaderDescriptions.VSMeshDefault, DefaultPSShaderDescriptions.PSEffectXRayGrid }, BlendStateDescription = DefaultBlendStateDescriptions.BSAlphaBlend, DepthStencilStateDescription = DefaultDepthStencilDescriptions.DSSEffectMeshXRayGridP3, StencilRef = 1 }, new ShaderPassDescription(DefaultPassNames.EffectMeshDiffuseXRayGridP3) { ShaderList = new[] { DefaultVSShaderDescriptions.VSMeshDefault, DefaultPSShaderDescriptions.PSEffectDiffuseXRayGrid }, BlendStateDescription = DefaultBlendStateDescriptions.BSAlphaBlend, DepthStencilStateDescription = DefaultDepthStencilDescriptions.DSSEffectMeshXRayGridP3, StencilRef = 1 }, } }; AddTechnique(customMesh); }
/// <summary> /// Render the frame /// </summary> protected void RenderScene() { if (needsResizing) { needsResizing = false; renderTargetView.Dispose(); SwapChainDescription sd = swapChain.Description; swapChain.ResizeBuffers(sd.BufferCount, (uint)directControl.ClientSize.Width, (uint)directControl.ClientSize.Height, sd.BufferDescription.Format, sd.Options); SetViews(); // Update the projection matrix projectionMatrix = DXUtil.Camera.MatrixPerspectiveFovLH((float)Math.PI * 0.25f, ((float)directControl.ClientSize.Width / (float)directControl.ClientSize.Height), 0.1f, 100.0f); projectionVariable.Matrix = projectionMatrix; } Matrix4x4F worldMatrix1; Matrix4x4F worldMatrix2; t = (Environment.TickCount - dwTimeStart) / 50.0f; // 1st Cube: Rotate around the origin //WPF transforms used here use degrees as opposed to D3DX which uses radians in the native tutorial //360 degrees == 2 * Math.PI //world1 matrix rotates the first cube by t degrees RotateTransform3D rt1 = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 1, 0), t)); worldMatrix1 = rt1.Value.ToMatrix4x4F(); // 2nd Cube: Rotate around the 1st cube Transform3DGroup tg = new Transform3DGroup(); //spin the cube tg.Children.Add(new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 0, 1), -t))); //scale it down tg.Children.Add(new ScaleTransform3D(0.3, 0.3, 0.3)); //translate it (move to orbit) tg.Children.Add(new TranslateTransform3D(-4, 0, 0)); //orbit around the big cube tg.Children.Add(new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 1, 0), -2 * t))); worldMatrix2 = tg.Value.ToMatrix4x4F(); // Clear the backbuffer device.ClearRenderTargetView(renderTargetView, backColor); // Clear the depth buffer to 1.0 (max depth) device.ClearDepthStencilView(depthStencilView, ClearOptions.Depth, 1.0f, (byte)0); TechniqueDescription techDesc = technique.Description; // // Update variables that change once per frame // worldVariable.Matrix = worldMatrix1; // // Render the 1st cube // for (uint p = 0; p < techDesc.Passes; ++p) { technique.GetPassByIndex(p).Apply(); device.DrawIndexed(36, 0, 0); } // // Render the 2nd cube // worldVariable.Matrix = worldMatrix2; for (uint p = 0; p < techDesc.Passes; ++p) { technique.GetPassByIndex(p).Apply(); device.DrawIndexed(36, 0, 0); } swapChain.Present(0, PresentOptions.None); }
private void RenderPart(Part part, Matrix3D parentMatrix, ShaderResourceView parentTextureOverride) { // set part transform Transform3DGroup partGroup = new Transform3DGroup(); partGroup.Children.Add(new MatrixTransform3D(PartAnimation(part.name))); partGroup.Children.Add(new MatrixTransform3D(part.partTransform.ToMatrix3D())); partGroup.Children.Add(new MatrixTransform3D(parentMatrix)); parentMatrix = partGroup.Value; ShaderResourceView textureOverride = UpdateRasterizerStateForPart(part); if (textureOverride == null) { textureOverride = parentTextureOverride; } else { parentTextureOverride = textureOverride; } if (part.vertexBuffer != null) { EffectTechnique technique; if (textureOverride != null) { technique = this.manager.techniqueRenderTexture; this.manager.diffuseVariable.Resource = textureOverride; } else if (part.material == null) { technique = this.manager.techniqueRenderVertexColor; } else { if (part.material.textureResource != null) { technique = this.manager.techniqueRenderTexture; this.manager.diffuseVariable.Resource = part.material.textureResource; } else { technique = this.manager.techniqueRenderMaterialColor; this.manager.materialColorVariable.FloatVector = part.material.materialColor; } } this.manager.worldVariable.Matrix = parentMatrix.ToMatrix4x4F(); //set up vertex buffer and index buffer uint stride = (uint)Marshal.SizeOf(typeof(XMeshVertex)); uint offset = 0; this.manager.device.IA.SetVertexBuffers(0, new D3DBuffer[] { part.vertexBuffer }, new uint[] { stride }, new uint[] { offset }); //Set primitive topology this.manager.device.IA.PrimitiveTopology = PrimitiveTopology.TriangleList; TechniqueDescription techDesc = technique.Description; for (uint p = 0; p < techDesc.Passes; ++p) { technique.GetPassByIndex(p).Apply(); PassDescription passDescription = technique.GetPassByIndex(p).Description; using (InputLayout inputLayout = this.manager.device.CreateInputLayout( part.dataDescription, passDescription.InputAssemblerInputSignature, passDescription.InputAssemblerInputSignatureSize)) { // set vertex layout this.manager.device.IA.InputLayout = inputLayout; // draw part this.manager.device.Draw((uint)part.vertexCount, 0); // Note: In Direct3D 10, the device will not retain a reference // to the input layout, so it's important to reset the device's // input layout before disposing the object. Were this code // using Direct3D 11, the device would in fact retain a reference // and so it would be safe to go ahead and dispose the input // layout without resetting it; in that case, there could be just // a single assignment to null outside the 'for' loop, or even // no assignment at all. this.manager.device.IA.InputLayout = null; } } } foreach (Part childPart in part.parts) { RenderPart(childPart, parentMatrix, parentTextureOverride); } }
public bool InitializeApplication(TMOForm form) { PresentParameters pp = new PresentParameters(); try { // Set up the structure used to create the D3DDevice. Since we are now pp.Windowed = true; pp.SwapEffect = SwapEffect.Discard; pp.BackBufferFormat = Format.X8R8G8B8; pp.BackBufferCount = 1; pp.EnableAutoDepthStencil = true; pp.AutoDepthStencilFormat = DepthFormat.D16; int ret, quality; if (Manager.CheckDeviceMultiSampleType((int)Manager.Adapters.Default.Adapter, DeviceType.Hardware, pp.BackBufferFormat, pp.Windowed, MultiSampleType.FourSamples, out ret, out quality)) { pp.MultiSample = MultiSampleType.FourSamples; pp.MultiSampleQuality = quality - 1; } // Create the D3DDevice device = new Device(0, DeviceType.Hardware, form.Handle, CreateFlags.HardwareVertexProcessing, pp); } catch (DirectXException ex) { Console.WriteLine("Error: " + ex); return(false); } string effect_file = @"toonshader.cgfx"; if (!File.Exists(effect_file)) { Console.WriteLine("File not found: " + effect_file); return(false); } string compile_error; effect = Effect.FromFile(device, effect_file, null, ShaderFlags.None, null, out compile_error); if (compile_error != null) { Console.WriteLine(compile_error); return(false); } Console.WriteLine("Parameters:"); int nparam = effect.Description.Parameters; for (int i = 0; i < nparam; i++) { EffectHandle param = effect.GetParameter(null, i); DumpParameter(param, i); } Console.WriteLine("Techniques:"); int ntech = effect.Description.Techniques; for (int i = 0; i < ntech; i++) { EffectHandle tech = effect.GetTechnique(i); DumpTechnique(tech, i); } Console.WriteLine("Valid techniques:"); { EffectHandle tech = null; while ((tech = effect.FindNextValidTechnique(tech)) != null) { TechniqueDescription td = effect.GetTechniqueDescription(tech); Console.WriteLine("\t{0}", td.Name); } } return(true); }
private bool LoadScript() { // Remove previous data Techniques.Clear(); Passes.Clear(); #region Global script for (int i = 0; i < Effect.Description.Parameters; i++) { EffectHandle handleParam = Effect.GetParameter(null, i); ParameterDescription desc = Effect.GetParameterDescription(handleParam); if (desc.Type == ParameterType.Float && StringUtils.EqualsIgnoreCase(desc.Semantic, "STANDARDSGLOBAL")) { Log.Info("Loading SAS script"); string script = SasHelper.FindAnnotationString(Effect, handleParam, "script"); GlobalScript = ParseScript(script); ScriptType = ScriptEffectType.Object; string scriptClass = SasHelper.FindAnnotationString(Effect, handleParam, "ScriptClass"); if (scriptClass != null) { if (StringUtils.EqualsIgnoreCase(scriptClass, "scene")) { ScriptType = ScriptEffectType.Scene; } } // Stop after the first global script was found break; } } #endregion if (GlobalScript == null || GlobalScript.Count == 0) { return(false); } #region Technique & pass scripts for (int technique = 0; technique < Effect.Description.Techniques; technique++) { EffectHandle handleTech = Effect.GetTechnique(technique); string script = SasHelper.FindTechniqueAnnotationString(Effect, handleTech, "script"); var list = ParseScript(script); if (list == null || list.Count == 0) { Log.Warn("Failed to load SAS script: Couldn't find technique script"); return(false); } Techniques.Add(handleTech, list); TechniqueDescription techDesc = Effect.GetTechniqueDescription(handleTech); for (int pass = 0; pass < techDesc.Passes; pass++) { EffectHandle handlePass = Effect.GetPass(handleTech, pass); string passScript = SasHelper.FindPassAnnotationString(Effect, handlePass, "script"); var passList = ParseScript(passScript); if (passList == null || passList.Count == 0) { Log.Warn("Failed to load SAS script: Couldn't find pass script"); return(false); } Passes.Add(handlePass, passList); } } #endregion return(true); }