Beispiel #1
0
        /// <summary>
        /// If no "Node" instance, this method tries to create one
        /// given the Collide.Shape component in this game object.
        /// </summary>
        /// <returns>True if the node was created - otherwise false.</returns>
        private bool TryInitialize(Shape shape, DebugRenderManager manager)
        {
            if (Node != null)
            {
                return(false);
            }

            Collide.Mesh   mesh           = shape as Collide.Mesh;
            HeightField    heightField    = shape as HeightField;
            Cone           cone           = shape as Cone;
            HollowCone     hollowCone     = shape as HollowCone;
            HollowCylinder hollowCylinder = shape as HollowCylinder;

            if (mesh != null)
            {
                Node = InitializeMesh(mesh);
            }
            else if (heightField != null)
            {
                Node = InitializeHeightField(heightField);
            }
            else if (hollowCone != null)
            {
                Node = new GameObject(PrefabName);
                Node.AddComponent <MeshRenderer>().sharedMaterial = manager.ShapeRenderMaterial;
                Node.AddComponent <MeshFilter>().sharedMesh       = ShapeVisualHollowCone.GenerateMesh(shape);
            }
            else if (hollowCylinder != null)
            {
                Node = new GameObject(PrefabName);
                Node.AddComponent <MeshRenderer>().sharedMaterial = manager.ShapeRenderMaterial;
                Node.AddComponent <MeshFilter>().sharedMesh       = ShapeVisualHollowCylinder.GenerateMesh(shape);
            }
            else if (cone != null)
            {
                Node = new GameObject(PrefabName);
                Node.AddComponent <MeshRenderer>().sharedMaterial = manager.ShapeRenderMaterial;
                Node.AddComponent <MeshFilter>().sharedMesh       = ShapeVisualCone.GenerateMesh(shape);
            }
            else
            {
                Node = PrefabLoader.Instantiate <GameObject>(PrefabName);
                Node.transform.localScale = GetShape().GetScale();
            }

            if (Node != null)
            {
                var renderers = Node.GetComponentsInChildren <Renderer>();
                foreach (var renderer in renderers)
                {
                    renderer.sharedMaterial = manager.ShapeRenderMaterial;
                }
            }

            return(Node != null);
        }
Beispiel #2
0
        /// <summary>
        /// Synchronize the scale/size of the debug render object to match the shape size.
        /// Scaling is ignore if the node hasn't been created (i.e., this method doesn't
        /// create the render node).
        /// </summary>
        /// <param name="shape">Shape this component belongs to.</param>
        public void SynchronizeScale(Shape shape)
        {
            if (Node == null)
            {
                return;
            }

            Node.transform.localScale = shape.GetScale();

            Cone           cone           = shape as Cone;
            HollowCone     hollowCone     = shape as HollowCone;
            HollowCylinder hollowCylinder = shape as HollowCylinder;

            if (shape is Collide.Mesh)
            {
                if (m_storedLossyScale != transform.lossyScale)
                {
                    var mesh = shape as Collide.Mesh;
                    for (int i = 0; i < mesh.SourceObjects.Length; ++i)
                    {
                        var sub = mesh.SourceObjects[i];
                        RescaleRenderedMesh(mesh, sub, Node.transform.GetChild(i).GetComponent <MeshFilter>());
                    }
                    m_storedLossyScale = transform.lossyScale;
                }
            }
            else if (cone != null)
            {
                if (m_height != cone.Height || m_topRadius != cone.TopRadius || m_bottomRadius != cone.BottomRadius)
                {
                    Node.GetComponent <MeshFilter>().sharedMesh = ShapeVisualCone.GenerateMesh(shape);
                    m_height       = cone.Height;
                    m_topRadius    = cone.TopRadius;
                    m_bottomRadius = cone.BottomRadius;
                }
            }
            else if (hollowCone != null)
            {
                if (m_height != hollowCone.Height || m_topRadius != hollowCone.TopRadius || m_bottomRadius != hollowCone.BottomRadius || m_thickness != hollowCone.Thickness)
                {
                    Node.GetComponent <MeshFilter>().sharedMesh = ShapeVisualHollowCone.GenerateMesh(shape);
                    m_height       = hollowCone.Height;
                    m_topRadius    = hollowCone.TopRadius;
                    m_bottomRadius = hollowCone.BottomRadius;
                    m_thickness    = hollowCone.Thickness;
                }
            }
            else if (hollowCylinder != null)
            {
                if (m_height != hollowCylinder.Height || m_bottomRadius != hollowCylinder.Radius || m_thickness != hollowCylinder.Thickness)
                {
                    Node.GetComponent <MeshFilter>().sharedMesh = ShapeVisualHollowCylinder.GenerateMesh(shape);
                    m_height       = hollowCylinder.Height;
                    m_bottomRadius = hollowCylinder.Radius;
                    m_thickness    = hollowCylinder.Thickness;
                }
            }
            else if (shape is Collide.HollowCylinder)
            {
                Node.GetComponent <MeshFilter>().sharedMesh = ShapeVisualHollowCylinder.GenerateMesh(shape);
            }
            else if (shape is Capsule)
            {
                Capsule capsule = shape as Capsule;
                SetCapsuleSize(Node, capsule.Radius, capsule.Height);
            }
        }