Questions in topic: "triangles"
http://answers.unity.com/questions/topics/single/1360.html
The latest questions for the topic "triangles"How to make sure two meshes have the same vertex count
http://answers.unity.com/questions/1617917/how-to-make-sure-two-meshes-have-the-same-vertex-c.html
I am creating meshes that have the same vertex counts in blender but once in unity the mesh I have altered further gains more vertices than the less altered mesh (think of a sphere and a spikey sphere where the spiky sphere gains vertices) I know that unity ads UV vertices and such (they have the same normal count) but is there a way to keep their vertex count the same?
Edit: for further clarity the two meshes have the same vertex count in blender but not in unitymeshverticesnormalstrianglesuvsMon, 01 Apr 2019 05:56:03 GMTDejojotheawsomeBest way to transition one mesh to another with the option to change the target mesh
http://answers.unity.com/questions/1617769/best-way-to-transition-one-mesh-to-another-with-th.html
I can't think of a better way to word it but I understand that the title may be confusing. What I am looking for is a program that will take the mesh of, for example, a cube and begin to transform it into the mesh of a sphere over a set period of time. I also want the ability to change the target mesh, so if halfway through the spherical transformation the user wished to turn the current mesh into a pyramid, the half sphere half cube would then start to transition into the pyramid. I had an idea of simply increasing and decreasing the number of triangles on the current shape and then moving the vertices of the shape toward the direction of the new vertices but I feel like that could result in issues, and I am fairly new to the mesh class. I really just want to make sure that my method is not going to be horribly slow or just plain wrong. I hope that I have provided enough information and thank you for you time. <3meshruntimedynamicverticestrianglesSun, 31 Mar 2019 16:12:01 GMTDejojotheawsomeHow to find connected mesh triangles?
http://answers.unity.com/questions/1615363/how-to-find-connecting-mesh-triangles.html
I have an object that I'm raycasting to, and when I hit a button I want to start a "signal" on the mesh surface, starting from the triangle that the raycast hit. The signal is just vertex coloring: I want the first triangle to turn yellow and then I want yellow to spread outwards from the triangle to other triangles that are touching it, and then to the triangles that are touching those, etc. [Like so](https://gfycat.com/unsungshockingibis.gif).
<br>
<br>
My problem is that the triangle array isn't really sorted like that. Triangles that share a face share two vertices. I printed out the triangle array of the default sphere mesh, and it seems like touching triangles are *near* each other in the triangle array, but not necessarily and not predictably near each other.
<br>
<br>
Is there a convenient way to do this, besides for parsing through the entire triangle array and looking for matches? I'd like to do this with a 14,000+ vertex geometry eventually, so if you can't rely on the touching triangles to be near each other in the array, that would be quite a performance cost to find the touching triangles.scripting problemraycastmeshverticestrianglesSat, 23 Mar 2019 19:54:30 GMTtuf91497Quick way to get touching triangles?
http://answers.unity.com/questions/1613424/quick-way-to-get-touching-triangles.html
I have a mesh with unsorted vertices/triangles. I would like to send a color animation through the object that looks like a color "spreads" through the object by a few triangles at a time. Is it feasible to sort the triangle array somehow, or make a data structure for each triangle that stores the other triangles that it shares at least one vertex with?scripting problemverticesmeshestrianglesSun, 17 Mar 2019 17:06:12 GMTtuf91497Issues with Low Poly Shader on Mesh
http://answers.unity.com/questions/1602040/issues-with-low-poly-shader-on-mesh.html
I am trying to make a randomly generated mesh with a low poly look to it, and have made a shader using this tutorial: https://www.youtube.com/watch?v=e3oYQL0oDEs. I also created a script to procedurally generate a mesh. Unfortunately, when I run it, this happens:
![alt text][1]
My issue is the random black triangles everywhere, and when I view the scene and pan around while its running, the triangles change in color. Ideally, I would want the mesh to have this same look, except for without the random black triangles.
The project uses a Lightweight Render Pipeline and uses the script pasted below. I made the script it's own object in the scene and set the xSize and zSize to 20. I also made a PBR Shader with this ShaderGraph: ![alt text][2]
Any help is appreciated. Here is the Mesh Generator Script:'
public int xSize = 10;
public int zSize = 10;
Mesh mesh;
Vector3[] vertices;
int[] triangles;
Color[] colors;
float minTerrainHeight;
float maxTerrainHeight;
void Start()
{
mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;
CreateShape();
UpdateMesh();
}
void CreateShape()
{
vertices = new Vector3[(xSize + 1) * (zSize + 1)];
for (int i = 0, z = 0; z <= zSize; z++)
{
for (int x = 0; x <= xSize; x++)
{
float y = Mathf.PerlinNoise(x * .2f, z * .2f) * 3.5f;
vertices[i] = new Vector3(x, y, z);
if (y > maxTerrainHeight)
{
maxTerrainHeight = y;
}
if (y < minTerrainHeight)
{
minTerrainHeight = y;
}
i++;
}
}
triangles = new int[xSize * zSize * 6];
int vert = 0;
int tris = 0;
for (int z = 0; z < zSize; z++)
{
for (int x = 0; x < xSize; x++)
{
triangles[tris + 0] = vert + 0;
triangles[tris + 1] = vert + xSize + 1;
triangles[tris + 2] = vert + 1;
triangles[tris + 3] = vert + 1;
triangles[tris + 4] = vert + xSize + 1;
triangles[tris + 5] = vert + xSize + 2;
//yield return new WaitForSeconds(0.1f);
vert += 1;
tris += 6;
}
vert += 1;
}
Color currentColor = new Color();
colors = new Color[vertices.Length];
for (int i = 0, z = 0; z <= zSize; z++)
{
for (int x = 0; x <= xSize; x++)
{
if (i % 3 == 0)
{
float side = Random.Range(0f, 0.6f);
float opac = Random.Range(0.7f, 1f);
currentColor = new Color(side, opac, side, 1);
}
colors[i] = currentColor;
i++;
}
}
}
void UpdateMesh()
{
mesh.Clear();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.colors = colors;
mesh.RecalculateNormals();
}
'
[1]: /storage/temp/133165-screen-shot-2019-02-15-at-24914-am.png
[2]: /storage/temp/133167-screen-shot-2019-02-15-at-24835-am.pngmeshshaderstriangleslow polyFri, 15 Feb 2019 11:16:02 GMTDixitsHow can I calculate the angle between two adjacent triangles in 3D? (for detecting concave or convex triangles)
http://answers.unity.com/questions/1600038/how-can-i-calculate-the-angle-between-two-adjacent.html
Hi guys,
I am trying to calculate the angle between two triangles in 3D space.
Let's assume that triangle one consists of three points
`P1=(x1,y1,z1), P2=(x2,y2,z2), and **P3=(x3,y3,z3).**`
Triangle two consists also of three points
`P1=(x1,y1,z1), P2=(x2,y2,z2), and **P3=(x4,y4,z4).**`
Thank you in advance!!3danglestrianglestrigonometrySat, 09 Feb 2019 13:06:35 GMTkliment5Is there a premade triangle asset?
http://answers.unity.com/questions/1594750/is-there-a-premade-triangle-asset.html
Hi! I'm new to Unity and my project is to make a prefab out of primitives. So, I decided to try to make a house, I was able to do it mostly with cubes of varying sizes, but when it comes to the roof, if I want the triangular shaped kind I need a triangle for the front and back of the roof. I read that you need a modeling program to make it. I am not a fan of that situation since I have never used one, is there really no way to create a triangle in Unity without modeling software? It just seems so strange they give you Cubes and Spheres and even Capsules but NOT Triangles, doesn't? Even more strange is that the Asset Store seems to be devoid of anything triangles when I tried looking for one.triangles3d modelSat, 26 Jan 2019 22:00:21 GMTajrosenbachMesh adaption
http://answers.unity.com/questions/1592831/mesh-adaption.html
Hey guys, I'm currently working on a project where I import a mesh through code (C#) into my application. The triangles on the mesh are all in different sizes, meaning the edges and the area of the triangles are quite different. See the image below. Does anyone know a way to shape the triangles uniformly, while approximately keeping the shape of the whole mesh? Appreciate your answers!
![alt text][1]
[1]: /storage/temp/131679-mesh.jpgc#meshverticestrianglesTue, 22 Jan 2019 07:51:17 GMTShorayn_3d model with lower poly count than another model causing tons of lag
http://answers.unity.com/questions/1592674/3d-model-with-lower-poly-count-than-another-model.html
Hi, I have a 3d model of an infantryman that has about 45k triangles. When I spawn them, my fps drops a lot (if I spawn like 10, it drops from over 100 fps down to around 40 fps). But none of my other models cause this much lag. I have some tank models that have over 100k triangles, and they cause minimal drop in the fps. I checked the materials and bones on the infantryman, and came to a definite conclusion that it is the polycount, as when I lower its triangles to around 10k, I get minimal fps drop. So why would a model that has a lower triangles and vert count than a lot of my other models cause a worse drop in fps? When I spawn the infantry, my top CPU usage in the profiler is usually gfx.waitforpresent, which I read is the CPU waiting for the GPU. Any help would be much appreciated! Thanks! :)fpslagvertices3d modelstrianglesMon, 21 Jan 2019 19:41:18 GMTjobo22Want to create a new mesh (single triangle) "cloned" from an already existing triangle ....
http://answers.unity.com/questions/1592186/want-to-create-a-new-mesh-single-triangle-cloned-f.html
Hi,
I have a sort of a planet earth object, a mesh
when i click on a it, i register the triangle i clicked on (i got to get its triangle index)
from there i want to "create" a new mesh that is based on that very same triangle but would be like "hovering" over the original mesh (to achieve like a "floating" white clone of that triangle , this would show that the tile was selected, as it will turn into a white material)
(originally i planned to do it thru shader - but it's too hard for me now and i thought doing it with a mesh would make me learn about them - i thought about a Decal , but they are not in standard Unity yet, ....)
I have been struggling but i learned a lot about vertices and all, which is good, still i am at a stuck :
**the new object is created and all, but it is empty and at the center of the world instead of being hovering the original triangle**
![alt text][1]
**here is the code of the function that does it all :**
// WHEN WHE click on a triangle on the "Earth Mesh", a new one is created, like a clone of that triange, just a bit hovering it, and as a separate object
void SelectedTile(int triangleindex)
{
//check if a "Selected Tile" was already created (previous click) if yes, destroy it
GameObject[] olderTiles;
olderTiles = GameObject.FindGameObjectsWithTag("stile");
if (olderTiles.Length != 0)
{
Debug.Log("OLDER TILE FOUND, must destroy it0");
Destroy(olderTiles[0]);
}
Debug.Log("TILE SELECTED");
Debug.Log("Triangle id : " + triangleindex);
//get the whole mesh we clicked on, single out the vertices and the normals and the triangles
Mesh mesh = transform.GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
Vector3[] normals = mesh.normals;
int[] triangles = mesh.triangles;
//Single out the 3 vertices from a specific triangle (the one that has the index passed as a parameter triangleindex in the method)
Vector3 p0 = vertices[triangles[triangleindex * 3 + 0]];
Vector3 p1 = vertices[triangles[triangleindex * 3 + 1]];
Vector3 p2 = vertices[triangles[triangleindex * 3 + 2]];
//
Transform hitTransform = transform;
p0 = hitTransform.TransformPoint(p0);
p1 = hitTransform.TransformPoint(p1);
p2 = hitTransform.TransformPoint(p2);
// Draw the triangle debug lines
Color color = new Color(1f, 0f, 0f);
Debug.DrawLine(p0, p1, color);
Debug.DrawLine(p1, p2, color);
Debug.DrawLine(p2, p0, color);
// CREATE a new MESH to create a flat triangle
// Set up game object with mesh;
GameObject Selectedtile = new GameObject("Selectedtile_"+triangleindex);
Selectedtile.tag = "stile";
// add the right needed components
Mesh newmesh = new Mesh();
Selectedtile.AddComponent<MeshFilter>();
Selectedtile.AddComponent<MeshRenderer>().material.color = Color.white;
Selectedtile.GetComponent<MeshFilter>().mesh = newmesh;
//create temp arrays for vertices and UVS and triangle
Vector3[] newVertices = new Vector3[3]; //just need 3 i guess....
Vector2[] newUV = new Vector2[3]; // same thing?
int[] newTriangles = new int[3]; //i guess one would even be enough... ??
//tell these vertices to be like the ones we got from the original target triangle but with little displacement so it "hovers" over it
for (int i = 0; i < 3; i++)
{
newVertices[i] += vertices[triangles[triangleindex * 3 + 0]] + new Vector3(0.5f,0.5f,0.5f); //not sure about that....
}
//same thing for UVs....not sure about what i am doing.....
for (int i = 0; i < 3; i++)
{
newUV[i] = new Vector2(vertices[i].x, vertices[i].z);
}
// assign the temp calculated arrays into the arrays of the new Mesh.
newmesh.vertices = newVertices;
newmesh.uv = newUV;
newmesh.triangles = newTriangles;
newmesh.RecalculateNormals();
newmesh.RecalculateBounds();
}
[1]: /storage/temp/131530-newtile.pngmeshverticestrianglesSun, 20 Jan 2019 17:04:33 GMTYa7yasugarihow to render 60k cubes as efficiently as possible?
http://answers.unity.com/questions/1591912/how-to-render-60k-cubes-as-efficiently-as-possible.html
Hello, i would like to render a large number of cubes efficiently. the user will also be interacting with the cubes on every frame, so i will be storing my cubes on a mesh and modifying the cube's alpha and color every frame.
i would like to minimize the number of vertices, but i see unity no longer supports triangle strips (8 vertices per cube) so i am limited to triangles (24 vertices per cube), which will presumably perform 3x worse.
does anyone know a fast way to render and interact with a large number of cubes (60k+)? also while i'm here doe anyone know why unity removed triangle strips?
thanks,meshfpscubemeshfiltertrianglesSat, 19 Jan 2019 16:48:02 GMTrustyruss1985Is it possible to group selected verticies(of a 3D model) into a single clickable/controllable object?
http://answers.unity.com/questions/1587316/is-it-possible-to-group-selected-verticiesof-a-3d.html
Taking this ball for example:
![alt text][1]
[1]: /storage/temp/130641-ball.jpg
The 3D model is a single object made of many verticies and triangles. Would it be possible to group them into the red, blue and green groups respectively? This way I would be able to code different functions according to which "group" collides with another object.
Pardon me if this was a silly question. I'm open to all suggestions and opinions.
Thank you Unity community :)shadergameobject3d modelstriangleshighlightSun, 06 Jan 2019 19:36:08 GMTAshleyWolfYapAdding texture to vertex ,I don't want to apply texture each vertex
http://answers.unity.com/questions/1577049/adding-texture-to-vertex-i-dont-want-to-apply-text.html
![alt text][1]
[1]: /storage/temp/128786-screen-shot-2018-12-02-at-55232-pm.png
I am using exactly the same code from this website https://catlikecoding.com/unity/tutorials/rounded-cube/ and I don't want to apply texture to EACH vertex I want to apply texture that covers the ENTRIE cube, not each vertex. Right now a grid image is applied to each vertex. I don't know how to fix the shader...
this is the code for shader
Shader "Custom/Rounded Cube Grid" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
[KeywordEnum(X, Y, Z)] _Faces ("Faces", Float) = 0
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
//#pragma shader_feature _FACES_X _FACES_Y _FACES_Z
#pragma surface surf Standard fullforwardshadows vertex:vert
#pragma target 3.0
sampler2D _MainTex;
struct Input {
float2 cubeUV;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
void vert (inout appdata_full v, out Input o) {
UNITY_INITIALIZE_OUTPUT(Input, o);
o.cubeUV = v.color.xyz;
#if defined(_FACES_X)
o.cubeUV = v.color.yz * 255;
#elif defined(_FACES_Y)
o.cubeUV = v.color.xz * 255;
#elif defined(_FACES_Z)
o.cubeUV = v.color.xy * 255;
#endif
}
void surf (Input IN, inout SurfaceOutputStandard o) {
*fixed4 c = tex2D (_MainTex, IN.cubeUV) * _Color;
o.Albedo = c.rgb;
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}shadertexturecubeverticestrianglesSun, 02 Dec 2018 23:01:21 GMTheejoojinHow can i merge two triangles together?
http://answers.unity.com/questions/1572878/how-can-i-merge-two-triangles-together.html
Hello. I want to create a platformer where platform is made of a pack of traingles (every triangle have its own amount of health). So the problem is i dont know how merge two triangles, so there is no space between them and there is no stiuation when one triangle is lying on another. How can i achieve this?
Also which collider should i use?scenetrianglesmergingSun, 18 Nov 2018 15:10:41 GMTRouniXdGetting the amount of triangles and vertices being rendered by the camera ?
http://answers.unity.com/questions/1563377/getting-the-amount-of-triangles-and-vertices-being.html
I want to try something unusual.
I want to change the camera clipping values based on the amount of vertices and triangles being rendered on the screen.
My question is how can I get the amount of triangles and vertices being rendered and then referencing it in a script ? I know there is a way to get all triangles and vertices in a scene, but I only want the amount of the ones being rendered by the camera.renderingverticestrianglescountWed, 17 Oct 2018 12:23:32 GMTCherryPickerIs there any way to find the mesh triangles which are colliding with another mesh?
http://answers.unity.com/questions/1559437/is-there-any-way-to-find-the-mesh-triangles-which.html
I'm working on a task which requires me to highlight the perimeter of collision and the perimeter will change according the collision depth. For that I need to find the mesh triangles which are colliding with another mesh. Keeping in mind that both colliders are mesh colliders with **convex** property on. Can someone suggest me how to approach this problem?
I'm attaching some reference screenshots of a particular shader from [this site][1]. Initially this seemed to work fine but I had two issues. First is that I'm using orthographic camera and this shader works for perspective camera. Secondly even in perspective camera mode if I apply it to all models they tend fail at rendering the intersection contour. I don't know why because I don't have any experience with shader writing.
So any suggestion?
![alt text][2]
![alt text][3]
[1]: https://gamedev.stackexchange.com/questions/158243/surface-depth-intersection-shader
[2]: /storage/temp/125586-screen-shot-2018-10-04-at-115507-am.png
[3]: /storage/temp/125587-screen-shot-2018-10-04-at-115528-am.pngcollisionmeshcollidertrianglescontactThu, 04 Oct 2018 07:07:33 GMThassanyawarhow to connect and triangulate drawlines?
http://answers.unity.com/questions/1557985/how-to-connect-and-triangulate-drawlines.html
How can I sequentially connect a list of objects within an array?
example:
I have 12 objects (it is not a fixed amount), and I would like to connect the first (Length [0]) with the next (1), and the 1 with the next (2), and then return to [0] to form a triangle . and so with the others, (2-> 3, 3> 0 - 3-> 4, 4-> 0) and so on.
it had occurred to me to create a script for each object, and that object is connect with the previous one and with the 0. but unfortunately, the current int in which the script is not recognized.
I have a script with a list of objects (gameobject []), what I would like to know is how I identify in another script, what the object number is.
if the script is inside object 4, that the "current" int to recognize that object, is the value of the object (4), among 150 more objects.
any ideas?![alt text][1]
[1]: /storage/temp/125358-idea.pngc#meshtrianglesdrawlineSat, 29 Sep 2018 18:14:02 GMTgonzalitosk8Adding triangles to a mesh
http://answers.unity.com/questions/1557348/adding-triangles-to-a-mesh.html
I've got a flatshaded mesh (no shared vertices). At runtime i flatten the triangles by moving the triangles vertices Y- value to all be the same. However when i do so the neighboring triangles will be disconnected from the flattened triangles. One way to fix this is by moving the neighboring overlapping vertices to the same Y-value around it. But this distorts already flattened triangles if the new flattened area is at a different Y-value beside the previously flattened area.
So i would like to connect the overlapping vertices with triangles but im not sure how to do so?
the mesh-script is created from Sebastian Lague's Procedural terrain generation tutorial.
![alt text][1]
The red lines is where i want new triangle faces
![alt text][2]
The 2 triangles in the square are flattened out
[2]: /storage/temp/125258-vertices.png
[1]: /storage/temp/125259-new-triangles.png
using System.Collections;
using UnityEngine;
public class AlterMesh : MonoBehaviour
{
public MeshFilter meshFilter;
public MeshCollider meshCollider;
public Mesh mesh;
Vector3[] vertices;
int[] triangles;
int[] newTriangles;
// Use this for initialization
void Start()
{
meshCollider = this.GetComponent<MeshCollider>();
meshFilter = this.GetComponent<MeshFilter>();
mesh = meshFilter.mesh;
vertices = mesh.vertices;
triangles = mesh.triangles;
newTriangles = new int[triangles.Length*2];
AddTriangles();
}
// Update is called once per frame
void Update()
{
}
public void AddTriangles()
{
for (int i = 0; i < triangles.Length; i++)
{
newTriangles[i] = triangles[i];
}
for (int i = 0; i < triangles.Length / 3 - 1; i++)
{
newTriangles[triangles.Length - 1 + (i * 3) + 0] = triangles[];
newTriangles[triangles.Length - 1 + (i * 3) + 1] = triangles[];
newTriangles[triangles.Length - 1 + (i * 3) + 2] = triangles[];
}
mesh.triangles = newTriangles;
meshFilter.mesh = mesh;
meshCollider.sharedMesh = mesh;
}
}
This code is about as far as i've gotten. I can't seem to figure out a way to add these triangles correctly.meshverticestrianglesThu, 27 Sep 2018 14:07:49 GMTtheswedishswedeConnecting flatshaded vertices
http://answers.unity.com/questions/1557074/connecting-flatshaded-vertices.html
Using Sebastian Lague's tutorial on procedural landmass generation I've been able to create a working mesh.
However i would like to add faces between the overlapping vertices to connect them.
![alt text][1]
[1]: /storage/temp/125221-mesh.png
(The green rectangles is where i want the faces).
How can this be done?
using System.Collections;
using UnityEngine;
public class MeshGenerator : MonoBehaviour {
public static MeshData GenerateTerrainMesh(float[,] heightMap,float heightMultiplier,AnimationCurve _heightCurve,int levelOfDetail, bool useFlatShading)
{
AnimationCurve heightCurve = new AnimationCurve(_heightCurve.keys);
int meshSimplificationIncrement = (levelOfDetail == 0) ? 1 : levelOfDetail * 2;
int borderedSize = heightMap.GetLength(0);
int meshSize = borderedSize - 2 * meshSimplificationIncrement;
int meshSizeUnsimplified = borderedSize - 2;
float topLeftX = (meshSizeUnsimplified - 1) / -2f;
float topLeftZ = (meshSizeUnsimplified - 1) / 2f;
int verticesPerLine = (meshSize - 1) / meshSimplificationIncrement + 1;
MeshData meshData = new MeshData(verticesPerLine, useFlatShading);
int[,] vertexIndicesMap = new int[borderedSize, borderedSize];
int meshVertexIndex = 0;
int borderVertexIndex = -1;
for (int y = 0; y < borderedSize; y += meshSimplificationIncrement)
{
for (int x = 0; x < borderedSize; x += meshSimplificationIncrement)
{
bool isBorderVertex = y == 0 || y == borderedSize - 1 || x == 0 || x == borderedSize - 1;
if(isBorderVertex)
{
vertexIndicesMap[x, y] = borderVertexIndex;
borderVertexIndex--;
}
else
{
vertexIndicesMap[x, y] = meshVertexIndex;
meshVertexIndex++;
}
}
}
for (int y = 0; y < borderedSize; y += meshSimplificationIncrement)
{
for (int x = 0; x < borderedSize; x += meshSimplificationIncrement)
{
int vertexIndex = vertexIndicesMap[x, y];
Vector2 percent = new Vector2((x - meshSimplificationIncrement) / (float)meshSize, (y - meshSimplificationIncrement) / (float)meshSize);
float height = heightCurve.Evaluate(heightMap[x, y])*heightMultiplier;
Vector3 vertexPosition = new Vector3(topLeftX + percent.x * meshSizeUnsimplified, height, topLeftZ - percent.y * meshSizeUnsimplified);
meshData.AddVertex(vertexPosition, percent, vertexIndex);
if (x < borderedSize - 1 && y < borderedSize - 1)
{
int a = vertexIndicesMap[x, y];
int b = vertexIndicesMap[x + meshSimplificationIncrement, y];
int c = vertexIndicesMap[x, y + meshSimplificationIncrement];
int d = vertexIndicesMap[x + meshSimplificationIncrement, y + meshSimplificationIncrement];
meshData.AddTriangle(a,d,c);
meshData.AddTriangle(d,a,b);
}
vertexIndex++;
}
}
meshData.Finalize();
return meshData;
}
}
public class MeshData
{
Vector3[] vertices;
int[] triangles;
Vector2[] uvs;
Vector3[] borderVertices;
int[] borderTriangles;
int triangleIndex;
int borderTriangleIndex;
bool useFlatShading;
public MeshData(int verticesPerLine,bool useFlatShading)
{
this.useFlatShading = useFlatShading;
vertices = new Vector3[verticesPerLine * verticesPerLine];
uvs = new Vector2[verticesPerLine * verticesPerLine];
triangles = new int[(verticesPerLine - 1) * (verticesPerLine - 1) * 6];
borderVertices = new Vector3[verticesPerLine * 4 + 4];
borderTriangles = new int[24 * verticesPerLine];
}
public void AddVertex(Vector3 vertexPosition,Vector2 uv, int vertexIndex)
{
if (vertexIndex < 0)
{
borderVertices[-vertexIndex - 1] = vertexPosition;
}
else
{
vertices[vertexIndex] = vertexPosition;
uvs[vertexIndex] = uv;
}
}
public void AddTriangle(int a, int b , int c)
{
if (a < 0 || b < 0 || c < 0)
{
borderTriangles[borderTriangleIndex] = a;
borderTriangles[borderTriangleIndex + 1] = b;
borderTriangles[borderTriangleIndex + 2] = c;
borderTriangleIndex += 3;
}
else
{
triangles[triangleIndex] = a;
triangles[triangleIndex + 1] = b;
triangles[triangleIndex + 2] = c;
triangleIndex += 3;
}
}
public void Finalize()
{
if (useFlatShading)
{
FlatShading();
}
else
{
}
}
void FlatShading()
{
Vector3[] flatShadedVertices = new Vector3[triangles.Length];
Vector2[] flatShadedUvs = new Vector2[triangles.Length];
for(int i = 0; i < triangles.Length; i++)
{
flatShadedVertices[i] = vertices[triangles[i]];
flatShadedUvs[i] = uvs[triangles[i]];
triangles[i] = i;
}
vertices = flatShadedVertices;
uvs = flatShadedUvs;
}
Vector3[] CalculateNormals()
{
Vector3[] vertexNormals = new Vector3[vertices.Length];
int triangleCount = triangles.Length / 3;
for(int i = 0; i < triangleCount; i++)
{
int normalTriangleIndex = i * 3;
int vertexIndexA = triangles[normalTriangleIndex];
int vertexIndexB = triangles[normalTriangleIndex + 1];
int vertexIndexC = triangles[normalTriangleIndex + 2];
Vector3 triangleNormal = SurfaceNormalFromIndices(vertexIndexA, vertexIndexB, vertexIndexC);
vertexNormals[vertexIndexA] += triangleNormal;
vertexNormals[vertexIndexB] += triangleNormal;
vertexNormals[vertexIndexC] += triangleNormal;
}
int borderTriangleCount = borderTriangles.Length / 3;
for (int i = 0; i < borderTriangleCount; i++)
{
int normalTriangleIndex = i * 3;
int vertexIndexA = borderTriangles[normalTriangleIndex];
int vertexIndexB = borderTriangles[normalTriangleIndex + 1];
int vertexIndexC = borderTriangles[normalTriangleIndex + 2];
Vector3 triangleNormal = SurfaceNormalFromIndices(vertexIndexA, vertexIndexB, vertexIndexC);
if(vertexIndexA >= 0)
{
vertexNormals[vertexIndexA] += triangleNormal;
}
if(vertexIndexB >= 0)
{
vertexNormals[vertexIndexB] += triangleNormal;
}
if(vertexIndexC >= 0)
{
vertexNormals[vertexIndexC] += triangleNormal;
}
}
for(int i = 0; i < vertexNormals.Length; i++)
{
vertexNormals[i].Normalize();
}
return vertexNormals;
}
Vector3 SurfaceNormalFromIndices(int indexA,int indexB, int indexC)
{
Vector3 pointA = (indexA < 0) ? borderVertices[-indexA - 1] : vertices[indexA];
Vector3 pointB = (indexB < 0) ? borderVertices[-indexB - 1] : vertices[indexB];
Vector3 pointC = (indexC < 0) ? borderVertices[-indexC - 1] : vertices[indexC];
Vector3 sideAB = pointB - pointA;
Vector3 sideAC = pointC - pointA;
return Vector3.Cross(sideAB, sideAC).normalized;
}
public Mesh CreateMesh()
{
Mesh mesh = new Mesh();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.uv = uvs;
mesh.normals = CalculateNormals();
if(useFlatShading)
{
mesh.RecalculateNormals();
}
else
{
}
return mesh;
}
}meshverticesgenerationtrianglesWed, 26 Sep 2018 20:10:38 GMTtheswedishswedeMesh triangles and vertices, searching for adjacent triangle
http://answers.unity.com/questions/1538972/mesh-triangles-and-vertices-searching-for-adjacent.html
I made a 3D model using 3ds Max. It has 160 vertices, 316 triangles and 4 smoothing groups. I set it as a mesh collider for game object in Unity. When I'm spherecasting against this object and hitting an edge I want to know the angle between 2 triangles adjacent to this edge. I know if the edge was hit by using my own function that calculates barycentric coordinates (built-in property doesn't work for spherecast for some reason). By using "triangleIndex" hit property and barycentric coordinates, I'm getting the vertex indices of the edge we hit. The only thing to do is to find 2 triangles that has these 2 vertices in common, right? The first triangle is already found and its index written in "triangleIndex" property. My problem is that I just can't find the second one. Analysing triangles[] and vertices[] arrays in debug mode I found out that mesh has 328 vertices and 948 triangles! That's way much more then model actually has. I heard it can be because of smoothing groups, but model without smoothing groups shows the same numbers. Also I searched how many triangles are connected with, for example, the first vertex. It turned out there are only 2 triangles! How can it be? My model is closed and it has no holes.
So the question is how the hell these triangles and vertices are numbered and how to find adjacent triangle knowing 2 vertices. Or how to make mesh collider with correct structure.scripting problemverticesmeshcollidertrianglesMon, 06 Aug 2018 16:10:37 GMTNikolpixHoles in procedural mesh
http://answers.unity.com/questions/1530369/holes-in-procedural-mesh-1.html
![alt text][1]
So I have a text file that defines coordinates for every vertex and indices for every triangle in a mesh. I have a script that I taught to read the data file and form a vertex and triangle array from. I then told my script to generate the normals array by using the RecalculateNormals function. My shape comes out almost correct, but it appears that some of the normals are facing the wrong way. I believe that it's the normals facing the wrong way because you can't see through the object like this when it is facing the other way:
![alt text][2]
Additionally, the original file is a vtk file, and when I put it into Paraview, the file displays without problem, so I don't think that it's an issue with the vertex or triangle information.
I think that the problem is in the RecalculateNormals method, but I'm not sure how to fix it.
[1]: /storage/temp/120972-meshholesfront.png
[2]: /storage/temp/120973-meshholesback.pngmeshverticesnormalsprocedural meshtrianglesTue, 17 Jul 2018 17:05:48 GMTtuf91497Change Color of model from Down to up!
http://answers.unity.com/questions/1530026/change-color-of-model-from-down-to-up.html
I created this small effect few days back
what i am doing here is, I am getting all the Triangles of sphere model and creating them as a new GameObject and then rotating them.
[Click here][1]
By creating this effect i got this idea of "can i change color of model from Down to up(triangle by triangle) to create a nice effect like model changing to something else?"
But i don't really know how to do it!
i tried to get all the Triangles and vertices of the model but they don't really have their own material data!
so i can't really change the color.
Has anyone any idea on how can i do it?
[1]: https://www.instagram.com/p/BlQD8HxllHS/c#meshcolortrianglesMon, 16 Jul 2018 20:34:59 GMTbhavinbhai2707Making Different vertices,triangle,uv maps in the same mesh
http://answers.unity.com/questions/1521050/making-different-verticestriangleuv-maps-in-the-sa.html
Hi friends.
<br>
Im working on a big project, where i procedually generate a "minecraft like" terrain, and i've created a building creator in the same way. Heres how it works (code is tooo complex to share here).
<br>
Each building is divided into levels which have each their own chunk of boxes. each chunk is a mesh created via vertex, triangle and uv mapping.
<br>
Aslong as i keep to just using cube shaped forms in the algoritem, it seems to be working perfectly. **BUT**, i tried creating a different shape the same way, in a static class, that generates the verts, uvs and tris and return those to the chunk class, and then adding them to the rest of the verts, uvs and triangles.. This resulted in a huge mashup of the shapes, It did not make any sence.
<br>
My solution was to create a different mesh all together that works together with the other ones in a complex way, and this way each shape would have its own mesh generator and chunk class. (no problem with 1-5 shapes, buuut i have plans to make 20-30 different shapes, so you see the problem).
<br>
**So my question is:**
Is it possible to mix several shapes in the same mesh like im trying to do?
<br>
If not, do you have any suggestions ?
<br>
ALSO, Why not? and is there anywhere i can read a full guidelines of what the rules for creating meshes manually? Something like "Do's and Dont's".
<br>
Thanks for any help.meshverticesuv mappingtrianglessciptingFri, 22 Jun 2018 19:49:34 GMTMatriXzLow poly advanced chunk procedural generation
http://answers.unity.com/questions/1511181/low-poly-advanced-chunk-procedural-generation.html
First of all this 2 images is maded in blender.
The (image Nr. 1) is the subdivided plane with flat faces and the second one is the same plane but with Decimate modifier to 0.5
Talking about the low poly generation i have done this ( image Nr.1 ) in unity using C# of course. How i can generate the second image in unity ? Do you know maybe some similar situations or some generations of this type of triangulated low poly ?
![alt text][1]
![alt text][2]
[1]: /storage/temp/117717-blender12.png
[2]: /storage/temp/117718-blender122.pngc#blendertrianglesterrain generationlow polySun, 27 May 2018 11:40:09 GMToskar2771How can I identify the location of mesh triangles at run time? (Procedural Generation)
http://answers.unity.com/questions/1508607/how-can-i-identify-the-location-of-mesh-triangles.html
Having a hard time finding the location of triangle coordinates of a mesh. So far I have found the world coordinates of vertices. To give a little context, I am trying to generate a mesh at run time and then accurately place objects on the vertices and triangles. The script below is what i have to find the vertices. Can anybody point me in the right direction for triangles? I think since the triangle has 3 separate points it wont let me choose just one. Any thoughts? Appreciate the help.
`using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CustomMesh : MonoBehaviour {
// Use this for initialization
void Start () {
}
void FixedUpdate () {
Matrix4x4 localToWorld = transform.localToWorldMatrix;
MeshFilter mf = GetComponent<MeshFilter>();
for (int i = 0; i < mf.mesh.vertices.Length; ++i)
{
Vector3 world_v = localToWorld.MultiplyPoint3x4(mf.mesh.vertices[i]);
}
print(transform.localToWorldMatrix);
}
}`meshverticesprocedural meshtrianglesprocedural generationSun, 20 May 2018 19:21:30 GMTClrj14delete specific triangles / polygons in a mesh
http://answers.unity.com/questions/1507633/delete-specific-triangles-polygons-in-a-mesh.html
Hello,
I have a specific problem that I don't know how to achieve. I've looked through the Mesh class documentation but I didn't get it.
I have a simple mesh, like a road really. It's basically a row of triangles similar as in the illustration. I need to interrupt that row by deleting polygons according to the position of some marker game objects. Here's the image:
![alt text][1]
I think I know how to find the vertices that are closest to to the markers. but I don't understand how to find the right triangles and how to remove them. The fact that this mesh will be split into two parts afterwards seems to be making this problem even more complicated.
Thanks for reading and I hope someone can help.
[1]: /storage/temp/117093-mesh-delete-tris.pngverticesvertexmeshestrianglesmesh manipulationThu, 17 May 2018 20:26:14 GMTwlad_sHow do I find a point along a line with a forced x and z values
http://answers.unity.com/questions/1484007/how-do-i-find-a-point-along-a-line-with-a-forced-x.html
I'm having trouble getting my Aim Target to the red star location. I'm aware that the magnitude of defaultAimVec isn't long enough to get to the star position but it's escaping me how to get the correct distance along aimVec with the given information. Thanks for the help.
`
Debug.DrawLine(transform.position, lookPos, Color.white);
Vector3 aimVec = lookPos - transform.position;
Vector3 defaultAimVec = defaultTarget.position - transform.position;
Debug.DrawLine(defaultTarget.position, transform.position, Color.yellow);
Vector3 newYPos = aimVec.normalized * defaultAimVec.magnitude;
Debug.DrawLine(defaultTarget.position, transform.position + newYPos, Color.magenta);
aimTarget.position = new Vector3(defaultTarget.position.x, (transform.position + newYPos).y, defaultTarget.position.z);`
![problem image][1]
[1]: https://i.imgur.com/ISyVOGA.pngvector3distancemathtrianglesaimingThu, 22 Mar 2018 22:48:23 GMTwightwhaleDark triangle shadows on my mesh
http://answers.unity.com/questions/1477803/dark-triangle-shadows-on-my-mesh.html
Hello, first of all i have to say I am new to this stuff and I didn't really now what to search for on google. So my problem is, I made a diamond in blender and then saved it to .blend file, after I put that diamond into my scene this is what happens, some weird dark triangles on my mesh appeared. Does anyone have any idea what to search for or what to do to get rid of them?
![alt text][1]
![alt text][2]
[1]: /storage/temp/112583-diamond-shader-bug.png
[2]: /storage/temp/112584-diamond-blender-wireframe.pngshaderblendershadowstrianglesblackThu, 08 Mar 2018 12:11:08 GMTJakubK12Track patch ID's in Tessellation rendering pipeline
http://answers.unity.com/questions/1455543/track-patch-ids-in-tessellation-rendering-pipeline.html
Is it possible in a shader to track the ID's over primitives generated during the tessellation process? I tried semantics such as SV_PrimitiveId as well as passing the control point down the pipeline but it always returns the id of the primitive that was tessellated.
If it isn't possible to track an ID for each newly generated triangle, is there any way to get this new triangle data to a compute shader?shadertrianglesprimitivestessellationgeometry shaderTue, 16 Jan 2018 19:59:39 GMTIVIachineGetting triangle center in fragment shader
http://answers.unity.com/questions/1437705/getting-triangle-center-in-fragment-shader.html
Hello,
I am creating a custom shader to display a point cloud.
The shader creates a triangle for each vertex centered on the vertex itself.
I would like to color the triangle as a circle. More in detail, I am trying to set the alpha of each pixel decreasing as it gets further away from the center of the triangle.
However my code right now always displays alpha = 1
How might I solve this problem?
Shader "Custom/SpherePoint" {
Properties {
//_Color ("Color", Color) = (1,1,1,1)
_Radius ("Sphere Radius", float) = 0.01
}
SubShader {
Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
LOD 200
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma geometry geom
#pragma target 4.0 // Use shader model 3.0 target, to get nicer looking lighting
#include "UnityCG.cginc"
struct vertexIn {
float4 pos : POSITION;
float4 color : COLOR;
};
struct vertexOut {
float4 pos : SV_POSITION;
float4 color : COLOR;
float3 normal : NORMAL;
};
struct geomOut {
float4 pos : SV_POSITION;
float4 color : COLOR;
float3 normal : NORMAL;
float3 triangleCenter : TANGENT;
float norm : PSIZE;
};
//Vertex shader: computes normal wrt camera
vertexOut vert (vertexIn i) {
vertexOut o;
//o.pos = i.pos;
//o.pos = UnityObjectToClipPos(i.pos);
o.pos = mul(unity_ObjectToWorld, i.pos);
o.color = i.color;
//o.normal = normalize(_WorldSpaceCameraPos - mul(unity_ObjectToWorld, o.pos).xyz); //normal is towards the camera
o.normal = ObjSpaceViewDir(o.pos);
//o.normal = i.normal;
return o;
}
float _Radius;
//Geometry shaders: Creates an equilateral triangle with the original vertex in the orthocenter
[maxvertexcount(3)]
void geom(point vertexOut i[1], inout TriangleStream<geomOut> OutputStream)
{
float3 perpendicular = normalize(float3(i[0].normal.y - i[0].normal.x, i[0].normal.x, i[0].normal.x)) * _Radius;
geomOut o, p;
o.color = i[0].color;
//o.normal = i[0].normal;
//o.pos = i[0].pos;
//o.pos = float4(i[0].pos.x * _Radius, i[0].pos.yz, 1);
o.pos = float4(perpendicular + i[0].pos.xyz, 1);
//o.pos = float4(normalize(perpendicular + i[0].pos).xyz * _Radius , 1); //Generic perpendicular to the normal
o.pos = UnityObjectToClipPos(o.pos);
o.normal = ObjSpaceViewDir(o.pos);
//o.triangleCenter = UnityObjectToClipPos(i[0].pos).xyz;
o.triangleCenter = o.pos;
o.norm = distance(o.pos, o.triangleCenter);
OutputStream.Append(o);
p.color = i[0].color;
//p.normal = i[0].normal;
//p.pos = float4(perpendicular.xyz * _Radius, 1);
//p.pos = float4(i[0].pos.x, i[0].pos.y * _Radius, i[0].pos.z, 1);
//p.pos = float4( (normalize(cross(i[0].pos.xyz, perp.xyz)) - perp.xyz / 2 + i[0].pos) * _Radius,1);
p.pos = float4((-(normalize(cross(i[0].pos.xyz, perpendicular)) * _Radius) + perpendicular / 2) + i[0].pos.xyz, 1);
p.pos = UnityObjectToClipPos(p.pos);
p.normal = ObjSpaceViewDir(p.pos);
p.triangleCenter = o.triangleCenter;
p.norm = o.norm;
OutputStream.Append(p);
p.color = i[0].color;
//p.pos = float4(perpendicular.xyz * _Radius * -1, 1);
//o.pos = float4(i[0].pos.x * _Radius, i[0].pos.yz, 1);
//p.pos = float4(-1 * (normalize(cross(i[0].pos.xyz, perp.xyz)) * _Radius) /* - perp.xyz / 2 + i[0].pos*/, 1);
p.pos = float4((-(normalize(cross(i[0].pos.xyz, perpendicular)) * _Radius) - perpendicular / 2) + i[0].pos.xyz, 1);
p.pos = UnityObjectToClipPos(p.pos);
ObjSpaceViewDir(o.pos);
p.normal = ObjSpaceViewDir(p.pos);
p.triangleCenter = o.triangleCenter;
p.norm = o.norm;
OutputStream.Append(p);
OutputStream.RestartStrip();
}
float4 frag(geomOut i) : COLOR
{
float4 col = i.color;
col.a = distance(i.pos.xyz, i.triangleCenter.xyz)/ i.norm;
return col;
}
ENDCG
}
}
FallBack "Diffuse"
}shader programmingcoordinatestrianglesfragmentgeometry shaderSat, 02 Dec 2017 12:44:23 GMTandyMill