Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Spiderbean · May 11, 2014 at 07:00 PM · raycastmathfvoxeldeformationmarching cubes

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.

alt text

 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;
         }
 }
terrain.png (92.1 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges