GLSL Shaders - Game development - MDN Web Docs
文章推薦指數: 80 %
Shaders use GLSL (OpenGL Shading Language), a special OpenGL Shading ... Vertex Shaders transform shape positions into 3D drawing coordinates. SkiptomaincontentSkiptosearchSkiptoselectlanguageGamedevelopmentTechniquesforgamedevelopment3DgamesontheWebGLSLShadersArticleActionsEnglish(US)ShadertypesDemoFinalcodeConclusionSeealsoRelatedTopics Introduction Introduction Anatomy Examples APIsforgamedevelopment Canvas CSS Fullscreen Gamepad IndexedDB JavaScript PointerLock SVG TypedArrays WebAudio WebGL WebRTC WebSockets WebVR WebWorkers XMLHttpRequest Techniques Usingasyncscriptsforasm.js Optimizingstartupperformance UsingWebRTCpeer-to-peerdatachannels Efficientanimationforwebgames AudioforWebGames 2Dcollisiondetection Tilesandtilemapsoverview 3DgamesontheWeb 3DgamesontheWeboverview Explainingbasic3Dtheory BuildingupabasicdemowithA-Frame BuildingupabasicdemowithBabylon.js BuildingupabasicdemowithPlayCanvas BuildingupabasicdemowithThree.js WebVR 3Dcollisiondetection BoundingvolumecollisiondetectionwithTHREE.js Implementinggamecontrolmechanisms Controlmechanisms Mobiletouch Desktopwithmouseandkeyboard Desktopwithgamepad Other Tutorials 2DbreakoutgameusingpureJavaScript 2DbreakoutgameusingPhaser 2Dmaze_gamewithdeviceorientation 2DplatformgameusingPhaser Publishinggames Publishinggamesoverview Gamedistribution Gamepromotion Gamemonetization ShadertypesDemoFinalcodeConclusionSeealsoGLSLShadersShadersuseGLSL(OpenGLShadingLanguage),aspecialOpenGLShadingLanguagewithsyntaxsimilartoC.GLSLisexecuteddirectlybythegraphicspipeline.Thereareseveralkindsofshaders,buttwoarecommonlyusedtocreategraphicsontheweb:VertexShadersandFragment(Pixel)Shaders.VertexShaderstransformshapepositionsinto3Ddrawingcoordinates.FragmentShaderscomputetherenderingsofashape'scolorsandotherattributes. GLSLisnotasintuitiveasJavaScript.GLSLisstronglytypedandthereisalotofmathinvolvingvectorsandmatrices.Itcangetverycomplicated—veryquickly.Inthisarticlewewillmakeasimplecodeexamplethatrendersacube.TospeedupthebackgroundcodewewillbeusingtheThree.jsAPI. Asyoumayrememberfromthebasictheoryarticle,avertexisapointina3Dcoordinatesystem.Verticesmay,andusuallydo,haveadditionalproperties.The3Dcoordinatesystemdefinesspaceandtheverticeshelpdefineshapesinthatspace.ShadertypesAshaderisessentiallyafunctionrequiredtodrawsomethingonthescreen.ShadersrunonaGPU(graphicsprocessingunit),whichisoptimizedforsuchoperations.UsingaGPUtodealwithshadersoffloadssomeofthenumbercrunchingfromtheCPU.ThisallowstheCPUtofocusitsprocessingpoweronothertasks,likeexecutingcode.VertexshadersVertexshadersmanipulatecoordinatesina3Dspaceandarecalledoncepervertex.Thepurposeofthevertexshaderistosetupthegl_Positionvariable—thisisaspecial,global,andbuilt-inGLSLvariable.gl_Positionisusedtostorethepositionofthecurrentvertex. Thevoidmain()functionisastandardwayofdefiningthegl_Positionvariable.Everythinginsidevoidmain()willbeexecutedbythevertexshader.Avertexshaderyieldsavariablecontaininghowtoprojectavertex'spositionin3Dspaceontoa2Dscreen.FragmentshadersFragment(ortexture)shadersdefineRGBA(red,green,blue,alpha)colorsforeachpixelbeingprocessed—asinglefragmentshaderiscalledonceperpixel.Thepurposeofthefragmentshaderistosetupthegl_FragColorvariable.gl_FragColorisabuilt-inGLSLvariablelikegl_Position. ThecalculationsresultinavariablecontainingtheinformationabouttheRGBAcolor.DemoLet'sbuildasimpledemotoexplainthoseshadersinaction.BesuretoreadThree.jstutorialfirsttograsptheconceptofthescene,itsobjects,andmaterials. Note:Rememberthatyoudon'thavetouseThree.jsoranyotherlibrarytowriteyourshaders—pureWebGL(WebGraphicsLibrary)ismorethanenough.We'veusedThree.jsheretomakethebackgroundcodealotsimplerandclearertounderstand,soyoucanjustfocusontheshadercode.Three.jsandother3Dlibrariesabstractalotofthingsforyou—ifyouwantedtocreatesuchanexampleinrawWebGL,you'dhavetowritealotofextracodetoactuallymakeitwork. EnvironmentsetupTostartwiththeWebGLshadersyoudon'tneedmuch.Youshould: MakesureyouareusingamodernbrowserwithgoodWebGLsupport,suchasthelatestFirefoxorChrome. Createadirectorytostoreyourexperimentsin. SaveacopyofthelatestminimizedThree.jslibraryinsideyourdirectory. HTMLstructureHere'stheHTMLstructurewewilluse.
延伸文章資訊
- 1gl_Position - OpenGL 4 Reference Pages - Khronos Group
- 2高级GLSL
本文多多少少地深入探讨了一些GLSL有趣的知识,它们可能在将来能帮助你。基本来说有些不可不知的内容和功能在你去使用GLSL创建OpenGL应用的时候能让你的生活更轻松。
- 3Coordinate Systems - LearnOpenGL
We want to define a position for each object to position them inside a larger ... To transform ve...
- 4What is "gl_TexCoord[0].st"? - OpenGL - Khronos Forums
- 5What is a General Ledger (GL)? - TechTarget