GPU가 해당 data를 process한다.
geometry의 vertex를 위치시킨다.
vertex의 coordinates, mesh transformation, camera 정보 등을 shader를 통해서 GPU에 낸다.
GPU는 해당 지시에 따라서 vertex를 배치하고 render한다.
각 vertex의 위치 같이 모든 vertex들 다 다른 data를 attribute라고 한다.
mesh의 위치와 같이 모든 vertex들이 공유하는 data를 uniform이라고 한다.
vertex shader가 먼저 실행되고, vertex들이 배치되면 GPU가 geometry의 보여지는 pixel을 판단하고 fragment shader가 실행된다.
const material = new THREE.RawShaderMaterial()
// vertex shader
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelMatrix;
attribute vec3 position;
void main()
{
gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(position, 1.0);
}
// fragment shader
precision mediump float;
void main()
{
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
import glsl from 'vite-plugin-glsl'
// ...
export default {
// ...
plugins:
[
restart({ restart: [ '../static/**', ] }), // Restart server on static file change
glsl() // Handle shader files
]
}
import testVertexShader from './shaders/test/vertex.glsl'
import testFragmentShader from './shaders/test/fragment.glsl'
const material = new THREE.RawShaderMaterial({
vertexShader: testVertexShader,
fragmentShader: testFragmentShader
})
RawShaderMaterial의 properties
- vertexShader
- fragmentShader
- wireframe
- map, alphaMap, opacity, color 등은 glsl shader 파일에서 직접 조작해야한다.
float a = 1.0;
float b = 2.0;
float c = a/b;
float a = 1.0;
int b = 2;
float c = a* float(b);
bool foo = true;
bool bar = flase;
vec2 foo = vec2(1.0 , 2.0);
vec3 bar = vec3(1.0,2.0,3.0);
vec3 purpleColor = vec3(0.0);
purpleColor.r = 0.5;
purpleColor.b = 1.0;
vec2 foo = vec2(1.0,2.0);
vec3 bar = vec3(foo,3.0);
vec3 bar = vec3(foo,3.0);
vec2 temp = bar.xy;
temp = bar.xz;
vec4 foo = vec4(1.0,2.0,3.0,4.0);
float bar = foo.w;
float bar = foo.a;(alpha)
float loremIpsum(){
float a = 1.0;
float b = 2.0;
return a+b;
}
shader 함수들을 찾아볼 수 있다.
https://shaderific.com/glsl.html
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/indexflat.php
https://thebookofshaders.com/
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelMatrix;
attribute vec3 position;
void main()
{
gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(position, 1.0);
}
modelMatrix
viewMatrix
projectionMatrix
precision mediump float;
void main()
{
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
const count = geometry.attributes.position.count
const randoms = new Float32Array(count)
for(let i = 0; i < count; i++)
{
randoms[i] = Math.random()
}
geometry.setAttribute('aRandom', new THREE.BufferAttribute(randoms, 1))
// ...
attribute float aRandom;
void main()
{
// ...
modelPosition.z += aRandom * 0.1;
// ...
}
precision mediump float;
varying float vRandom;
void main()
{
gl_FragColor = vec4(0.5, vRandom, 1.0, 1.0);
}