@dave1707 oh ok,I konw thank you ,now I’m learning .so if in future I have questions can I ask you??
@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 ?
@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.
@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 ,
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.
I will post a few of the better links for you when I can dig them up.
Bri_G
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
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
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??
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.
You could use another app, Polygonify, to create 3d objects. See this discussion.
https://codea.io/talk/discussion/7455/3d-tool-alert
@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.