Skip to content

Commit

Permalink
#150 Implement plane instancing mesh & fix shadow shader bias
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Aug 6, 2022
1 parent 28cd85a commit 876ca94
Show file tree
Hide file tree
Showing 19 changed files with 199 additions and 28 deletions.
4 changes: 2 additions & 2 deletions Assets/BuiltIn/Shader/Shadow/GLSL/LibShadow.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ float shadow(const vec4 shadowCoord[3], const float shadowDistance[3], const flo
{
int id = 0;
float visible = 1.0;
float bias = 0.0001;
const float bias[3] = float[3](0.0001, 0.0001, 0.0003);
float depth = 0.0;

float result = 0.0;
Expand All @@ -37,7 +37,7 @@ float shadow(const vec4 shadowCoord[3], const float shadowDistance[3], const flo
vec3 shadowUV = shadowCoord[id].xyz / shadowCoord[id].w;

depth = shadowUV.z;
depth -= bias;
depth -= bias[id];

vec2 uv = shadowUV.xy;

Expand Down
6 changes: 3 additions & 3 deletions Assets/BuiltIn/Shader/Shadow/HLSL/LibShadow.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ result += COMPARE(float3(rand, id), depth);\
float shadow(const float4 shadowCoord[3], const float shadowDistance[3], const float farDistance)
{
int id = 0;
float visible = 1.0;
float bias = 0.0001;
float visible = 1.0;
const float bias[3] = {0.0001, 0.0001, 0.0004};
float depth = 0.0;

float result = 0.0;
Expand All @@ -37,7 +37,7 @@ float shadow(const float4 shadowCoord[3], const float shadowDistance[3], const f
float3 shadowUV = shadowCoord[id].xyz / shadowCoord[id].w;

depth = shadowUV.z;
depth -= bias;
depth -= bias[id];

float2 uv = shadowUV.xy;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ float shadow(const vec4 shadowCoord[3], const float shadowDistance[3], const flo
{
int id = 0;
float visible = 1.0;
float bias = 0.0001;
const float bias[3] = float[3](0.0001, 0.0001, 0.0003);
float depth = 0.0;
float result = 0.0;
if (farDistance < shadowDistance[0])
Expand All @@ -34,7 +34,7 @@ float shadow(const vec4 shadowCoord[3], const float shadowDistance[3], const flo
return 1.0;
vec3 shadowUV = shadowCoord[id].xyz / shadowCoord[id].w;
depth = shadowUV.z;
depth -= bias;
depth -= bias[id];
vec2 uv = shadowUV.xy;
float size = 1.0/2048.0;
vec2 off;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ float shadow(const vec4 shadowCoord[3], const float shadowDistance[3], const flo
{
int id = 0;
float visible = 1.0;
float bias = 0.0001;
const float bias[3] = float[3](0.0001, 0.0001, 0.0003);
float depth = 0.0;
float result = 0.0;
if (farDistance < shadowDistance[0])
Expand All @@ -34,7 +34,7 @@ float shadow(const vec4 shadowCoord[3], const float shadowDistance[3], const flo
return 1.0;
vec3 shadowUV = shadowCoord[id].xyz / shadowCoord[id].w;
depth = shadowUV.z;
depth -= bias;
depth -= bias[id];
vec2 uv = shadowUV.xy;
float size = 1.0/2048.0;
vec2 off;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ float shadow(const vec4 shadowCoord[3], const float shadowDistance[3], const flo
{
int id = 0;
float visible = 1.0;
float bias = 0.0001;
const float bias[3] = float[3](0.0001, 0.0001, 0.0003);
float depth = 0.0;
float result = 0.0;
if (farDistance < shadowDistance[0])
Expand All @@ -37,7 +37,7 @@ float shadow(const vec4 shadowCoord[3], const float shadowDistance[3], const flo
return 1.0;
vec3 shadowUV = shadowCoord[id].xyz / shadowCoord[id].w;
depth = shadowUV.z;
depth -= bias;
depth -= bias[id];
vec2 uv = shadowUV.xy;
float size = 1.0/2048.0;
vec2 off;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ float shadow(const float4 shadowCoord[3], const float shadowDistance[3], const f
{
int id = 0;
float visible = 1.0;
float bias = 0.0001;
const float bias[3] = {0.0001, 0.0001, 0.0004};
float depth = 0.0;
float result = 0.0;
if (farDistance < shadowDistance[0])
Expand All @@ -44,7 +44,7 @@ float shadow(const float4 shadowCoord[3], const float shadowDistance[3], const f
return 1.0;
float3 shadowUV = shadowCoord[id].xyz / shadowCoord[id].w;
depth = shadowUV.z;
depth -= bias;
depth -= bias[id];
float2 uv = shadowUV.xy;
float size = 1.0/2048;
float2 off;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ float shadow(const float4 shadowCoord[3], const float shadowDistance[3], const f
{
int id = 0;
float visible = 1.0;
float bias = 0.0001;
const float bias[3] = {0.0001, 0.0001, 0.0004};
float depth = 0.0;
float result = 0.0;
if (farDistance < shadowDistance[0])
Expand All @@ -44,7 +44,7 @@ float shadow(const float4 shadowCoord[3], const float shadowDistance[3], const f
return 1.0;
float3 shadowUV = shadowCoord[id].xyz / shadowCoord[id].w;
depth = shadowUV.z;
depth -= bias;
depth -= bias[id];
float2 uv = shadowUV.xy;
float size = 1.0/2048;
float2 off;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ float shadow(const float4 shadowCoord[3], const float shadowDistance[3], const f
{
int id = 0;
float visible = 1.0;
float bias = 0.0001;
const float bias[3] = {0.0001, 0.0001, 0.0004};
float depth = 0.0;
float result = 0.0;
if (farDistance < shadowDistance[0])
Expand All @@ -48,7 +48,7 @@ float shadow(const float4 shadowCoord[3], const float shadowDistance[3], const f
return 1.0;
float3 shadowUV = shadowCoord[id].xyz / shadowCoord[id].w;
depth = shadowUV.z;
depth -= bias;
depth -= bias[id];
float2 uv = shadowUV.xy;
float size = 1.0/2048;
float2 off;
Expand Down
4 changes: 2 additions & 2 deletions Assets/BuiltIn/Shader/Toon/GLSL/ToonShadowFS.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ float shadow(const vec4 shadowCoord[3], const float shadowDistance[3], const flo
{
int id = 0;
float visible = 1.0;
float bias = 0.0001;
const float bias[3] = float[3](0.0001, 0.0001, 0.0003);
float depth = 0.0;
float result = 0.0;
if (farDistance < shadowDistance[0])
Expand All @@ -46,7 +46,7 @@ float shadow(const vec4 shadowCoord[3], const float shadowDistance[3], const flo
return 1.0;
vec3 shadowUV = shadowCoord[id].xyz / shadowCoord[id].w;
depth = shadowUV.z;
depth -= bias;
depth -= bias[id];
vec2 uv = shadowUV.xy;
float size = 1.0/2048.0;
vec2 off;
Expand Down
4 changes: 2 additions & 2 deletions Assets/BuiltIn/Shader/Toon/HLSL/ToonShadowFS.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ float shadow(const float4 shadowCoord[3], const float shadowDistance[3], const f
{
int id = 0;
float visible = 1.0;
float bias = 0.0001;
const float bias[3] = {0.0001, 0.0001, 0.0004};
float depth = 0.0;
float result = 0.0;
if (farDistance < shadowDistance[0])
Expand All @@ -52,7 +52,7 @@ float shadow(const float4 shadowCoord[3], const float shadowDistance[3], const f
return 1.0;
float3 shadowUV = shadowCoord[id].xyz / shadowCoord[id].w;
depth = shadowUV.z;
depth -= bias;
depth -= bias[id];
float2 uv = shadowUV.xy;
float size = 1.0/2048;
float2 off;
Expand Down
53 changes: 53 additions & 0 deletions Projects/Skylicht/Components/Source/Primitive/CPlane.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
!@
MIT License
Copyright (c) 2022 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 "CPlane.h"

namespace Skylicht
{
ACTIVATOR_REGISTER(CPlane);

CATEGORY_COMPONENT(CPlane, "Plane", "Renderer/Primitive");

CPlane::CPlane()
{
m_type = CPrimiviteData::Plane;
}

CPlane::~CPlane()
{

}

void CPlane::initComponent()
{
CPrimitive::initComponent();
}

void CPlane::updateComponent()
{

}
}
44 changes: 44 additions & 0 deletions Projects/Skylicht/Components/Source/Primitive/CPlane.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
!@
MIT License
Copyright (c) 2022 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
!#
*/

#pragma once

#include "CPrimitive.h"

namespace Skylicht
{
class CPlane : public CPrimitive
{
public:
CPlane();

virtual ~CPlane();

virtual void initComponent();

virtual void updateComponent();

DECLARE_GETTYPENAME(CPlane)
};
}
8 changes: 8 additions & 0 deletions Projects/Skylicht/Components/Source/Primitive/CPrimitive.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ namespace Skylicht
bool m_useCustomMaterial;

CMaterial* m_material;

CMaterial* m_customMaterial;

std::string m_materialPath;
Expand All @@ -59,8 +60,15 @@ namespace Skylicht

virtual CEntity* spawn();

public:

CEntity* addPrimitive(const core::vector3df& pos, const core::vector3df& rotDeg, const core::vector3df& scale);

inline void setInstancing(bool b)
{
m_instancing = b;
}

inline CPrimiviteData::EPrimitive getType()
{
return m_type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ namespace Skylicht
initMesh(m, CPrimiviteData::Sphere);
m->drop();
}

// plane
{
initPlane();
}
}

CPrimitiveBaseRenderer::~CPrimitiveBaseRenderer()
Expand Down Expand Up @@ -165,6 +170,55 @@ namespace Skylicht
meshBuffer->drop();
}

void CPrimitiveBaseRenderer::initPlane()
{
IVideoDriver* driver = getVideoDriver();

IMeshBuffer* meshBuffer = new CMeshBuffer<S3DVertex>(driver->getVertexDescriptor(EVT_STANDARD), EIT_16BIT);
IIndexBuffer* ib = meshBuffer->getIndexBuffer();
IVertexBuffer* vb = meshBuffer->getVertexBuffer();

video::SColor clr(255, 255, 255, 255);

vb->reallocate(4);

video::S3DVertex Vertices[] = {
video::S3DVertex(0, 0, 0, 0, 1, 0, clr, 0, 1),
video::S3DVertex(0, 0, 1, 0, 1, 0, clr, 1, 1),
video::S3DVertex(1, 0, 1, 0, 1, 0, clr, 1, 0),
video::S3DVertex(1, 0, 0, 0, 1, 0, clr, 0, 0),
};

for (u32 i = 0; i < 4; ++i)
{
Vertices[i].Pos -= core::vector3df(0.5f, 0.0f, 0.5f);
vb->addVertex(&Vertices[i]);
}

// Create indices
const u16 u[6] =
{
0, 1, 2,
0, 2, 3,
};

ib->set_used(6);

for (u32 i = 0; i < 6; ++i)
ib->setIndex(i, u[i]);

CMesh* mesh = new CMesh();
mesh->addMeshBuffer(meshBuffer);

meshBuffer->recalculateBoundingBox();
mesh->recalculateBoundingBox();
mesh->setHardwareMappingHint(EHM_STATIC);

m_mesh[CPrimiviteData::Plane] = mesh;

meshBuffer->drop();
}

void CPrimitiveBaseRenderer::initMesh(IMesh* primitiveMesh, CPrimiviteData::EPrimitive primitive)
{
CMesh* mesh = new CMesh();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,7 @@ namespace Skylicht
void initMesh(IMesh* primitiveMesh, CPrimiviteData::EPrimitive primitive);

void initCube();

void initPlane();
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace Skylicht
Unknown = 0,
Cube,
Sphere,
Plane,
Count
};

Expand Down
Loading

0 comments on commit 876ca94

Please sign in to comment.