我是SharpDX的新手。我需要能够控制模型中人脸的颜色和阴影;阴影应随面相对于光源位置的方向而变化。我真的不知道从哪里开始表格。 我已经将SharpDX Minicube示例转换为VB.Net,因为这是我的工作方式,尽管C#中的任何答复都将不胜感激。在此示例中,每个面具有不同的颜色,并且不应用任何阴影。
在下面的代码中,Form1是标准的Windows窗体(使用VS 2013)。多维数据集显示在Form1的显示区域中。调整大小并双击均可。
如何为所有面孔施加相同的颜色?然后如何获得阴影?
任何人都可以提供线索解决该问题吗?
Imports Device = SharpDX.Direct3D11.Device
Imports Buffer = SharpDX.Direct3D11.Buffer
Imports SharpDX.Windows
Imports SharpDX.DXGI
Imports SharpDX.Direct3D11
Imports SharpDX.Direct3D
Imports SharpDX.D3DCompiler
Imports SharpDX
Imports System.Windows.Forms
Imports System.Diagnostics
Imports System
Public Class Form1
' Declare texture for rendering
Dim userResized As Boolean = True
Dim backBuffer As Texture2D = Nothing
Dim renderView As RenderTargetView = Nothing
Dim depthBuffer As Texture2D = Nothing
Dim depthView As DepthStencilView = Nothing
Dim angle As Single = Nothing
Dim iMatrix As Matrix = Nothing
Dim iData As Matrix = Nothing
Dim desc As SwapChainDescription = Nothing
'Device and SwapChain
Dim iDevice As SharpDX.Direct3D11.Device = Nothing
Dim iSwapChain As SwapChain = Nothing
Dim iDevContext As DeviceContext = Nothing
'Ignore all windows events
Dim iFactory As Factory = Nothing
' Vertex and Pixel shaders
Dim ShaderByte As CompilationResult = Nothing
Dim VertShader As VertexShader = Nothing
Dim ShaderByte2 As CompilationResult = Nothing
Dim PixShader As PixelShader = Nothing
Dim inputSignature As ShaderSignature = Nothing
' Layout from VertexShader input signature
Dim iElements As InputElement() = Nothing
Dim iLayout As InputLayout = Nothing
' Vertex buffer
Dim vectorArray As Vector4() = Nothing
' Create Constant Buffer
Dim iBuffer As SharpDX.Direct3D11.Buffer = Nothing
Dim contantBuffer As SharpDX.Direct3D11.Buffer = Nothing
' Viewing / projection matrices
Dim iView As Matrix = Nothing
Dim iProj As Matrix = Nothing
' Use clock
Dim iClock As New Stopwatch
' ==============================================
Private Sub Form1_Load( sender As Object, e As EventArgs) Handles MyBase.Load
Me.Text = "SharpDX - MiniCube Direct3D11 Sample"
'Dim Form1 As RenderForm = New RenderForm("SharpDX - MiniCube Direct3D11 Sample")
'SwapChain description
desc = New SwapChainDescription With { _
.BufferCount = 1, _
.ModeDescription = New ModeDescription(Me.ClientSize.Width, Me.ClientSize.Height, New Rational(60, 1), Format.R8G8B8A8_UNorm), _
.IsWindowed = 1, _
.OutputHandle = Me.Handle, _
.SampleDescription = New SampleDescription(1, 0), _
.SwapEffect = SwapEffect.Discard, _
.Usage = Usage.RenderTargetOutput _
}
'Create Device and SwapChain
SharpDX.Direct3D11.Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, desc, _
iDevice, iSwapChain)
iDevContext = idevice.ImmediateContext
'Ignore all windows events
iFactory = iSwapChain.GetParent(Of Factory)
iFactory.MakeWindowAssociation(Me.Handle, WindowAssociationFlags.IgnoreAll)
' Compile Vertex shader
'
ShaderByte = ShaderBytecode.CompileFromFile("MiniCube.fx", "VS", "vs_4_0", ShaderFlags.None, EffectFlags.None, Nothing, Nothing)
VertShader = New VertexShader(iDevice, CType(ShaderByte, Byte()), Nothing)
' Compile Pixel shader
'
ShaderByte2 = ShaderBytecode.CompileFromFile("MiniCube.fx", "PS", "ps_4_0", ShaderFlags.None, EffectFlags.None, Nothing, Nothing)
PixShader = New PixelShader(iDevice, CType(ShaderByte2, Byte()), Nothing)
inputSignature = ShaderSignature.GetInputSignature(CType(ShaderByte, Byte()))
' Layout from VertexShader input signature
iElements = New InputElement() _
{ New InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0), _
New InputElement("COLOR", 0, Format.R32G32B32A32_Float, &H10, 0) }
iLayout = New InputLayout(iDevice, CType(inputSignature, Byte()), iElements)
' Instantiate Vertex buffer from vertex data
' Vector4(X, Y, Z, ScalingFactor)
'
vectorArray = New Vector4() { New Vector4(-1!, -1!, -1!, 1!), New Vector4(1!, 0!, 0!, 1!), _
New Vector4(-1!, 1!, -1!, 1!), New Vector4(1!, 0!, 0!, 1!), _
New Vector4( 1!, 1!, -1!, 1!), New Vector4(1!, 0!, 0!, 1!), _
New Vector4(-1!, -1!, -1!, 1!), New Vector4(1!, 0!, 0!, 1!), _
New Vector4( 1!, 1!, -1!, 1!), New Vector4(1!, 0!, 0!, 1!), _
New Vector4( 1!, -1!, -1!, 1!), New Vector4(1!, 0!, 0!, 1!), _
New Vector4(-1!, -1!, 1!, 1!), New Vector4(0!, 1!, 0!, 1!), _
New Vector4( 1!, 1!, 1!, 1!), New Vector4(0!, 1!, 0!, 1!), _
New Vector4(-1!, 1!, 1!, 1!), New Vector4(0!, 1!, 0!, 1!), _
New Vector4(-1!, -1!, 1!, 1!), New Vector4(0!, 1!, 0!, 1!), _
New Vector4( 1!, -1!, 1!, 1!), New Vector4(0!, 1!, 0!, 1!), _
New Vector4( 1!, 1!, 1!, 1!), New Vector4(0!, 1!, 0!, 1!), _
New Vector4(-1!, 1!, -1!, 1!), New Vector4(0!, 0!, 1!, 1!), _
New Vector4(-1!, 1!, 1!, 1!), New Vector4(0!, 0!, 1!, 1!), _
New Vector4( 1!, 1!, 1!, 1!), New Vector4(0!, 0!, 1!, 1!), _
New Vector4(-1!, 1!, -1!, 1!), New Vector4(0!, 0!, 1!, 1!), _
New Vector4( 1!, 1!, 1!, 1!), New Vector4(0!, 0!, 1!, 1!), _
New Vector4( 1!, 1!, -1!, 1!), New Vector4(0!, 0!, 1!, 1!), _
New Vector4(-1!, -1!, -1!, 1!), New Vector4(1!, 1!, 0!, 1!), _
New Vector4( 1!, -1!, 1!, 1!), New Vector4(1!, 1!, 0!, 1!), _
New Vector4(-1!, -1!, 1!, 1!), New Vector4(1!, 1!, 0!, 1!), _
New Vector4(-1!, -1!, -1!, 1!), New Vector4(1!, 1!, 0!, 1!), _
New Vector4( 1!, -1!, -1!, 1!), New Vector4(1!, 1!, 0!, 1!), _
New Vector4( 1!, -1!, 1!, 1!), New Vector4(1!, 1!, 0!, 1!), _
New Vector4(-1!, -1!, -1!, 1!), New Vector4(1!, 0!, 1!, 1!), _
New Vector4(-1!, -1!, 1!, 1!), New Vector4(1!, 0!, 1!, 1!), _
New Vector4(-1!, 1!, 1!, 1!), New Vector4(1!, 0!, 1!, 1!), _
New Vector4(-1!, -1!, -1!, 1!), New Vector4(1!, 0!, 1!, 1!), _
New Vector4(-1!, 1!, 1!, 1!), New Vector4(1!, 0!, 1!, 1!), _
New Vector4(-1!, 1!, -1!, 1!), New Vector4(1!, 0!, 1!, 1!), _
New Vector4( 1!, -1!, -1!, 1!), New Vector4(0!, 1!, 1!, 1!), _
New Vector4( 1!, 1!, 1!, 1!), New Vector4(0!, 1!, 1!, 1!), _
New Vector4( 1!, -1!, 1!, 1!), New Vector4(0!, 1!, 1!, 1!), _
New Vector4( 1!, -1!, -1!, 1!), New Vector4(0!, 1!, 1!, 1!), _
New Vector4( 1!, 1!, -1!, 1!), New Vector4(0!, 1!, 1!, 1!), _
New Vector4( 1!, 1!, 1!, 1!), New Vector4(0!, 1!, 1!, 1!) }
' Create Constant Buffer
'
iBuffer = SharpDX.Direct3D11.Buffer.Create(Of Vector4) _
(iDevice, BindFlags.VertexBuffer, vectorArray, 0, ResourceUsage.Default, _
CpuAccessFlags.None, ResourceOptionFlags.None, 0)
contantBuffer = New SharpDX.Direct3D11.Buffer(iDevice, Utilities.SizeOf(Of Matrix), ResourceUsage.Default, _
BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0)
' Prepare All the stages
'
iDevContext.InputAssembler.InputLayout = iLayout
iDevContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList
iDevContext.InputAssembler.SetVertexBuffers(0, New VertexBufferBinding(iBuffer, (Utilities.SizeOf(Of Vector4) * 2), 0))
iDevContext.VertexShader.SetConstantBuffer(0, contantBuffer)
iDevContext.VertexShader.Set(VertShader)
iDevContext.PixelShader.Set(PixShader)
' Prepare matrices
'
' Viewing position (Eye, Target, UpDirection)
'
iView = SharpDX.Matrix.LookAtLH(New Vector3(0!, 0!, -5!), New Vector3(0!, 0!, 0!), Vector3.UnitY)
iProj = SharpDX.Matrix.Identity
' Use clock
'
iClock.Start
' Form is resized initially
'
Call CreateViews()
' Main loop
'
RenderLoop.Run(Me, Function()
' Viewing angle based on time elapsed
'
angle = (CSng(iClock.ElapsedMilliseconds) / 1000!)
' View: Eye -> Target
' Proj: Identity matrix
'
iMatrix = Matrix.Multiply(iView, iProj)
' Clear the views
iDevContext.ClearDepthStencilView(depthView, DepthStencilClearFlags.Depth, 1!, 0)
' Background color
iDevContext.ClearRenderTargetView(renderView, CType(Color.White, RawColor4))
' Update WorldViewProj Matrix
iData = (((Matrix.RotationX(angle ) * _
Matrix.RotationY(angle * 2! )) * _
Matrix.RotationZ(angle * 0.7!)) * iMatrix)
iData.Transpose()
iDevContext.UpdateSubresource(Of Matrix)(iData, contantBuffer, 0, 0, 0, Nothing)
iDevContext.Draw(&H24, 0)
' Show the display
iSwapChain.Present(0, PresentFlags.None)
End Function)
End Sub
Private Sub Form1_FormClosing( sender As Object, _
e As FormClosingEventArgs) Handles MyBase.FormClosing
' Release all resources
'
inputSignature.Dispose()
ShaderByte.Dispose()
VertShader.Dispose()
ShaderByte2.Dispose()
PixShader.Dispose()
iBuffer.Dispose()
iLayout.Dispose()
contantBuffer.Dispose()
depthBuffer.Dispose()
depthView.Dispose()
renderView.Dispose()
backBuffer.Dispose()
iDevContext.ClearState()
iDevContext.Flush()
iDevice.Dispose()
iDevContext.Dispose()
iSwapChain.Dispose()
iFactory.Dispose()
End Sub
Private Sub Form1_ResizeEnd( sender As Object, _
e As EventArgs) Handles MyBase.ResizeEnd
Call CreateViews()
End Sub
Sub CreateViews()
' Dispose all previously allocated resources
'
Utilities.Dispose(Of Texture2D)(backBuffer)
Utilities.Dispose(Of RenderTargetView)(renderView)
Utilities.Dispose(Of Texture2D)(depthBuffer)
Utilities.Dispose(Of DepthStencilView)(depthView)
' Resize the back buffers
'
iSwapChain.ResizeBuffers(desc.BufferCount, Me.ClientSize.Width, Me.ClientSize.Height, Format.Unknown, SwapChainFlags.None)
' Get the Backbuffer from the Swapchain
backBuffer = SharpDX.Direct3D11.Resource.FromSwapChain(Of Texture2D)(iSwapChain, 0)
' Render the view on the BackBuffer
'
renderView = New RenderTargetView(iDevice, backBuffer)
' Depth buffer
'
Dim description As New Texture2DDescription With { _
.Format = Format.D32_Float_S8X24_UInt, _
.ArraySize = 1, _
.MipLevels = 1, _
.Width = Me.ClientSize.Width, _
.Height = Me.ClientSize.Height, _
.SampleDescription = New SampleDescription(1, 0), _
.Usage = ResourceUsage.Default, _
.BindFlags = BindFlags.DepthStencil, _
.CpuAccessFlags = CpuAccessFlags.None, _
.OptionFlags = ResourceOptionFlags.None _
}
depthBuffer = New Texture2D(iDevice, description)
' Create the Depth buffer view
depthView = New DepthStencilView(iDevice, depthBuffer)
' Setup targets and viewport for rendering
iDevContext.Rasterizer.SetViewport(CType(New Viewport(0, 0, Me.ClientSize.Width, Me.ClientSize.Height, 0!, 1!), RawViewportF))
iDevContext.OutputMerger.SetTargets(depthView, renderView)
' Setup new projection matrix with correct aspect ratio
iProj = SharpDX.Matrix.PerspectiveFovLH(0.7853982!, (CSng(Me.ClientSize.Width) / CSng(Me.ClientSize.Height)), 0.1!, 100!)
End Sub
End Class
我的目标是能够给面孔施加一种给定的颜色,并且每个面孔的阴影都与光源的位置有关。