Editing the shader

Okay, so that's all the Java code we need to write in order to calculate and set our new distance variable. Next, we want to open up the shader program and modify the code for our needs. Follow the given steps to modify the shader program:

  1. Open the point_cloud_vertex.shader file under the res/raw folder, as shown:
>
Opening point_cloud_vertex.shader
  1. Make the highlighted code changes, as follows:
uniform mat4 u_ModelViewProjection;
uniform vec4 u_Color;
uniform float u_PointSize;
uniform float u_FurthestPoint;

attribute vec4 a_Position;

varying vec4 v_Color;

void main() {
float t = length(a_Position)/u_FurthestPoint;
v_Color = vec4(t, 1.0-t,t,1.0);
gl_Position = u_ModelViewProjection * vec4(a_Position.xyz, 1.0);
gl_PointSize = u_PointSize;
}
  1. The first line of code is new. All we are doing is taking the length of the a_Position vector, determining its length or distance to the camera, and then normalizing that value between 0 and 1. The second line then creates a new vec4 for color based on our calculations of the t variable. This new vector represents the color in the form red blue green alpha (RGBA), where alpha is set to a constant of 1.0.
  2. Save the file, connect your device, and build and run the app on your device. You should now see the cloud points colorized by distance to the camera, as follows:
Screenshot of colored point cloud points by depth

Imagine if we had to write Java code in order to do the same colorization of the points. We would certainly need a lot more code than what we wrote. Also, any Java code we used would certainly be much slower than a shader. Now, for our example, the app's performance is less critical, but when you develop a real AR app, you will want to squeeze all the performance you can; that's why our discussion and knowledge of shaders is so important.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.146.236