Question about 3D terrain (was: Hellow everyone!!!)

You can set the colors for each vertices. See the mesh documentation.

Here’s an example of setting colors. Play with the :color lines to see the changes. The first parameter is the vertice number, followed by the r,g,b values.

function setup()
    m2 = mesh()
    m2.vertices={vec2(10,100),vec2(WIDTH-10,100),vec2((WIDTH-20)/2,500)}
    m2:setColors(0,0,0,255)
    m2:color(1,255,0,0)
    m2:color(2,0,255,0)
    m2:color(3,0,0,255)
end

function draw()
    background(0)
    m2:draw()  
end

@dave1707 I mean how to setting color in your code,because in your code the color is random…maybe should I to change some part???

There could be thousands of vertices, so I’m not sure how you can set specific colors without adding a lot more code.

@steelswing I don’t know if this answers your question, but you could also say:

m2.colors={
color(255,0,0),
color(0,255,0),
...
}

Except the number of colors in the table have to match the number of vertices.

Hi All,

You can set colours using a LERP and img:get() or img:rawGet() then img:set() or img:rawSet() - just need a heightmap and the LERP.

Plenty of examples on the net.

Bri_G
:slight_smile:

@dave1707 yes? you are right there are thousand of vertices…but I want let my object like a thing…so maybe put a texture?but how to do that…

@steelswing a texture is much more difficult with the more vertices added. You need to set up texture coordinates and then assign a texture for the mesh. I think you should do a bit more forum searching. I think you’ll learn a lot.

@CamelCoder thank you but,it really need I to write …that…there have thousands of vertices…

Hi @steelwing,

You can imitate a LERP by setting up a table of colours versus heights and reading the grey image pixel values and then building a new image with the corresponding colour (or you could print over the heightmap.

Little demo here - shows you don’t need to specify each pixel colour just use a colour for a range of heights. The demo is a crude example but it shows the principle you can then link in colours to vertices.


displayMode(OVERLAY)
function setup()
    -- read in heightmap note must be png file
    Hmap = readImage("Dropbox:HMCSHeightmap")
    hw, hh = spriteSize(Hmap)
    Cmap = BWtoCol(Hmap, hw, hh)
end

-- This function gets called once every frame
function draw()
    cx, ch = WIDTH/2, HEIGHT/2
    -- This sets a dark background color 
    background(40, 40, 50)
    -- display sprites
    sprite(Hmap, cx,ch+ch/2)
    sprite(Cmap, cx, ch-ch/2)
end

function BWtoCol(hm,w,h)
    -- build new sprite in colour according to ranges below - mathematical 'lerp'
    local row, col
    local temp = image(w,h)
    for row = 1, w do
        for col = 1, h do
            r,g,b,a = hm:get(row, col )
            if r > 1 and r <= 25 then
                pix = color(32, 35, 160, 255)
            elseif r > 25 and r <= 50 then
                pix = color(26, 31, 226, 255)
            elseif r > 50 and r <= 75 then
                pix = color(30, 122, 238, 255)
            elseif r > 75 and r <= 100 then
                pix = color(47, 198, 232, 255)
            elseif r > 100 and r <= 125 then
                pix = color(216, 205, 44, 255)
            elseif r > 125 and r <= 150 then
                pix = color(103, 160, 51, 255)
            elseif r > 150 and r <= 175 then
                pix = color(72, 138, 36, 255)
            elseif r > 175 and r <= 200 then
                pix = color(53, 98, 39, 255)
            elseif r > 200 and r <= 225 then
                pix = color(165, 106, 24, 255)
            elseif r > 225 and r <= 250 then
                pix = color(250, 250, 249, 255)
            end
            temp:set(row,col,pix)
        end
    end
  -- saveImage("Documents:HMCSTerrain", temp)
    return temp
end

Note you can save the image of the Terrain by deleting the comments markers in the third line from the bottom (replace them when you’ve saved your image or you’ll keep overwriting the previous version - or change the filename given).

I used the heightmap supplied by @MMGames to demo this - note if you want to use this you’ll have to convert it to a .png as the image is a .gif (I used Gimp 2 for that).

Images of before and after follow - crude but should make the point.

Hi @steelswing,

Updated and thanks for the help.

You can give the terrain more definition by making the colour ranges smaller and varying the shade for each.

Bri_G
:slight_smile:

@Bri_G

@Bri_G hello,can you give that two pictures once again? Because I can’t see them

Hi @steelswing,

Sorry about that, I’ve had this problem before none of the described formats for posting images seem to work. Try holding your finger on the image until the local menu pops up then try saving it.

Anyone else - can you tell me the syntax for posting images on this forum - tried markdown and HTML tags but didn’t work.

Thanks,

Bri_G
:slight_smile:

p.s. Just follow my suggestion, download MMGames image, convert to png then copy and run the code.

Can you see it?

Hi @steelswing,

Worked fine - visible. What format have you used?

Bri_G
:slight_smile: :*

@Bri_G hello,I give you three pictures and show you how to do it(but…there are few…Chinese…but I think you also can understand )on the fist picture you need to click document button and click the button after “or” and then on second picture you need to choose source of the picture and on third picture and you can see I successfully uploaded two pictures :slight_smile:

@Bri_G hello…can you say it again please? I don’t understand what’s you mean…

@Bri_G and how use the code that you give me…because I changed something but it still doesn’t work

Hi @steelswing,

Whenever there is a code problem - always easier to paste in your code and describe what is going wrong and what you want to happen.

Bri_G
:slight_smile: