Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shahramshakiba/shader-flag-p16
A shader is a program written in GLSL that runs on the GPU to determine how 3D objects are rendered on the screen. ⁞|⁞ ⚪Three.js 🔵GLSL
https://github.com/shahramshakiba/shader-flag-p16
glslang shaders three-js
Last synced: about 1 month ago
JSON representation
A shader is a program written in GLSL that runs on the GPU to determine how 3D objects are rendered on the screen. ⁞|⁞ ⚪Three.js 🔵GLSL
- Host: GitHub
- URL: https://github.com/shahramshakiba/shader-flag-p16
- Owner: ShahramShakiba
- Created: 2024-07-12T12:56:11.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2024-07-22T08:48:53.000Z (4 months ago)
- Last Synced: 2024-10-11T22:06:47.906Z (about 1 month ago)
- Topics: glslang, shaders, three-js
- Language: GLSL
- Homepage: https://shader-waving-flag-shahram.netlify.app/
- Size: 1.36 MB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# _Shader | Waving Flag_
### _Description_
> To begin with, it is important to note that, this project have been sourced from an exceptional `Three.js Journey` Course.
### 👤 Instructed by a _proficient_ and _expert educator_ _"Bruno Simon"_ .
## What is a Shader
A shader is a program written in GLSL (OpenGL Shading Language) that runs on the GPU (Graphics Processing Unit) to determine how 3D objects are rendered on the screen.
Shaders are essential for defining the visual appearance of objects in a 3D scene, including their color, lighting, texture, and special effects.#### Simple Explanation :
Shaders in Three.js are like special effects artists in a movie. Just like an artist adds visual effects to make a movie scene look amazing, shaders enhance the graphics in a 3D scene to make it look more realistic or artistic. They do this by manipulating how objects look when they are drawn on the screen.#### We send a lot of data to the shader :
- Vertices coordinates
- Mesh transformation
- Information about the camera
- Colors
- Textures
- Lights
- Fog
- Etc
## Two types of shaders
> ### _1. Vertex Shaders_
_Define the position of each vertex (point) in 3D space. They shape the geometry of objects._
- _`Function :`_
Transform 3D coordinates to 2D screen coordinates, manage vertex positions, and pass data to the fragment shader.
- _`Example :`_
Adjusting the position of vertices to animate a waving flag.
- _`Real-Life Analogy :`_
Imagine you're designing a sculpture. The vertex shader is like the sculptor who shapes the clay into a specific form, deciding where each part of the sculpture should be.
#### _vertex.glsl :_
``` glsl
uniform mat4 modelMatrix; // apply transformations relative to the Mesh (position, rotation, scale).
uniform mat4 viewMatrix; // apply transformations relative to the Camera (position, rotation, fov, near, far).
uniform mat4 projectionMatrix; // transform the coordinates into the Clip Space coordinates.attribute vec3 position;
void main() {
vec4 modelPosition = modelMatrix * vec4(position, 1.0);
vec4 viewPosition = viewMatrix * modelPosition;
vec4 projectionPosition = projectionMatrix * viewPosition;gl_Position = projectionPosition;
}
```
> ### _2. Fragment Shaders_
_Define the color and texture of each pixel on the object. They handle the details of how an object looks._
- _`Function :`_
Determine the final color of pixels, handle lighting calculations, apply textures, and create visual effects.
- _`Example :`_
Making a surface look shiny and reflective, like a metal.
- _`Real-Life Analogy :`_
Once the sculpture is shaped, the fragment shader is like the painter who adds color, texture, and details to the sculpture to make it look realistic or stylized.#### _fragment.glsl :_
``` glsl
precision mediump float; // sets the default precision for floating-point variables.void main() {
gl_FragColor = vec4(1.0, 0.6, 0.0, 1.0); // specifies the color of the pixel.
}
```
> ### _Attributes_
Attributes are variables that hold data specific to each vertex in a 3D model. These variables change from vertex to vertex.
- Attributes could be the position of each vertex, its color, or its texture coordinates.
- _Analogy_ :
When rendering a 3D model of a tree, each vertex might have attributes for its position in 3D space, its normal direction (for lighting calculations), and its UV coordinates (for applying textures).> ### _Uniforms_
Uniforms are variables in shaders that remain constant for all vertices and pixels during a single rendering pass.
- Uniforms could be the light direction, the color of a light source, or the current time for animations.
- _Analogy_ :
Uniforms are like the oven temperature or cooking time in a recipe—these values are the same for the entire batch of cookies (all vertices and fragments).> ### _Varyings_
Varyings are variables used to pass data from the "vertex" shader to the "fragment" shader. These values are interpolated, meaning they are smoothly transitioned between vertices across the surface of a polygon.
- Varyings are used for data that needs to be shared between vertex and fragment shaders, such as interpolated colors or texture coordinates.
- _Analogy_ :
Varyings are like the sauce that is spread across the pizza. You apply the sauce at a few key vertices (points), and it gets spread out evenly across the whole pizza (interpolated across fragments).
> ### What is GLSL?
GLSL (OpenGL Shading Language) is the programming language used to write shaders. It is designed to run on the GPU and allows fine control over the graphics pipeline. It's close to C language.#### GLSL Syntax and Features
1. C-like Syntax :
- GLSL is syntactically similar to the C programming language, making it familiar to many developers.
- Supports data types such as int, float, vec2, vec3, vec4, mat2, mat3, mat4, etc.2. Built-in Functions :
- Provides a rich set of built-in functions for mathematical operations, texture sampling, geometric calculations, and more.
- Examples include sin(), cos(), normalize(), dot(), cross(), texture(), etc.3. Precision Qualifiers :
- Allows specifying the precision of floating-point calculations.
- Common qualifiers include highp, mediump, and lowp.
> [!IMPORTANT]
> #### Benefits of Using Shaders
>> 1. Enhanced Visuals :
Create realistic lighting, shadows, reflections, and textures.
>> 2. Performance Optimization :
Offload heavy computations to the GPU, making rendering more efficient.
>> 3. Customization :
Craft unique visual effects that are not possible with standard materials.
>> 4. Interactivity :
Create dynamic effects that respond to user input or time, like animated water or glowing buttons.
> [!CAUTION]
> ### How to fix, SyntaxError: Unexpected identifier 'mat4'
> Install: ` npm i vite-plugin-glsl --save-dev `
> Go to "vite.config.js" :
``` vite.config.js
import glsl from 'vite-plugin-glsl';
plugins: [
glsl(), // Handle shader files
],
```
Feel free to delve into the code as it has been written in a straightforward manner for easy understanding.
#### _Try it out immediately, and if you enjoy it, please give me a star._ _Shaders | Waving Flag_
https://github.com/user-attachments/assets/5d38ae04-0373-4767-99dc-7bc7b28e90b7
***