waverider

Hole in a terrain

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

 

Share this post


Link to post
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);
	}

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.