<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nate K.</title>
    <description>The latest articles on DEV Community by Nate K. (@digitalanthro).</description>
    <link>https://dev.to/digitalanthro</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F279687%2Ff3f63030-d9f2-459e-889f-61b8b75beba9.png</url>
      <title>DEV Community: Nate K.</title>
      <link>https://dev.to/digitalanthro</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/digitalanthro"/>
    <language>en</language>
    <item>
      <title>Drawing a Triangle in Vanilla WebGL</title>
      <dc:creator>Nate K.</dc:creator>
      <pubDate>Thu, 10 Oct 2024 17:23:06 +0000</pubDate>
      <link>https://dev.to/digitalanthro/breaking-down-the-webgl-triangle-setup-3c1m</link>
      <guid>https://dev.to/digitalanthro/breaking-down-the-webgl-triangle-setup-3c1m</guid>
      <description>&lt;p&gt;WebGL has a track record of being one of Javascript’s more complex API’s. As a web developer intrigued by all that’s interactive, I decided to dive into WebGL’s MDN documentation. I’ve worked with Three.js which abstracts a lot of the hardships of WebGL but I couldn’t help but open up the hood!&lt;/p&gt;

&lt;p&gt;The goal of this deep dive was to see if I could make sense of the documentation enough to explain it in simpler terms. &lt;strong&gt;Disclaimer&lt;/strong&gt; — I’ve worked with &lt;a href="https://threejs.org/" rel="noopener noreferrer"&gt;Three.js&lt;/a&gt; and have a bit of knowledge in 3D graphic terminology and patterns. I’ll be sure to explain these concepts if they apply in any way.&lt;/p&gt;

&lt;p&gt;To start, we’re going to focus on the creation of a triangle. The reason for this is to establish an understanding of the parts needed to set up webGL.&lt;/p&gt;

&lt;p&gt;To get a feel of what will be covered, here are the steps we’ll be going through.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Set up the HTML canvas&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Get the WebGL context&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Clear the canvas color and set a new one&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create an array of triangle (x,y )coordinates&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add vertex and fragment shader code&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Process and compile the shader code&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a webGL program&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create and bind buffers to the webGL program&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use the program&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Link the GPU information to the CPU&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Draw out the triangle&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Setup HTML Canvas
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Create a folder called “RenderTriangle”&lt;/li&gt;
&lt;li&gt;In that folder create an index.html and main.js file&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Within the index.html file add the following code:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;index.js&lt;/em&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
&amp;lt;head&amp;gt;
&amp;lt;meta charset="UTF-8" /&amp;gt;
&amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0" /&amp;gt;
&amp;lt;title&amp;gt;Document&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
&amp;lt;canvas id="canvas"&amp;gt;&amp;lt;/canvas&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;script src="main.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;/strong&gt; is the entry point for rendering the WebGL context&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We set up the basic HTML code and link the &lt;em&gt;main.js&lt;/em&gt; file.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the &lt;em&gt;main.js&lt;/em&gt; file we will access the canvas id to render webGL content.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Prepare the HTML Canvas
&lt;/h3&gt;

&lt;p&gt;Within the main.js file add the following code which prepares the HTML canvas:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;main.js&lt;/em&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// get canvas
const canvas = document.getElementById("canvas");

// set width and height of canvas
canvas.width = window.innerWidth;

canvas.height = window.innerHeight;

// get the webgl context
const gl = canvas.getContext("webgl2");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Get the HTML canvas by id and store it in a variable called “canvas” (you can use any name).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Set the &lt;strong&gt;canvas.width&lt;/strong&gt; and &lt;strong&gt;canvas.height&lt;/strong&gt; properties of the canvas by accessing the &lt;strong&gt;window.innerWidth&lt;/strong&gt; and &lt;strong&gt;window.innerHeight&lt;/strong&gt;. This sets the rendered display to the size of the browser window.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Get the WebGL context using &lt;strong&gt;canvas.getContext(“webgl2”)&lt;/strong&gt; and store it in a variable called “gl”.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Clear Color
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;main.js&lt;/em&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gl.clearColor(0.1, 0.2, 0.3, 1.0);
gl.clear(gl.DEPTH_BUFFER_BIT | gl.COLOR_BUFFER_BIT);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Before writing any webGL program you need to set the background color of your canvas. WebGL has two methods for making this happen.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/clearColor" rel="noopener noreferrer"&gt;clearColor() &lt;/a&gt;&lt;/strong&gt; — is a method that sets a specific background color. It’s called “clearColor” because when you render WebGL into the HTML canvas, CSS sets the background to the color black. When you call &lt;strong&gt;clearColor()&lt;/strong&gt;, it clears the default color and sets whatever color you want. We can see this below.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: The &lt;strong&gt;clearColor()&lt;/strong&gt; has 4 parameters (r, g, b, a)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/clear" rel="noopener noreferrer"&gt;clear()&lt;/a&gt;&lt;/strong&gt; — after &lt;strong&gt;clearColor()&lt;/strong&gt; is called and an explicit background color is set,&lt;br&gt;
**clear()** must be called to “clear” or reset the buffers to the preset values (the buffers are temporary storage for color and depth information).&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;clear()&lt;/strong&gt; method is one of the drawing methods which means it’s the method that actually renders the color. Without calling the &lt;strong&gt;clear()&lt;/strong&gt; method the canvas won’t show the &lt;strong&gt;clearColor&lt;/strong&gt;. The &lt;strong&gt;clear()&lt;/strong&gt; parameter options are,&lt;br&gt;
are &lt;code&gt;gl.COLOR_BUFFER_BIT&lt;/code&gt;, &lt;code&gt;gl.DEPTH_BUFFER_BIT&lt;/code&gt;, or &lt;code&gt;gl.STENCIL_BUFFER_BIT&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In the code below you can add multiple parameters to reset in different scenarios.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;gl.DEPTH_BUFFER_BIT — indicates buffers for pixel depth information&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;gl.COLOR_BUFFER_BIT — indicates the buffers for pixel color information&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Set Triangle Coordinates
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;main.js&lt;/em&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// set position coordinates for shape
const triangleCoords = [0.0, -1.0, 0.0, 1.0, 1.0, 1.0];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Once the the background is set we can set the coordinates needed to create the triangle. The coordinates are stored in an array as (x, y) coordinates.&lt;/p&gt;

&lt;p&gt;The below array holds 3-point coordinates. These points connect to form the triangle.&lt;/p&gt;

&lt;p&gt;0.0, -1.0&lt;br&gt;
0.0 , 1.0&lt;br&gt;
1.0, 1.0&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding Vertex and Fragment Shaders
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;main.js&lt;/em&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const vertexShader = `#version 300 es
precision mediump float;

in vec2 position;

void main() {
 gl_Position = vec4(position.x, position.y, 0.0, 1.0); //x,y,z,w
}
`;


const fragmentShader = `#version 300 es
precision mediump float;

out vec4 color;

void main () {
 color = vec4(0.0,0.0,1.0,1.0); //r,g,b,a
}
`;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;After creating a variable for the triangle coordinates, we can set up the shaders.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;shader&lt;/strong&gt; is a program written in OpenGL ES Shading Language. The program takes position and color information about each vertice point. This information is what’s needed to render geometry.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;There are two types of shaders functions that are needed to draw webgl content, the &lt;strong&gt;vertex shader&lt;/strong&gt; and &lt;strong&gt;fragment shader&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;*&lt;em&gt;vertex shader *&lt;/em&gt;— The vertex shader function uses position information to render each pixel. Per every render, the vertex shader function runs on each vertex. The vertex shader then transforms each vertex from it’s original coordinates to &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/WebGL_model_view_projection#clip_space" rel="noopener noreferrer"&gt;WebGL coordinates&lt;/a&gt;. Each transformed vertex is then saved to the &lt;em&gt;gl_Position&lt;/em&gt; variable in the vertex shader program.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;fragment shader *&lt;/em&gt;— The fragment shader function is called once for every pixel on a shape to be drawn. This occurs after the vertex shader runs. The fragment shader determines how the color of each pixel and “texel (pixel within a texture)” should be applied. The fragment shader color is saved in the &lt;em&gt;gl_FragColor&lt;/em&gt; variable in the fragment shader program.&lt;/p&gt;

&lt;p&gt;In the code above we are creating both a &lt;strong&gt;vertexShader&lt;/strong&gt; and &lt;strong&gt;fragmentShader&lt;/strong&gt; constant and storing the shader code in them. &lt;a href="https://thebookofshaders.com/" rel="noopener noreferrer"&gt;The Book of Shaders&lt;/a&gt; is a great resource for learning how to write GLSL code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Processing the Vertex and Fragment Shaders
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;main.js&lt;/em&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// process vertex shader
const shader = gl.createShader(gl.VERTEX_SHADER);

gl.shaderSource(shader, vertexShader);

gl.compileShader(shader);

if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
console.log(gl.getShaderInfoLog(vertexShader));
}


// process fragment shader
const shader = gl.createShader(gl.FRAGMENT_SHADER);

gl.shaderSource(shader, fragmentShader);

gl.compileShader(shader);

if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
console.log(gl.getShaderInfoLog(fragmentShader));
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now that we wrote the shader code (GLSL), we need to create the shader. The shader code still needs to compile. To do this we call the following functions:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;createShader()&lt;/strong&gt; — This creates the shader within the WebGL context&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;shaderSource()&lt;/strong&gt; — This takes the GLSL source code that we wrote and sets it into the webGLShader object that was created with &lt;em&gt;createShader&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;compileShader()&lt;/strong&gt; — This compiles the GLSL shader program into data for the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/WebGLProgram" rel="noopener noreferrer"&gt;WebGLProgram&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The code above processes the vertex and fragment shaders to eventually compile into the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/WebGLProgram" rel="noopener noreferrer"&gt;WebGLProgram&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Note: An if conditional is added to check if both shaders have compiled properly. If not, an info log will appear. Debugging can be tricky in WebGL so adding these checks is a must.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a WebGL Program
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;main.js&lt;/em&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const program = gl.createProgram();

// Attach pre-existing shaders
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);

gl.linkProgram(program);

if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
  const info = gl.getProgramInfoLog(program);
  throw "Could not compile WebGL program. \n\n${info}";
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Let’s review the code above:&lt;/p&gt;

&lt;p&gt;After compiling the &lt;strong&gt;vertexShader&lt;/strong&gt; and &lt;strong&gt;fragmentShader&lt;/strong&gt;, we can now create a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/WebGLProgram" rel="noopener noreferrer"&gt;WebGLProgram&lt;/a&gt;. A &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/WebGLProgram" rel="noopener noreferrer"&gt;WebGLProgram&lt;/a&gt; is an object that holds the compiled &lt;strong&gt;vertexShader&lt;/strong&gt; and &lt;strong&gt;fragmentShader&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;createProgram()&lt;/strong&gt; — Creates and initializes the WebGLProgram&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;attachShader()&lt;/strong&gt; — This method attaches the shader to the webGLProgram&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;linkProgram()&lt;/strong&gt; — This method links the program object with the shader objects&lt;/p&gt;

&lt;p&gt;Lastly, we need to make a conditional check to see if the program is running properly. We do this with the &lt;strong&gt;gl.getProgramParameter.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Create and Bind the Buffers
&lt;/h2&gt;

&lt;p&gt;Now that the WebGL Program is created and the shader programs are linked to it, it’s time to create the buffers. What are &lt;code&gt;buffers&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;To simplify it as much as possible, buffers are objects that store vertices and colors. Buffers don’t have any methods or properties that are accessible. Instead, the WebGL context has it’s own methods for handling buffers.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Buffer — “a temporary storage location for data that’s being moved from one place to another”&lt;br&gt;
-wikipedia&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We need to create a buffer so that we can store our triangle colors and vertices.&lt;/p&gt;

&lt;p&gt;To do this we add the following:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;main.js&lt;/em&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);

gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(triangleCoords), gl.STATIC_DRAW);

gl.bindBuffer(gl.ARRAY_BUFFER, null);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the code above we’re creating a buffer (or temporary storage object) using the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/createBuffer" rel="noopener noreferrer"&gt;createBuffer()&lt;/a&gt; method. Then we store it in a constant. Now that we have a buffer object we need to bind it to a target. There are several different &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/bufferData" rel="noopener noreferrer"&gt;target &lt;/a&gt;but since we’re storing coordinates in an array we will be using the &lt;em&gt;gl.ARRAY_BUFFER&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;To bind the buffer to a target, we use &lt;strong&gt;gl.bindBuffer()&lt;/strong&gt; and pass in the &lt;em&gt;gl.ARRAY_BUFFER&lt;/em&gt; (target) and the buffer itself as parameters.&lt;/p&gt;

&lt;p&gt;The next step would be to use &lt;strong&gt;gl.bufferData()&lt;/strong&gt; which creates the data store. &lt;strong&gt;gl.bufferData()&lt;/strong&gt; takes the following parameters:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;target&lt;/strong&gt; — &lt;em&gt;gl.ARRAY_BUFFER&lt;/em&gt;&lt;br&gt;
&lt;strong&gt;data&lt;/strong&gt; — &lt;em&gt;new Float32Array(triangleCoords)&lt;/em&gt;&lt;br&gt;
&lt;strong&gt;usage&lt;/strong&gt; (draw type) — &lt;em&gt;gl.STATIC_DRAW&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Lastly, we unbind the buffer from the target to reduce side effects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use Program
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;main.js&lt;/em&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gl.useProgram(program);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Once the buffer creation and binding is complete, we can now call the method that sets the WebGLProgram to the rendering state.&lt;/p&gt;

&lt;h2&gt;
  
  
  Link GPU and CPU
&lt;/h2&gt;

&lt;p&gt;As we get closer to the final step, we need to talk about attributes.&lt;/p&gt;

&lt;p&gt;In WebGL, vertex data is stored in a special variable called &lt;strong&gt;attributes&lt;/strong&gt;. &lt;strong&gt;attributes&lt;/strong&gt; are only available to the javascript program and the vertex shader. To access the &lt;strong&gt;attributes&lt;/strong&gt; variable we need to first get the location of the attributes from the GPU. The GPU uses an index to reference the location of the attributes.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;main.js&lt;/em&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// get index that holds the triangle position information
const position = gl.getAttribLocation(obj.program, obj.gpuVariable);

gl.enableVertexAttribArray(position);

gl.bindBuffer(gl.ARRAY_BUFFER, buffer);

gl.vertexAttribPointer(position, 2, gl.FLOAT, obj.normalize, obj.stride, obj.offset);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Let’s review the code above:&lt;/p&gt;

&lt;p&gt;Since we’re rendering a triangle we need the index of the position variable that we set in the vertex shader. We do this using the &lt;strong&gt;gl.getAttribLocation()&lt;/strong&gt; method. By passing in the WebGL &lt;strong&gt;program&lt;/strong&gt; and the &lt;strong&gt;position&lt;/strong&gt; variable name (from the vertex shader) we can get the &lt;strong&gt;position&lt;/strong&gt; attribute’s index.&lt;/p&gt;

