Normal de terraindata

Questions à propos du scripting. Hors Shader, GUI, Audio et Mobile.
darkwolf79
Messages : 4
Inscription : 20 Jan 2021 15:46

Normal de terraindata

Message par darkwolf79 » 20 Jan 2021 16:18

Salut à tous,
Je ne peux pas lire la normale d'un terrain
Faire un raycast J'ai le bon résultat, le lire à partir de terraindata me donne une valeur incorrecte.
Je joins du code et des captures d'écran

in Update :

Code : Tout sélectionner

 RaycastHit hit;
        Ray raggio = Camera.main.ScreenPointToRay(Input.mousePosition);
        Vector3 posiz = new Vector3(36f, 15f, 7f);
        if (Physics.Raycast(posiz + (Vector3.up*5),Vector3.down, out hit, Mathf.Infinity, MyGLB_E.layer_Terreno))
        {
            Terrain at = Terrain.activeTerrain;
            float normalizedX, normalizedY; Vector3 normale;
            hPoint = hit.point;
            normalizedX = posiz.x / at.terrainData.size.x ;
            normalizedY = posiz.z / at.terrainData.size.z ;
            normale = at.terrainData.GetInterpolatedNormal(normalizedX, normalizedY);
            hpointAngolo = at.terrainData.GetSteepness(normalizedX, normalizedY);          
            hString = "TERRAINDDATA:\n\rSP : " +  hpointAngolo.ToString() + "\n\rUP : " + Vector3.Angle(Vector3.up,normale ).ToString() + "\n\rDW : " + Vector3.Angle(normale, Vector3.down).ToString() + "\n\rFW : " +  Vector3.Angle(normale, Vector3.forward).ToString() + "\n\rBW : " + Vector3.Angle(normale, Vector3.back).ToString() + "\n\rRG : " + Vector3.Angle(normale, Vector3.right).ToString() + "\n\rLF : " + Vector3.Angle(normale, Vector3.left).ToString() + "\n\rNR : " +  normale;
            hN = normale;
            normale = hit.normal;
            hN0 = normale;
            hString0 = "RAYCAST\n\rUP : " +  Vector3.Angle(normale, Vector3.up).ToString() + "\n\rDW : " + Vector3.Angle(normale, Vector3.down).ToString() + "\n\rFW : " + Vector3.Angle(normale, Vector3.forward).ToString() + "\n\rBW : " + Vector3.Angle(normale, Vector3.back).ToString() + "\n\rRG : " + Vector3.Angle(normale, Vector3.right).ToString() + "\n\rLF : " + Vector3.Angle(normale, Vector3.left).ToString() + "\n\rNR : " + hit.normal;
 
        }
onGizmo :

Code : Tout sélectionner

 GUIStyle style = new GUIStyle(GUI.skin.label);
        style.alignment = TextAnchor.MiddleLeft;
        Handles.Label(hPoint, hString0, style);
        Handles.Label(hPoint + Vector3.right, hString, style);
        Gizmos.color = Color.green;
        Gizmos.DrawLine(hPoint,hPoint + ( hN * 1));
        Gizmos.color = Color.red;
        Gizmos.DrawLine(hPoint, hPoint + (hN0 * 1));
Result :
Immagine.jpg
Immagine.jpg (89.24 Kio) Consulté 1586 fois
réservoirs pour votre aide

Avatar de l’utilisateur
Max
Messages : 8771
Inscription : 30 Juil 2011 13:57
Contact :

Re: Normal de terraindata

Message par Max » 20 Jan 2021 19:24

Bonsoir,

Ben en testant rapidement avec un OnDrawGizmo() en déplaçant une petite sphère sur le terrain,, les résultats semblent bien ceux attendu.

Code : Tout sélectionner

    private void OnDrawGizmos()
    {
        Gizmos.color = Color.cyan;
        Vector3 n = terrain.terrainData.GetInterpolatedNormal(boule.transform.position.x / terrain.terrainData.size.x,
                                                                boule.transform.position.z / terrain.terrainData.size.z);
        Gizmos.DrawLine(boule.transform.position, boule.transform.position + n * 8.0f);
    }
Image
Pas d'aide par MP, le forum est là pour ça.
En cas de doute sur les bonnes pratiques à adopter sur le forum, consulter la Charte et sa FAQ

darkwolf79
Messages : 4
Inscription : 20 Jan 2021 15:46

Re: Normal de terraindata

Message par darkwolf79 » 20 Jan 2021 19:57

Merci pour votre réponse

C'est la même normale incorrecte (celle tirée de TerrainData)
Ligne rouge: raycast> exact
Ligne verte: terraindata> faux

