Here’s a smaller version of my above code to create a textured terrain.
displayMode(FULLSCREEN)
function setup()
assert(craft, "Please include Craft as a dependency")
assert(OrbitViewer, "Please include Cameras (not Camera) as a dependency")
scene = craft.scene()
skyMaterial=scene.sky.material
skyMaterial.sky=color(137, 186, 223, 255)
skyMaterial.horizon=color(98, 142, 229, 255)
scene.sun.rotation=quat.eulerAngles(30,30,0)
v=scene.camera:add(OrbitViewer,vec3(0,0,0), 1000, 0, 2000)
v.camera.farPlane=3000
v.rx=15
texture=readImage("Dropbox:grass") -- your grass image
local size=1000
local groundHeight=200
local step=.2
h=craft.noise.perlin()
for x=-2,2,step do
for z=-2,2,step do
local y=h:getValue(x,0,z)
local p1=vec3(x*size,y*groundHeight,z*size)
y=h:getValue(x+step,0,z)
local p2=vec3((x+step)*size,y*groundHeight,z*size)
y=h:getValue(x+step,0,z+step)
local p3=vec3((x+step)*size,y*groundHeight,(z+step)*size)
y=h:getValue(x,0,z+step)
local p4=vec3(x*size,y*groundHeight,(z+step)*size)
createTile(p1,p2,p3,p4)
end
end
end
function draw()
update(DeltaTime)
scene:draw()
end
function update(dt)
scene:update(dt)
end
function createTile(p1,p2,p3,p4)
local c=color(255)
local pt=scene:entity()
pt.model = craft.model()
pt.model.positions={p1,p3,p4,p1,p2,p3}
pt.model.indices={1,2,3,4,5,6,6,5,4,3,2,1}
pt.model.colors={c,c,c,c,c,c}
pt.model.uvs={vec2(0,0),vec2(1,1),vec2(0,1),vec2(0,0),vec2(1,0),vec2(1,1)}
pt.material = craft.material("Materials:Basic")
pt.material.map=texture
end