Ejemplo n.º 1
0
        protected void OnEnable()
        {
            if (_random == null)
            {
                _random = XorShift128Plus.Create(randomSeedText);
            }

            if (_normalFaceTriangulation == null)
            {
                _normalFaceTriangulation = new SeparatedFacesUmbrellaTriangulation(
                    (Topology.FaceEdge edge, DynamicMesh.IIndexedVertexAttributes vertexAttributes) =>
                {
                    vertexAttributes.normal = (_facePositions[edge.nearFace] + _surface.normal * 5f - vertexAttributes.position).normalized;
                    vertexAttributes.uv     = new Vector2(0.25f, 0f);
                    vertexAttributes.Advance();
                    vertexAttributes.normal = (_facePositions[edge.nearFace] + _surface.normal * 5f - vertexAttributes.position).normalized;
                    vertexAttributes.uv     = new Vector2(0.25f, 0.5f);
                    vertexAttributes.Advance();
                },
                    (Topology.Face face, DynamicMesh.IIndexedVertexAttributes vertexAttributes) =>
                {
                    vertexAttributes.uv = new Vector2(0.25f, 1f);
                    vertexAttributes.Advance();
                });
            }

            if (_selectedFaceTriangulation == null)
            {
                _selectedFaceTriangulation = new SeparatedFacesUmbrellaTriangulation(
                    (Topology.FaceEdge edge, DynamicMesh.IIndexedVertexAttributes vertexAttributes) =>
                {
                    vertexAttributes.normal = (_facePositions[edge.nearFace] + _surface.normal * 1f - vertexAttributes.position).normalized;
                    vertexAttributes.uv     = new Vector2(0.75f, 0f);
                    vertexAttributes.Advance();
                    vertexAttributes.normal = (_facePositions[edge.nearFace] + _surface.normal * 1f - vertexAttributes.position).normalized;
                    vertexAttributes.uv     = new Vector2(0.75f, 0.5f);
                    vertexAttributes.Advance();
                },
                    (Topology.Face face, DynamicMesh.IIndexedVertexAttributes vertexAttributes) =>
                {
                    vertexAttributes.uv = new Vector2(0.75f, 1f);
                    vertexAttributes.Advance();
                });
            }
        }
Ejemplo n.º 2
0
        protected void OnEnable()
        {
            if (!_initialized)
            {
                _random = XorShift128Plus.Create(948675);

                _faceTriangulation = new SeparatedFacesUmbrellaTriangulation(
                    (Topology.FaceEdge edge, DynamicMesh.IIndexedVertexAttributes vertexAttributes) =>
                {
                    var face             = edge.nearFace;
                    var gridOverlayU     = GetGridOverlayU(false, _faceSeenStates[face], _faceSightCounts[face]);
                    vertexAttributes.uv2 = new Vector2(gridOverlayU, 0f);
                    vertexAttributes.Advance();
                    vertexAttributes.uv2 = new Vector2(gridOverlayU, 0.5f);
                    vertexAttributes.Advance();
                },
                    (Topology.Face face, DynamicMesh.IIndexedVertexAttributes vertexAttributes) =>
                {
                    vertexAttributes.uv2 = new Vector2(GetGridOverlayU(false, _faceSeenStates[face], _faceSightCounts[face]), 1f);
                    vertexAttributes.Advance();
                });

                _selectedFaceTriangulation = new SeparatedFacesUmbrellaTriangulation(
                    (Topology.FaceEdge edge, DynamicMesh.IIndexedVertexAttributes vertexAttributes) =>
                {
                    var face             = edge.nearFace;
                    var gridOverlayU     = GetGridOverlayU(true, _faceSeenStates[face], _faceSightCounts[face]);
                    vertexAttributes.uv2 = new Vector2(gridOverlayU, 0f);
                    vertexAttributes.Advance();
                    vertexAttributes.uv2 = new Vector2(gridOverlayU, 0.5f);
                    vertexAttributes.Advance();
                },
                    (Topology.Face face, DynamicMesh.IIndexedVertexAttributes vertexAttributes) =>
                {
                    vertexAttributes.uv2 = new Vector2(GetGridOverlayU(true, _faceSeenStates[face], _faceSightCounts[face]), 1f);
                    vertexAttributes.Advance();
                });

                _pathFinder = new PathFinder();

                _initialized = true;
            }
        }
Ejemplo n.º 3
0
        private void CreateMesh(IEnumerable <Topology.Face> faces, DynamicMesh.ITriangulation triangulation, MeshFilter meshPrefab)
        {
            var dynamicMesh = DynamicMesh.Create(
                faces,
                DynamicMesh.VertexAttributes.Position |
                DynamicMesh.VertexAttributes.Normal |
                DynamicMesh.VertexAttributes.UV,
                triangulation);

            int existingChildren = transform.childCount;

            for (int i = 0; i < existingChildren; ++i)
            {
                Destroy(transform.GetChild(i).gameObject);
            }

            foreach (var submesh in dynamicMesh.submeshes)
            {
                var meshObject = Instantiate(meshPrefab);
                meshObject.mesh = submesh;
                meshObject.transform.SetParent(transform, false);
            }
        }
Ejemplo n.º 4
0
        public override IEnumerator BeginGeneration()
        {
            var topology = topologyInputSlot.GetAsset <Topology>();

            var vertexAttributeSources = new VertexAttributeSources();

            vertexAttributeSources.ringPositions   = VertexAttributeSources.GetRingAttributes <Vector3>(topology, ringVertexPositionsInputSlots);
            vertexAttributeSources.ringNormals     = VertexAttributeSources.GetRingAttributes <Vector3>(topology, ringVertexNormalsInputSlots);
            vertexAttributeSources.ringColors      = VertexAttributeSources.GetRingAttributes <Color>(topology, ringVertexColorsInputSlots);
            vertexAttributeSources.ringColor32s    = VertexAttributeSources.GetRingAttributes <Color32>(topology, ringVertexColor32sInputSlots);
            vertexAttributeSources.ringUV1s        = VertexAttributeSources.GetRingAttributes <Vector2>(topology, ringVertexUV1sInputSlots);
            vertexAttributeSources.ringUV2s        = VertexAttributeSources.GetRingAttributes <Vector2>(topology, ringVertexUV2sInputSlots);
            vertexAttributeSources.ringUV3s        = VertexAttributeSources.GetRingAttributes <Vector2>(topology, ringVertexUV3sInputSlots);
            vertexAttributeSources.ringUV4s        = VertexAttributeSources.GetRingAttributes <Vector2>(topology, ringVertexUV4sInputSlots);
            vertexAttributeSources.ringTangents    = VertexAttributeSources.GetRingAttributes <Vector4>(topology, ringVertexTangentsInputSlots);
            vertexAttributeSources.centerPositions = VertexAttributeSources.GetCenterAttributes <Vector3>(centerVertexPositionsInputSlots);
            vertexAttributeSources.centerNormals   = VertexAttributeSources.GetCenterAttributes <Vector3>(centerVertexNormalsInputSlots);
            vertexAttributeSources.centerColors    = VertexAttributeSources.GetCenterAttributes <Color>(centerVertexColorsInputSlots);
            vertexAttributeSources.centerColor32s  = VertexAttributeSources.GetCenterAttributes <Color32>(centerVertexColor32sInputSlots);
            vertexAttributeSources.centerUV1s      = VertexAttributeSources.GetCenterAttributes <Vector2>(centerVertexUV1sInputSlots);
            vertexAttributeSources.centerUV2s      = VertexAttributeSources.GetCenterAttributes <Vector2>(centerVertexUV2sInputSlots);
            vertexAttributeSources.centerUV3s      = VertexAttributeSources.GetCenterAttributes <Vector2>(centerVertexUV3sInputSlots);
            vertexAttributeSources.centerUV4s      = VertexAttributeSources.GetCenterAttributes <Vector2>(centerVertexUV4sInputSlots);
            vertexAttributeSources.centerTangents  = VertexAttributeSources.GetCenterAttributes <Vector4>(centerVertexTangentsInputSlots);

            System.Action <Topology.FaceEdge, DynamicMesh.IIndexedVertexAttributes> setRingVertexAttributes =
                (Topology.FaceEdge edge, DynamicMesh.IIndexedVertexAttributes va) =>
            {
                for (int i = 0; i < ringDepth; ++i)
                {
                    if (vertexAttributeSources.ringPositions != null)
                    {
                        va.position = vertexAttributeSources.ringPositions[i][edge];
                    }
                    if (vertexAttributeSources.ringNormals != null)
                    {
                        va.normal = vertexAttributeSources.ringNormals[i][edge];
                    }
                    if (vertexAttributeSources.ringColors != null)
                    {
                        va.color = vertexAttributeSources.ringColors[i][edge];
                    }
                    if (vertexAttributeSources.ringColor32s != null)
                    {
                        va.color32 = vertexAttributeSources.ringColor32s[i][edge];
                    }
                    if (vertexAttributeSources.ringUV1s != null)
                    {
                        va.uv1 = vertexAttributeSources.ringUV1s[i][edge];
                    }
                    if (vertexAttributeSources.ringUV2s != null)
                    {
                        va.uv2 = vertexAttributeSources.ringUV2s[i][edge];
                    }
                    if (vertexAttributeSources.ringUV3s != null)
                    {
                        va.uv3 = vertexAttributeSources.ringUV3s[i][edge];
                    }
                    if (vertexAttributeSources.ringUV4s != null)
                    {
                        va.uv4 = vertexAttributeSources.ringUV4s[i][edge];
                    }
                    if (vertexAttributeSources.ringTangents != null)
                    {
                        va.tangent = vertexAttributeSources.ringTangents[i][edge];
                    }
                    va.Advance();
                }
            };

            System.Action <Topology.Face, DynamicMesh.IIndexedVertexAttributes> setCenterVertexAttributes =
                (Topology.Face face, DynamicMesh.IIndexedVertexAttributes va) =>
            {
                if (vertexAttributeSources.centerPositions != null)
                {
                    va.position = vertexAttributeSources.centerPositions[face];
                }
                if (vertexAttributeSources.centerNormals != null)
                {
                    va.normal = vertexAttributeSources.centerNormals[face];
                }
                if (vertexAttributeSources.centerColors != null)
                {
                    va.color = vertexAttributeSources.centerColors[face];
                }
                if (vertexAttributeSources.centerColor32s != null)
                {
                    va.color32 = vertexAttributeSources.centerColor32s[face];
                }
                if (vertexAttributeSources.centerUV1s != null)
                {
                    va.uv1 = vertexAttributeSources.centerUV1s[face];
                }
                if (vertexAttributeSources.centerUV2s != null)
                {
                    va.uv2 = vertexAttributeSources.centerUV2s[face];
                }
                if (vertexAttributeSources.centerUV3s != null)
                {
                    va.uv3 = vertexAttributeSources.centerUV3s[face];
                }
                if (vertexAttributeSources.centerUV4s != null)
                {
                    va.uv4 = vertexAttributeSources.centerUV4s[face];
                }
                if (vertexAttributeSources.centerTangents != null)
                {
                    va.tangent = vertexAttributeSources.centerTangents[face];
                }
                va.Advance();
            };

            DynamicMesh.ITriangulation triangulationInstance = null;

            switch (triangulation)
            {
            case Triangulation.Strip:
                triangulationInstance = new SeparatedFacesStripTriangulation(ringDepth, setRingVertexAttributes);
                break;

            case Triangulation.Fan:
                triangulationInstance = new SeparatedFacesFanTriangulation(ringDepth, setRingVertexAttributes);
                break;

            case Triangulation.Umbrella:
                triangulationInstance = new SeparatedFacesUmbrellaTriangulation(ringDepth, setRingVertexAttributes, setCenterVertexAttributes);
                break;

            default:
                throw new System.NotImplementedException();
            }

            DynamicMesh dynamicMesh;

            switch (sourceType)
            {
            case SourceType.InternalFaces:
                dynamicMesh = DynamicMesh.Create(topology.enumerableInternalFaces, vertexAttributes, triangulationInstance, maxVerticesPerSubmesh);
                break;

            case SourceType.FaceGroupCollection:
                dynamicMesh = DynamicMesh.Create(faceGroupCollectionInputSlot.GetAsset <FaceGroupCollection>(), vertexAttributes, triangulationInstance, maxVerticesPerSubmesh);
                break;

            case SourceType.FaceGroup:
                dynamicMesh = DynamicMesh.Create(faceGroupInputSlot.GetAsset <FaceGroup>(), vertexAttributes, triangulationInstance, maxVerticesPerSubmesh);
                break;

            default:
                throw new System.NotImplementedException();
            }

            if (meshOutputSlots.Length != dynamicMesh.submeshCount)
            {
                var newMeshes = new OutputSlot[dynamicMesh.submeshCount];

                System.Array.Copy(meshOutputSlots, newMeshes, Mathf.Min(meshOutputSlots.Length, newMeshes.Length));
                for (var i = meshOutputSlots.Length; i < newMeshes.Length; ++i)
                {
                    newMeshes[i] = OutputSlot.CreateGrouped <Mesh>(this, "Mesh", "Meshes");
                }

                meshOutputSlots = newMeshes;
            }

            if (meshOutputSlots.Length == 1)
            {
                meshOutputSlots[0].name = "Mesh";
            }
            else if (meshOutputSlots.Length > 1)
            {
                for (int i = 0; i < meshOutputSlots.Length; ++i)
                {
                    meshOutputSlots[i].name = string.Format("Submesh {0}", i);
                }
            }

            for (int i = 0; i < meshOutputSlots.Length; ++i)
            {
                meshOutputSlots[i].SetAsset(dynamicMesh.GetSubmesh(i));
                dynamicMesh.ReplaceSubmesh(i, meshOutputSlots[i].GetAsset <Mesh>());
            }

            dynamicMeshOutputSlot.SetAsset(dynamicMesh);

            yield break;
        }