Immagine.jpg
Immagine.jpg (200.75 Kio) Consulté 1570 fois

Avatar de l’utilisateur
Max
Messages : 8771
Inscription : 30 Juil 2011 13:57
Contact :

Re: Normal de terraindata

Message par Max » 20 Jan 2021 21:22

Là tu sembles travailler quasiment au niveau de chaque tri sur le terrain.
GetInterpolatedNormal, comme son nom l'indique, effectue une interpolation par rapport aux hauteurs entourant une positon normalisée, et non un calcul directement lié au tri qui serait pointé. Je pense que le soucis il est là dans ce cas.
Problème qui ne doit pas se retrouver avec le moteur physique, qui lui vient taper au niveau des tri.
Image
Pas d'aide par MP, le forum est là pour ça.
En cas de doute sur les bonnes pratiques à adopter sur le forum, consulter la Charte et sa FAQ

darkwolf79
Messages : 4
Inscription : 20 Jan 2021 15:46

Re: Normal de terraindata

Message par darkwolf79 » 21 Jan 2021 13:28

Merci, même si je comprends comment je peux interpoler dans cette direction ...
en tout cas je continuerai à faire des tests, il devrait y avoir une solution, même si maintenant je perds espoir et je pense que je vais devoir opter pour le Raycast

darkwolf79
Messages : 4
Inscription : 20 Jan 2021 15:46

Pente du terrain

Message par darkwolf79 » 21 Jan 2021 15:49

Salut à tous, j'ai ouvert une discussion sur les normales de terrain, mais je pense que pour trouver une solution concrète, j'ai besoin de comprendre pourquoi j'ai 2 angles de pente différents dans la carte.
J'ai besoin de comprendre sur le terrain où l'angle de la pente dépasse 35 degrés.
J'ai essayé d'utiliser la fonction terraindata.GetStepness, mais le résultat est incorrect.
Grâce à Raycast et à la fonction Vectro3.Angle, j'ai un résultat différent et beaucoup plus précis.
Depuis que j'ai vu diverses discussions où la fonction GetStepness est utilisée avec succès, je ne comprends pas pourquoi je dois avoir des résultats étranges.
Je joins du code et des captures d'écran.
Merci à tous!
angle.jpg
angle.jpg (187.62 Kio) Consulté 1525 fois
CODE ON UPDATE :

Code : Tout sélectionner

		RaycastHit hit;
		Ray raggio = Camera.main.ScreenPointToRay(Input.mousePosition);
		Vector3 posiz; // = new Vector3(36, 15f, 7);
		//if (Physics.Raycast(posiz + (Vector3.up * 5), Vector3.down, out hit, Mathf.Infinity, MyGLB_E.layer_Terreno))
		if (Physics.Raycast(raggio, out hit, Mathf.Infinity, MyGLB_E.layer_Terreno))
		{
			posiz = hit.point;
			Terrain at = Terrain.activeTerrain;
			float hh;
			float normalizedX, normalizedY; Vector3 normale;
			hPoint = hit.point;
			posizp = new Vector3(posiz.x, at.terrainData.GetInterpolatedHeight(posiz.x / at.terrainData.size.x, posiz.z / at.terrainData.size.z),posiz.z);
			normalizedX = posizp.x / at.terrainData.size.x ;
			normalizedY = posizp.z / at.terrainData.size.z ;
			normale = at.terrainData.GetInterpolatedNormal(normalizedX, normalizedY);
			
			hpointAngolo = at.terrainData.GetSteepness(normalizedX, normalizedY) ;			
			hString = "TERRAINDDATA (Green):\n\rSTEPNESS : " +  hpointAngolo.ToString()  + "\n\rNMap : " +  normale + "\n\rAT : " + posizp;
			hN = normale;
			normale = hit.normal;
			hN0 = normale;
			hString0 = "RAYCAST (Red)\n\rANGLE UP : " +  Vector3.Angle(normale, Vector3.up).ToString() +  "\n\rNMap : " + hit.normal + "\n\rAT : " + hit.point;
		}
CODE ON GIZMO :

Code : Tout sélectionner

		GUIStyle style = new GUIStyle(GUI.skin.label);
		style.alignment = TextAnchor.MiddleLeft;
		Handles.Label(hPoint, hString0 + "\n\r\n\r" + hString, style);
		Gizmos.color = Color.green;
		Gizmos.DrawRay(posizp, hN * 1);
		Gizmos.color = Color.red;
		Gizmos.DrawRay(hPoint, hN0 * 0.5f);

Répondre

Revenir vers « Scripting »