Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

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

edited January 2017 in Questions Posts: 56

Hellow ,I'm the new person to use codea,I have a problem ,how to make 3D terrain in codea ,please give me some idea or some help~~~~~ please ;)

«1

Comments

  • I mean I want to make a 3D game world ,so I need to make...for example mountain,lake,river,or something else......I really want to make and do then so can someone give me some idea or some examples ,please ~

  • SimeonSimeon Admin Mod
    Posts: 4,958

    How familiar are you with Codea? If you're just starting out you might be better off trying something a little simpler: something like drawing some shapes that move when you touch them, and then maybe try making a 2D top-down game world with terrain, rivers and trees.

    3D is possible (and you would do it with mesh) but it's more involved.

  • dave1707dave1707 Mod
    Posts: 7,605

    @steelswing Tell us your coding experience. Obviously you're new to Codea, but have you done any coding in any other languages. How we answer your questions would depend on your experience.

  • @Simeon @dave1707
    Thank you to answer me ,I made a mistake,I'm not a new person of codea but is the "codea talk" ,befor I have tried some simple things, as you said @Simeon .but I haven't done any coding in any other language,include haven't learnt any other language

  • dave1707dave1707 Mod
    Posts: 7,605

    @steelswing You said you tried simple things. Have you tried doing anything with meshes, or anything with 3D. For 3D terrain, think of a very large checker board where you can raise or lower by different amounts where the squares meet each other to give the effect of hills or valleys. Each of the squares could have a different mesh texture or the whole board could be overlayed with one large mesh. Even after you create the terrain, then you have to write the code to move the camera around the terrain. It takes a lot of work and can be done kind of easy if you have the experience, but if you don't it could be almost impossible even if you have examples. So my suggestion is to play with meshes and simple 3D code until you're comfortable with it.

  • @dave1707 thanks for your suggestion , I haven't done any things about 3D or mesh~~ so can you give me some course of study or teach me some about thatbecause the reference in codea is.........hard to understand~~~ so,can you help me :) ?

  • dave1707dave1707 Mod
    Posts: 7,605

    @steelswing See the personal message I sent you.

  • dave1707dave1707 Mod
    Posts: 7,605

    @steelswing Here's a small example of a 3D terrain. This just shows a 3 by 3 array, and the pos slider lets you raise or lower one portion of the terrain. A real terrain would have a larger array and the high and low points would be calculated when the terrain is created. Also, I'm using a small single texture over and over, but multiple textures could be used to create different effects. A large single texture that contains a landscape could be used to cover the whole array.

    function setup()
        parameter.integer("eyeX",-4000,4000,0)
        parameter.integer("eyeY",-100,900,600)
        parameter.integer("eyeZ",-4000,4000,2000)
        ground={}
        ss=200
        ss2=ss/2
        buf={}
        for x=-1,1 do
            for z=-1,1 do
                table.insert(ground,chunks(x*ss,0,z*ss))
            end
        end
        parameter.integer("pos",-400,200,-100,adjust)
    end
    
    function draw()   
        background(40, 40, 50)
        fill(255)
        perspective()
        camera(eyeX,eyeY,eyeZ, 0,0,0,0,1,0)
        for a,b in pairs(ground) do
            b:draw()
        end
    end
    
    chunks=class()
    
    function chunks:init(x,y,z)
        v={ vec3(-ss2+x, -ss2+y, -ss2+z),   -- front left
            vec3( ss2+x, -ss2+y, -ss2+z),   -- front right 
            vec3( ss2+x, -ss2+y,  ss2+z),   -- back right    
            vec3(-ss2+x, -ss2+y,  ss2+z)    -- back left
          }
        t={ vec2(0,0),vec2(1,0),vec2(0,1),vec2(1,1) }
        self.ms=mesh()
        self.ms.vertices={ v[1],v[2],v[3],v[1],v[3],v[4] }
        self.ms.texCoords={ t[1],t[2],t[4],t[1],t[4],t[3] } 
        self.ms.texture="Platformer Art:Block Brick"
        table.insert(buf,self.ms:buffer("position"))
    end
    
    function chunks:draw()
        self.ms:draw()
    end
    
    function adjust()
        buf[1][3]=vec3(buf[1][3].x,pos,buf[1][3].z)
        buf[1][5]=vec3(buf[1][5].x,pos,buf[1][3].z)
        buf[2][2]=vec3(buf[2][2].x,pos,buf[2][2].z)
        buf[4][6]=vec3(buf[4][6].x,pos,buf[4][6].z)
        buf[5][1]=vec3(buf[5][1].x,pos,buf[5][1].z)
        buf[5][4]=vec3(buf[5][4].x,pos,buf[5][4].z)
    end
    
  • @dave1707 thank you and thanks for your suggestion,I will learn more ,and try to make more things and getting better,thank you :)

  • dave1707dave1707 Mod
    Posts: 7,605

    @steelswing I guess I didn't ask what kind of 3D terrain you wanted to create. Was it a smooth landscape with slight hills and valleys, or a block landscape made up from a bunch of cubes. If it's cubes, then it would be easier than the hills and valleys.

  • @dave1707 hello Dave ,my goal is to create a smooth landscape ,like you said ,mountain valleys...and so on,because that is for kind of RTS game ,but it is not very hurry, it is my final goal.

  • @steelswing do a forum search, I remember seeing the code for a 3D terrain with mountains and hills, etc. and I also remember seeing one called 3D fractal mountains

  • @CamelCoder oh~really?thank you ~

  • @CamelCoder excuse me ,where can I do the forum search?

  • dave1707dave1707 Mod
    Posts: 7,605

    @steelswing The upper right of the screen, just above New Discussion.

  • @dave1707 ok ,thank you

  • But.....I can't find anything........only a few websites???

  • @dave1707 and yesterday you asked me what kind of terrain I want to create ...........en~why do you want to ask me??? I mean en~~~~

  • dave1707dave1707 Mod
    Posts: 7,605

    @steelswing Just curious. The creation of a smooth terrain is different than a cube terrain.

  • @dave1707 oh ok,I konw thank you ,now I'm learning .so if in future I have questions can I ask you?? :)

  • dave1707dave1707 Mod
    Posts: 7,605

    @steelswing That's the whole purpose of this forum, to ask and answer questions. It's suggested to search the forum before asking questions because probably 95% of the information or examples are already there. Plus, if you search the forum, you don't have to wait for an answer and you might even find something useful that you weren't searching for.

  • @but.......I only can find some web....sites ?

  • dave1707dave1707 Mod
    Posts: 7,605

    @steelswing I'm not saying you can't ask questions or everything you want to know will be found in a forum search. I'm just saying to try a forum search and see what you find before asking questions. Sometimes when I answer a question, I search the forum first. There's about 5 years of information stored there.

  • @dave1707 oh~thank you and one more question ~ why my title is changed?0.0

  • dave1707dave1707 Mod
    Posts: 7,605

    @steelswing I'm not sure who changed it, probably one of the other Mods if you didn't. It's was probably changed to better reflect the information contained in the discussion.

  • Oh~ thank you very much,I'm very happy :) ,

  • edited January 2017 Posts: 1,226

    Hi @steelswing,

    I am interested in the working on the same area - I find 3D a little difficult but am slowly getting to understand it. I find the hardest thing visualizing the scene.

    There is a wealth of code which has been provided by many members over the years - mostly easy to find but there are some gems which are not obvious.

    Probably the best source is @ignatz - both for his website and the volumes he has written to support Codea development. Here is a link to his 3D summary page on his website. Small steps will get you there in the end.

    Ignatz 3D

    I will post a few of the better links for you when I can dig them up.

    Bri_G

    :)

  • Posts: 105

    Here is an implemention:


    --# Main function setup() --Settings must have sin,cos,rad=math.sin,math.cos,math.rad --UserDefined vars TerrainRes=64 --Resolution of terrain TerrainSize=2 --Size in x and z TerrainHeight=4 --Movement Angle=vec2(0,0) parameter.number("X",-1,2,1) parameter.number("Y",-1,2,1) parameter.number("Z",-1,2,0) --Variables Ires=TerrainSize/TerrainRes EpicTerrain=image(TerrainRes/2,TerrainRes/2) setContext(EpicTerrain) sprite("Documents:MT2") --IMPORTANT! Your heightmap here!! setContext() --Meshing local X,Index,px,py,y1,y2,y3,y4 m=mesh() verts={} for x=1,TerrainRes-1 do X=(x-1)*(TerrainRes-1)*6+1 px=(x-1)/TerrainRes px=px*TerrainSize for y=1,TerrainRes-1 do Index=X+(y-1)*6 py=(y-1)/TerrainRes py=py*TerrainSize --All the heights y1=color(EpicTerrain:rawGet(x,y)).x/255*TerrainHeight y2=color(EpicTerrain:rawGet(x+1,y)).x/255*TerrainHeight y3=color(EpicTerrain:rawGet(x,y+1)).x/255*TerrainHeight y4=color(EpicTerrain:rawGet(x+1,y+1)).x/255*TerrainHeight --Constructing the vertices verts[Index]=vec3(px,y1,py) verts[Index+1]=vec3(px+Ires,y2,py) verts[Index+2]=vec3(px+Ires,y4,py+Ires) verts[Index+3]=vec3(px+Ires,y4,py+Ires) verts[Index+4]=vec3(px,y3,py+Ires) verts[Index+5]=vec3(px,y1,py) end end m.vertices=verts m.shader=shader(MSV,MSF) m.shader.TerrainSize=TerrainSize*1.5 end function draw() background(0, 255, 252, 255) perspective(60,WIDTH/HEIGHT,0.01,60) RR=RotationRate Angle.x=Angle.x+RR.y*6*DeltaTime Angle.y=Angle.y+RR.x*6*DeltaTime Eye=vec3(X,Y,Z)+vec3(sin(Angle.x),Angle.y,cos(Angle.x)):normalize() camera(X,Y,Z,Eye.x,Eye.y,Eye.z) m:draw() m.shader.Player=vec3(X,Y,Z) --2D ortho() resetMatrix() viewMatrix(matrix()) sprite(EpicTerrain,0,0,300,300) end MSV=[[ uniform mat4 modelViewProjection; attribute vec4 position; varying vec3 Pos; void main() { Pos=position.xyz; gl_Position=modelViewProjection*position; } ]] MSF=[[ precision highp float; varying vec3 Pos; uniform vec3 Player; uniform float TerrainSize; void main() { gl_FragColor=vec4(pow(vec3(length(Player-Pos)/TerrainSize),vec3(0.45)),1.); } ]]

    I recommend this heightmap (Save the image and inport it into codea. Use it in the "setContext")
    https://docs.unrealengine.com/udk/Three/rsrc/Three/TerrainHeightmaps/HMCSHeightmap.gif

  • edited January 2017 Posts: 1,226

    Hi @MMGames,

    Neat, and such a short length of code. Thanks, that's given me something to play with for a while. Already tried it with bigger height maps.

    Bri_G
    :)

  • @MMGames thanks for your implementation.that give me some idea.it is very good,thank you very much :) :)

  • edited January 2017 Posts: 1,226

    Hi @steelswing,

    Best way to learn is in a good community, which you have with Codea in the forum, I'll keep firing in links and code.

    Also, it is important to learn by trial and error and experience with the code - you need to learn progressively. I'm still learning - the code posted by @MMGames gave me a nudge in the right direction - I'd totally forgotten about rawGet() and rawSet() for retina iPads !!

    Lay out a plan for how you want to progress - I'm currently working on terrrain. Where next?

    Believe me I've seen many variants on coding in Codea - mostly good and all bring something new to the table (many above my head).

    Good luck with it.

    Bri_G
    :)

  • @Bri_G oh~thanks for your suggestion,I will learn more about that,and how is your level on codea now?? :)

  • Posts: 1,226

    Hi @steelswing,

    I've got several partially unfinished projects and one or two which I'm keen on which I tackle for so long then shelve. Don't get much time to program nowadays - too many other distractions - I'd say I'm about middle of the pack as far as coding goes (still lots to learn).

    Bri_G
    :)

  • @Bri_G and do you now how to make a 3D object for example a tank or maybe some simple things,because they have some part maybe are smooth or in other words means without edges and corners.

  • dave1707dave1707 Mod
    Posts: 7,605

    You could use another app, Polygonify, to create 3d objects. See this discussion.

    https://codea.io/talk/discussion/7455/3d-tool-alert
    
  • @dave1707 thank you very much,and have you downloaded it?

  • @dave1707 Could you lend me Apple ID,I want to download it ............... :) :)

  • dave1707dave1707 Mod
    Posts: 7,605

    @steelswing I downloaded Polygonify and played with it. In the link I gave you above, I wrote a program that will take the Polygonify vertices and convert them to a format that Codea can use. The instructions are in the discussion of the link.

  • @dave1707 I downloaded another application also for make 3D object ,so it is the same way to use it ?I mean use your code........the name of the application I downloaded is called"123D design" maybe you can have a look :) :)

  • edited January 2017 Posts: 1,226

    Hi @steelwing,

    @dave1707 is right - you can get hold of many free apps for the PC/Mac or iPad to build models - in many formats but usually .obj is used here. You can't get 123D Sculpt+ now the website re-directs you to an on-line package called Tinkercad.

    Here are the links if you are interested:

    Autodesk 123D
    Sculpt3D+
    Tinkercad

    Hope that helps - but search online for other packages you may find other free ones that meet your needs better. I find them all a little fiddly to use.

    I should point you back to the @ignatz website as he has a number of objects that he has used in his programming, like a spitfire and a mescherschmidt (pardon the spelling) and others he has built himself like tanks and warships.

    Have fun.

    Bri_G
    :)

  • @Bri_G hello,and thanks for your help,now I'm using the application called "123D design",it is downloaded from App Store ,it is free ,but I'm not sure it can use in codea,so maybe you can search it in App Store and have a look :)

  • dave1707dave1707 Mod
    Posts: 7,605

    @steelswing I never tried 123D design so I don't know if my program will work with it. Since I have no plans to use it, I doubt if I'll take the time to try it.

  • @dave1707 ok,thank you......so,could you lend me your Apple ID ,I want to download polygonify,please~ :)

  • dave1707dave1707 Mod
    edited January 2017 Posts: 7,605

    I think Polygonify is free. I don't remember paying anything for it.

  • @dave1707 thanks,I'm wrong ,polygonify is free,but how to send an e-mail ? I tried many times to click Export,but when I click it the application will flashback

  • dave1707dave1707 Mod
    Posts: 7,605

    @steelswing When I tap export, it brings up an email page where I can enter my email address and send it to myself. If that's not happening for you, then try looking thru the instructions for Polygonify to see what's wrong.

  • @dave1707 there no instruction to display ,when I tap export ,it will directly to close the application,do you understand what I mean? I'm very worried about that,I use iPad mini

  • dave1707dave1707 Mod
    Posts: 7,605

    @steelswing Here's the steps that I do.

    1. Start Polygonify.
    2. Tap the + in the upper right.
    3. Enter a name for the model, press OK.
    4. A cube is shown, that's good enough for now.
    5. Tap the menu icon top left.
    6. Tap models.
    7. Tap the i in the circle for your model.
    8. Tap export.
    9. An email page comes up where you can enter your email address.
    10. Enter your email address and tap send.
  • @dave1707 thank you ,but I konw the hole step,the problem is on step number 8,it always stop at step number 8, I don't know what going on,but always stop at that step..................and what equipment do you use now?

Sign In or Register to comment.