This commit is contained in:
SamGondelman 2018-12-05 17:18:26 -08:00
parent 305cb6d323
commit 9923e76968
4 changed files with 43 additions and 32 deletions

View file

@ -85,7 +85,7 @@ void Grid3DOverlay::render(RenderArgs* args) {
DependencyManager::get<GeometryCache>()->renderGrid(*batch, minCorner, maxCorner,
_minorGridRowDivisions, _minorGridColDivisions, MINOR_GRID_EDGE,
_majorGridRowDivisions, _majorGridColDivisions, MAJOR_GRID_EDGE,
gridColor, _drawInFront, _geometryId);
gridColor, _geometryId);
}
}

View file

@ -91,7 +91,13 @@ Item::Bound GridEntityRenderer::getBound() {
}
ShapeKey GridEntityRenderer::getShapeKey() {
return render::ShapeKey::Builder().withOwnPipeline().withUnlit().withDepthBias();
auto builder = render::ShapeKey::Builder().withOwnPipeline().withUnlit().withDepthBias();
if (isTransparent()) {
builder.withTranslucent();
}
return builder.build();
}
void GridEntityRenderer::doRender(RenderArgs* args) {
@ -116,7 +122,7 @@ void GridEntityRenderer::doRender(RenderArgs* args) {
if (_followCamera) {
// Get the camera position rounded to the nearest major grid line
// This grid is for UI and should lie on worldlines
glm::vec3 localCameraPosition = glm::inverse(transform.getRotation()) * args->getViewFrustum().getPosition();
glm::vec3 localCameraPosition = glm::inverse(transform.getRotation()) * (args->getViewFrustum().getPosition() - renderTransform.getTranslation());
localCameraPosition.z = 0;
localCameraPosition = (float)_majorGridEvery * glm::round(localCameraPosition / (float)_majorGridEvery);
transform.setTranslation(renderTransform.getTranslation() + transform.getRotation() * localCameraPosition);
@ -135,10 +141,8 @@ void GridEntityRenderer::doRender(RenderArgs* args) {
const float MINOR_GRID_EDGE = 0.0025f;
const float MAJOR_GRID_EDGE = 0.005f;
// FIXME: add layered props to entities
const float LAYERED = false;
DependencyManager::get<GeometryCache>()->renderGrid(*batch, minCorner, maxCorner,
minorGridRowDivisions, minorGridColDivisions, MINOR_GRID_EDGE,
majorGridRowDivisions, majorGridColDivisions, MAJOR_GRID_EDGE,
gridColor, LAYERED, _geometryId);
gridColor, _geometryId);
}

View file

@ -946,7 +946,7 @@ void GeometryCache::renderWireSphere(gpu::Batch& batch, const glm::vec4& color)
void GeometryCache::renderGrid(gpu::Batch& batch, const glm::vec2& minCorner, const glm::vec2& maxCorner,
int majorRows, int majorCols, float majorEdge,
int minorRows, int minorCols, float minorEdge,
const glm::vec4& color, bool isLayered, int id) {
const glm::vec4& color, int id) {
static const glm::vec2 MIN_TEX_COORD(0.0f, 0.0f);
static const glm::vec2 MAX_TEX_COORD(1.0f, 1.0f);
@ -978,7 +978,7 @@ void GeometryCache::renderGrid(gpu::Batch& batch, const glm::vec2& minCorner, co
}
// Set the grid pipeline
useGridPipeline(batch, _registeredGridBuffers[id], isLayered);
useGridPipeline(batch, _registeredGridBuffers[id], color.a < 1.0);
renderQuad(batch, minCorner, maxCorner, MIN_TEX_COORD, MAX_TEX_COORD, color, id);
}
@ -2115,26 +2115,38 @@ void GeometryCache::useSimpleDrawPipeline(gpu::Batch& batch, bool noBlend) {
}
}
void GeometryCache::useGridPipeline(gpu::Batch& batch, GridBuffer gridBuffer, bool isLayered) {
if (!_gridPipeline) {
void GeometryCache::useGridPipeline(gpu::Batch& batch, GridBuffer gridBuffer, bool transparent) {
if (!_gridPipelineOpaque || !_gridPipelineTransparent) {
auto program = gpu::Shader::createProgram(shader::render_utils::program::grid);
_gridSlot = 0;
auto stateLayered = std::make_shared<gpu::State>();
stateLayered->setBlendFunction(true, gpu::State::SRC_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::INV_SRC_ALPHA);
PrepareStencil::testMask(*stateLayered);
_gridPipelineLayered = gpu::Pipeline::create(program, stateLayered);
auto state = std::make_shared<gpu::State>(stateLayered->getValues());
const float DEPTH_BIAS = 0.001f;
state->setDepthBias(DEPTH_BIAS);
state->setDepthTest(true, false, gpu::LESS_EQUAL);
PrepareStencil::testMaskDrawShape(*state);
_gridPipeline = gpu::Pipeline::create(program, state);
{
auto state = std::make_shared<gpu::State>();
state->setDepthTest(true, true, gpu::LESS_EQUAL);
state->setBlendFunction(false,
gpu::State::SRC_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::INV_SRC_ALPHA,
gpu::State::FACTOR_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::ONE);
PrepareStencil::testMaskDrawShape(*state);
state->setCullMode(gpu::State::CULL_NONE);
state->setDepthBias(DEPTH_BIAS);
_gridPipelineOpaque = gpu::Pipeline::create(program, state);
}
{
auto state = std::make_shared<gpu::State>();
state->setDepthTest(true, true, gpu::LESS_EQUAL);
state->setBlendFunction(true,
gpu::State::SRC_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::INV_SRC_ALPHA,
gpu::State::FACTOR_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::ONE);
PrepareStencil::testMask(*state);
state->setCullMode(gpu::State::CULL_NONE);
state->setDepthBias(DEPTH_BIAS);
_gridPipelineTransparent = gpu::Pipeline::create(program, state);
}
}
gpu::PipelinePointer pipeline = isLayered ? _gridPipelineLayered : _gridPipeline;
batch.setPipeline(pipeline);
batch.setUniformBuffer(_gridSlot, gridBuffer);
batch.setPipeline(transparent ? _gridPipelineTransparent : _gridPipelineOpaque);
batch.setUniformBuffer(0, gridBuffer);
}

View file

@ -270,11 +270,7 @@ public:
void renderGrid(gpu::Batch& batch, const glm::vec2& minCorner, const glm::vec2& maxCorner,
int majorRows, int majorCols, float majorEdge,
int minorRows, int minorCols, float minorEdge,
const glm::vec4& color, bool isLayered, int id);
void renderGrid(gpu::Batch& batch, const glm::vec2& minCorner, const glm::vec2& maxCorner,
int rows, int cols, float edge, const glm::vec4& color, bool isLayered, int id) {
renderGrid(batch, minCorner, maxCorner, rows, cols, edge, 0, 0, 0.0f, color, isLayered, id);
}
const glm::vec4& color, int id);
void renderBevelCornersRect(gpu::Batch& batch, int x, int y, int width, int height, int bevelDistance, const glm::vec4& color, int id);
@ -414,9 +410,8 @@ private:
};
using GridBuffer = gpu::BufferView;
void useGridPipeline(gpu::Batch& batch, GridBuffer gridBuffer, bool isLayered);
gpu::PipelinePointer _gridPipeline;
gpu::PipelinePointer _gridPipelineLayered;
int _gridSlot;
gpu::PipelinePointer _gridPipelineOpaque;
gpu::PipelinePointer _gridPipelineTransparent;
class BatchItemDetails {
public: