简体   繁体   English

马赛克画布2D游戏中的Terrains Union

[英]Terrains union in a mosaic canvas 2d game

I have a mosaic map made of hexagons in HTML 5 Canvas. 我有一个由HTML 5 Canvas中的六边形组成的镶嵌图。 What I would like to be able to do is that when two different terrains come together, they mix with each other. 我想做的是,当两个不同的地形汇聚在一起时,它们会相互融合。 I would like to move on from image 1 to image 2 我想从图片1转到图片2

Image 1 图片1

图片1

Image 2 图片2

这个

Without much context to what you are using: You could always create a hexagon with a custom made graphic that merges the two outside of your program, and then whenever you detect two different terrains touching, change them to this custom made graphic. 在没有太多背景信息的情况下:您总是可以使用自定义图形创建一个六边形,该图形将程序的两个外部合并,然后每当您检测到两个不同的地形时,将其更改为该自定义图形。

If you wish to explore going into a more detailed approach, then you may wish to consider looking at heightmaps . 如果您想探索一种更详细的方法,则不妨考虑查看heightmaps This article provides a nice description and tutorial, from which you can derive your own solution. 本文提供了一个不错的描述和教程,您可以从中获得自己的解决方案。 http://www.playfuljs.com/realistic-terrain-in-130-lines/ http://www.playfuljs.com/realistic-terrain-in-130-lines/

Here's the idea: take a flat square. 想法是:取一个正方形。 Split it into four sub-squares, and move their center points up or down by a random offset. 将其分成四个子正方形,然后将其中心点向上或向下移动一个随机偏移量。 Split each of those into more sub-squares and repeat, each time reducing the range of the random offset so that the first choices matter most while the later choices provide smaller details. 将每个分割成更多的子正方形并重复,每次减小随机偏移的范围,以便第一个选择最重要,而后面的选择则提供较小的细节。 - http://www.playfuljs.com/realistic-terrain-in-130-lines/ -http://www.playfuljs.com/realistic-terrain-in-130-lines/

A suggestion : Create more varied textures, and use heightmaps to decide what graphic should be shown in a hexagon piece. 建议 :创建更多不同的纹理,并使用高度图来确定应在六边形中显示的图形。

Otherwise... 除此以外...

Merging Images : Canvas - Combing two images, return one img html object? 合并图像画布- 合并 两个图像,返回一个img html对象?

Masking tile transitions for 2D tile maps 遮罩2D瓷砖贴图的瓷砖过渡

Composite operations 复合操作

For a 2D game you can use masks to blend different images. 对于2D游戏,您可以使用遮罩混合不同的图像。 A mask is just an image with the Alpha channels value determining how much of one image is added to another. 遮罩只是具有Alpha通道值的图像,该值确定将一个图像中的多少添加到另一图像中。

The 2D canvas context has a variety of composite modes that aid in all types of masking needs. 2D画布上下文具有多种组合模式,可满足所有类型的遮罩需求。 See MDN globalCompositeOperation for details. 有关详细信息,请参见MDN globalCompositeOperation

Use symmetry. 使用对称性。

You can build masks programmatically or create them by hand (hand drawn usually has a better look). 您可以通过编程方式来构建蒙版,也可以手动创建它们(手绘通常具有更好的外观)。 You can exploit the symmetry of the hexagon so that you need only 2 masks for a transition between two types of game tile. 您可以利用六边形的对称性,以便只需要2个遮罩即可在两种类型的游戏图块之间进行过渡。

The next image shows a single hexagon, that is created from 6 triangle (one is offset to show the single triangle unit. 下一个图像显示了一个由6个三角形创建的单个六边形(一个偏移量显示了单个三角形的单位。

These triangle are then cut in two again for A and B showing a transition from water to sand. 然后将这些三角形再次切成两半,用于A和B,表示从水到沙子的过渡。 (sorry the images are a little big, I forgot what resolution I was working in) (抱歉,图像有些大,我忘记了当时的分辨率)

在此处输入图片说明

Using the two parts and rotating and mirroring them (via context transforms) you can then build up a connected transition from one tile type to another. 使用这两个部分并旋转和镜像它们(通过上下文转换),您可以建立从一种瓦片类型到另一种瓦片类型的连接过渡。 In effect you are working with 12 small triangles rather than one large hexagon. 实际上,您正在使用12个小三角形而不是一个大六边形。

The image shows colours but you would have them as masks and create the hexagons as images as you need them (resolution, tile size, tile count, and amount of CPU time will determine how you construct the tiles. You may have to do it at start up and use up some memory, or if the game is low res and simple you could do it on the fly) 该图像显示颜色,但是您可以将它们用作遮罩,并根据需要将六边形创建为图像(分辨率,图块大小,图块计数和CPU时间量将决定如何构造图块。您可能必须在启动并消耗一些内存,或者如果游戏的分辨率较低且简单,则可以随时进行操作)

在此处输入图片说明

A in the above image are two As next to each other with one the mirror of the other. 上图中的A是两个As彼此相邻,另一个是镜子。

Remember the colors represent masks not actual image content, so for the example you would have 2 triangles (A,B) for yellow, 2 (A,B) for cyan, and two for blue as masks with alpha at 0 where you don't want the associated texture to show. 请记住,颜色代表的是蒙版,​​而不是实际的图像内容,因此对于本例,您将有2个三角形(A,B)代表黄色,有2个(A,B)代表青色,另外两个代表蓝色,其中alpha值为0,而您没有希望显示关联的纹理。

Each mask can be rotated 60 deg (PI / 3 radians) and mirrored to create the 12 possible corner positions. 每个蒙版可以旋转60度(PI / 3弧度)并镜像以创建12个可能的角位置。

Note that if you have tiles that a join with 3 or more different types you will have to create more complex transitions. 请注意,如果您的图块具有3种或更多不同类型的联接,则必须创建更复杂的过渡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM