/// <summary>
    /// Set all the properties (verts, uvs, tris) of the mesh and recalculate all relevant settings (normals, bounds, tangents)
    /// </summary>
    /// <param name="mesh"></param>
    /// <param name="definition"></param>
    private void UpdateMeshDefinition(Mesh mesh, MeshDefinition definition)
    {
        mesh.vertices  = definition._vertices;
        mesh.uv        = definition._uv;
        mesh.triangles = definition._triangles;

        mesh.RecalculateNormals();
        mesh.RecalculateBounds();
        mesh.RecalculateTangents();
    }
Ejemplo n.º 2
0
    private static void CreateVertices(MeshDefinition definition, int width, int height, Vector3 dimension1, Vector3 dimension2, Vector3 offset)
    {
        for (int y = 0; y <= height; y++)
        {
            for (int x = 0; x <= width; x++)
            {
                var index = x + y * (width + 1);

                definition._vertices[index] = dimension1 * x + dimension2 * y + offset;
                definition._uv[index]       = new Vector2(dimension1.normalized.magnitude * x, dimension2.normalized.magnitude * y);
            }
        }
    }
Ejemplo n.º 3
0
    protected override void onAwake()
    {
        Rig component = GetComponent <Rig>();
        SkinnedMeshDefinition skinnedMeshDefinition = new SkinnedMeshDefinition(UseGpuSkinning);

        skinnedMeshDefinition.RootBoneName = component.RootBone.name;
        skinnedMeshDefinition.BoneNames    = new string[component.Bones.Length];
        for (int i = 0; i < component.Bones.Length; i++)
        {
            skinnedMeshDefinition.BoneNames[i] = component.Bones[i].name;
        }
        meshDef = skinnedMeshDefinition;
        rend    = meshDef.CreateRenderer(base.gameObject);
    }
Ejemplo n.º 4
0
        // I/F
        public void Save(IResource resource, object asset)
        {
            var mesh = asset as Mesh;

            var definition = new MeshDefinition
            {
                Name   = mesh.Name,
                Top    = ToMeshPartDefinition(mesh.MeshParts[Side.Top]),
                Bottom = ToMeshPartDefinition(mesh.MeshParts[Side.Bottom]),
                Front  = ToMeshPartDefinition(mesh.MeshParts[Side.Front]),
                Back   = ToMeshPartDefinition(mesh.MeshParts[Side.Back]),
                Left   = ToMeshPartDefinition(mesh.MeshParts[Side.Left]),
                Right  = ToMeshPartDefinition(mesh.MeshParts[Side.Right])
            };

            serializer.Serialize(resource, definition);
        }
Ejemplo n.º 5
0
    private bool LoadMeshDefinition(MeshDefinition mesh)
    {
        List <Mesh> meshes = pb_Stl_Importer.ImportBytes(mesh.contents);

        if (meshes == null)
        {
            Logger.Error("RobotLoader::LoadMeshDefinition::Cannot load: {} from MeshDefinition contents.", mesh.name);
            return(false);
        }

        if (meshes.Count == 0)
        {
            Logger.Error("RobotLoader::LoadMeshDefinition::No meshes in MeshDefinition for: {}.", mesh.name);
            return(false);
        }

        return(CreateMeshAsset(assembly_parts_, mesh.name, mesh.scale, meshes));
    }
Ejemplo n.º 6
0
    private static void CreateSheet(int width, int height, Vector3 dimension1, Vector3 dimension2, Vector3 offset, MeshGenerator generator)
    {
        if ((width + 1) * (height + 1) > UInt16.MaxValue)
        {
            // https://forum.unity.com/threads/65535-vertices-limit.294585/
            // and https://docs.unity3d.com/ScriptReference/Mesh-indexFormat.html
            Debug.LogWarning("Vertices exceed the Unity maximum, you may experience issues with rendering...");
        }

        var vertCount     = (width + 1) * (height + 1);
        var triangleCount = width * height * 2 * 3;

        var definition = new MeshDefinition(vertCount, triangleCount);

        CreateVertices(definition, width, height, dimension1, dimension2, offset);
        CreateTriangles(definition, width, height);

        generator._meshDefinition = definition;

        generator.CreateMesh();
    }
    private static void CreateCubes(int width, int height, int depth, Vector3 offset, Texture texture, Color color, MeshGenerator generator)
    {
        var vertexCount = width * height * depth * verticesPerCube;

        if (vertexCount > UInt16.MaxValue)
        {
            // https://forum.unity.com/threads/65535-vertices-limit.294585/
            // and https://docs.unity3d.com/ScriptReference/Mesh-indexFormat.html
            Debug.LogWarning("Vertices exceed the Unity maximum, you may experience issues with rendering...");
        }

        var triangleCount = width * height * depth * 6 * 6;
        var definition    = new MeshDefinition(vertexCount, triangleCount, texture, color);

        CreateVerticesAndUvs(definition, width, height, depth, offset);
        CreateTriangles(definition, new Vector3Int(width, height, depth));

        generator._meshDefinition = definition;

        generator.CreateMesh();
    }
Ejemplo n.º 8
0
        private void StartObject()
        {
            if (ParentObject == null)
            {
                base.Mesh = new Mesh();
                MeshDefinition     MeshD     = Configured.i.Meshes.Find(new Guid("1e73382b-e366-41ec-931e-e17196340657"));
                MaterialDefinition MaterialD = Configured.i.Materials.Find(new Guid("2dbba7ea-61a9-4d76-bcb6-12b927f0ea08"));

                ParentObject = new GameObject();
                ParentObject.transform.position = GameWorldPosition;
                ParentObject.transform.rotation = GameWorldRotation;
                ParentObject.AddComponent <MeshFilter>().sharedMesh       = UnityEngine.Object.Instantiate(MeshD.Mesh);
                ParentObject.AddComponent <MeshRenderer>().sharedMaterial = UnityEngine.Object.Instantiate(MaterialD.Material);
                CarryThisWithUs(ParentObject);

                GO = CarryEmptyWithUs();
                GO.ObjectItself.transform.parent = ParentObject.transform;
                GO.ObjectItself.AddComponent <MeshFilter>();
                GO.ObjectItself.AddComponent <MeshRenderer>();
            }
        }
Ejemplo n.º 9
0
    private static void CreateTriangles(MeshDefinition definition, int width, int height)
    {
        var quadPoints1 = new int[] { 0, width + 2, 1 };
        var quadPoints2 = new int[] { 0, width + 1, width + 2 };

        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                var quad   = (x + y * width);
                var offset = quad * 6;

                for (int p = 0; p < triangleLength; p++)
                {
                    var index = offset + p;

                    definition._triangles[index] = (x + y * (width + 1)) + quadPoints1[p];
                    definition._triangles[index + triangleLength] = (x + y * (width + 1)) + quadPoints2[p];
                }
            }
        }
    }
 /// <summary>
 /// Create the vertices and uvs making up a cube
 /// </summary>
 /// <param name="definition"></param>
 /// <param name="width"></param>
 /// <param name="height"></param>
 /// <param name="depth"></param>
 /// <param name="offset"></param>
 private static void CreateVerticesAndUvs(MeshDefinition definition, int width, int height, int depth, in Vector3 offset)
