Home Web Front-end JS Tutorial THREE.JS introductory tutorial (2) Shader-Part 1_Basic knowledge

THREE.JS introductory tutorial (2) Shader-Part 1_Basic knowledge

May 16, 2016 pm 05:43 PM

Translation
Three.js is a great open source WebGL library. WebGL allows JavaScript to operate the GPU and achieve true 3D on the browser side. However, this technology is still in the development stage, and the information is extremely scarce. Enthusiasts basically have to learn through the Demo source code and the source code of Three.js itself.

0. Introduction
I have already given an article "Start Using Three.js" before. If you haven't read it yet, you may want to. The basis of this article is based on that tutorial.

I would like to discuss shaders. Native WebGL was great until Three.js saved you a lot of trouble. Sometimes, you may want to achieve some specific effects, or want to delve a little deeper into what is displayed on your screen, then shaders will definitely enter your field of vision. If you're like me, you also want to implement something more interesting than the basics in the last tutorial. In this tutorial, I will explain the basics of Three.js, which actually do a lot of boring work for us.
Before I start, I would also like to say that this tutorial will spend quite a bit of time explaining the shader code. There will be a tutorial later that will go a little further on the basis of the shader code and use the shader to do something. . This is because shaders are not easy to understand at first glance and require some explanation.
1. Two shaders
WebGL does not have a fixed rendering pipeline, and you cannot directly use a black box shader (Translator's Note: Graphics cards in the last century basically only support Fixed rendering pipeline); WebGL provides a programmable pipeline, which is more powerful but also harder to understand and use. Long story short, a programmable rendering pipeline means that the programmer is responsible for getting the vertices and drawing them to the screen. Shaders are part of the rendering pipeline. There are two types of shaders:
1. Vertex shader
2. Fragment shader
What you should know is that both of these shaders run entirely on the graphics card On the GPU, we offload the data that needs to be processed from the CPU and load it on the GPU, which reduces the burden on the CPU. Modern GPUs have greatly optimized the types of operations that shaders need to call, and it is worth doing so.
2. Vertex shader
Primitive shapes, such as a sphere, are made up of vertices, right? The vertex shader is passed one of these vertices in turn and processes it. How to handle each vertex is freely customizable, but one thing the vertex shader must do is to assign a value to a variable named gl_Position, which is a 4-dimensional array that represents the final position of the vertex on the screen. This in itself is an interesting process, because we are actually talking about how to convert a three-dimensional coordinate (a vertex with x, y, z values) into, or project to a two-dimensional screen. Thankfully, we can access gl_Position so easily if we use a tool like Three.js.
3. Fragment shader
Now that we have a three-dimensional object containing vertices, we need to project the object onto a two-dimensional screen, but where does the color go? What about textures and lighting? This is what the fragment shader handles.
Similar to the vertex shader, the fragment shader has a task that must be completed: setting or eliminating the variable gl_FragColor, another four-dimensional floating point variable, which is the final color of the fragment point. What is a piece? Imagine a triangle with three vertices. Fragments are all points inside the triangle calculated from these three vertices. Therefore, fragment values ​​are interpolated from vertex values. If the color of one vertex is red and the color of the adjacent vertex is blue, then we can observe the color gradient from near the red vertex, from red to purple, and finally to blue near the blue vertex.
4. Shader variables
When it comes to shader variables, there are three types: Uniforma, Attributes and Varyings. When I first heard these three words, I was confused because they didn't match anything I had used before. But now, you can understand them like this:
1. Uniforms variables can be passed into either the vertex shader or the fragment shader. They contain variables that remain unchanged throughout the rendering process, such as, The position of a point light source.
2.Attributes variables correspond to each vertex, they can only be passed into the vertex shader, for example, each vertex has a color. The relationship between Attributes variables and vertices is one-to-one correspondence.
3.Varyings variables are variables defined in the vertex shader and prepared to be passed to the fragment shader. To ensure this, we need to ensure that the variables are typed and named exactly the same in both shaders. A classic application is the normal vector, because normals are needed when calculating lighting.
In the next tutorial, I will use these three variables, and you will also learn how these three variables are actually used.
Now, we've talked about the vertex shader, the fragment shader, and the three shader variables. It's time to look at the simplest shader we can create.
5. Hello World(번역자 Tucao: 프랑스어 자랑 좀 그만해주세요)
여기 가장 간단한 버텍스 셰이더가 있습니다:
코드 복사 코드는 다음과 같습니다.

/**
* 각 정점 좌표에 모델 뷰 행렬을 곱한 후 투영 행렬을 곱합니다
* 2D 화면의 좌표를 구합니다
*/
void main() {
gl_Position =projectionMatrix *
modelViewMatrix *
vec4(position,1.0);
}

간단한 프래그먼트 셰이더:
코드 복사 코드는 다음과 같습니다.

/**
* 모든 픽셀 색상을 분홍색으로 설정
*/
void main() {
gl_FragColor = vec4(1.0, / / R
0.0, // G
1.0, // B
1.0) // A
}

그게 다입니다. 지금 바로 실행해보면 화면에 "매트" 핑크색 모양이 나타나는 것을 볼 수 있습니다. 별로 복잡하지 않죠?

버텍스 셰이더에서는 Three.js를 통해 일부 유니폼 변수를 전달합니다. 두 개의 4×4 행렬 균일 변수가 있습니다: 모델뷰 행렬과 투영 행렬. 이 두 행렬이 어떻게 작동하는지에 대해 많이 알 필요는 없습니다. 간단히 말해서, 이 두 행렬은 3차원 점 좌표가 2차원 화면의 좌표에 투영되는 방식을 설명합니다.

사실 이 두 개의 짧은 단편만 다루었습니다. Three.js는 이를 셰이더 코드 앞에 추가하므로 걱정할 필요가 없습니다. 솔직히 말해서 Three.js는 조명 데이터, 노드 색상, 노드 법선 벡터 등과 같은 많은 항목을 코드 앞에 추가하기도 합니다. Three.js가 없었다면 이러한 개체를 직접 만들고 설정해야 했습니다.
6. 셰이더 소재 사용
코드 복사 코드는 다음과 같습니다

/**
* JQuery
*를 사용하여 DOM에서 셰이더 코드 텍스트를 추출할 수 있다고 가정합니다
*/
var vShader = $('vertexshader')
var fShader = $('fragmentsshader')
varshaderMaterial =
new THREE.ShaderMaterial ({
vertexShader: vShader.text(),
fragmentShader: fShader.text()
})

여기에서 Three.js가 컴파일되고 셰이더를 실행하고 생성한 재질에 연결하면 재질이 생성한 메시에 연결됩니다. 실제보다 더 쉬워지는 것은 아닙니다. 그럴 수도 있지만 우리는 브라우저 3D 프로그래밍에 대해 생각하고 있으며 이 주제에는 어느 정도 복잡성이 있을 것이라고 예상해야 한다고 생각합니다.

셰이더 재질과 같은 두 가지 다른 속성인 유니폼과 속성을 추가할 수도 있습니다. 벡터, 정수 또는 부동 소수점일 수 있지만 이전에 말했듯이 유니폼 변수는 모든 지점에서 일정하게 유지되므로 단일 값일 가능성이 더 높지만 속성 변수는 정점별로 있으므로 배열이어야 합니다. 메쉬에서 속성 변수와 정점은 일대일 대응을 가져야 합니다.
7. 요약
사실 이번 튜토리얼은 여기까지입니다. 사실 많은 이야기를 했지만 여러 면에서 훑어볼 뿐입니다. 다음 튜토리얼에서는 조명 효과를 시뮬레이션하기 위해 일부 속성 변수와 유니폼 변수를 전달하는 복잡한 셰이더를 제공할 것입니다.
이 튜토리얼의 소스 코드를 패키징했습니다. 다운로드하여 참조할 수 있습니다.
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

How to implement panel drag and drop adjustment function similar to VSCode in front-end development? How to implement panel drag and drop adjustment function similar to VSCode in front-end development? Apr 04, 2025 pm 02:06 PM

Explore the implementation of panel drag and drop adjustment function similar to VSCode in the front-end. In front-end development, how to implement VSCode similar to VSCode...

See all articles