Ejemplo n.º 5
0
        void Awake()
        {
            _triangulationNormal = new SeparatedFacesUmbrellaTriangulation(2,
                                                                           (Topology.FaceEdge edge, DynamicMesh.IIndexedVertexAttributes vertexAttributes) =>
            {
                vertexAttributes.Advance();

                vertexAttributes.normal = (vertexAttributes.position + Vector3.up - _facePositions[edge.nearFace]).normalized;
                vertexAttributes.color  = normalColor;
                vertexAttributes.Advance();
            },
                                                                           (Topology.Face face, DynamicMesh.IIndexedVertexAttributes vertexAttributes) =>
            {
                vertexAttributes.color = normalColor;
                vertexAttributes.Advance();
            });

            _triangulationPressed = new SeparatedFacesUmbrellaTriangulation(2,
                                                                            (Topology.FaceEdge edge, DynamicMesh.IIndexedVertexAttributes vertexAttributes) =>
            {
                vertexAttributes.Advance();

                vertexAttributes.normal = (_facePositions[edge.nearFace] + Vector3.up * 0.5f - vertexAttributes.position).normalized;
                vertexAttributes.color  = pressedColor;
                vertexAttributes.Advance();
            },
                                                                            (Topology.Face face, DynamicMesh.IIndexedVertexAttributes vertexAttributes) =>
            {
                vertexAttributes.color = pressedColor;
                vertexAttributes.Advance();
            });

            _triangulationPath = new SeparatedFacesUmbrellaTriangulation(2,
                                                                         (Topology.FaceEdge edge, DynamicMesh.IIndexedVertexAttributes vertexAttributes) =>
            {
                vertexAttributes.Advance();

                vertexAttributes.normal = (_facePositions[edge.nearFace] + Vector3.up * 0.5f - vertexAttributes.position).normalized;
                vertexAttributes.color  = pathColor;
                vertexAttributes.Advance();
            },
                                                                         (Topology.Face face, DynamicMesh.IIndexedVertexAttributes vertexAttributes) =>
            {
                vertexAttributes.color = pathColor;
                vertexAttributes.Advance();
            });

            _triangulationBlocked = new SeparatedFacesUmbrellaTriangulation(2,
                                                                            (Topology.FaceEdge edge, DynamicMesh.IIndexedVertexAttributes vertexAttributes) =>
            {
                vertexAttributes.Advance();

                vertexAttributes.normal = (vertexAttributes.position + Vector3.up * 0.25f - _facePositions[edge.nearFace]).normalized;
                vertexAttributes.color  = blockedColor;
                vertexAttributes.Advance();
            },
                                                                            (Topology.Face face, DynamicMesh.IIndexedVertexAttributes vertexAttributes) =>
            {
                vertexAttributes.color = blockedColor;
                vertexAttributes.Advance();
            });

            _pathFinder = new PathFinder();
        }