Ejemplo n.º 11
0
    void BlockEdit()
    {
        blockEditScroll = GUILayout.BeginScrollView(blockEditScroll, new GUIStyle(GUI.skin.box)
        {
            padding = new RectOffset(15, 15, 15, 15),
            margin  = new RectOffset(15, 0, 0, 19)
        }, new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true) });
        if (selectedBlock < blocks.Length && selectedBlock >= 0 && blocks.Length != 0 && textureNames.Count > 0)
        {
            GUILayout.Label("Edit: " + definitions[selectedBlock].GetType(), new GUIStyle {
                fontSize = 24, alignment = TextAnchor.MiddleCenter
            }, new GUILayoutOption[] { GUILayout.ExpandWidth(true) });
            GUILayout.Space(30);

            GUILayout.BeginHorizontal();
            GUILayout.Label("Name", new GUIStyle {
                fontSize = 14, alignment = TextAnchor.MiddleCenter
            }, new GUILayoutOption[] { GUILayout.Width(200) });
            definitions[selectedBlock].blockName = EditorGUILayout.TextField(definitions[selectedBlock].blockName,
                                                                             new GUIStyle(GUI.skin.textField)
            {
                fontSize = 14,
            }, new GUILayoutOption[] { GUILayout.Height(18), GUILayout.ExpandWidth(true) });
            GUILayout.EndHorizontal();
            GUILayout.Space(10);

            if (definitions[selectedBlock].GetType() == typeof(CrossMeshDefinition))
            {
                GUILayout.BeginHorizontal();
                CrossMeshDefinition crossMesh = (CrossMeshDefinition)definitions[selectedBlock];
                GUILayout.Label("Texture", new GUIStyle {
                    fontSize = 14, alignment = TextAnchor.MiddleCenter
                }, new GUILayoutOption[] { GUILayout.Width(200) });

                int i = EditorGUILayout.Popup(textureNames.IndexOf(crossMesh.texture), textureNames.ToArray(),
                                              new GUIStyle(GUI.skin.textField)
                {
                    fontSize = 14
                }, new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(18) });
                if (i >= 0)
                {
                    crossMesh.texture = textureNames[i];
                }

                GUILayout.EndHorizontal();
            }
            else if (definitions[selectedBlock].GetType() == typeof(CubeDefinition))
            {
                CubeDefinition cube = (CubeDefinition)definitions[selectedBlock];

                GUILayout.Space(10);
                int i = 0;
                GUILayout.BeginHorizontal();
                GUILayout.Label("Texture Top", new GUIStyle {
                    fontSize = 14, alignment = TextAnchor.MiddleCenter
                }, new GUILayoutOption[] { GUILayout.Width(200) });
                i = EditorGUILayout.Popup(textureNames.IndexOf(cube.textures[0]), textureNames.ToArray(),
                                          new GUIStyle(GUI.skin.textField)
                {
                    fontSize = 14
                }, new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(18) });
                if (i >= 0)
                {
                    cube.textures[0] = textureNames[i];
                }
                GUILayout.EndHorizontal();

                GUILayout.Space(10);

                GUILayout.BeginHorizontal();
                GUILayout.Label("Texture Bottom", new GUIStyle {
                    fontSize = 14, alignment = TextAnchor.MiddleCenter
                }, new GUILayoutOption[] { GUILayout.Width(200) });
                i = EditorGUILayout.Popup(textureNames.IndexOf(cube.textures[1]), textureNames.ToArray(),
                                          new GUIStyle(GUI.skin.textField)
                {
                    fontSize = 14
                }, new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(18) });
                if (i >= 0)
                {
                    cube.textures[1] = textureNames[i];
                }
                GUILayout.EndHorizontal();

                GUILayout.Space(10);

                GUILayout.BeginHorizontal();
                GUILayout.Label("Texture North", new GUIStyle {
                    fontSize = 14, alignment = TextAnchor.MiddleCenter
                }, new GUILayoutOption[] { GUILayout.Width(200) });
                i = EditorGUILayout.Popup(textureNames.IndexOf(cube.textures[2]), textureNames.ToArray(),
                                          new GUIStyle(GUI.skin.textField)
                {
                    fontSize = 14
                }, new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(18) });
                if (i >= 0)
                {
                    cube.textures[2] = textureNames[i];
                }
                GUILayout.EndHorizontal();

                GUILayout.Space(10);

                GUILayout.BeginHorizontal();
                GUILayout.Label("Texture East", new GUIStyle {
                    fontSize = 14, alignment = TextAnchor.MiddleCenter
                }, new GUILayoutOption[] { GUILayout.Width(200) });
                i = EditorGUILayout.Popup(textureNames.IndexOf(cube.textures[3]), textureNames.ToArray(),
                                          new GUIStyle(GUI.skin.textField)
                {
                    fontSize = 14
                }, new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(18) });
                if (i >= 0)
                {
                    cube.textures[3] = textureNames[i];
                }
                GUILayout.EndHorizontal();

                GUILayout.Space(10);

                GUILayout.BeginHorizontal();
                GUILayout.Label("Texture South", new GUIStyle {
                    fontSize = 14, alignment = TextAnchor.MiddleCenter
                }, new GUILayoutOption[] { GUILayout.Width(200) });
                i = EditorGUILayout.Popup(textureNames.IndexOf(cube.textures[4]), textureNames.ToArray(),
                                          new GUIStyle(GUI.skin.textField)
                {
                    fontSize = 14
                }, new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(18) });
                if (i >= 0)
                {
                    cube.textures[4] = textureNames[i];
                }
                GUILayout.EndHorizontal();

                GUILayout.Space(10);

                GUILayout.BeginHorizontal();
                GUILayout.Label("Texture West", new GUIStyle {
                    fontSize = 14, alignment = TextAnchor.MiddleCenter
                }, new GUILayoutOption[] { GUILayout.Width(200) });
                i = EditorGUILayout.Popup(textureNames.IndexOf(cube.textures[5]), textureNames.ToArray(),
                                          new GUIStyle(GUI.skin.textField)
                {
                    fontSize = 14
                }, new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(18) });
                if (i >= 0)
                {
                    cube.textures[5] = textureNames[i];
                }
                GUILayout.EndHorizontal();

                GUILayout.Space(30);

                GUILayout.BeginHorizontal();
                GUILayout.Label("Block is solid", new GUIStyle {
                    fontSize = 14, alignment = TextAnchor.MiddleCenter
                }, new GUILayoutOption[] { GUILayout.Width(200) });
                cube.blockIsSolid = EditorGUILayout.Toggle(cube.blockIsSolid,
                                                           new GUIStyle(GUI.skin.toggle)
                {
                    fontSize = 14
                }, new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(18) });
                GUILayout.EndHorizontal();

                if (!cube.blockIsSolid)
                {
                    GUILayout.Space(10);

                    GUILayout.BeginHorizontal();
                    GUILayout.Label("Solid towards same type", new GUIStyle {
                        fontSize = 14, alignment = TextAnchor.MiddleCenter
                    }, new GUILayoutOption[] { GUILayout.Width(200) });
                    cube.solidTowardsSameType = EditorGUILayout.Toggle(cube.solidTowardsSameType,
                                                                       new GUIStyle(GUI.skin.toggle)
                    {
                        fontSize = 14
                    }, new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(18) });
                    GUILayout.EndHorizontal();
                }
            }
            else if (definitions[selectedBlock].GetType() == typeof(MeshDefinition))
            {
                MeshDefinition mesh = (MeshDefinition)definitions[selectedBlock];

                GUILayout.BeginHorizontal();
                GUILayout.Label("Mesh name", new GUIStyle {
                    fontSize = 14, alignment = TextAnchor.MiddleCenter
                }, new GUILayoutOption[] { GUILayout.Width(200) });
                mesh.meshName = EditorGUILayout.TextField(mesh.meshName,
                                                          new GUIStyle(GUI.skin.textField)
                {
                    fontSize = 14,
                }, new GUILayoutOption[] { GUILayout.Height(18), GUILayout.ExpandWidth(true) });
                GUILayout.EndHorizontal();
                GUILayout.Space(10);

                GUILayout.BeginHorizontal();
                GUILayout.Label("Texture", new GUIStyle {
                    fontSize = 14, alignment = TextAnchor.MiddleCenter
                }, new GUILayoutOption[] { GUILayout.Width(200) });
                int i = EditorGUILayout.Popup(textureNames.IndexOf(mesh.texture), textureNames.ToArray(),
                                              new GUIStyle(GUI.skin.textField)
                {
                    fontSize = 14
                }, new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(18) });
                if (i >= 0)
                {
                    mesh.texture = textureNames[i];
                }
                GUILayout.EndHorizontal();

                GUILayout.Space(10);

                GUILayout.BeginHorizontal();
                GUILayout.Label("Block is solid upwards", new GUIStyle {
                    fontSize = 14, alignment = TextAnchor.MiddleCenter
                }, new GUILayoutOption[] { GUILayout.Width(200) });
                mesh.blockIsSolid[0] = EditorGUILayout.Toggle(mesh.blockIsSolid[0],
                                                              new GUIStyle(GUI.skin.toggle)
                {
                    fontSize = 14
                }, new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(18) });
                GUILayout.EndHorizontal();
                GUILayout.Space(10);

                GUILayout.BeginHorizontal();
                GUILayout.Label("Block is solid downwards", new GUIStyle {
                    fontSize = 14, alignment = TextAnchor.MiddleCenter
                }, new GUILayoutOption[] { GUILayout.Width(200) });
                mesh.blockIsSolid[1] = EditorGUILayout.Toggle(mesh.blockIsSolid[1],
                                                              new GUIStyle(GUI.skin.toggle)
                {
                    fontSize = 14
                }, new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(18) });
                GUILayout.EndHorizontal();
                GUILayout.Space(10);

                GUILayout.BeginHorizontal();
                GUILayout.Label("Block is solid north", new GUIStyle {
                    fontSize = 14, alignment = TextAnchor.MiddleCenter
                }, new GUILayoutOption[] { GUILayout.Width(200) });
                mesh.blockIsSolid[2] = EditorGUILayout.Toggle(mesh.blockIsSolid[2],
                                                              new GUIStyle(GUI.skin.toggle)
                {
                    fontSize = 14
                }, new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(18) });
                GUILayout.EndHorizontal();
                GUILayout.Space(10);

                GUILayout.BeginHorizontal();
                GUILayout.Label("Block is solid east", new GUIStyle {
                    fontSize = 14, alignment = TextAnchor.MiddleCenter
                }, new GUILayoutOption[] { GUILayout.Width(200) });
                mesh.blockIsSolid[3] = EditorGUILayout.Toggle(mesh.blockIsSolid[3],
                                                              new GUIStyle(GUI.skin.toggle)
                {
                    fontSize = 14
                }, new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(18) });
                GUILayout.EndHorizontal();
                GUILayout.Space(10);

                GUILayout.BeginHorizontal();
                GUILayout.Label("Block is solid south", new GUIStyle {
                    fontSize = 14, alignment = TextAnchor.MiddleCenter
                }, new GUILayoutOption[] { GUILayout.Width(200) });
                mesh.blockIsSolid[4] = EditorGUILayout.Toggle(mesh.blockIsSolid[4],
                                                              new GUIStyle(GUI.skin.toggle)
                {
                    fontSize = 14
                }, new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(18) });
                GUILayout.EndHorizontal();

                GUILayout.Space(10);

                GUILayout.BeginHorizontal();
                GUILayout.Label("Block is solid west", new GUIStyle {
                    fontSize = 14, alignment = TextAnchor.MiddleCenter
                }, new GUILayoutOption[] { GUILayout.Width(200) });
                mesh.blockIsSolid[5] = EditorGUILayout.Toggle(mesh.blockIsSolid[5],
                                                              new GUIStyle(GUI.skin.toggle)
                {
                    fontSize = 14
                }, new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(18) });
                GUILayout.EndHorizontal();

                GUILayout.Space(10);

                GUILayout.BeginHorizontal();
                GUILayout.Label("Mesh position offset", new GUIStyle {
                    fontSize = 14, alignment = TextAnchor.MiddleCenter
                }, new GUILayoutOption[] { GUILayout.Width(200) });
                mesh.positionOffset = EditorGUILayout.Vector3Field("", mesh.positionOffset,
                                                                   new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(18) });
                GUILayout.EndHorizontal();
            }


            GUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();
            GUILayout.BeginVertical();
            GUILayout.FlexibleSpace();

            if (GUILayout.Button("Delete",
                                 new GUIStyle(GUI.skin.button)
            {
                fontSize = 20, padding = new RectOffset(10, 10, 10, 10)
            },
                                 new GUILayoutOption[] { GUILayout.Width(200), GUILayout.Height(40) }
                                 ))
            {
                Object.DestroyImmediate(definitions[selectedBlock]);
                GetDefinedBlocks();
                selectedBlock = -1;
            }


            GUILayout.EndVertical();
            GUILayout.EndHorizontal();

            if (GUI.changed)
            {
                EditorUtility.SetDirty(definitions[selectedBlock]);
            }
        }
        else if (selectedBlock == -1)
        {
            NewBlock();
        }

        GUILayout.EndScrollView();
    }