Esempio n. 1
0
 /** 箱。作成。
  */
 public static UnityEngine.Mesh CreateMesh(System.Collections.Generic.List <UnityEngine.Vector3> a_vertex_list, System.Collections.Generic.List <int> a_index_list)
 {
     UnityEngine.Mesh t_mesh = new UnityEngine.Mesh();
     {
         t_mesh.SetVertices(a_vertex_list);
         t_mesh.SetTriangles(a_index_list, 0);
         t_mesh.RecalculateBounds();
         t_mesh.RecalculateNormals();
         t_mesh.RecalculateTangents();
     }
     return(t_mesh);
 }
Esempio n. 2
0
            private void DrawLineMesh(UnityEngine.Mesh mesh,
                                      int vertex_count,
                                      UnityEngine.Color colour,
                                      GLLines.Style style)
            {
                mesh.vertices = VertexBuffer.vertices;
                int index_count = style == GLLines.Style.Dashed ? vertex_count & ~1
                                                    : vertex_count;
                var indices = new int[index_count];

                for (int i = 0; i < index_count; ++i)
                {
                    indices[i] = i;
                }
                var colours = new UnityEngine.Color[VertexBuffer.size];

                if (style == GLLines.Style.Faded)
                {
                    for (int i = 0; i < vertex_count; ++i)
                    {
                        var faded_colour = colour;
                        // Fade from the opacity of |colour| (when i = 0) down to 20% of that
                        // opacity.
                        faded_colour.a *= 1 - 0.8f * (i / (float)vertex_count);
                        colours[i]      = faded_colour;
                    }
                }
                else
                {
                    for (int i = 0; i < vertex_count; ++i)
                    {
                        colours[i] = colour;
                    }
                }
                mesh.colors = colours;
                mesh.SetIndices(
                    indices,
                    style == GLLines.Style.Dashed ? UnityEngine.MeshTopology.Lines
                                      : UnityEngine.MeshTopology.LineStrip,
                    submesh: 0);
                mesh.RecalculateBounds();
                // If the lines are drawn in layer 31 (Vectors), which sounds more
                // appropriate, they vanish when zoomed out.  Layer 9 works; pay no
                // attention to its name.
                UnityEngine.Graphics.DrawMesh(
                    mesh,
                    UnityEngine.Vector3.zero,
                    UnityEngine.Quaternion.identity,
                    GLLines.line_material,
                    (int)PrincipiaPluginAdapter.UnityLayers.Atmosphere,
                    PlanetariumCamera.Camera);
            }
Esempio n. 3
0
        private static void MenuItem_LoadBinaryWithAssetsPath()
        {
            //SaveBinaryWithAssetsPath
            {
                UnityEngine.Mesh t_mesh = new UnityEngine.Mesh();
                {
                    System.Collections.Generic.List <UnityEngine.Vector3> t_vertex_list = new System.Collections.Generic.List <UnityEngine.Vector3>();
                    System.Collections.Generic.List <UnityEngine.Vector3> t_nomal_list  = new System.Collections.Generic.List <UnityEngine.Vector3>();
                    System.Collections.Generic.List <int> t_index_list = new System.Collections.Generic.List <int>();
                    {
                        t_vertex_list.Add(new UnityEngine.Vector3(0.0f, 0.0f, 0.0f));
                        t_vertex_list.Add(new UnityEngine.Vector3(1.0f, 0.0f, 0.0f));
                        t_vertex_list.Add(new UnityEngine.Vector3(0.0f, 1.0f, 0.0f));
                        t_vertex_list.Add(new UnityEngine.Vector3(0.0f, 0.0f, 1.0f));
                        t_nomal_list.Add(new UnityEngine.Vector3(0.0f, 0.0f, 1.0f));
                        t_nomal_list.Add(new UnityEngine.Vector3(0.0f, 0.0f, 1.0f));
                        t_nomal_list.Add(new UnityEngine.Vector3(0.0f, 0.0f, 1.0f));
                        t_nomal_list.Add(new UnityEngine.Vector3(0.0f, 0.0f, 1.0f));
                        t_index_list.Add(0);
                        t_index_list.Add(2);
                        t_index_list.Add(1);
                        t_index_list.Add(0);
                        t_index_list.Add(1);
                        t_index_list.Add(3);
                        t_index_list.Add(1);
                        t_index_list.Add(2);
                        t_index_list.Add(3);

                        t_index_list.Add(2);
                        t_index_list.Add(0);
                        t_index_list.Add(3);
                    }
                    t_mesh.vertices  = t_vertex_list.ToArray();
                    t_mesh.normals   = t_nomal_list.ToArray();
                    t_mesh.triangles = t_index_list.ToArray();
                    t_mesh.RecalculateNormals();
                    t_mesh.RecalculateBounds();
                    t_mesh.RecalculateTangents();
                }

                BlueBack.AssetLib.Editor.CreateDirectoryWithAssetsPath.Create("Out");
                BlueBack.AssetLib.Editor.SaveAssetWithAssetsPath.SaveConverter(t_mesh, new BlueBack.AssetLib.StlConverterAssetToBinary(new UnityEngine.Vector3(50.0f, 50.0f, 50.0f)), "Out/test.stl");
                BlueBack.AssetLib.Editor.RefreshAssetDatabase.Refresh();
            }

            //LoadBinaryWithAssetsPath
            {
                UnityEngine.Mesh t_mesh = BlueBack.AssetLib.Editor.LoadAssetWithAssetsPath.LoadConverter(new BlueBack.AssetLib.StlConverterBinaryToAsset(new UnityEngine.Vector3(1.0f, 1.0f, 1.0f)), "Out/test.stl");
                UnityEngine.Debug.Log(t_mesh.triangles.Length.ToString());
            }
        }
