Exemple #1
0
        public override void ApplyMeshData(bool removeFromMemory)
        {
            if (topologyCache != GetTopology() || numIndicesCache != indexCount || numVerticesCache != vertexCount)
            {
                unityMesh.Clear();

                topologyCache    = GetTopology();
                numIndicesCache  = indexCount;
                numVerticesCache = vertexCount;
            }

            if (IsDynamic())
            {
                unityMesh.MarkDynamic();
            }

            ApplyVertices();
            ApplyNormals();
            ApplyTextureCoordinates();
            ApplyTangents();
            ApplyColors();
            ApplyIndices();

            RecalculateBounds();

            if (removeFromMemory)
            {
                Clear();
            }

            unityMesh.UploadMeshData(removeFromMemory);
        }
Exemple #2
0
        /// <summary>Copies the contents of the generated mesh to a [UnityEngine.Mesh](https://docs.unity3d.com/ScriptReference/Mesh.html).</summary>
        /// <param name="mesh">The mesh to copy the <see cref="Chisel.Core.GeneratedMeshContents"/> into</param>
        /// <remarks><code>
        /// MeshDescription meshDescription = ... ;
        /// GeneratedMeshContents contents = tree.GetGeneratedMesh(meshDescription);
        /// UnityEngine.Mesh unityMesh = new UnityEngine.Mesh();
        /// contents.CopyTo(unityMesh);
        /// </code>
        /// See the [Create Unity Meshes](~/documentation/createUnityMesh.md) article for more information.
        /// </remarks>
        /// <exception cref="System.ArgumentNullException">Thrown when <paramref name="mesh"/> is null.</exception>
        /// <exception cref="System.ArgumentException">Thrown when <paramref name="mesh"/> is invalid. This can happen when the mesh has already been destroyed.</exception>
        public void CopyTo(UnityEngine.Mesh mesh)
        {
            if (object.ReferenceEquals(mesh, null))
            {
                throw new ArgumentNullException("mesh");
            }
            if (!mesh)
            {
                throw new ArgumentException("mesh", "mesh is not valid, it might have already been destroyed");
            }

            if (description.vertexCount < 3 ||
                description.indexCount < 3)
            {
                mesh.Clear();
                return;
            }

            mesh.vertices = positions;
            if (normals != null)
            {
                mesh.normals = normals;
            }
            if (tangents != null)
            {
                mesh.tangents = tangents;
            }
            if (uv0 != null)
            {
                mesh.uv = uv0;
            }

            mesh.SetTriangles(indices, 0, false);
            mesh.bounds = bounds;
        }
        /// <summary>Copies the contents of the generated mesh to a [UnityEngine.Mesh](https://docs.unity3d.com/ScriptReference/Mesh.html).</summary>
        /// <param name="mesh">The mesh to copy the <see cref="Chisel.Core.GeneratedMeshContents"/> into</param>
        /// <remarks><code>
        /// MeshDescription meshDescription = ... ;
        /// GeneratedMeshContents contents = tree.GetGeneratedMesh(meshDescription);
        /// UnityEngine.Mesh unityMesh = new UnityEngine.Mesh();
        /// contents.CopyTo(unityMesh);
        /// </code>
        /// See the [Create Unity Meshes](~/documentation/createUnityMesh.md) article for more information.
        /// </remarks>
        /// <exception cref="System.ArgumentNullException">Thrown when <paramref name="mesh"/> is null.</exception>
        /// <exception cref="System.ArgumentException">Thrown when <paramref name="mesh"/> is invalid. This can happen when the mesh has already been destroyed.</exception>
        public void CopyTo(UnityEngine.Mesh mesh)
        {
            if (object.ReferenceEquals(mesh, null))
            {
                throw new ArgumentNullException("mesh");
            }
            if (!mesh)
            {
                throw new ArgumentException("mesh", "mesh is not valid, it might have already been destroyed");
            }

            if (description.vertexCount < 3 ||
                description.indexCount < 3)
            {
                mesh.Clear();
                return;
            }

            mesh.SetVertices(positions);
            if (normals.IsCreated)
            {
                mesh.SetNormals(normals);
            }
            if (tangents.IsCreated)
            {
                mesh.SetTangents(tangents);
            }
            if (uv0.IsCreated)
            {
                mesh.SetUVs(0, uv0);
            }

            mesh.SetTriangles(indices.ToArray(), 0, false);
            mesh.bounds = bounds;
        }
