Three.js efficient way to build a block terrain

what I’m trying to achieve is not easy to explain.
But what I’m trying to do is build a “boxy” terrain that represents an elevation grid.

The issue that I’m now facing is that three.js is doing a great smooth terrain.
From this example: https://threejs.org/examples/#physics_ammo_terrain

I manage to build an utility class that build a terrain.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>export class CubeGrid extends THREE.Group {
private terrain: THREE.Mesh<THREE.BufferGeometry, THREE.MeshPhongMaterial>;
private width: number;
private depth: number;
private terrainWidth = 30;
private terrainDepth = 30;
constructor(depth: number, width: number) {
super();
this.depth = depth;
this.width = width;
const terrainMaxHeight = 2;
const terrainMinHeight = 0;
const heightData = generateHeight(this.terrainWidth, this.terrainDepth, terrainMinHeight, terrainMaxHeight);
const geometry = new THREE.PlaneGeometry(this.width, this.depth, this.terrainWidth - 1, this.terrainDepth - 1);
geometry.rotateX(Math.PI / 2);
const vertices = geometry.attributes.position.array;
for (let i = 0, j = 0, l = vertices.length; i < l; i++, j += 3) {
// j + 1 because it is the y component that we modify
vertices[j + 1] = heightData[i];
}
geometry.computeVertexNormals();
const groundMaterial = new THREE.MeshPhongMaterial({ color: 0xc7c7c7, side: THREE.DoubleSide });
this.terrain = new THREE.Mesh(geometry, groundMaterial);
this.terrain.castShadow = false;
this.terrain.receiveShadow = false;
this.terrain.rotateX(Math.PI);
this.add(this.terrain);
}
updateGrid(data) {
const heightData = data;
const geometry = new THREE.PlaneGeometry(this.width, this.depth, this.terrainWidth - 1, this.terrainDepth - 1);
geometry.rotateX(Math.PI / 2);
const vertices = geometry.attributes.position.array;
for (let i = 0, j = 0, l = vertices.length; i < l; i++, j += 3) {
// j + 1 because it is the y component that we modify
vertices[j + 1] = heightData[i];
}
geometry.computeVertexNormals();
this.terrain.geometry.dispose();
this.terrain.geometry = geometry;
}
}
</code>
<code>export class CubeGrid extends THREE.Group { private terrain: THREE.Mesh<THREE.BufferGeometry, THREE.MeshPhongMaterial>; private width: number; private depth: number; private terrainWidth = 30; private terrainDepth = 30; constructor(depth: number, width: number) { super(); this.depth = depth; this.width = width; const terrainMaxHeight = 2; const terrainMinHeight = 0; const heightData = generateHeight(this.terrainWidth, this.terrainDepth, terrainMinHeight, terrainMaxHeight); const geometry = new THREE.PlaneGeometry(this.width, this.depth, this.terrainWidth - 1, this.terrainDepth - 1); geometry.rotateX(Math.PI / 2); const vertices = geometry.attributes.position.array; for (let i = 0, j = 0, l = vertices.length; i < l; i++, j += 3) { // j + 1 because it is the y component that we modify vertices[j + 1] = heightData[i]; } geometry.computeVertexNormals(); const groundMaterial = new THREE.MeshPhongMaterial({ color: 0xc7c7c7, side: THREE.DoubleSide }); this.terrain = new THREE.Mesh(geometry, groundMaterial); this.terrain.castShadow = false; this.terrain.receiveShadow = false; this.terrain.rotateX(Math.PI); this.add(this.terrain); } updateGrid(data) { const heightData = data; const geometry = new THREE.PlaneGeometry(this.width, this.depth, this.terrainWidth - 1, this.terrainDepth - 1); geometry.rotateX(Math.PI / 2); const vertices = geometry.attributes.position.array; for (let i = 0, j = 0, l = vertices.length; i < l; i++, j += 3) { // j + 1 because it is the y component that we modify vertices[j + 1] = heightData[i]; } geometry.computeVertexNormals(); this.terrain.geometry.dispose(); this.terrain.geometry = geometry; } } </code>
export class CubeGrid extends THREE.Group {
  private terrain: THREE.Mesh<THREE.BufferGeometry, THREE.MeshPhongMaterial>;
  private width: number;
  private depth: number;

  private terrainWidth = 30;
  private terrainDepth = 30;

  constructor(depth: number, width: number) {
    super();

    this.depth = depth;
    this.width = width;

    const terrainMaxHeight = 2;
    const terrainMinHeight = 0;

    const heightData = generateHeight(this.terrainWidth, this.terrainDepth, terrainMinHeight, terrainMaxHeight);

    const geometry = new THREE.PlaneGeometry(this.width, this.depth, this.terrainWidth - 1, this.terrainDepth - 1);
    geometry.rotateX(Math.PI / 2);
    const vertices = geometry.attributes.position.array;

    for (let i = 0, j = 0, l = vertices.length; i < l; i++, j += 3) {
      // j + 1 because it is the y component that we modify
      vertices[j + 1] = heightData[i];
    }
    geometry.computeVertexNormals();
    const groundMaterial = new THREE.MeshPhongMaterial({ color: 0xc7c7c7, side: THREE.DoubleSide });

    this.terrain = new THREE.Mesh(geometry, groundMaterial);
    this.terrain.castShadow = false;
    this.terrain.receiveShadow = false;
    this.terrain.rotateX(Math.PI);
    this.add(this.terrain);
  }

  updateGrid(data) {
    const heightData = data;

    const geometry = new THREE.PlaneGeometry(this.width, this.depth, this.terrainWidth - 1, this.terrainDepth - 1);
    geometry.rotateX(Math.PI / 2);
    const vertices = geometry.attributes.position.array;

    for (let i = 0, j = 0, l = vertices.length; i < l; i++, j += 3) {
      // j + 1 because it is the y component that we modify
      vertices[j + 1] = heightData[i];
    }
    geometry.computeVertexNormals();
    this.terrain.geometry.dispose();
    this.terrain.geometry = geometry;
  }
}

The main issue is that this does not have the “boxy” effect I’m looking for.

This is my result:

But I’m trying to do something like this:

In that example, the “data” I’d be using to create the terrain would be something like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const data = [
2,1,1,
4,1,1,
6,4,1
]
</code>
<code>const data = [ 2,1,1, 4,1,1, 6,4,1 ] </code>
const data = [
    2,1,1,
    4,1,1,
    6,4,1
]

How could I achieve that “boxy” effect?
I tried to simply create a lot of boxes, but I will be instantiating a loot of boxes (100×100 grid), and I dont want to loose performance.

Thanks in advance!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật