Animated lines with WebGL | Sample Code - ArcGIS Developer
文章推薦指數: 80 %
... polyline graphics. This description assumes familiarity with WebGL and custom WebGL layer views. ... This sample instead uses polyline triangulation.
ArcGISAPIforJavaScriptSampleCodeOverviewSampleCodeAPIReferenceShowcaseBlogMenuGetstartedLatestsamplesMappingandviewsLayersFeatureLayerSceneLayerBuildingSceneLayerCSVandGeoJSONRasterlayersElevationLayerIntegratedMeshLayerVoxelLayerMediaLayerMapImageLayerOGCLayersPointCloudLayerVectorTileLayerRouteLayerSubtypeGroupLayerCustomlayersCustomTileLayerCustomDynamicLayerCustomLERCLayerCustomBlendLayerCustomElevationLayer-ExaggeratingelevationCustomElevationLayer-ThematicdataaselevationCustomWebGLlayerviewsAnimatedlineswithWebGLTessellationhelpersforcustomWebGLlayerviewsTilingsupportforcustomWebGLlayerviewsMaskingeffectusingacustomlayerviewBuildacustomlayerviewusingdeck.glOthertiledlayersLivefeedsQueryEditingLabelsDrawVisualizationPopupsGraphicsRoutingSearchTimeAnalysisWidgetsOtherNavigationMenuAnimatedlineswithWebGLExploreinthesandboxOpeninCodePenViewliveImportantnotes:Thissampleshowsexperimentalfunctionality,pleasereadthedocumentationcarefullybeforeusingitinaproduct.ThissampleiswrittenforexpertdevelopersfamiliarwithWebGLandhardware-acceleratedrendering.Thissampledemonstrateshowtoimplementananimatedtrail(orflow)effectforpolylinegraphics.ThisdescriptionassumesfamiliaritywithWebGLandcustomWebGLlayerviews.It'ssimilartotheCustomWebGLlayerviewsample,whichtriangulatespointsintoquads.Thissampleinsteadusespolylinetriangulation.TheupdatePositions()methodhasbeenmodifiedtoconvertthegeometryofpolylinegraphicsintotrianglemeshes.Specialper-vertexattributesarecomputedandstoredontheGPUandthenusedinconjunctionwithper-frameuniformvaluestoimplementanefficientanimationsystemthatcansupportthousandsoftrailsatthesametime.Inthissamplewetessellatedpolylinesusingcustomcode;version4.14oftheArcGISAPIintroducesgenerictessellationroutinesthatthedevelopercanusetotessellatearbitrarygeometries;seetheSDKtessellationsampleformoredetails.CreatingthemeshWebGLdrawcallsoperateongeometricprimitives.WhileinWebGLthereissupportforthegl.LINESprimitive,thisisnotveryflexibleandcannotbeusedtoimplementadvancedeffectssuchastheoneshowcasedinthissample.Themostflexibleprimitiveistheindexedtrianglelist,whichcanberenderedusinggl.drawElements(gl.TRIANGLES,...).Thesampleapplicationrendersallpolylinesintheviewusingonesinglesuchcall.Tofeedtherasterizationweneedtosetupappropriatevertexattributesandanindexbuffer.TriangulatinglinesApolylinecanberepresentedusingtriangles.AsimpletriangulationschemeistheoneinwhicheveryvertexoftheoriginalpolylineisextrudedintotwoGPUverticesinoppositedirections.GroupsoffourGPUverticesarethenconnectedusingtwotrianglesforatotalofsixindices.Thegeometryoftheextrusionisexemplifiedbythefigurebelow.Letαbetheangleofaturn,andwthewidthofthepolylineonscreen,inpixels.Theextrusiondirectionformsanangleofα/2witheachofthenormalstothesegments;theamountoftheextrusionisw/(2cos(α/2)).
Weusevectoralgebratostudyvertexextrusion.Let'sconsiderthreeconsecutiveverticesa,bandcofapolylineandsupposethatwewanttocomputetheoffsetvectorthatdrivestheextrusionofpointb.Let'sconsidersegmentsabandbcseparately.WecomputethevectorthatgoesfromatobasΔ1=b-a;let(dx,dy)=Δ1/||Δ1||bethenormalizedvectorthatexpressesthedirectionofsegmentab;thenn1=(-dy,dx)isthenormaltothesegment.Normaln2canbecomputedinthesamewayforsegmentbc.Thedirectionoftheoffsetvectorcanthenbecomputedasthenormalizedaverageofn1andn2,i.e.offsetDir=(n1+n2)/||n1+n2||.
Wehavecharacterizedthedirectionoftheextrusion,butnotitsamount;intuitively,asharpturnshouldresultinalargerextrusionthanashallowone;also,weshouldextrudemoreifwewantthepolylinetoappearthicker.Let'snormalizethewidthofthepolylineto2sothatitsedgesjusttouchthetipsofthenormalvectors.Considertherighttrianglethathasn1(orn2)andoffsetassides.Itiseasytoseethatoffsetcos(α/2)mustequal1becausethenormalisunitlength;fromthiswecanconcludethelengthoftheoffsetvectorisexactly1/cos(α/2).Forapolylineofwidthwwehavetoscaletheoffsetbyafactorofw/2,whichleadstothew/(2cos(α/2))factorweintroducedatthebeginningofthissection.
Eachpolylinegraphicisprocessedonce,andtheextrusioninformationiscapturedinattributesstoredinavertexbuffer.Theextrudedverticesareconnectedintotrianglesusinganindexbuffer.Thesegeneratedbuffersdrivetherasterizationprocessanddonotneedtoberegeneratedateachframe.Weregeneratethemonlywhentheviewbecomesstationarytoaccountforthelimitedprecisionoffloating-pointnumbersonGPUs.SeeCustomWebGLlayerviewforadiscussionofthistechnique.VertexformatEachvertexoftheoriginalpolylineresultsintwoGPUvertices.Thesehaveapositionattribute,whichisthesameasthevertextheyoriginatefromandusethesameunitsofthespatialreference.GPUverticesalsohaveanoffsetattribute,whichisaunitvectorthatpointstowardtheextrudedGPUvertex.Thetwooffsetvectorsinapairareoppositeofeachother.Notethatwestore_normalized_offsetvectors.Thesearetheoffsetsthatwouldmakeapolylineappearhavingwidthequalto2pixels.Thevertexshaderisresponsibleforscalingtheoffsetattributesothepolylinerendersthicker.
Toapplyatexturetotheline,weneedtobuildaUVspaceonit.Thisrequirestwonewattributes:distance-Definesacoordinatethatincreasesalongtheline.Itismeasuredinthespatialreferenceunitsandstartsa0onthefirstvertexandincreaseswitheachpolylinesegment.side-Thisissetto+1or-1dependingontheedgeoftheextrudedpolylinetheGPUvertexbelongsto.Wewanteverylinetobeofadifferentcolor.Wecoulduseauniformtostorethecolor,butthiswouldpreventusfromdrawingalllinesusingasinglecall.Therefore,weencodethecolorasa32-bitRGBAattribute.
ThetriangulationalgorithmThepolylinetriangulationalgorithmisimplementedintheupdatePositions()methodandassumesthatallgraphicsstoredonthelayerhavepolylinegeometries.Westartbycountingthenumberofverticesandindicesrequired,whichwedobyiteratingonallgraphics.Forsimplicity,weonlytriangulatethefirstpathofeachgraphic.ApolylinewithNverticeshasN-1segments.Foreachvertexweneed2GPUverticesextrudedinoppositedirections.Foreachsegmentweneedtwotriangles(i.e.6indices).UsedarkcolorsforcodeblocksCopy 1
2
3
4
5
6
7
8
9
10
letvtxCount=0;
letidxCount=0;
for(leti=0;i
延伸文章資訊
- 1WebGL polyline tessellation with tesspathy - gists · GitHub
WebGL polyline tessellation with tesspathy. GitHub Gist: instantly share code, notes, and snippets.
- 2在WebGL 中繪製直線 - GetIt01
之前我們介紹了「在WebGL 中繪製地圖(多邊形篇)」,本文會介紹關於直線的繪製 ... https://github.com/mattdesl/polyline-miter-util/blob...
- 3Drawing Lines with WebGL - Scott Logic Blog
Connecting points on a chart with a line is one of the most basic uses of a charting library. Whe...
- 4Don't blend a polyline that has crossed lines with itself in webgl
I'm drawing two polylines (which are lines in the sample) in webgl with enabled blending. gl.unif...
- 5在WebGL 中绘制直线 - 知乎专栏
之前我们介绍了「 在WebGL 中绘制地图(多边形篇)」,本文会介绍关于直线 ... https://github.com/mattdesl/polyline-miter-util/blob/m...