Esempio n. 4
0
        static StackObject *RecalculateBounds_5(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.RecalculateBounds();

            return(__ret);
        }
Esempio n. 5
0
        public static UnityEngine.Mesh projectedMesh(Unity.Mathematics.float4[] verts, int[] tris)
        {
            UnityEngine.Mesh mesh = new UnityEngine.Mesh();
            System.Collections.Generic.List <UnityEngine.Vector3> verticies =
                new System.Collections.Generic.List <UnityEngine.Vector3>();
            System.Collections.Generic.List <int> triangles = new System.Collections.Generic.List <int>();
            //Vector2[] uvs;
            //Vector3[] normals;
            //iterate through each face of the mesh

            if (SpencerStudyControl.ins.projectionMethod == ProjectionMethod.stereographic)
            {
                for (int i = 0; i < tris.Length / 3; i++)
                {
                    Unity.Mathematics.float4[] inputVerts =
                    { verts[tris[i * 3]], verts[tris[i * 3 + 1]], verts[tris[i * 3 + 2]] };
                    Unity.Mathematics.float3[] tmpVerts;
                    int[] tmpTris;
                    projectTriangle(inputVerts, IMRE.ScaleDimension.SpencerStudyControl.ins.subdiv,
                                    out tmpVerts, out tmpTris, IMRE.ScaleDimension.SpencerStudyControl.ins.projectionMethod);
                    //stich faces into common mesh

                    tmpTris.ToList().ForEach(idx => triangles.Add(idx + verticies.Count));
                    tmpVerts.ToList().ForEach(vert => verticies.Add(vert));

                    //TODO handle uvs;
                    //TODO handle normals;
                }
            }
            else
            {
                for (int i = 0; i < verts.Length; i++)
                {
                    verticies.Add(projectPosition(verts[i]));
                    ;
                }

                triangles = tris.ToList();
            }

            mesh.vertices  = verticies.ToArray();
            mesh.triangles = triangles.ToArray();
            //mesh.uv = uvs;
            //mesh.normals = normals;
            mesh.RecalculateNormals();
            mesh.RecalculateBounds();
            return(mesh);
        }
        private UnityEngine.Mesh ScaleItemMesh(UnityEngine.Mesh itemMesh, UnityEngine.RectTransform meshRendererUIRect)
        {
            //UI is on x,y coordinates but mesh still needs scaling in all directions, we will assume that the item slot has a square shape for now.
            //So we take the y axis for z scaling
            //Scale it up to the size of the "cell"
            float3 scaling = new float3(meshRendererUIRect.sizeDelta.x, meshRendererUIRect.sizeDelta.y, meshRendererUIRect.sizeDelta.y) - new float3(itemMesh.bounds.size);

            //This vertices array needs to be copied before modyfying! Otherwise Unity will throw invalid AABB errors because some vertices are already scaled while others are not yet
            UnityEngine.Vector3[] vertices = itemMesh.vertices;

            for (int i = 0; i < vertices.Length; ++i)
            {
                vertices[i].x *= scaling.x;
                vertices[i].y *= scaling.y;
                vertices[i].z *= scaling.z;
            }

            itemMesh.vertices = vertices;

            itemMesh.RecalculateBounds();

            return(itemMesh);
        }
        /// <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();
        }
Esempio n. 8
0
        private static UMesh CreateMesh(UMatrix[] bindposes, UVector3[] vertices, Mesh destMesh, bool recalculateNormals)
        {
            // TODO: Support blend shapes also?
            if (recalculateNormals)
            {
                // If we recalculate the normals, we also recalculate the tangents
                destMesh.RecalculateNormals();
                destMesh.RecalculateTangents();
            }

            int subMeshCount   = destMesh.SubMeshCount;
            var newNormals     = FromSimplifyVec(destMesh.Normals);
            var newTangents    = FromSimplifyVec(destMesh.Tangents);
            var newUV1         = FromSimplifyVec(destMesh.UV1);
            var newUV2         = FromSimplifyVec(destMesh.UV2);
            var newUV3         = FromSimplifyVec(destMesh.UV3);
            var newUV4         = FromSimplifyVec(destMesh.UV4);
            var newColors      = FromSimplifyColor(destMesh.Colors);
            var newBoneWeights = FromSimplifyBoneWeights(destMesh.BoneWeights);

            UMesh newMesh = new UMesh();

            if (bindposes != null)
            {
                newMesh.bindposes = bindposes;
            }
            newMesh.subMeshCount = subMeshCount;
            newMesh.vertices     = vertices;
            if (newNormals != null)
            {
                newMesh.normals = newNormals;
            }
            if (newTangents != null)
            {
                newMesh.tangents = newTangents;
            }
            if (newUV1 != null)
            {
                newMesh.uv = newUV1;
            }
            if (newUV2 != null)
            {
                newMesh.uv2 = newUV2;
            }
            if (newUV3 != null)
            {
                newMesh.uv3 = newUV3;
            }
            if (newUV4 != null)
            {
                newMesh.uv4 = newUV4;
            }
            if (newColors != null)
            {
                newMesh.colors = newColors;
            }
            if (newBoneWeights != null)
            {
                newMesh.boneWeights = newBoneWeights;
            }
            for (int i = 0; i < subMeshCount; i++)
            {
                var subMeshIndices = destMesh.GetIndices(i);
                newMesh.SetTriangles(subMeshIndices, i);
            }

            newMesh.RecalculateBounds();
            return(newMesh);
        }
Esempio n. 9
0
        private static UMesh CreateMesh(UMatrix[] bindposes, UVector3[] vertices, Mesh destMesh, bool recalculateNormals)
        {
            // TODO: Support blend shapes also?
            if (recalculateNormals)
            {
                // If we recalculate the normals, we also recalculate the tangents
                destMesh.RecalculateNormals();
                destMesh.RecalculateTangents();
            }

            int subMeshCount   = destMesh.SubMeshCount;
            var newNormals     = FromSimplifyVec(destMesh.Normals);
            var newTangents    = FromSimplifyVec(destMesh.Tangents);
            var newUV1         = FromSimplifyVec(destMesh.UV1);
            var newUV2         = FromSimplifyVec(destMesh.UV2);
            var newUV3         = FromSimplifyVec(destMesh.UV3);
            var newUV4         = FromSimplifyVec(destMesh.UV4);
            var newColors      = FromSimplifyColor(destMesh.Colors);
            var newBoneWeights = FromSimplifyBoneWeights(destMesh.BoneWeights);

            int maxIndex = 0;

            for (int i = 0; i < subMeshCount; i++)
            {
                var subMeshIndices = destMesh.GetIndices(i);
                for (int j = 0; j < subMeshIndices.Length; j++)
                {
                    if (subMeshIndices[j] > maxIndex)
                    {
                        maxIndex = subMeshIndices[j];
                    }
                }
            }

            UMesh newMesh = new UMesh();

#if UNITY_2017_3 || !UNITY_2017_4 || UNITY_2018
            newMesh.indexFormat = (maxIndex > ushort.MaxValue ? UnityEngine.Rendering.IndexFormat.UInt32 : UnityEngine.Rendering.IndexFormat.UInt16);
#endif
            if (bindposes != null)
            {
                newMesh.bindposes = bindposes;
            }
            newMesh.subMeshCount = subMeshCount;
            newMesh.vertices     = vertices;
            if (newNormals != null)
            {
                newMesh.normals = newNormals;
            }
            if (newTangents != null)
            {
                newMesh.tangents = newTangents;
            }
            if (newUV1 != null)
            {
                newMesh.uv = newUV1;
            }
            if (newUV2 != null)
            {
                newMesh.uv2 = newUV2;
            }
            if (newUV3 != null)
            {
                newMesh.uv3 = newUV3;
            }
            if (newUV4 != null)
            {
                newMesh.uv4 = newUV4;
            }
            if (newColors != null)
            {
                newMesh.colors = newColors;
            }
            if (newBoneWeights != null)
            {
                newMesh.boneWeights = newBoneWeights;
            }
            for (int i = 0; i < subMeshCount; i++)
            {
                var subMeshIndices = destMesh.GetIndices(i);
                newMesh.SetTriangles(subMeshIndices, i);
            }

            newMesh.RecalculateBounds();
            return(newMesh);
        }
