Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(217)

Side by Side Diff: chrome/browser/android/vr_shell/vr_shell_renderer.cc

Issue 2915143002: VR: Soften the floor grid (Closed)
Patch Set: Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/android/vr_shell/vr_shell_renderer.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/android/vr_shell/vr_shell_renderer.h" 5 #include "chrome/browser/android/vr_shell/vr_shell_renderer.h"
6 6
7 #include <math.h> 7 #include <math.h>
8 #include <algorithm> 8 #include <algorithm>
9 #include <string> 9 #include <string>
10 10
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 mediump float back_fade_factor = 218 mediump float back_fade_factor =
219 clamp((uv.y - fade_point) / (fade_end - fade_point), 0.0, 1.0); 219 clamp((uv.y - fade_point) / (fade_end - fade_point), 0.0, 1.0);
220 mediump float total_fade = front_fade_factor * back_fade_factor; 220 mediump float total_fade = front_fade_factor * back_fade_factor;
221 lowp vec4 texture_color = texture2D(texture_unit, uv); 221 lowp vec4 texture_color = texture2D(texture_unit, uv);
222 lowp vec4 final_color = color * texture_color; 222 lowp vec4 final_color = color * texture_color;
223 lowp float final_opacity = final_color.w * total_fade * u_Opacity; 223 lowp float final_opacity = final_color.w * total_fade * u_Opacity;
224 gl_FragColor = vec4(final_color.xyz * final_opacity, final_opacity); 224 gl_FragColor = vec4(final_color.xyz * final_opacity, final_opacity);
225 } 225 }
226 /* clang-format on */); 226 /* clang-format on */);
227 case vr_shell::ShaderID::GRADIENT_QUAD_FRAGMENT_SHADER: 227 case vr_shell::ShaderID::GRADIENT_QUAD_FRAGMENT_SHADER:
228 case vr_shell::ShaderID::GRADIENT_GRID_FRAGMENT_SHADER: 228 return SHADER(
229 return OEIE_SHADER(
230 /* clang-format off */ 229 /* clang-format off */
231 precision highp float; 230 precision lowp float;
232 varying vec2 v_GridPosition; 231 varying vec2 v_GridPosition;
233 uniform vec4 u_CenterColor; 232 uniform vec4 u_CenterColor;
234 uniform vec4 u_EdgeColor; 233 uniform vec4 u_EdgeColor;
235 uniform mediump float u_Opacity; 234 uniform mediump float u_Opacity;
236 235
237 void main() { 236 void main() {
238 float edgeColorWeight = clamp(length(v_GridPosition), 0.0, 1.0); 237 float edgeColorWeight = clamp(length(v_GridPosition), 0.0, 1.0);
239 float centerColorWeight = 1.0 - edgeColorWeight; 238 float centerColorWeight = 1.0 - edgeColorWeight;
240 vec4 color = u_CenterColor * centerColorWeight + 239 vec4 color = u_CenterColor * centerColorWeight +
241 u_EdgeColor * edgeColorWeight; 240 u_EdgeColor * edgeColorWeight;
242 gl_FragColor = vec4(color.xyz * color.w * u_Opacity, 241 gl_FragColor = vec4(color.xyz * color.w * u_Opacity,
243 color.w * u_Opacity); 242 color.w * u_Opacity);
244 } 243 }
245 /* clang-format on */); 244 /* clang-format on */);
245 case vr_shell::ShaderID::GRADIENT_GRID_FRAGMENT_SHADER:
246 return SHADER(
247 /* clang-format off */
248 precision lowp float;
249 varying vec2 v_GridPosition;
250 uniform vec4 u_CenterColor;
251 uniform vec4 u_EdgeColor;
252 uniform vec4 u_GridColor;
253 uniform mediump float u_Opacity;
254 uniform int u_LinesCount;
255
256 void main() {
257 float edgeColorWeight = clamp(length(v_GridPosition), 0.0, 1.0);
258 float centerColorWeight = 1.0 - edgeColorWeight;
259 vec4 bg_color = u_CenterColor * centerColorWeight +
260 u_EdgeColor * edgeColorWeight;
261 bg_color = vec4(bg_color.xyz * bg_color.w, bg_color.w);
262 float linesCountF = float(u_LinesCount) / 2.0;
263 float pos_x = abs(v_GridPosition.x) * linesCountF;
264 float pos_y = abs(v_GridPosition.y) * linesCountF;
265 float diff_x = abs(pos_x - floor(pos_x + 0.5));
266 float diff_y = abs(pos_y - floor(pos_y + 0.5));
267 float diff = min(diff_x, diff_y);
268 if (diff < 0.01) {
269 float opacity = 1.0 - diff / 0.01;
270 opacity = opacity * opacity * centerColorWeight * u_GridColor.w;
271 vec3 grid_color = u_GridColor.xyz * opacity;
272 gl_FragColor = vec4(
273 grid_color.xyz + bg_color.xyz * (1.0 - opacity),
274 opacity + bg_color.w * (1.0 - opacity));
275 } else {
276 gl_FragColor = bg_color;
277 }
278 }
279 /* clang-format on */);
246 case vr_shell::ShaderID::CONTROLLER_FRAGMENT_SHADER: 280 case vr_shell::ShaderID::CONTROLLER_FRAGMENT_SHADER:
247 return SHADER( 281 return SHADER(
248 /* clang-format off */ 282 /* clang-format off */
249 precision mediump float; 283 precision mediump float;
250 uniform sampler2D u_texture; 284 uniform sampler2D u_texture;
251 varying vec2 v_TexCoordinate; 285 varying vec2 v_TexCoordinate;
252 uniform mediump float u_Opacity; 286 uniform mediump float u_Opacity;
253 287
254 void main() { 288 void main() {
255 lowp vec4 texture_color = texture2D(u_texture, v_TexCoordinate); 289 lowp vec4 texture_color = texture2D(u_texture, v_TexCoordinate);
(...skipping 456 matching lines...) Expand 10 before | Expand all | Expand 10 after
712 746
713 glDrawArrays(GL_TRIANGLES, 0, kVerticesNumber); 747 glDrawArrays(GL_TRIANGLES, 0, kVerticesNumber);
714 748
715 glDisableVertexAttribArray(position_handle_); 749 glDisableVertexAttribArray(position_handle_);
716 glDisableVertexAttribArray(tex_coord_handle_); 750 glDisableVertexAttribArray(tex_coord_handle_);
717 } 751 }
718 752
719 GradientQuadRenderer::~GradientQuadRenderer() = default; 753 GradientQuadRenderer::~GradientQuadRenderer() = default;
720 754
721 GradientGridRenderer::GradientGridRenderer() 755 GradientGridRenderer::GradientGridRenderer()
722 : BaseRenderer(GRADIENT_QUAD_VERTEX_SHADER, GRADIENT_QUAD_FRAGMENT_SHADER) { 756 : BaseQuadRenderer(GRADIENT_QUAD_VERTEX_SHADER,
757 GRADIENT_GRID_FRAGMENT_SHADER) {
723 model_view_proj_matrix_handle_ = 758 model_view_proj_matrix_handle_ =
724 glGetUniformLocation(program_handle_, "u_ModelViewProjMatrix"); 759 glGetUniformLocation(program_handle_, "u_ModelViewProjMatrix");
725 scene_radius_handle_ = glGetUniformLocation(program_handle_, "u_SceneRadius"); 760 scene_radius_handle_ = glGetUniformLocation(program_handle_, "u_SceneRadius");
726 center_color_handle_ = glGetUniformLocation(program_handle_, "u_CenterColor"); 761 center_color_handle_ = glGetUniformLocation(program_handle_, "u_CenterColor");
727 edge_color_handle_ = glGetUniformLocation(program_handle_, "u_EdgeColor"); 762 edge_color_handle_ = glGetUniformLocation(program_handle_, "u_EdgeColor");
763 grid_color_handle_ = glGetUniformLocation(program_handle_, "u_GridColor");
728 opacity_handle_ = glGetUniformLocation(program_handle_, "u_Opacity"); 764 opacity_handle_ = glGetUniformLocation(program_handle_, "u_Opacity");
765 lines_count_handle_ = glGetUniformLocation(program_handle_, "u_LinesCount");
729 } 766 }
730 767
731 void GradientGridRenderer::Draw(const vr::Mat4f& view_proj_matrix, 768 void GradientGridRenderer::Draw(const vr::Mat4f& view_proj_matrix,
732 SkColor edge_color, 769 SkColor edge_color,
733 SkColor center_color, 770 SkColor center_color,
771 SkColor grid_color,
734 int gridline_count, 772 int gridline_count,
735 float opacity) { 773 float opacity) {
736 // In case the tile number changed we have to regenerate the grid lines. 774 PrepareToDraw(model_view_proj_matrix_handle_, view_proj_matrix);
737 if (grid_lines_.size() != static_cast<size_t>(2 * (gridline_count + 1))) {
738 MakeGridLines(gridline_count);
739 }
740
741 glUseProgram(program_handle_);
742
743 // Pass in model view project matrix.
744 glUniformMatrix4fv(model_view_proj_matrix_handle_, 1, false,
745 MatrixToGLArray(view_proj_matrix).data());
746 775
747 // Tell shader the grid size so that it can calculate the fading. 776 // Tell shader the grid size so that it can calculate the fading.
748 glUniform1f(scene_radius_handle_, kHalfSize); 777 glUniform1f(scene_radius_handle_, kHalfSize);
778 glUniform1i(lines_count_handle_, gridline_count);
749 779
750 // Set the edge color to the fog color so that it seems to fade out. 780 // Set the edge color to the fog color so that it seems to fade out.
751 SetColorUniform(edge_color_handle_, edge_color); 781 SetColorUniform(edge_color_handle_, edge_color);
752 SetColorUniform(center_color_handle_, center_color); 782 SetColorUniform(center_color_handle_, center_color);
783 SetColorUniform(grid_color_handle_, grid_color);
753 glUniform1f(opacity_handle_, opacity); 784 glUniform1f(opacity_handle_, opacity);
754 785
755 // Draw the grid. 786 glDrawArrays(GL_TRIANGLES, 0, kVerticesNumber);
756 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_);
757
758 glVertexAttribPointer(position_handle_, kPositionDataSize, GL_FLOAT, false, 0,
759 VOID_OFFSET(0));
760 glEnableVertexAttribArray(position_handle_);
761 glEnable(GL_BLEND);
762 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
763 int verticesNumber = 4 * (gridline_count + 1);
764 glDrawArrays(GL_LINES, 0, verticesNumber);
765 787
766 glDisableVertexAttribArray(position_handle_); 788 glDisableVertexAttribArray(position_handle_);
789 glDisableVertexAttribArray(tex_coord_handle_);
767 } 790 }
768 791
769 GradientGridRenderer::~GradientGridRenderer() = default; 792 GradientGridRenderer::~GradientGridRenderer() = default;
770 793
771 void GradientGridRenderer::MakeGridLines(int gridline_count) {
772 int linesNumber = 2 * (gridline_count + 1);
773 grid_lines_.resize(linesNumber);
774
775 for (int i = 0; i < linesNumber - 1; i += 2) {
776 float position = -kHalfSize + (i / 2) * kHalfSize * 2.0f / gridline_count;
777
778 // Line parallel to the z axis.
779 Line3d& zLine = grid_lines_[i];
780 // Line parallel to the x axis.
781 Line3d& xLine = grid_lines_[i + 1];
782
783 zLine.start.x = position;
784 zLine.start.y = kHalfSize;
785 zLine.start.z = 0.0f;
786 zLine.end.x = position;
787 zLine.end.y = -kHalfSize;
788 zLine.end.z = 0.0f;
789 xLine.start.x = -kHalfSize;
790 xLine.start.y = -position;
791 xLine.start.z = 0.0f;
792 xLine.end.x = kHalfSize;
793 xLine.end.y = -position;
794 xLine.end.z = 0.0f;
795 }
796
797 size_t vertex_buffer_size = sizeof(Line3d) * linesNumber;
798
799 glGenBuffersARB(1, &vertex_buffer_);
800 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_);
801 glBufferData(GL_ARRAY_BUFFER, vertex_buffer_size, grid_lines_.data(),
802 GL_STATIC_DRAW);
803 }
804
805 VrShellRenderer::VrShellRenderer() 794 VrShellRenderer::VrShellRenderer()
806 : external_textured_quad_renderer_( 795 : external_textured_quad_renderer_(
807 base::MakeUnique<ExternalTexturedQuadRenderer>()), 796 base::MakeUnique<ExternalTexturedQuadRenderer>()),
808 textured_quad_renderer_(base::MakeUnique<TexturedQuadRenderer>()), 797 textured_quad_renderer_(base::MakeUnique<TexturedQuadRenderer>()),
809 webvr_renderer_(base::MakeUnique<WebVrRenderer>()), 798 webvr_renderer_(base::MakeUnique<WebVrRenderer>()),
810 reticle_renderer_(base::MakeUnique<ReticleRenderer>()), 799 reticle_renderer_(base::MakeUnique<ReticleRenderer>()),
811 laser_renderer_(base::MakeUnique<LaserRenderer>()), 800 laser_renderer_(base::MakeUnique<LaserRenderer>()),
812 controller_renderer_(base::MakeUnique<ControllerRenderer>()), 801 controller_renderer_(base::MakeUnique<ControllerRenderer>()),
813 gradient_quad_renderer_(base::MakeUnique<GradientQuadRenderer>()), 802 gradient_quad_renderer_(base::MakeUnique<GradientQuadRenderer>()),
814 gradient_grid_renderer_(base::MakeUnique<GradientGridRenderer>()) { 803 gradient_grid_renderer_(base::MakeUnique<GradientGridRenderer>()) {
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
855 GradientGridRenderer* VrShellRenderer::GetGradientGridRenderer() { 844 GradientGridRenderer* VrShellRenderer::GetGradientGridRenderer() {
856 Flush(); 845 Flush();
857 return gradient_grid_renderer_.get(); 846 return gradient_grid_renderer_.get();
858 } 847 }
859 848
860 void VrShellRenderer::Flush() { 849 void VrShellRenderer::Flush() {
861 textured_quad_renderer_->Flush(); 850 textured_quad_renderer_->Flush();
862 } 851 }
863 852
864 } // namespace vr_shell 853 } // namespace vr_shell
OLDNEW
« no previous file with comments | « chrome/browser/android/vr_shell/vr_shell_renderer.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698