-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3da23d1
commit e7cfb87
Showing
13 changed files
with
289 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#define LENGTH 20.0 | ||
|
||
float3 SSR(const float3 position, const float3 reflection, const float roughness) | ||
{ | ||
float4 projectedCoord; | ||
|
||
float3 beginPosition; | ||
float3 endPosition; | ||
|
||
float3 rayPosition = position; | ||
float4 viewPosition; | ||
|
||
float3 dir = reflection * LENGTH; | ||
|
||
float mipLevel = roughness * 7.0; | ||
|
||
// base color | ||
// convert 3d position to 2d texture coord | ||
viewPosition = mul(float4(position, 1.0), uView); | ||
projectedCoord = mul(float4(viewPosition.xyz, 1.0), uProjection); | ||
projectedCoord.xy = projectedCoord.xy / projectedCoord.w; | ||
projectedCoord.xy = float2(0.5, -0.5) * projectedCoord.xy + float2(0.5, 0.5); | ||
|
||
// default reflect color is from position | ||
float3 baseColor = uTexLastFrame.SampleLevel(uTexLastFrameSampler, projectedCoord.xy, mipLevel).rgb; | ||
|
||
// ray test | ||
for (int i = 6; i >= 0; --i) | ||
{ | ||
// begin ray | ||
beginPosition = rayPosition; | ||
|
||
// end ray | ||
endPosition = rayPosition + dir; | ||
|
||
// mid ray | ||
rayPosition += dir * 0.5; | ||
|
||
// convert 3d position to 2d texture coord | ||
viewPosition = mul(float4(rayPosition, 1.0), uView); | ||
projectedCoord = mul(float4(viewPosition.xyz, 1.0), uProjection); | ||
projectedCoord.xy = projectedCoord.xy / projectedCoord.w; | ||
projectedCoord.xy = float2(0.5, -0.5) * projectedCoord.xy + float2(0.5, 0.5); | ||
|
||
float3 testPosition = uTexPosition.Sample(uTexPositionSampler, projectedCoord.xy).xyz; | ||
|
||
float3 d1 = testPosition - beginPosition; | ||
float lengthSQ1 = d1.x*d1.x + d1.y*d1.y + d1.z*d1.z; | ||
|
||
float3 d2 = testPosition - endPosition; | ||
float lengthSQ2 = d2.x*d2.x + d2.y*d2.y + d2.z*d2.z; | ||
|
||
// beginPosition is nearer | ||
if (lengthSQ1 < lengthSQ2) | ||
{ | ||
rayPosition = beginPosition; | ||
} | ||
|
||
// binary search test | ||
dir *= 0.5; | ||
} | ||
|
||
float z = mul(float4(reflection, 0.0), uView).z; | ||
z = clamp(z, 0.0, 1.0); | ||
|
||
// convert 3d position to 2d texture coord | ||
viewPosition = mul(float4(rayPosition, 1.0), uView); | ||
projectedCoord = mul(float4(viewPosition.xyz, 1.0), uProjection); | ||
projectedCoord.xy = projectedCoord.xy / projectedCoord.w; | ||
projectedCoord.xy = float2(0.5, -0.5) * projectedCoord.xy + float2(0.5, 0.5); | ||
|
||
// default reflect color is from position | ||
float3 color = uTexLastFrame.SampleLevel(uTexLastFrameSampler, projectedCoord.xy, mipLevel).rgb; | ||
|
||
// edge factor | ||
float2 dCoords = smoothstep(float2(0.0, 0.0), float2(0.5, 0.5), abs(float2(0.5, 0.5) - projectedCoord.xy)); | ||
float screenEdgefactor = clamp(1.0 - (dCoords.x + dCoords.y), 0.0, 1.0); | ||
|
||
return lerp(baseColor * 0.7, color, screenEdgefactor * z); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
Projects/Skylicht/Engine/Source/Material/Shader/ShaderCallback/CShaderDeferred.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
!@ | ||
MIT License | ||
Copyright (c) 2020 Skylicht Technology CO., LTD | ||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files | ||
(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, | ||
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | ||
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
This file is part of the "Skylicht Engine". | ||
https://github.com/skylicht-lab/skylicht-engine | ||
!# | ||
*/ | ||
|
||
#include "pch.h" | ||
#include "CShaderDeferred.h" | ||
|
||
namespace Skylicht | ||
{ | ||
core::matrix4 CShaderDeferred::s_projection; | ||
core::matrix4 CShaderDeferred::s_view; | ||
|
||
CShaderDeferred::CShaderDeferred() | ||
{ | ||
|
||
} | ||
|
||
CShaderDeferred::~CShaderDeferred() | ||
{ | ||
|
||
} | ||
|
||
void CShaderDeferred::OnSetConstants(CShader *shader, SUniform *uniform, IMaterialRenderer* matRender, bool vertexShader) | ||
{ | ||
switch (uniform->Type) | ||
{ | ||
case DEFERRED_VIEW: | ||
{ | ||
if (vertexShader == true) | ||
matRender->setShaderVariable(uniform->UniformShaderID, s_view.pointer(), uniform->SizeOfUniform, video::EST_VERTEX_SHADER); | ||
else | ||
matRender->setShaderVariable(uniform->UniformShaderID, s_view.pointer(), uniform->SizeOfUniform, video::EST_PIXEL_SHADER); | ||
} | ||
break; | ||
case DEFERRED_PROJECTION: | ||
{ | ||
if (vertexShader == true) | ||
matRender->setShaderVariable(uniform->UniformShaderID, s_projection.pointer(), uniform->SizeOfUniform, video::EST_VERTEX_SHADER); | ||
else | ||
matRender->setShaderVariable(uniform->UniformShaderID, s_projection.pointer(), uniform->SizeOfUniform, video::EST_PIXEL_SHADER); | ||
} | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
} |
Oops, something went wrong.