Esempio n. 10
0
        public UnityEngine.Mesh ToUnity(Utils.Progress progress = null)
        {
            if (unityMesh == null)
            {
                unityMesh = new UnityEngine.Mesh();

                if (name != null)
                {
                    unityMesh.name = name;
                }
                if (vertices != null)
                {
                    unityMesh.vertices = vertices;
                }
                if (normals != null)
                {
                    unityMesh.normals = normals;
                }
                if (tangents != null)
                {
                    unityMesh.tangents = tangents;
                }
                if (uv1 != null)
                {
                    unityMesh.uv = uv1;
                }
                if (uv2 != null)
                {
                    unityMesh.uv2 = uv2;
                }
                if (colors != null)
                {
                    unityMesh.colors = colors;
                }
                if (submeshes != null)
                {
                    int nb_submeshes = submeshes.Length;

                    unityMesh.subMeshCount = nb_submeshes;

                    for (int i = 0; i < nb_submeshes; i++)
                    {
                        SubMesh submesh = submeshes[i];

                        unityMesh.SetIndices(submesh.triangles, submesh.topology, i);
                    }
                }

                unityMesh.RecalculateBounds();

#if !UNITY_5_5_OR_NEWER
                unityMesh.Optimize();
#endif

                if (progress != null)
                {
                    progress.Update(1);
                }
            }

            return(unityMesh);
        }
        public UnityEngine.Mesh GetOrCreateCube(float xScale, float yScale, float zScale)
        {
            lock (padlock)
            {
                if (this.cachedCubeMeshes == null)
                {
                    this.cachedCubeMeshes = new Dictionary <Tuple <float, float, float>, UnityEngine.Mesh>();
                }

                Tuple <float, float, float> key = new Tuple <float, float, float>(xScale, yScale, zScale);

                if (!this.cachedCubeMeshes.ContainsKey(key))
                {
                    //First create primitive geometry then apply scale
                    UnityEngine.Mesh      cubeMesh = new UnityEngine.Mesh();
                    UnityEngine.Vector3[] vertices =
                    {
                        new float3(0,      yScale,      0),
                        new float3(0,           0,      0),
                        new float3(xScale, yScale,      0),
                        new float3(xScale,      0,      0),

                        new float3(0,           0, zScale),
                        new float3(xScale,      0, zScale),
                        new float3(0,      yScale, zScale),
                        new float3(xScale, yScale, zScale),

                        new float3(0,      yScale,      0),
                        new float3(xScale, yScale,      0),

                        new float3(0,      yScale,      0),
                        new float3(0,      yScale, zScale),

                        new float3(xScale, yScale,      0),
                        new float3(xScale, yScale, zScale),
                    };
                    cubeMesh.vertices = vertices;

                    int[] triangles =
                    {
                        0,  2,  1, // front
                        1,  2,  3,
                        4,  5,  6, // back
                        5,  7,  6,
                        6,  7,  8, //top
                        7,  9,  8,
                        1,  3,  4, //bottom
                        3,  5,  4,
                        1, 11, 10, // left
                        1,  4, 11,
                        3, 12,  5, //right
                        5, 12, 13
                    };
                    cubeMesh.triangles = triangles;

                    UnityEngine.Vector2[] uv =
                    {
                        new float2(0,     0.66f),
                        new float2(0.25f, 0.66f),
                        new float2(0,     0.33f),
                        new float2(0.25f, 0.33f),

                        new float2(0.5f,  0.66f),
                        new float2(0.5f,  0.33f),
                        new float2(0.75f, 0.66f),
                        new float2(0.75f, 0.33f),

                        new float2(1,     0.66f),
                        new float2(1,     0.33f),

                        new float2(0.25f,     1),
                        new float2(0.5f,      1),

                        new float2(0.25f,     0),
                        new float2(0.5f,      0),
                    };

                    cubeMesh.Optimize();
                    cubeMesh.RecalculateNormals();

                    cubeMesh.RecalculateBounds();

                    this.cachedCubeMeshes.Add(key, cubeMesh);
                }

                return(this.cachedCubeMeshes[key]);
            }
        }