- Home /
Marching cubes raycasting issue
Im trying to implement terrain deformation in my marching cubes algorithm. Somehow it doesn´t work and randomly leaves out raycasts. I started debugging and using all sorts of normals and other stuff to correct the raycasting. It just doesn´t work! Here I use the Debug.DrawLine . You can clearly see that the Mathf.FloorToInt works(in order to get 3D array index). But it still randomly not work there.
using UnityEngine;
using System.Collections;
public class PlayerIO : MonoBehaviour {
public int viewRange;
public float intRange;
public Chunk ChunkPrefab;
public static int width {
get { return World.thisWorld.chunkWidth; }
}
public static int height {
get { return World.thisWorld.chunkHeight; }
}
void Start() {
if(viewRange == 0) viewRange = 30;
StartCoroutine(UpdatePos1());
StartCoroutine(UpdatePos2());
}
void Update() {
if(Input.GetMouseButtonDown(0)) {
StartCoroutine(RemoveBlock());
}
}
IEnumerator RemoveBlock() {
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray,out hit, intRange)) {
Vector3 p = hit.point;
Chunk chunk = Chunk.FindChunk(p);
float x=p.x-chunk.transform.position.x, y =p.y - chunk.transform.position.y,z =p.z-chunk.transform.position.z;
int a = Mathf.FloorToInt(x),b= Mathf.FloorToInt(y),c=Mathf.FloorToInt(z);
Debug.Log(p.normalized);
Debug.DrawLine(ray.origin, hit.point, Color.green, 15 * Time.deltaTime);
Debug.DrawLine(hit.point, new Vector3(a,b,c) + chunk.transform.position, Color.red, 15 * Time.deltaTime);
if(x < width -1 && z < width -1) {
chunk.data[a,b,c]=0f;
chunk.data[a+1,b,c]=0f;
chunk.data[a,b,c+1]=0f;
chunk.data[a+1,b,c+1]=0f;
}else if(x == width - 1 && z == width -1 ) {
chunk.data[a,b,c]=0f;
Chunk chunk1 = Chunk.FindChunk(new Vector3(x+chunk.transform.position.x+2,y+chunk.transform.position.y,z+chunk.transform.position.z+2));
chunk1.data[a-width,b,c-width] = 0f;
StartCoroutine(chunk1.UpdateAt());
Chunk chunk2 = Chunk.FindChunk(new Vector3(x+chunk.transform.position.x+2,y+chunk.transform.position.y,z+chunk.transform.position.z));
chunk2.data[a-width,b,c]=0f;
StartCoroutine(chunk2.UpdateAt());
Chunk chunk3 = Chunk.FindChunk(new Vector3(x+chunk.transform.position.x,y+chunk.transform.position.y,z+chunk.transform.position.z + 2));
chunk3.data[a,b,c-width] = 0f;
StartCoroutine(chunk3.UpdateAt());
StartCoroutine(chunk.UpdateAt());
}else if(x == width - 1 ) {
chunk.data[a,b,c]=0f;
chunk.data[a,b,c+1]=0f;
Chunk chunk1 = Chunk.FindChunk(new Vector3(x+chunk.transform.position.x+2,y+chunk.transform.position.y,z+chunk.transform.position.z));
chunk.data[a-width,b,c]=0f;
chunk.data[a-width,b,c+1]=0f;
StartCoroutine(chunk1.UpdateAt());
}else if(z == width - 1 ) {
chunk.data[a,b,c]=0f;
chunk.data[a+1,b,c]=0f;
Chunk chunk1 = Chunk.FindChunk(new Vector3(x+chunk.transform.position.x,y+chunk.transform.position.y,z+chunk.transform.position.z+2));
chunk.data[a,b,c-width]=0f;
chunk.data[a+1,b,c-width]=0f;
StartCoroutine(chunk1.UpdateAt());
}
StartCoroutine(chunk.UpdateAt());
if(x == width -1 || z == width -1) {
StartCoroutine(chunk.UpdateAt());
}
if(x <= 1) {
Chunk atchunk = Chunk.FindChunk(hit.point - new Vector3(2,0,0));
StartCoroutine(atchunk.UpdateAt());
}
if(z <= 1){
Chunk atchunk = Chunk.FindChunk(hit.point - new Vector3(0,0,2));
StartCoroutine(atchunk.UpdateAt());
}
if(z <= 1 && x <=1){
Chunk atchunk = Chunk.FindChunk(hit.point - new Vector3(2,0,2));
StartCoroutine(atchunk.UpdateAt());
}
}
yield return 0;
}
IEnumerator UpdatePos1 () {
while(viewRange > 0){
for(float x = transform.position.x - viewRange;x < transform.position.x + viewRange; x += width ) {
for(float z = transform.position.z - viewRange;z < transform.position.z + viewRange; z += width ) {
Vector3 pos = new Vector3(x,0,z);
pos.x = Mathf.Floor(pos.x / (float)width) * width;
pos.z = Mathf.Floor(pos.z / (float)width) * width;
Vector3 delta = pos - transform.position;
delta.y = 0;
if(delta.magnitude > viewRange) continue;
Chunk chunk = Chunk.FindChunk(pos);
if(chunk != null) continue;
chunk = (Chunk)Instantiate(ChunkPrefab, pos, Quaternion.identity);
yield return new WaitForEndOfFrame();
}
}
yield return new WaitForSeconds(0.5f);
}
yield return 0;
}
IEnumerator UpdatePos2 () {
while(viewRange > 0){
for (int a = 0; a < Chunk.chunks.Count; a++) {
Vector3 pos = Chunk.chunks[a].transform.position;
Vector3 delta = pos - transform.position;
delta.y = 0;
if(delta.magnitude < viewRange) continue;
Destroy(Chunk.chunks[a].gameObject);
yield return new WaitForEndOfFrame();
}
yield return new WaitForSeconds(3);
}
yield return 0;
}
}
Your answer
Follow this Question
Related Questions
Marching Cubes terrain deformation problem 2 Answers
How to remove small & thin triangles from a marching cubes voxel engine? 2 Answers
How would I find the closest Vector3 point to a given Ray? 2 Answers
Where should I start on creating a procedural generated terrain that can be deformed? 0 Answers
How can I smoothen my cubic mesh using marching cubes? 0 Answers