&lt;p&gt;Next, we need to use the &lt;strong&gt;gl.enableVertexAttribArray()&lt;/strong&gt; method and pass in the &lt;strong&gt;position&lt;/strong&gt; index that we just obtained. This will enable the attributes` storage so we can access it.&lt;/p&gt;

&lt;p&gt;We will then rebind our buffer using &lt;strong&gt;gl.bindBuffer()&lt;/strong&gt; and pass in the &lt;em&gt;&lt;em&gt;gl.ARRAY_BUFFER&lt;/em&gt;&lt;/em&gt; and &lt;strong&gt;buffer&lt;/strong&gt; parameters (the same as when we created the buffers before). &lt;em&gt;Remember in the “Create and Bind the Buffers” section we set the buffer to null to avoid side effects.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;When we binded the buffer to the &lt;em&gt;&lt;em&gt;gl.ARRAY_BUFFER&lt;/em&gt;&lt;/em&gt; we are now able to store our attributes in a specific order. &lt;strong&gt;gl.vertexAttribPointer()&lt;/strong&gt; allows us to do that.&lt;/p&gt;

&lt;p&gt;By using &lt;strong&gt;gl.vertexAttribPointer()&lt;/strong&gt; we can pass in the attributes we’d like to store in a specific order. The parameters are ordered first to last.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/vertexAttribPointer" rel="noopener noreferrer"&gt;The &lt;strong&gt;gl.vertexAttribPointer&lt;/strong&gt; is a more complex concept that may take some additional research. You can think of it as a method that allows you to store your attributes in the vertex buffer object in a specific order of your choosing.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Sometimes 3D geometry already has a certain format in which the geometry information is set. &lt;strong&gt;vertexAttribPointer&lt;/strong&gt; comes in handy if you need to make modifications to how that geometry information is organized.&lt;/p&gt;

&lt;h2&gt;
  
  
  Draw Triangle
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;main.js&lt;/em&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gl.drawArrays(gl.TRIANGLES, 0, 3);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Lastly, we can use the &lt;strong&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/drawArrays" rel="noopener noreferrer"&gt;gl.drawArrays&lt;/a&gt;&lt;/strong&gt; method to render the triangle. There are other draw methods, but since our vertices are in an array, this method should be used.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;gl.drawArrays()&lt;/strong&gt; takes three parameters:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;mode&lt;/strong&gt; — which specifies the type of primitive to render. (in this case were rendering a triangle)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;first&lt;/strong&gt; — specifies the starting index in the array of vector points (our triangle coordinates). In this case it’s 0.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;count&lt;/strong&gt; — specifies the number of indices to be rendered. ( since it's a triangle we’re rendering 3 indices)&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: For more complex geometry with a lot of vertices you can use **triangleCoords.length / 2 **to ****get how many indices your geometry has.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Finally, your triangle should be rendered to the screen! Let’s review the steps.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr76vfvddfordm41adc19.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr76vfvddfordm41adc19.png" width="800" height="393"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Set up the HTML canvas&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Get the WebGL context&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Clear the canvas color and set a new one&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create an array of triangle (x,y )coordinates&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add vertex and fragment shader code&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Process and compile the shader code&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a webGL program&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create and bind buffers to the webGL program&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use the program&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Link the GPU information to the CPU&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Draw out the triangle&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The API is a complex one so there’s still a lot to learn but understanding this setup has given me a better foundation.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Set up the HTML canvas
const canvas = document.getElementById("canvas");

canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

// get the webgl context
const gl = canvas.getContext("webgl2");

// Clear the canvas color and set a new one
gl.clearColor(0.1, 0.2, 0.3, 1.0);
gl.clear(gl.DEPTH_BUFFER_BIT | gl.COLOR_BUFFER_BIT);


// Create an array of triangle (x,y )coordinates
const triangleCoords = [0.0, -1.0, 0.0, 1.0, 1.0, 1.0];

// Add vertex and fragment shader code
const vertexShader = `#version 300 es
precision mediump float;
in vec2 position;

void main() {
gl_Position = vec4(position.x, position.y, 0.0, 1.0); //x,y,z,w
}
`;

const fragmentShader = `#version 300 es
precision mediump float;
out vec4 color;

void main () {
color = vec4(0.0,0.0,1.0,1.0); //r,g,b,a
}
`;


// Process and compile the shader code
const vShader = gl.createShader(gl.VERTEX_SHADER);

gl.shaderSource(vShader, vertexShader);
gl.compileShader(vShader);

if (!gl.getShaderParameter(vShader, gl.COMPILE_STATUS)) {
 console.log(gl.getShaderInfoLog(vertexShader));
}


const fShader = gl.createShader(gl.FRAGMENT_SHADER);

gl.shaderSource(fShader, fragmentShader);
gl.compileShader(fShader);

if (!gl.getShaderParameter(fShader, gl.COMPILE_STATUS)) {
console.log(gl.getShaderInfoLog(fragmentShader));
}

// Create a webGL program
const program = gl.createProgram();


// Link the GPU information to the CPU
gl.attachShader(program, vShader);
gl.attachShader(program, fShader);

gl.linkProgram(program);


if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
const info = gl.getProgramInfoLog(program);
throw "Could not compile WebGL program. \n\n${info}";
}


// Create and bind buffers to the webGL program
const buffer = gl.createBuffer();

gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(triangleCoords), gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, null);


// Use the program
gl.useProgram(program);



// Link the GPU information to the CPU
const position = gl.getAttribLocation(program, "position");

gl.enableVertexAttribArray(position);
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.vertexAttribPointer(position, 2, gl.FLOAT, gl.FALSE, 0, 0);


// render triangle
gl.drawArrays(gl.TRIANGLES, 0, 3);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here are some invaluable references to help understand this material better.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.udemy.com/course/webgl-internals/" rel="noopener noreferrer"&gt;https://www.udemy.com/course/webgl-internals/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://webglfundamentals.org/" rel="noopener noreferrer"&gt;https://webglfundamentals.org/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webgl</category>
      <category>javascript</category>
      <category>frontend</category>
      <category>threejs</category>
    </item>
    <item>
      <title>Visually Mapping out 3D with Three.js</title>
      <dc:creator>Nate K.</dc:creator>
      <pubDate>Fri, 07 Aug 2020 15:14:46 +0000</pubDate>
      <link>https://dev.to/digitalanthro/visually-mapping-out-3d-with-three-js-3oj7</link>
      <guid>https://dev.to/digitalanthro/visually-mapping-out-3d-with-three-js-3oj7</guid>
      <description>&lt;p&gt;Every developer has their favorite tools, languages, and technologies to work with. Sometimes it derives from another passion such as solving mathematical problems, white-hat hacking, or organization. Luckily, open-source technologies allow everyone to pick up their tool of choice and create something amazing. Now let me show you how you can leverage one of my favorite tools, &lt;a href="https://threejs.org/" rel="noopener noreferrer"&gt;Three.js&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  What is Three.js?
&lt;/h1&gt;

&lt;p&gt;Three.js is a JavaScript library for creating, rendering, and even animating 3D graphics in the browser. If you are into developing web-based games, creating web animations, or creative coding, this would be a great option to explore.&lt;/p&gt;

&lt;p&gt;Under the hood, Three.js utilizes a low-level API called &lt;a href="https://get.webgl.org/" rel="noopener noreferrer"&gt;WebGL&lt;/a&gt;. The creator of Three.js, Ricardo Cabello (Mr.doob), knew the complexities of WebGL and simplified it into an easily accessible library for any developer to utilize.&lt;/p&gt;

&lt;h1&gt;
  
  
  Overview
&lt;/h1&gt;

&lt;p&gt;There's a lot this library is capable of, but for this article, I will just be focusing on the process of setting up a &lt;a href="https://threejs.org/docs/#manual/en/introduction/Creating-a-scene" rel="noopener noreferrer"&gt;three.js scene&lt;/a&gt;. Personally, this was the most difficult part to remember. As a visual learner, I had to use mental images to understand how a 3D scene could be produced on a flat-screen.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Disclaimer: Practice coding out the set up every time you start a new project. This process will never change but if you copy and paste the setup you won't understand why certain things aren't working as expected.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Also, Three.js is a Javascript library. There may be a fair amount of references to Object-Oriented Programming and the Javascript language.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;With that being said. Let's begin!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxff4z0uw7q84hvd0nfo2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxff4z0uw7q84hvd0nfo2.png" alt="Image for post" width="800" height="482"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Setting up Three.js
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;To start off with a metaphoric example. Pretend you are building a stage set for a play. Right now your stage is empty, but you need to add things to it before your actors and actresses can use it.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Setting up Three.js is very similar to that scenario above. You start with one Scene and pull the parts needed into that scene.&lt;/p&gt;

&lt;p&gt;Here are the things you will need: &lt;a href="https://threejs.org/docs/#api/en/scenes/Scene" rel="noopener noreferrer"&gt;Scene&lt;/a&gt;, &lt;a href="https://threejs.org/docs/#api/en/cameras/Camera" rel="noopener noreferrer"&gt;Camera&lt;/a&gt; and &lt;a href="https://threejs.org/docs/#api/en/renderers/WebGLRenderer" rel="noopener noreferrer"&gt;Renderer&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Scene
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://threejs.org/docs/#api/en/scenes/Scene" rel="noopener noreferrer"&gt;The Scene allows you to set up what is to be rendered by three.js. This is where you place objects, lights, and cameras.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Your scene is the first component you will set up. Three.js uses the &lt;a href="https://en.wikipedia.org/wiki/Object-oriented_programming" rel="noopener noreferrer"&gt;Object-Oriented Paradigm&lt;/a&gt; so Scene is an &lt;a href="https://medium.com/@likewaterdesignco/creating-an-object-using-constructors-in-javascript-c1b33e07b3e2" rel="noopener noreferrer"&gt;Object Constructor&lt;/a&gt;. This allows you to create one or more instances of Scene and use it in your app. &lt;a href="https://medium.com/@likewaterdesignco/creating-an-object-using-constructors-in-javascript-c1b33e07b3e2" rel="noopener noreferrer"&gt;&lt;em&gt;If your unfamiliar with Constructors check out this article&lt;/em&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbbwie26dyjbp8q7vp04v.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbbwie26dyjbp8q7vp04v.jpeg" alt="Image for post" width="613" height="473"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Camera
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://threejs.org/docs/#api/en/cameras/Camera" rel="noopener noreferrer"&gt;The Camera is an abstract base class for cameras. This class should always be inherited when you build a new camera.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The next thing you will need to set up is your Camera. Just like setting up a stage for a performance or show, the camera is one of the most essential objects needed to capture everything within the scene.&lt;/p&gt;

&lt;p&gt;Similarly to the Scene, the Camera is just another Object Constructor.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0evw0hhhszfbgry0puy4.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0evw0hhhszfbgry0puy4.jpeg" alt="Image for post" width="613" height="473"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Renderer
&lt;/h2&gt;

&lt;p&gt;Once your Scene and Camera are set up, you will need to add them to your app. Like any other web components, you'll have to add the Scene to the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction" rel="noopener noreferrer"&gt;Document Object Model&lt;/a&gt;. This is where the Renderer comes in.&lt;/p&gt;

&lt;p&gt;Three.js uses a Constructor called &lt;a href="https://threejs.org/docs/#api/en/renderers/WebGLRenderer" rel="noopener noreferrer"&gt;WebGLRenderer&lt;/a&gt; to add your Scene and Camera to the DOM. The Three.js documentation states, "&lt;a href="https://threejs.org/docs/#api/en/renderers/WebGLRenderer" rel="noopener noreferrer"&gt;The WebGL renderer displays your beautifully crafted scenes using WebGL&lt;/a&gt;".&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flnws70yb9o18f9y19awj.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flnws70yb9o18f9y19awj.jpeg" alt="Image for post" width="613" height="473"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Recap
&lt;/h1&gt;

&lt;p&gt;So to recap the visuals above:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Create a Scene&lt;/li&gt;
&lt;li&gt; Set up a Camera&lt;/li&gt;
&lt;li&gt; Add a Renderer&lt;/li&gt;
&lt;li&gt; Place the Scene and Camera into the Renderer&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These are the initial steps to create a scene and add it to your application.&lt;/p&gt;

&lt;h1&gt;
  
  
  The Code
&lt;/h1&gt;

&lt;p&gt;Now that you have a visual idea of how the Scene, Camera, and Renderer work, you are better suited for understanding the code.&lt;/p&gt;

&lt;p&gt;Below is the Javascript code for setting up a Three.js project. I'll go over each section in detail.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Disclaimer: I recommend using &lt;/em&gt;&lt;a href="https://codepen.io/" rel="noopener noreferrer"&gt;&lt;em&gt;CodePen&lt;/em&gt;&lt;/a&gt;&lt;em&gt; to follow along. This will allow us to focus only on the Javascript set up.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let scene, camera, renderer;
init();
function init(){
    scene = new THREE.Scene();
    camera = new THREE.PerspectiveCamera(75, window.innerWidth /    window.innerHeight, 0.01, 1000);
    camera.position.set(0,0,10);

    renderer = new THREE.WebGLRenderer();
    renderer.setSize(window.innerWidth, window.innerHeight);

    document.body.appendChild(renderer.domElement);

    window.addEventListener('resize', resize, false);

    update();
}

function resize(){
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();
    renderer.setSize( window.innerWidth, window.innerHeight);
}

function update(){
    requestAnimationFrame(update);
    renderer.render(scene, camera);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Set variables
&lt;/h2&gt;

&lt;p&gt;Initialize your global variables. We're making these variables global in order to access them in all of our functions. When working on a real application, you will most likely have these within the scope of your 3D component, so they don't clash with other components of your application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let scene, camera, renderer;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Create Initialize function
&lt;/h2&gt;

&lt;p&gt;The initialization, also known as the init function, is the main function that holds all your three.js objects. This includes your Scene, Camera, and Renderer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;init()
function init(){ 
//threejs init code goes here
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Create a Scene
&lt;/h2&gt;

&lt;p&gt;Within the init function you will create a Scene, Camera, and Renderer instance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let scene, camera, renderer;
function init(){
    scene = new THREE.Scene();
    camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.01, 1000);
    camera.position.set(0,0,10);
    renderer = new THREE.WebGLRenderer();
    renderer.setSize(window.innerWidth, window.innerHeight);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Camera Setup
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.01, 1000);
camera.position.set(0,0,10);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Perspective Camera constructor takes 4 arguments:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Point of View --- I usually start with 75&lt;/li&gt;
&lt;li&gt; Aspect Ratio --- the window's inner width divided by the window's inner height&lt;/li&gt;
&lt;li&gt; Near Clipping Path&lt;/li&gt;
&lt;li&gt; Far Clipping Path&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Clipping Paths are the limits in which your scene can be seen. In this example, anything before 0.01 and anything farther than 1000 will disappear. For a better description check the &lt;a href="https://threejs.org/docs/#api/en/cameras/PerspectiveCamera" rel="noopener noreferrer"&gt;PerspectiveCamera&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;After your Camera is set you'll need to position your camera. A good start could be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// positions.set parameters are (x,y,z) axis\
camera.position.set(0,0,20);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Renderer Setup
&lt;/h2&gt;

&lt;p&gt;To create the renderer, you'll need to use the &lt;a href="https://threejs.org/docs/#api/en/renderers/WebGLRenderer" rel="noopener noreferrer"&gt;WebGLRenderer&lt;/a&gt; Constructor. Once created, you can use the setSize method to set the renderer size to fit the whole screen.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Create a DOM Element
&lt;/h2&gt;

&lt;p&gt;Once your Scene, Camera, and Renderer is set up, we now have to create the &lt;a href="https://www.w3schools.com/html/html5_canvas.asp" rel="noopener noreferrer"&gt;canvas&lt;/a&gt; which will house all the components. The &lt;a href="https://www.w3schools.com/html/html5_canvas.asp" rel="noopener noreferrer"&gt;canvas&lt;/a&gt; will also allow us to display our 3D application in the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction" rel="noopener noreferrer"&gt;DOM&lt;/a&gt;. The WebGLRenderer's has a method called &lt;a href="https://threejs.org/docs/#api/en/renderers/WebGLRenderer.domElement" rel="noopener noreferrer"&gt;domElement&lt;/a&gt;. When &lt;a href="https://threejs.org/docs/#api/en/renderers/WebGLRenderer.domElement" rel="noopener noreferrer"&gt;domElement&lt;/a&gt; is called it will display the canvas to the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction" rel="noopener noreferrer"&gt;DOM&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Resize and Update Functions
&lt;/h2&gt;

&lt;p&gt;Lastly, we need to create two essential functions and call them inside your init function.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; resize --- a function to resize your application on tablet and mobile.&lt;/li&gt;
&lt;li&gt; update --- a function to render the scene and camera and update them every frame.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let scene, camera, renderer;

init();

function init(){
    // init code goes here
}

//resize function
function resize(){
  camera.aspect = window.innerWidth / window.innerHeight;
  camera.updateProjectionMatrix();
  renderer.setSize( window.innerWidth, window.innerHeight);
}

//update function
function update(){
  requestAnimationFrame(update);
  renderer.render(scene, camera);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Final Steps
&lt;/h2&gt;

&lt;p&gt;Both the resize and update functions will be called within the init function. Lastly, call the init function, if the screen turns black, that means you've just rendered your first Three.js scene!&lt;/p&gt;

&lt;p&gt;You're now set up to start adding geometry to your scene. Check out the &lt;a href="https://threejs.org/docs/#api/en/geometries/BoxGeometry" rel="noopener noreferrer"&gt;Three.js documentation&lt;/a&gt; for more information.&lt;/p&gt;

&lt;p&gt;View the final steps below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let scene, camera, renderer;

init();

function init(){

//scene
scene = new THREE.Scene();

//camera
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.01, 1000);
camera.position.set(0,0,10);

//renderer
renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);

//render to dom  
document.body.appendChild(renderer.domElement);

// call resize function on resize
window.addEventListener('resize', resize, false);

//update
update();
}

//resize function
function resize(){
  camera.aspect = window.innerWidth / window.innerHeight;
  camera.updateProjectionMatrix();
  renderer.setSize( window.innerWidth, window.innerHeight);
}

//update function
function update(){
  requestAnimationFrame(update);
  renderer.render(scene, camera);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  See code in action below
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://codepen.io/lwdstudio/pen/NWxKWdb" rel="noopener noreferrer"&gt;https://codepen.io/lwdstudio/pen/NWxKWdb&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