Exemple #4
0
        static StackObject *Clear_2(ILIntepreter __intp, StackObject *__esp, IList <object> __mStack, CLRMethod __method, bool isNewObj)
        {
            ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain;
            StackObject *ptr_of_this_method;
            StackObject *__ret = ILIntepreter.Minus(__esp, 1);

            ptr_of_this_method = ILIntepreter.Minus(__esp, 1);
            UnityEngine.Mesh instance_of_this_method = (UnityEngine.Mesh) typeof(UnityEngine.Mesh).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack));
            __intp.Free(ptr_of_this_method);

            instance_of_this_method.Clear();

            return(__ret);
        }
        /// <summary>
        ///     method for rendering a sphere
        ///     by taking a centerpoint, radius, mesh, and n vertices
        /// </summary>
        /// <param name="crossSectionRadius"></param>
        /// <param name="center"></param>
        /// <param name="crossSectionRenderer"></param>
        /// <param name="n"></param>
        public static void RenderSphere(float crossSectionRadius, Unity.Mathematics.float3 center,
                                        UnityEngine.Mesh crossSectionRenderer, int n)
        {
            crossSectionRenderer.Clear();
            int nbLong = n;
            int nbLat  = n;

            #region Vertices

            UnityEngine.Vector3[] vertices = new UnityEngine.Vector3[(nbLong + 1) * nbLat + 2];
            float pi   = UnityEngine.Mathf.PI;
            float _2pi = pi * 2f;

            vertices[0] = UnityEngine.Vector3.up * crossSectionRadius;
            for (int lat = 0; lat < nbLat; lat++)
            {
                float a1   = pi * (lat + 1) / (nbLat + 1);
                float sin1 = UnityEngine.Mathf.Sin(a1);
                float cos1 = UnityEngine.Mathf.Cos(a1);

                for (int lon = 0; lon <= nbLong; lon++)
                {
                    float a2   = _2pi * (lon == nbLong ? 0 : lon) / nbLong;
                    float sin2 = UnityEngine.Mathf.Sin(a2);
                    float cos2 = UnityEngine.Mathf.Cos(a2);

                    vertices[lon + lat * (nbLong + 1) + 1] =
                        new UnityEngine.Vector3(sin1 * cos2, cos1, sin1 * sin2) * crossSectionRadius;
                }
            }

            vertices[vertices.Length - 1] = UnityEngine.Vector3.up * -crossSectionRadius;

            #endregion

            #region Normals

            UnityEngine.Vector3[] normals = new UnityEngine.Vector3[vertices.Length];
            for (int j = 0; j < vertices.Length; j++)
            {
                normals[j] = vertices[j].normalized;
            }

            #endregion

            #region UVs

            UnityEngine.Vector2[] uvs = new UnityEngine.Vector2[vertices.Length];
            uvs[0] = UnityEngine.Vector2.up;
            uvs[uvs.Length - 1] = UnityEngine.Vector2.zero;
            for (int lat = 0; lat < nbLat; lat++)
            {
                for (int lon = 0; lon <= nbLong; lon++)
                {
                    uvs[lon + lat * (nbLong + 1) + 1] =
                        new UnityEngine.Vector2((float)lon / nbLong, 1f - (float)(lat + 1) / (nbLat + 1));
                }
            }

            #endregion

            #region Triangles

            int   nbFaces     = vertices.Length;
            int   nbTriangles = nbFaces * 2;
            int   nbIndexes   = nbTriangles * 3;
            int[] triangles   = new int[nbIndexes];

            //Top Cap
            int i = 0;
            for (int lon = 0; lon < nbLong; lon++)
            {
                triangles[i++] = lon + 2;
                triangles[i++] = lon + 1;
                triangles[i++] = 0;
            }

            //Middle
            for (int lat = 0; lat < nbLat - 1; lat++)
            {
                for (int lon = 0; lon < nbLong; lon++)
                {
                    int current = lon + lat * (nbLong + 1) + 1;
                    int next    = current + nbLong + 1;

                    triangles[i++] = current;
                    triangles[i++] = current + 1;
                    triangles[i++] = next + 1;

                    triangles[i++] = current;
                    triangles[i++] = next + 1;
                    triangles[i++] = next;
                }
            }

            //Bottom Cap
            for (int lon = 0; lon < nbLong; lon++)
            {
                triangles[i++] = vertices.Length - 1;
                triangles[i++] = vertices.Length - (lon + 2) - 1;
                triangles[i++] = vertices.Length - (lon + 1) - 1;
            }

            #endregion

            crossSectionRenderer.vertices  = vertices;
            crossSectionRenderer.normals   = normals;
            crossSectionRenderer.uv        = uvs;
            crossSectionRenderer.triangles = triangles;

            crossSectionRenderer.RecalculateBounds();
        }