光照系統可以工作了。

來源:互聯網
上載者:User

光照系統可以工作了,新增加一個Const Buffer專用於傳遞光照相關的參數:ILightingEnvState.

做光照相對D9來說簡單了許多。不需要任何的setShaderParamter之類的調用了。

Simple.Lighting的shade Node 和用於光照的shader lib如下:

libLighting.hlsl:

struct xLightDesc
{
    float4 m_Diffuse;
    float4 m_Speculer;
    float4 m_Position;
    float4 m_dir;
};

cbuffer xLightState
{  
    int4       nLight;   
    xLightDesc m_Light[8];
}

float4 Phong_Lighting(float3 vNormal , float3 wPosition , float3 eyePosition)
{
   float4 vTotal  = float4(0.0f,0.0f,0.0f , 0.0f);
   for(int i = 0 ; i < nLight.x ; i ++ )
   {
      xLightDesc _desc = m_Light[i];
      float3 LightDirection = _desc.m_Position.xyz - wPosition;
     
      float3 fvLightDirection = normalize( LightDirection );
      float3 fvNormal         = normalize( vNormal );
      float  fNDotL           = dot( fvNormal, fvLightDirection );
  
      float3 fvReflection     = normalize( ( ( 2.0f * fvNormal ) * ( fNDotL ) ) - fvLightDirection );
      float3 fvViewDirection  = normalize( eyePosition -  wPosition);
      float  fRDotV           = max( 0.0f, dot( fvReflection, fvViewDirection ) );
     
      float4 fvTotalAmbient   = float4(0.0f,0.0f,0.0f,1.0f);
      float4 fvTotalDiffuse   = fNDotL * _desc.m_Diffuse;
      float4 fvTotalSpecular  = float4(_desc.m_Speculer.xyz , 1.0f) * pow( fRDotV, _desc.m_Speculer.w );
  
      vTotal += ( saturate( fvTotalAmbient + fvTotalDiffuse + fvTotalSpecular ) );
   }
   return vTotal;
}

 

//Simple.Lighting.hlsl

///{{Slot = Simple_Lighting
Input
{
     float4    Color          : COLOR0;
     float4    wNormal        : WNORMAL;
     float4    wPosition      : WPOSITION;
}

Output
{
     float4 color: COLOR0;
}
///}}

///>>Import = libLighting

///{{SourceCode
float4 Simple_Lighting(float4 _Color , float4 wNormal , float4    wPosition)
{
      float4 eyePositon = float4(0.0f,0.0f,0.0f,1.0f);
      return _Color * Phong_Lighting(wNormal.xyz , wPosition.xyz , eyePositon.xyz );
}
///}}

截個圖留念一下。

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.