Jump to content

Hole in a terrain


waverider
 Share

Recommended Posts

Hy i've been trying to create a hole in a terrain like in this video, but i don't know what approach to take, i was thinking maybe i should use shader, but the only problem with using shader is that the vertices can't be specified. i'll appreciate any demo or idea that can help me out

 

Link to comment
Share on other sites

I have a function to make a hole and to close it:

var vertices_toremove = [];
var faces_toswitch = [];

function createHole(ground, pickInfo, radius, ctrl)
	{
        let indices_array = ground.getIndices();
        let positions_array = ground.getVerticesData(BABYLON.VertexBuffer.PositionKind);
        let index;
        let face_id;
        let safety = 10000;
        let pickpoint = pickInfo.pickedPoint;
        let current_v = BABYLON.Vector3.Zero();
        let worldmatrix = ground.getWorldMatrix();

		if(!ground.original_indices) {
			ground.original_indices = indices_array.slice(0);
		}
		for(let i = 0, countPositionsArray = positions_array.length/3; i < countPositionsArray; i++) {
			current_v.x = positions_array[i*3];
			current_v.y = positions_array[i*3+1];
			current_v.z = positions_array[i*3+2];
			current_v = BABYLON.Vector3.TransformCoordinates(current_v, worldmatrix);
			if(BABYLON.Vector3.Distance(pickpoint, current_v) < (radius)) {
				vertices_toremove.push(i);
			}
		}
		for(let i = 0, countVerticeToRemove = vertices_toremove.length; i < countVerticeToRemove; i++) {
			while((index = indices_array.indexOf(vertices_toremove[i], index+1)) != -1)
			{
				face_id = index - index%3;
				faces_toswitch.push(face_id);
				safety--;
				if(safety <= 0) { break; }
			}
			if(safety <= 0) { break; }
		}
		let countFacesToWitch = faces_toswitch.length;
		if(ctrl == false) {
			for(let i = 0; i < countFacesToWitch; i++) {
				indices_array[faces_toswitch[i]+2] = ground.original_indices[faces_toswitch[i]+1];
				indices_array[faces_toswitch[i]+1] = ground.original_indices[faces_toswitch[i]+2];
			}
		} else {
			for(let i = 0; i < countFacesToWitch; i++) {
				indices_array[faces_toswitch[i]+1] = ground.original_indices[faces_toswitch[i]+1];
				indices_array[faces_toswitch[i]+2] = ground.original_indices[faces_toswitch[i]+2];
			}
		}
		ground.setIndices(indices_array);
	}

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...