WebGL: web-based interactive 2D/3D graphics engine

Recommendation: Use the NSDT editor to quickly build 3D application scenarios

In this guide, we aim to answer most questions related to WebGL technology. First, let's start with the WebGL definition.

What is WebGL?

WebGL (Web Graphics Library) is a JavaScript application programming interface (API) for implementing interactive web graphics.

It allows you to run GPU-accelerated graphics directly inside the HTML canvas without external plugins.

WebGL is a set of functions for drawing vector elements generated on the browser screen. This is done through the client's graphics processing unit (GPU). In other words, the GPU is used to render vector elements.

What sets it apart from other tools is the quality and complexity of the visual elements it can render. In fact, no HTML and CSS method can achieve the exact same impact.

Created by the KhronosGroup, WebGL technology is a direct descendant of OpenGL ES and is used for 3D visualization in games and VR. This innovative technology is currently used in 3D web design, interactive games, physics simulations, data visualization and artwork.

Now, let's understand the basics of WebGL.

WebGL basics

Contrary to common misconception, WebGL is not a graphics suite. Instead of using bitmaps, it uses code to draw geometric objects. It also uses the client's GPU engine to rasterize graphics objects on the HTML canvas.

In order to understand the basics of WebGL, you need to understand basic 3D graphics theory and rendering workflows.

In a 3D scene, each point is a vertex identified by its x, y, and z coordinates. The vertices are then connected to form a set of triangular shapes called primitives.

Apply light sources to create the appearance of shadows and depth. The primitives are then rasterized to create 3D vector graphics as projections of 2D pixels. This tricks the brain into seeing 2D objects on a 3D computer screen.

There are two types of functions in WebGL code:

  • vertex shader,
  • Fragment shader.

They are used to tell the computer how to draw pixels on the screen. While the main program code is written in JavaScript, shaders use the GL Shader language, which is very similar to C/C++.

The vertex shader calculates the coordinates of the vertices, and the fragment shader is responsible for calculating the pixel color. The shading process requires a computer to perform a lot of calculations in order to render an image smoothly. The workload handled by the CPU is usually too large. For this reason, WebGL makes use of the GPU to distribute calculations more efficiently.

Essentially, the WebGL API is all about custom shader state to control what gets drawn on the client screen.

Fortunately, there's no need to manually create a program to start adding 3D models to your 3D website. You can use resources like three.js, PlayCanvas, or Unity's WebGL build options to quickly design 3D experiences without much basic knowledge of WebGL.

Key concepts of 3D and 2D graphics

To summarize what was mentioned above: a set of shaders tells the GPU how to convert lines of code into graphics. In turn, shaders can receive data in a variety of ways. The following briefly describes four existing approaches to shader programming, which ultimately form the key concepts of WebGL.

attributes and buffers

Buffers are datasets that can contain almost any parameter to be communicated. For example, colors, texture coordinates, vertex positions, etc. Attributes describe how data is extracted from the buffer.

uniform

These are global variables that must be defined before playing a shader function.

texture

Textures are data that you can access publicly in both shader types. Typically, textures contain color and image data, but can contain virtually any other data type.

Variety

Variation is the method of transferring data between shaders. The value set in the variable will be applied when the shader is executed.

Ultimately, WebGL is a complex tool that uses complex mathematics and programming to generate graphics on the user's screen. Now, let's talk about its pros and cons.

Advantages and disadvantages of WebGL

When choosing the best technology stack for your purpose, consider the pros and cons of the technology you want to apply.

Pros and Cons of WebGL Development

The most important advantages of WebGL are as follows:

  • It's native and free in most common desktop and mobile browsers
  • The technique is relatively fast and easily mixed with HTML
  • Run WebGL API without installing anything on your device
  • You can leverage external libraries to craft great  3D web experiences

On the other hand, the disadvantage of WebGL is this:

  • It is slower than its predecessor OpenGL in many ways.
  • For native WebGL programming, the learning curve is very steep.
  • There are security holes, although the vendor claims to be addressing them
  • Might be responsible for browser crashes due to plugin incompatibilities etc.

If you need cross-platform and cross-browser compatibility, continuous integration with HTML, and seamless interaction with all elements. WebGL is the best solution.

Interactive App Orange Banner - Weisha Technology Blog

Advantages for business

Besides the purely technical features, WebGL has many other tangible advantages:

  • Spectacular graphics and great user experience

WebGL brings 3D graphics to life on the user's browser screen, and if you think it's no big deal, think again. This API provides real 3D and virtual reality experiences programmatically. This is what makes this technology so impactful.

  • fewer performance issues

WebGL uses client-side hardware, so it runs smoother on the user's screen and generally has lower playback latency.

  • Availability across platforms

WebGL will run on the most common browsers, devices and operating systems, which makes it accessible to a wide variety of user groups.

  • Safer than other solutions

Before the advent of WebGL, the display of 3D graphics and animations in the browser required specific plug-ins. They often crash and cause many security holes. While WebGL isn't completely free of security issues, it's still more secure than any other similar technology to date.

  • Open the door for browser-based games

Browser-based games have always been in high demand. However, the only way to deliver it prior to WebGL was through the use of plugins, which involved a number of issues. WebGL will allow you to easily create web-based games and enter this promising market segment.

WebGL vs OpenGL: Similarities and Differences

WebGL is often compared to OpenGL. OpenGL, also created by the Kronos group in 1992, is an API for 3D and 2D graphics.

WebGL vs OpenGL

It sounds like there's hardly any difference, however, there are many substantial nuances that we shouldn't overlook.

  • origin

WebGL originated from OpenGL ES2, which is a simplified version of OpenGL. On the other hand, OpenGL is more complex and has a plethora of features.

  • feature

In terms of features, OpenGL has a wider scope. It includes a wider variety of shader types in addition to fragment and vertex shaders.

  • Install

While WebGL runs in browsers on desktop and mobile devices, OpenGL requires drivers and requires installation.

  • programming language

WebGL is primarily written in JavaScript, so it integrates well with HTML and runs on an HTML canvas. The native language of OpenGL is C.

  • learning curve

OpenGL has a steeper learning curve because it's a lot more complex. However, once you get the hang of WebGL, it becomes easier to learn.

  • Use Cases

OpenGL is widely used in gaming technology to build video games. WebGL powers web services and applications (below, we explore examples of WebGL use in more detail).

In short, both OpenGL and WebGL are APIs for rendering images. However, WebGL is more suitable for 3D graphics for the Web and building Web-based services. WebGL is also easier to learn, can be enhanced with external libraries, and has better HTML and JavaScript integration.

Additional Libraries for WebGL

WebGL developers can use a wide variety of WebGL frameworks and libraries to build three-dimensional websites. Applying pre-written elements can greatly increase the speed of web development, rather than reinventing the wheel.

Best WebGL Framework

Let's now give a quick overview of some popular add-on libraries for developing 3D websites using WebGL.

Unity WebGL

If you're looking for resources for WebGL development, this is probably the first library you'll come across. Unity WebGL allows web developers to interact directly with the browser's JavaScript engine when creating content in a web page.

This way, all elements on the web page can communicate with each other. Unity WebGL provides different ways to do this. Call a Javascript or C function from your Unity script, or send some data to your Unity script from your browser's JavaScript.

Currently, most major desktop browsers support Unity WebGL content. However, it does not yet provide support for mobile devices.

Three.js

Three.js is a JavaScript element library specially prepared for WebGL. The library has a large collection of effects, objects, cameras, scenes, materials, shaders and other utilities. The manuals are still being written, but the large developer community on the web runs forums and discussions.

babylon.js

Another open-source library on Github, Babylon is often described as an engine for displaying 3D graphics in the browser.js. Its native language is Typescript, but its code is interpreted natively by all browsers that support WebGL and HTML5. Babylon.js has a wide range of applications including gaming, crime data visualization, fashion, healthcare education, and military training.

game canvas

Specifically for gaming, PlayCanvas is a 3D engine powered by a proprietary cloud-based development platform. This allows web development teams to edit web projects in real time from multiple computers. These features include in-browser 3D animation, rigid body physics simulation, and sound design. The engine was open-sourced in 2014 and also has a free repository on Github.

A-Frame

This framework is suitable for XR development (AR/VR and mixed reality experiences). It is also used to display 3D and VR elements in the browser. A-Frame is essentially a VR plugin that has a bunch of components like animations, geometry, cursors, controls, and more.

The code generated by A-Frame can run on most popular VR headsets, as well as display graphics on desktop and mobile devices. This makes A-Frame the perfect library to enable browser games to run on any device. A free repository is also available on Github.

Deck.gl

Deck.gl is primarily intended for visualization of geospatial data and is well suited for working with large datasets using WebGL. Create complex visualizations based on analyzed data.

It integrates well with React and gives great results when combined with MapboxGL. It creates attractive 2D or 3D graphic overlays on top of Mapbox maps. You can use the Deck.gl Github repository to create WebGL geospatial visualizations.

Filament

Filament is a Google open source library for mobile solutions. It is a real-time 3D renderer that also runs on many platforms. This is a new library built in C++ for best performance. The WebGL developers focused on keeping it as light and compact as possible.

Kick.js

Kick.js is a perfect choice for those who want to gain quick hands-on experience in WebGL. This game engine and graphics library is easy to learn with comprehensive documentation, tutorials, and samples. Great for web development and creating interactive games.

ClayGL

ClayGL is used to visualize large collections and data and can also be applied for geospatial mapping purposes. However, the library is also suitable for multipurpose web applications that need to scale on demand. The library provides users with highly complex and detailed graphics, examples and tutorials.

The total number of additional WebGL libraries exceeds 80, and describing them all is beyond the scope of this article. However, you can use GitHub's extensive list of libraries to find the one that best suits your business purposes.

2D and 3D Graphics Best Practices

In order for WebGL to reach its full potential, experts have developed a series of recommendations to help achieve optimal performance. Some WebGL best practices are as follows:

  • Eliminate errors

Typically, WebGL error messages appear as JavaScript warnings. The problem with browsers running WebGL is that after a certain number of errors they stop sending notifications about them. This really hinders fixing the WebGL code. Every effort is made to detect all bugs and eliminate them before the code becomes too hard to fix.getError

  • Learn about browser limitations

The client's browser may not support your app's extension. If using extensions, consider other ways to provide the same experience. However, some extensions provide cross-browser operability, so it makes sense to focus on them when building your product.

  • Understand system limitations

Systems running WebGL code may have limitations and may have capacities that differ significantly from your system's. Therefore, your projects may have different playback speeds, performance, etc. However, WebGL code is usually not that resource intensive and many functions will be universally supported.

  • Clean up code when necessary

Deleting objects clears the WebGL context when rendering results are no longer needed. Cleaning and removing unnecessary elements will help speed up code execution.

5 Lively Examples of WebGL Technology

What is WebGL used for?

Now let's explore what WebGL can do with these living examples. Some of the visual experiences below are truly mesmerizing and successfully demonstrate the capabilities of WebGL.

1. Virtual Physics Lab

In the field of education, WebGL helps create simulations that were previously only possible in real lab environments. At Weisai Technology, we built a virtual physics lab to help students at American universities hone their academic skills.

2. Digital Jellyfish

These 3D jellyfish look completely real, however, they are created using WebGL. Note that you can control parameters such as the jellyfish's size, speed, count, and water turbulence.

WebGL jellyfish

3. Chemical graffiti components

This is a JavaScript 3D library with chemical structures, reactions and other interactive components. ChemDoodle created by iChemLabs is a great tool for chemistry websites and scientists.

3D JS

4. Dissection Visualization

A prominent example of WebGL, this project by BioDigital visualizes all the systems of the human body. Provides comprehensive visualization of treatments and diseases and serves healthcare, scientific and educational purposes.

Bio-digital

5. Roma Music Video

3 Dreams of Black is a music video made by Rome using WebGL. Produced by Mirada in collaboration with director Chris Milk, the video is spectacular and includes game references. must see!

3 The Black Dream of Rome

Apart from these spectacular examples, an entire software development section is dedicated to WebGL games. Some of the best browser games powered by WebGL include Shell Shockers, Derby Crash 4, and Bullet Force. Also, consider taking a look at Google Experiments Showcase projects made using machine learning and WebGL.

Original Link: 6 Best WebGL Libraries for 3D Web Graphics Rendering (mvrlink.com)

Guess you like

Origin blog.csdn.net/ygtu2018/article/details/132583954