Ejemplo n.º 1
0
    private PhysicMaterial HandleSurface(XElement surfaceElement, GameObject collisionObject)
    {
        var material = new PhysicMaterial()
        {
            bounciness      = ParseSingle(surfaceElement.Element("bounce")?.Element("restitution_coefficient"), 0.0f),
            frictionCombine = PhysicMaterialCombine.Average,
            bounceCombine   = PhysicMaterialCombine.Average
        };

        var frictionElement = surfaceElement.Element("friction")?.Element("ode");

        if (frictionElement != null)
        {
            foreach (var childElement in frictionElement.Elements())
            {
                switch (childElement.Name.ToString())
                {
                case "mu":
                    material.staticFriction  = ParseSingle(childElement, 0.0f);
                    material.dynamicFriction = material.staticFriction * 0.7f;
                    break;

                default:
                    Debug.LogWarning("unhandled element: " + childElement + " within " + surfaceElement);
                    break;
                }
            }
        }

        document.CreateAsset(material, collisionObject);
        return(material);
    }
Ejemplo n.º 2
0
    private void HandleVisualMaterial(XElement materialElement, GameObject visual)
    {
        var renderer = visual.GetComponentInChildren <Renderer>();

        renderer.materials = renderer.sharedMaterials.Select(
            oldMat =>
        {
            var newMat = new Material(oldMat);
            foreach (var childElement in materialElement.Elements())
            {
                switch (childElement.Name.ToString())
                {
                case "emissive":
                    newMat.SetColor("_EmissiveColor", ParseColor(childElement));
                    break;

                case "diffuse":
                    var baseColor = ParseColor(childElement);
                    if (baseColor.a < 1.0f)
                    {
                        newMat.SetFloat("_SurfaceType", 1.0f);
                        newMat.SetFloat("_DstBlend", 10.0f);
                        newMat.SetFloat("_AlphaDstBlend", 10.0f);
                        newMat.SetFloat("_StencilRefDepth", 0);
                        newMat.SetFloat("_StencilRefGBuffer", 2);
                        newMat.SetFloat("_StencilRefMV", 32);
                        newMat.SetFloat("_ZTestDepthEqualForOpaque", 4);
                        newMat.SetFloat("_ZWrite", 0);

                        newMat.EnableKeyword("_SURFACE_TYPE_TRANSPARENT");
                        newMat.EnableKeyword("_ENABLE_FOG_ON_TRANSPARENT");
                        newMat.renderQueue = 3000;
                        newMat.SetOverrideTag("RenderType", "Transparent");
                    }
                    newMat.SetColor("_BaseColor", baseColor);
                    break;

                case "specular":
                    newMat.SetColor("_SpecularColor", ParseColor(childElement));
                    break;

                default:
                    Debug.LogWarning("unhandled element: " + childElement + " within " + materialElement);
                    break;
                }
            }
            document.CreateAsset(newMat, visual);

            return(newMat);
        }
            ).ToArray();
    }