Fundamentals
Core
Master
Shaders
Introduction to Shaders
ã€ãã«shadersã®äžçãžé£ã³èŸŒãæãæ¥ãŸãããshadersã¯ããããçš®é¡ã®èŠèŠå¹æãäœæããããã«ã¯äžå¯æ¬ ã§ãããã®ç« ã§ã¯ãshadersãšã¯äœããããã§äœãã§ããã®ãããããŠãããReact Three Fiberã§ã©ã®ããã«äœ¿çšããã®ããåŠã³ãŸãã
ãã¬ãªã¥ãŒã
å§ããåã«ãshadersã«æ £ãããŸã§ã«å°ãæéãããããããããªãããšããäŒãããããšæããŸãããããŸã§æžããŠããã³ãŒããšã¯ç°ãªãåäœãããŸããããã¯æ°ããçºæ³ãšèŠèŠå¹æã®äœææ¹æ³ã§ããã§ãå¿é ããªãã§ãã ãããããã»ã¹ãæ¡å ããŸãã®ã§ãåºç€ããå§ããŠãåŸã ã«ããé«åºŠãªãããã¯ã«é²ãã§ãããŸãã
æåã¯ãã¹ãŠãç解ã§ããªããŠãèœèããªãã§ãã ãããããã¯æ°ããæŠå¿µã§ãããæ £ããã«ã¯ç·Žç¿ãå¿ èŠã§ããæéããããŠãå®éšããç·Žç¿ããããšããå§ãããŸãããããã ãã®äŸ¡å€ã¯ãããŸãïŒ shadersã¯éåžžã«åŒ·åã§ãæ³åã§ããããããèŠèŠå¹æãäœæããããã®ã³ã³ãããŒã«ãæäŸããŸãã
ããã«ã人ããããç°ãªãåŠç¿ã¹ã¿ã€ã«ããããŸããèªãã§åŠã¶ã®ãåŸæãªäººãããã°ããããªãèŠãŠåŠã¶ã®ãåŸæãªäººãå®éã«ãã£ãŠã¿ãŠåŠã¶ã®ãåŸæãªäººãããŸãããã®ãããã¯ã«é¢ããŠã¯ãç°ãªããœãŒã¹ãçžäºåç §ããããšãéåžžã«åœ¹ç«ã¡ãŸãããã®ç« ã®çµããã«ç¥èãçµ±åããããã§ã«ããŒãã以äžã®ããšãåŠã¶ããã®ãªãœãŒã¹ãå ±æããŸãã
æããããã€ããã¯ãããŸããããshadersãåŠã¶ããšã«ã¯ã¯ã¯ã¯ããŠããããšãé¡ã£ãŠããŸãããããå§ããŸãããïŒ
Shadersãšã¯äœãïŒ
Shadersã¯GPUïŒã°ã©ãã£ãã¯ã¹åŠçè£ çœ®ïŒäžã§å®è¡ãããå°ããªããã°ã©ã ã§ããGLSLïŒOpenGL Shading LanguageïŒãšããCã«äŒŒãèšèªã§æžãããŠããŸãã
Shadersã¯ãã¡ãã·ã¥ã®é ç¹ãé 眮ããããã®ãã®ïŒVertex ShaderïŒã§ãããé¢ã®åãã¯ã»ã«ã圩è²ãããã®ïŒFragment ShaderïŒã§ãã
å®éã«ã¯ç§ãã¡ã¯åžžã«shadersã䜿çšããŠããŸãããmaterialãäœæããéãshadersã䜿çšããŠããŸããããšãã°ãMeshBasicMaterial
ãäœæããéã«ã¯ãã¡ãã·ã¥ãåäžã®è²ã§åœ©è²ããshaderã䜿çšããŠããŸããMeshStandardMaterial
ãäœæããéã«ã¯ãç
§æã圱ãããã³åå°ãã·ãã¥ã¬ãŒãããshaderã䜿çšããŠããŸãã
Vertex Shader
Vertex Shaderã¯ãžãªã¡ããªã®åé ç¹ã«å¯ŸããŠå®è¡ãããããã°ã©ã ã§ãããã®äž»ãªåœ¹å²ã¯ãé ç¹ã3D空éïŒç§ãã¡ã®3Dã¯ãŒã«ãïŒãã2D空éïŒã¹ã¯ãªãŒã³ããã¥ãŒããŒãïŒãžå€æããããšã§ãããã®å€æã¯ä»¥äžã®ããã€ãã®è¡åã䜿çšããŠå®çŸãããŸãïŒ
- View Matrix: ãã®è¡åã¯ã·ãŒã³å ã®ã«ã¡ã©ã®äœçœ®ãšæ¹åãè¡šããŸããé ç¹ãã¯ãŒã«ã空éããã«ã¡ã©ç©ºéãžå€æããŸãã
- Projection Matrix: ããŒã¹ãã¯ãã£ããŸãã¯ãªã«ãœã°ã©ãã£ãã¯ã®ã©ã¡ããã®ãã®è¡åã¯ãé ç¹ãã«ã¡ã©ç©ºéããæ£èŠåããã€ã¹åº§æšïŒNDCïŒã«å€æãããããã2Dã¹ã¯ãªãŒã³ãžã®æçµçãªæ圱ã®æºåãããŸãã
- Model Matrix: ãã®è¡åã¯ã·ãŒã³å ã®åãªããžã§ã¯ãã®äœçœ®ãå転ãã¹ã±ãŒã«ãã«ãã»ã«åããŸããé ç¹ããªããžã§ã¯ã空éããã¯ãŒã«ã空éãžå€æããŸãã
ããã«ãVertex Shaderã¯é ç¹ã®å ã®äœçœ®ããã³ããã«é¢é£ãããã®ä»ã®attributesãåãå ¥ããŸãã
ãžãªã¡ããªã®åé ç¹ã«å¯ŸããŠãVertex Shaderãå®è¡ãããŸãã
æçµçã«ã2D空éã§ã®å€æåŸã®é ç¹ã®äœçœ®ã¯ãäºåå®çŸ©ãããå€æ° gl_Position
ãä»ããŠè¿ãããŸãããã¹ãŠã®é ç¹ãå€æããããšãGPUã¯ãããã®éã®å€ãè£éããŠãžãªã¡ããªã®é¢ãçæãããããã©ã¹ã¿ãŒåãããŠã¹ã¯ãªãŒã³ã«æç»ãããŸãã
ãã©ã°ã¡ã³ãã·ã§ãŒããŒ
ãã©ã°ã¡ã³ãã·ã§ãŒããŒïŒãã¯ã»ã«ã·ã§ãŒããŒãšãåŒã°ããïŒã¯ãã©ã¹ã¿ã©ã€ãºããã»ã¹ã«ãã£ãŠçæãããåãã©ã°ã¡ã³ãïŒãŸãã¯ãã¯ã»ã«ïŒã«å¯ŸããŠå®è¡ãããããã°ã©ã ã§ããäž»ãªã¿ã¹ã¯ã¯ãç»é¢äžã®åãã¯ã»ã«ã®æçµçãªè²ã決å®ããããšã§ãã
ã©ã¹ã¿ã©ã€ãºäžã«çæãããåãã©ã°ã¡ã³ãã«å¯ŸããŠããã©ã°ã¡ã³ãã·ã§ãŒããŒãå®è¡ãããŸãã
ãã©ã°ã¡ã³ãã·ã§ãŒããŒã¯ãé ç¹ã·ã§ãŒããŒããã®è£éãããå€ïŒè²ããã¯ã¹ãã£åº§æšãæ³ç·ãããã³ãžãªã¡ããªã®é ç¹ã«é¢é£ãããã®ä»ã®å±æ§ïŒãåãåããŸãããããã®è£éãããå€ã¯varyingsãšåŒã°ããåãã©ã°ã¡ã³ãäœçœ®ã®è¡šé¢ç¹æ§ã«é¢ããæ å ±ãæäŸããŸãã
è£éãããå€ã«å ããŠããã©ã°ã¡ã³ãã·ã§ãŒããŒã¯ãã¯ã¹ãã£ããµã³ããªã³ã°ããããå šãŠã®ãã©ã°ã¡ã³ãã«ããã£ãŠäžå®ã®uniformå€æ°ã«ã¢ã¯ã»ã¹ãããããããšãã§ããŸãããããã®uniformå€æ°ã¯ãå ã®äœçœ®ãmaterialç¹æ§ããŸãã¯ã·ã§ãŒãã£ã³ã°èšç®ã«å¿ èŠãªãã®ä»ã®ããŒã¿ãè¡šãããšãã§ããŸãã
ãã®ã¬ãã¹ã³ã®åŸåã§ãattributesãšuniformsã«ã€ããŠè©³ãã説æããŸãã
å ¥åããŒã¿ã䜿çšããŠããã©ã°ã¡ã³ãã·ã§ãŒããŒã¯ãã©ã°ã¡ã³ãã®æçµè²ã決å®ããããã«ããŸããŸãªèšç®ãè¡ããŸããããã«ã¯ãè€éãªã©ã€ãã£ã³ã°èšç®ããã¯ã¹ãã£ãããã³ã°ãã·ã§ãŒãã£ã³ã°å¹æããŸãã¯ã·ãŒã³å ã®ä»»æã®èŠèŠå¹æãå«ãŸããå ŽåããããŸãã
è²èšç®ãå®äºãããšããã©ã°ã¡ã³ãã·ã§ãŒããŒã¯å®çŸ©æžã¿ã®å€æ°gl_FragColor
ã䜿çšããŠãã©ã°ã¡ã³ãã®æçµè²ãåºåããŸãã
ã·ã§ãŒããŒã«ã€ããŠã§ããã ãç°¡åã«èª¬æããããšããŸããããæè¡çãªè©³çŽ°ãæå³çã«çç¥ããŸãããããã§ãå°ãæœè±¡çãããããŸããããè©Šãã«ç°¡åãªã·ã§ãŒããŒãäœæããå®éã«ã©ãåäœããããèŠãŠã¿ãŸãããã
åããŠã®ã·ã§ãŒããŒ
ã¹ã¿ãŒã¿ãŒããã¯ãå®è¡ããŸããããç»é¢äžå€®ã«é»ãå¹³é¢ã衚瀺ãããã¯ãã§ãïŒ
ãã¡ã€ã«ShaderPlane.jsx
ãéããŸãããããã®ãã¡ã€ã«ã«ã¯ãsimple meshãšplane geometryãå«ãŸããŠãããåºæ¬çãªmaterialã䜿çšãããŠããŸãããã®materialãã«ã¹ã¿ã ã·ã§ãŒãmaterialã«çœ®ãæããŸãã
shaderMaterial
ã·ã§ãŒãmaterialãäœæããã«ã¯ãDreiã©ã€ãã©ãªã®shaderMaterial
é¢æ°ã䜿çšããŸãã
ãã®é¢æ°ã¯3ã€ã®ãã©ã¡ãŒã¿ãåããŸãïŒ
uniforms
: ã·ã§ãŒãã§äœ¿çšãããuniformå€æ°ãå«ããªããžã§ã¯ããä»ã¯ç©ºã«ããŠãããŠãã ãããvertexShader
: é ç¹ã·ã§ãŒãã®GLSLã³ãŒããå«ãæååãfragmentShader
: ãã©ã°ã¡ã³ãã·ã§ãŒãã®GLSLã³ãŒããå«ãæååã
ãã¡ã€ã«ã®æäžéšã«ãæ°ããã·ã§ãŒãmaterialãMyShaderMaterial
ãšããååã§å®£èšããŸãããïŒ
import { shaderMaterial } from "@react-three/drei"; const MyShaderMaterial = shaderMaterial( {}, ` void main() { gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); }`, ` void main() { gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0); } ` );
ããã«ã·ã§ãŒãã³ãŒãã®è©³çŽ°ã«è§ŠããŸãã
React Three Fiberã§å®£èšçã«äœ¿çšã§ããããã«ããããã«ãextend
ã¡ãœããã䜿çšããŸãïŒ
import { extend } from "@react-three/fiber"; // ... extend({ MyShaderMaterial });
ããã§<meshBasicMaterial>
ãæ°ããã·ã§ãŒãmaterialã«çœ®ãæããããšãã§ããŸãïŒ
import { shaderMaterial } from "@react-three/drei"; import { extend } from "@react-three/fiber"; const MyShaderMaterial = shaderMaterial( {}, ` void main() { gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); }`, ` void main() { gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0); } ` ); extend({ MyShaderMaterial }); export const ShaderPlane = ({ ...props }) => { return ( <mesh {...props}> <planeGeometry args={[1, 1]} /> <myShaderMaterial /> </mesh> ); };
ããã§åãšåãé»ãå¹³é¢ã衚瀺ãããã¯ãã§ãããŸã äœãå€æŽããŠããŸããããã«ã¹ã¿ã ã·ã§ãŒãmaterialã䜿çšããããã«ãªããŸããã
åäœããŠããã確èªããããã«ããã©ã°ã¡ã³ãã·ã§ãŒãã§è¿ããŠããè²ãå€æŽããŠã¿ãŸããããgl_FragColor
è¡ã次ã®ããã«çœ®ãæããŸãïŒ
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
gl_FragColor
ã¯ãã©ã°ã¡ã³ãã®è²ãè¡šãå®çŸ©æžã¿ã®å€æ°ã§ããããã¯4ã€ã®ã³ã³ããŒãã³ããæã€ãã¯ãã«ïŒvec4ïŒã§ãèµ€ãç·ãéãããã³ã¢ã«ãã¡ãã£ã³ãã«ãè¡šããŸããåã³ã³ããŒãã³ãã¯0ãã1ã®éã®floatã§ãã
æåã®ã³ã³ããŒãã³ãã1.0
ã«èšå®ããããšã§ãèµ€ãã£ã³ãã«ãæ倧å€ã«èšå®ããèµ€è²ã«ãªããŸãã
ç»é¢äžå€®ã«èµ€ãå¹³é¢ã衚瀺ãããã¯ãã§ãïŒ
ããã§ãšãããããŸãïŒåããŠã®ã·ã§ãŒãmaterialãäœæããŸãããããã¯ã·ã³ãã«ã§ãããè¯ãã¹ã¿ãŒãã§ãã
ã·ã§ãŒãã³ãŒã
次ã«é²ãåã«ãããå¿«é©ã«ã·ã§ãŒããæžãããã®éçºç°å¢ãã»ããã¢ããããŸãããã
ã·ã§ãŒãã³ãŒããæžãã«ã¯äºã€ã®ãªãã·ã§ã³ããããŸãïŒ
- ã€ã³ã©ã€ã³: ã·ã§ãŒãã³ãŒããJavaScriptãã¡ã€ã«ã«çŽæ¥æžãã
- å€éš:
.glsl
æ¡åŒµåã®å¥ãã¡ã€ã«ã«ã·ã§ãŒãã³ãŒããæžããŠããããJavaScriptãã¡ã€ã«ã«ã€ã³ããŒãããã
ç§ã¯ãéåžžã·ã§ãŒãã³ãŒããmaterialã®å®£èšã«è¿ãå Žæã«ãããããé©åãªmaterialãã¡ã€ã«å ã§ã®ã€ã³ã©ã€ã³ã®æ¹æ³ã奜ã¿ãŸãã
ããããæžãããèªãã ãããã®ã容æã«ããããã«ãGLSLçšã®ã·ã³ã¿ãã¯ã¹ãã€ã©ã€ã¿ãŒã䜿ãããšããå§ãããŸããVisual Studio Codeçšã®Comment tagget templatesæ¡åŒµæ©èœã䜿ãããšãã§ããŸãããã³ãã¬ãŒãæååå ã®GLSLã³ãŒãããã€ã©ã€ãããŠãããŸãã
ã€ã³ã¹ããŒã«ãå®äºããããã·ã³ã¿ãã¯ã¹ãã€ã©ã€ã¿ãŒãæå¹ã«ããããã«ã以äžã®ã³ã¡ã³ããã·ã§ãŒãã³ãŒãã®å é ã«è¿œå ããå¿ èŠããããŸãïŒ
const MyShaderMaterial = shaderMaterial( {}, /* glsl */ ` void main() { gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); }`, /* glsl */ ` void main() { gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); } ` );
ãã³ãã¬ãŒãæååå ã®GLSLã³ãŒãããã€ã©ã€ããããã®ããããã¯ãã§ãïŒ
äžã®é ç¹ã·ã§ãŒããæ£ããã·ã³ã¿ãã¯ã¹ãã€ã©ã€ããæã¡ãèªã¿ããããªã£ãŠããŸãã
ãããã€ã³ã©ã€ã³ã·ã§ãŒãã³ãŒãã«å¿ èŠãªãã¹ãŠã§ããã·ã§ãŒãã³ãŒããå¥ã ã«ä¿ã¡ããå Žåã¯ãå€éšãã¡ã€ã«ã䜿çšããããšãã§ããŸããã©ããããèŠãŠã¿ãŸãããã
GLSLãã¡ã€ã«ã®ã€ã³ããŒã
ãŸããsrc
ãã©ã«ããŒå
ã«shaders
ãšããååã®æ°ãããã©ã«ããŒãäœæããŸãããã®ãã©ã«ããŒå
ã«myshader.vertex.glsl
ãšmyshader.fragment.glsl
ãšãã2ã€ã®ãã¡ã€ã«ãäœæããããããã®ãã¡ã€ã«ã«å¯Ÿå¿ããã·ã§ãŒããŒã³ãŒããã³ããŒããŸãã
myshader.vertex.glsl
:
void main() { gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); }
myshader.fragment.glsl
:
void main() { gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); }
䜿çšããåœåèŠåã¯èªç±ã§ãããŸããå€ãã®ã·ã§ãŒããŒãæã£ãŠããå Žåã¯ããããããµããã©ã«ããŒã«ã°ã«ãŒãåããããšãã§ããŸãã
次ã«ããããã®ãã¡ã€ã«ãJavaScriptãã¡ã€ã«ã«ã€ã³ããŒãã§ããããã«ãããããéçºäŸåãšããŠvite-plugin-glslãã©ã°ã€ã³ãã€ã³ã¹ããŒã«ããå¿ èŠããããŸã:
yarn add vite-plugin-glsl --dev
次ã«ãvite.config.js
ãã¡ã€ã«ã«ãã©ã°ã€ã³ãã€ã³ããŒããããããplugins
é
åã«è¿œå ããŸã:
import react from "@vitejs/plugin-react"; import { defineConfig } from "vite"; import glsl from "vite-plugin-glsl"; // https://vitejs.dev/config/ export default defineConfig({ plugins: [react(), glsl()], });
ããã§ãJavaScriptãã¡ã€ã«ã«GLSLãã¡ã€ã«ãã€ã³ããŒãããã·ã§ãŒããŒã³ãŒããšããŠäœ¿çšããããšãã§ããŸã:
import myShaderFragment from "./shaders/myshader.fragment.glsl"; import myShaderVertex from "./shaders/myshader.vertex.glsl"; const MyShaderMaterial = shaderMaterial({}, myShaderVertex, myShaderFragment);
ããã§ãã·ã§ãŒããŒã³ãŒããå¿«é©ã«äœæããã³ã€ã³ããŒãã§ããããã«ãªããã·ã§ãŒããŒã³ãŒãã®ããŸããŸãªéšåãæ¢çŽ¢ãå§ããããšãã§ããŸãã
GLSL
ã·ã§ãŒããŒã³ãŒãã¯GLSLïŒOpenGL Shading LanguageïŒã§æžãããŠããŸããCã«äŒŒãèšèªã§ãã®ã§ãåºæ¬çãªå 容ãèŠãŠãããŸãããã
ã¿ã€ã
GLSLã«ã¯ããã€ãã®ã¿ã€ãããããŸãããæãäžè¬çãªã®ã¯ä»¥äžã®éãã§ãïŒ
- bool: ããŒã«å€ïŒ
true
ãŸãã¯false
ïŒã - int: æŽæ°ã
- float: æµ®åå°æ°ç¹æ°ã
- vectors: æ°ã®ã³ã¬ã¯ã·ã§ã³ã
vec2
ã¯2ã€ã®æµ®åå°æ°ç¹æ°ã®ã³ã¬ã¯ã·ã§ã³ïŒx
ãšy
ïŒãvec3
ã¯3ã€ã®æµ®åå°æ°ç¹æ°ã®ã³ã¬ã¯ã·ã§ã³ïŒx
ãy
ãããã³z
ïŒãããã³vec4
ã¯4ã€ã®æµ®åå°æ°ç¹æ°ã®ã³ã¬ã¯ã·ã§ã³ïŒx
ãy
ãz
ãããã³w
ïŒãx
ãy
ãz
ãããã³w
ã®ä»£ããã«ãè²ã®å Žåã¯r
ãg
ãb
ãããã³a
ã䜿çšã§ããŸãã - matrices: ãã¯ã¿ãŒã®ã³ã¬ã¯ã·ã§ã³ãããšãã°ã
mat2
ã¯2ã€ã®ãã¯ã¿ãŒã®ã³ã¬ã¯ã·ã§ã³ãmat3
ã¯3ã€ã®ãã¯ã¿ãŒã®ã³ã¬ã¯ã·ã§ã³ãmat4
ã¯4ã€ã®ãã¯ã¿ãŒã®ã³ã¬ã¯ã·ã§ã³ã§ãã
ã¹ãŠã£ãºãªã³ã°ãšæäœ
ã¹ãŠã£ãºãªã³ã°ã䜿çšããŠããã¯ã¿ãŒã®ã³ã³ããŒãã³ãã«ã¢ã¯ã»ã¹ã§ããŸããããšãã°ãä»ã®ãã¯ã¿ãŒã®ã³ã³ããŒãã³ãã䜿çšããŠæ°ãããã¯ã¿ãŒãäœæããããšãã§ããŸãã
vec3 a = vec3(1.0, 2.0, 3.0); vec2 b = a.xy;
ãã®äŸã§ã¯ãb
㯠a
ã® x
ããã³ y
ã³ã³ããŒãã³ããæã€ãã¯ã¿ãŒã«ãªããŸãã
ã³ã³ããŒãã³ãã®é åºãå€æŽããããã«ã¹ãŠã£ãºãªã³ã°ã䜿çšããããšãã§ããŸãïŒ
vec3 a = vec3(1.0, 2.0, 3.0); vec3 b = a.zyx;
ãã®äŸã§ã¯ãb
㯠vec3(3.0, 2.0, 1.0)
ãšçãããªããŸãã
ãã¹ãŠåãã³ã³ããŒãã³ããæã€æ°ãããã¯ã¿ãŒãäœæããããã«ãã³ã³ã¹ãã©ã¯ã¿ã䜿çšã§ããŸãïŒ
vec3 a = vec3(1.0);
ãã®äŸã§ã¯ãa
㯠vec3(1.0, 1.0, 1.0)
ãšçãããªããŸãã
æŒç®å
GLSLã«ã¯äžè¬çãªç®è¡æŒç®å: +
, -
, *
, /
, +=
, /=
, *=
ããã³äžè¬çãªæ¯èŒæŒç®å: ==
, !=
, >
, <
, >=
, <=
ããããŸãã
ãããã¯æ£ããåã§äœ¿çšããå¿ èŠããããŸããäŸãã°ãæŽæ°ãšæµ®åå°æ°ç¹ãå ç®ããããšã¯ã§ããŸããããŸããæŽæ°ãæµ®åå°æ°ç¹ã«å€æããå¿ èŠããããŸã:
int a = 1; float b = 2.0; float c = float(a) + b;
ãŸãããã¯ãã«ãè¡åã«ãæŒç®ãè¡ãããšãã§ããŸã:
vec3 a = vec3(1.0, 2.0, 3.0); vec3 b = vec3(4.0, 5.0, 6.0); vec3 c = a + b;
ããã¯æ¬¡ã®ããã«æžãæããããšãã§ããŸã:
vec3 c = vec3(a.x + b.x, a.y + b.y, a.z + b.z);
é¢æ°
é ç¹ã·ã§ãŒããšãã©ã°ã¡ã³ãã·ã§ãŒãã®ãšã³ããªãŒãã€ã³ãã¯main
é¢æ°ã§ããããã¯ã·ã§ãŒããåŒã³åºããããšãã«å®è¡ãããé¢æ°ã§ãã
void main() { // ããªãã®ã³ãŒãã¯ãã¡ã }
void ã¯é¢æ°ã®æ»ãå€ã®åã§ããããã¯é¢æ°ãäœãè¿ããªãããšãæå³ããŸãã
ç¬èªã®é¢æ°ãå®çŸ©ããããšãã§ããŸã:
float add(float a, float b) { return a + b; }
ãã®é¢æ°ãmain
é¢æ°å
ã§åŒã³åºãããšãã§ããŸã:
void main() { float result = add(1.0, 2.0); // ... }
GLSLã«ã¯ãsin
, cos
, max
, min
, abs
, round
, floor
, ceil
ãªã©ã®äžè¬çãªæäœåãã®çµã¿èŸŒã¿é¢æ°ãå€æ°æäŸãããŠããŸãããŸããmix
, step
, length
, distance
ã®ãããªäŸ¿å©ãªé¢æ°ããããŸãã
次ã®ã¬ãã¹ã³ã§ããããã®åºæ¬çãªé¢æ°ãçºèŠããç·Žç¿ããŠãããŸãã
ã«ãŒããšæ¡ä»¶
GLSL㯠for
ã«ãŒããš if
ã¹ããŒãã¡ã³ãããµããŒãããŠããŸãããããã¯JavaScriptãšåæ§ã«åäœããŸãïŒ
for (int i = 0; i < 10; i++) { // Your code here } if (condition) { // Your code here } else { // Your code here }
ãã®ã³ã°/ãããã°
ã·ã§ãŒããŒããã°ã©ã ã¯äžŠåã«åé ç¹ããã©ã°ã¡ã³ãã®ããã«å®è¡ããããããconsole.log
ã䜿çšããŠã³ãŒãããããã°ãããããã¬ãŒã¯ãã€ã³ããè¿œå ããããšã¯ã§ããŸããããããã·ã§ãŒããŒã®ãããã°ãé£ããããŠããçç±ã§ãã
ã·ã§ãŒããŒããããã°ããäžè¬çãªæ¹æ³ã¯ãgl_FragColor
ã䜿çšããŠå€æ°ã®å€ãèŠèŠåããããšã§ãã
ã³ã³ãã€ã«ãšã©ãŒ
ã·ã§ãŒããŒã³ãŒãã«ééããããå Žåãã³ã³ãœãŒã«ã«ã³ã³ãã€ã«ãšã©ãŒã衚瀺ãããŸãããšã©ãŒã®è¡ãšçš®é¡ãæããŠãããŸããç解ããã®ã¯ç°¡åã§ã¯ãªããããããŸããããåé¡ãèŠã€ããããã®è¯ãææšã«ãªããŸãã
ã¢ã«ãã¡ãã£ãã«ã gl_FragColor
ããåé€ããŠã¿ãŠãäœãèµ·ãããèŠãŠã¿ãŸãããïŒ
void main() { gl_FragColor = vec4(1.0, 0.0, 0.0); }
ã³ã³ãœãŒã«ã«ã³ã³ãã€ã«ãšã©ãŒã衚瀺ãããã¯ãã§ãïŒ
gl_FragColor
ã¯4ã€ã®ã³ã³ããŒãã³ããæåŸ
ããŠããŸããã3ã€ããæäŸããŠããªããšéç¥ãããŠããŸãã
ãšã©ãŒãåãé€ãããã«ã¢ã«ãã¡ãã£ãã«ã 1.0
ã«æ»ãããšãå¿ããªãã§ãã ããã
Uniforms
JavaScriptã³ãŒãããshaderã«ããŒã¿ãæž¡ãããã«ã¯ãuniformsã䜿çšããŸããuniformsã¯å šãŠã®é ç¹ãšãã©ã°ã¡ã³ãã«ãããŠäžå®ã§ãã
projectionMatrix
ãmodelViewMatrix
ãããã³position
ã¯ãshaderã«èªåçã«æž¡ããããã«ãã€ã³ã®uniformsã®äŸã§ãã
ã«ã¹ã¿ã ã®uniformãäœæããŠãè²ãshaderã«æž¡ããŸããããããã䜿çšããŠplaneãçè²ããŸãããããuColor
ãšåŒã³ãŸããuniformã®ååã®åã«u
ãä»ããã®ã¯ããããã³ãŒãã«ãããŠuniformã§ããããšãæ確ã«ããããã®è¯ãç¿æ
£ã§ãã
ãŸããshaderMaterial
ã®uniformã®ãªããžã§ã¯ãã§ããã宣èšããŸãïŒ
import { Color } from "three"; // ... const MyShaderMaterial = shaderMaterial( { uColor: new Color("pink"), } // ... ); // ...
次ã«ããã©ã°ã¡ã³ãã·ã§ãŒããŒã§ããã䜿çšããŸãïŒ
uniform vec3 uColor; void main() { gl_FragColor = vec4(uColor, 1.0); }
ãã³ã¯è²ã«çè²ãããplaneãèŠããã¯ãã§ãïŒ
ããã§ããã³ã¯è²ã¯uniformã®ããã©ã«ãå€ã§ããmaterialäžã§çŽæ¥å€æŽã§ããŸãïŒ
<MyShaderMaterial uColor={"lightblue"} />
planeã¯ä»ãã©ã€ããã«ãŒã«çè²ãããŠããŸãã
é ç¹ã·ã§ãŒããŒãšãã©ã°ã¡ã³ãã·ã§ãŒããŒã®äž¡æ¹ãuniformsã«ã¢ã¯ã»ã¹ã§ããŸããæéã第äºã®uniformãšããŠé ç¹ã·ã§ãŒããŒã«è¿œå ããplaneãäžäžã«åãããŸãããïŒ
End of lesson preview
To get access to the entire lesson, you need to purchase the course.