コード例 #1
0
    private void SetNeededComponents()
    {
        rb           = GetComponent <Rigidbody>();
        allRenderers = GetComponentsInChildren <Renderer>().ToList();
        allLights    = GetComponentsInChildren <Light>();
        if (allLights.Length == 0)
        {
            Debug.LogError(gameObject.name + " did not find any lights!");
        }

        Color.RGBToHSV(NPCColor, out float h, out float s, out float v);
        h        = Mathf.Clamp01(RandomGenerator.NextFloat(h - 0.01f, h + 0.01f));
        v        = Mathf.Clamp01(RandomGenerator.NextFloat(v - 0.1f, v + 0.1f));
        NPCColor = Color.HSVToRGB(h, s, v);

        MainCollider = GetComponentInChildren <MeshCollider>();

        // wheel collider holder
        wheelColliderHolder = new GameObject("WheelColliderHolder");
        wheelColliderHolder.transform.SetParent(transform.GetChild(0));
        wheelColliderHolder.SetActive(true);

        foreach (Renderer child in allRenderers)
        {
            if (child.name.Contains("Wheel") && !child.name.Contains("Spare"))
            {
                AddWheel(child.transform);
            }

            if (child.name.Contains("Body"))
            {
                var rendererMats = child.materials;
                for (int i = 0; i < rendererMats.Length; i++)
                {
                    if (rendererMats[i].name.Contains("Body"))
                    {
                        rendererMats[i].SetColor("_BaseColor", NPCColor);
                    }
                }
                if (MainCollider == null)
                {
                    MainCollider        = child.gameObject.AddComponent <MeshCollider>();
                    MainCollider.convex = true;
                }
            }
            {
                var rendererMats = child.materials;
                for (int i = 0; i < rendererMats.Length; i++)
                {
                    if (rendererMats[i].name.Contains("LightHead"))
                    {
                        headLight = new IndicatorRenderer()
                        {
                            renderer = child, materialIndex = i
                        }
                    }
                    ;
                    if (rendererMats[i].name.Contains("LightBrake"))
                    {
                        brakeLight = new IndicatorRenderer()
                        {
                            renderer = child, materialIndex = i
                        }
                    }
                    ;
                    if (rendererMats[i].name.Contains("IndicatorLeft"))
                    {
                        indicatorLeft = new IndicatorRenderer()
                        {
                            renderer = child, materialIndex = i
                        }
                    }
                    ;
                    if (rendererMats[i].name.Contains("IndicatorRight"))
                    {
                        indicatorRight = new IndicatorRenderer()
                        {
                            renderer = child, materialIndex = i
                        }
                    }
                    ;
                    if (rendererMats[i].name.Contains("IndicatorReverse"))
                    {
                        indicatorReverse = new IndicatorRenderer()
                        {
                            renderer = child, materialIndex = i
                        }
                    }
                    ;
                }
            }
        }

        MainCollider.enabled          = true;
        MainCollider.gameObject.layer = LayerMask.NameToLayer("NPC");

        foreach (Light light in allLights)
        {
            if (light.name.Contains("Head"))
            {
                headLights.Add(light);
            }
            else if (light.name.Contains("Brake"))
            {
                brakeLights.Add(light);
            }
            else if (light.name.Contains("IndicatorLeft"))
            {
                indicatorLeftLights.Add(light);
            }
            else if (light.name.Contains("IndicatorRight"))
            {
                indicatorRightLights.Add(light);
            }
            else if (light.name.Contains("IndicatorReverse"))
            {
                indicatorReverseLight = light;
            }
        }

        if (headLights.Count == 0)
        {
            Debug.LogWarning($"Asset {gameObject.name} missing light 'Head'");
        }
        if (brakeLights.Count == 0)
        {
            Debug.LogWarning($"Asset {gameObject.name} missing light 'Brake'");
        }
        if (indicatorLeftLights.Count == 0)
        {
            Debug.LogWarning($"Asset {gameObject.name} missing light 'IndicatorLeft'");
        }
        if (indicatorRightLights.Count == 0)
        {
            Debug.LogWarning($"Asset {gameObject.name} missing light 'IndicatorRight'");
        }
        if (indicatorReverseLight == null)
        {
            Debug.LogWarning($"Asset {gameObject.name} missing light 'IndicatorReverse'");
        }
        if (MainCollider == null)
        {
            Debug.LogWarning($"Asset {gameObject.name} missing renderer 'Body'");
        }
        if (headLight == null)
        {
            Debug.LogWarning($"Asset {gameObject.name} missing material 'LightHead'");
        }
        if (brakeLight == null)
        {
            Debug.LogWarning($"Asset {gameObject.name} missing material 'LightBrake'");
        }
        if (indicatorRight == null)
        {
            Debug.LogWarning($"Asset {gameObject.name} missing material 'IndicatorRight'");
        }
        if (indicatorLeft == null)
        {
            Debug.LogWarning($"Asset {gameObject.name} missing material 'IndicatorLeft'");
        }
        if (indicatorReverse == null)
        {
            Debug.LogWarning($"Asset {gameObject.name} missing material 'IndicatorReverse'");
        }

        Bounds = new Bounds(transform.position, Vector3.zero);
        foreach (Renderer renderer in allRenderers)
        {
            Bounds.Encapsulate(renderer.bounds); // renderer.bounds is world space
        }

        // centerOfMass is relative to the transform origin
        if (wheels.Count < 4 || name.Contains("Trailer"))
        {
            rb.centerOfMass = Bounds.center + new Vector3(0, -Bounds.extents.y * 0.15f, 0);
        }
        else
        {
            rb.centerOfMass = Bounds.center + new Vector3(0, 0, Bounds.extents.z * 0.3f);
        }

        // GroundTruth Box Collider
        var gtBox         = new GameObject("GroundTruthBox");
        var gtBoxCollider = gtBox.AddComponent <BoxCollider>();

        gtBoxCollider.isTrigger = true;
        gtBoxCollider.size      = Bounds.size;
        gtBoxCollider.center    = new Vector3(Bounds.center.x, Bounds.center.y, Bounds.center.z);
        gtBox.transform.parent  = transform;
        gtBox.layer             = LayerMask.NameToLayer("GroundTruth");

        // front transforms
        GameObject go = new GameObject("Front");

        go.transform.position = new Vector3(Bounds.center.x, Bounds.min.y + 0.5f, Bounds.center.z + Bounds.max.z);
        go.transform.SetParent(transform, true);
        frontCenter           = go.transform;
        go                    = new GameObject("FrontHigh");
        go.transform.position = new Vector3(Bounds.center.x, Bounds.max.y, Bounds.center.z + Bounds.max.z);
        go.transform.SetParent(transform, true);
        frontCenterHigh = go.transform;
        go = new GameObject("Right");
        go.transform.position = new Vector3(Bounds.center.x + Bounds.max.x, Bounds.min.y + 0.5f, Bounds.center.z + Bounds.max.z);
        go.transform.SetParent(transform, true);
        frontRight            = go.transform;
        go                    = new GameObject("Left");
        go.transform.position = new Vector3(Bounds.center.x - Bounds.max.x, Bounds.min.y + 0.5f, Bounds.center.z + Bounds.max.z);
        go.transform.SetParent(transform, true);
        frontLeft = go.transform;
    }
コード例 #2
0
    private void SetNeededComponents()
    {
        MetaData = GetComponentInChildren <NPCMetaData>();
        rb       = GetComponent <Rigidbody>();
        if (MetaData.RefRB != null)
        {
            rb.mass                   = MetaData.RefRB.mass;
            rb.drag                   = MetaData.RefRB.drag;
            rb.angularDrag            = MetaData.RefRB.angularDrag;
            rb.useGravity             = MetaData.RefRB.useGravity;
            rb.isKinematic            = MetaData.RefRB.isKinematic;
            rb.interpolation          = MetaData.RefRB.interpolation;
            rb.collisionDetectionMode = MetaData.RefRB.collisionDetectionMode;
            rb.constraints            = MetaData.RefRB.constraints;
            Destroy(MetaData.RefRB);
        }

        allRenderers = GetComponentsInChildren <Renderer>().ToList();
        allLights    = GetComponentsInChildren <Light>();

        Color.RGBToHSV(NPCColor, out float h, out float s, out float v);
        h        = Mathf.Clamp01(RandomGenerator.NextFloat(h - 0.01f, h + 0.01f));
        v        = Mathf.Clamp01(RandomGenerator.NextFloat(v - 0.1f, v + 0.1f));
        NPCColor = Color.HSVToRGB(h, s, v);

        MainCollider = GetComponentInChildren <MeshCollider>();

        // wheel collider holder
        var hasWheelColliders = true;

        wheelColliderHolder = MetaData.WheelColliderHolder;
        if (wheelColliderHolder == null)
        {
            wheelColliderHolder = new GameObject("WheelColliderHolder");
            wheelColliderHolder.transform.SetParent(transform.GetChild(0));
            wheelColliderHolder.SetActive(true);
            hasWheelColliders = false;
        }
        else
        {
            foreach (var data in MetaData.WheelData)
            {
                wheels.Add(data);
                DistributeTransform(data.transform);
            }
        }

        foreach (Renderer child in allRenderers)
        {
            if (!hasWheelColliders)
            {
                if (child.name.Contains("Wheel") && !child.name.Contains("Spare"))
                {
                    AddWheel(child.transform);
                }
            }

            if (child.name.Contains("Body"))
            {
                var rendererMats = child.materials;
                for (int i = 0; i < rendererMats.Length; i++)
                {
                    if (rendererMats[i].name.Contains("Body"))
                    {
                        rendererMats[i].SetColor("_BaseColor", NPCColor);
                    }
                }
                if (MainCollider == null)
                {
                    MainCollider        = child.gameObject.AddComponent <MeshCollider>();
                    MainCollider.convex = true;
                }
            }
            {
                var rendererMats = child.materials;
                for (int i = 0; i < rendererMats.Length; i++)
                {
                    if (rendererMats[i].name.Contains("LightHead"))
                    {
                        headLight = new IndicatorRenderer()
                        {
                            renderer = child, materialIndex = i
                        }
                    }
                    ;
                    if (rendererMats[i].name.Contains("LightBrake"))
                    {
                        brakeLight = new IndicatorRenderer()
                        {
                            renderer = child, materialIndex = i
                        }
                    }
                    ;
                    if (rendererMats[i].name.Contains("IndicatorLeft"))
                    {
                        indicatorLeft = new IndicatorRenderer()
                        {
                            renderer = child, materialIndex = i
                        }
                    }
                    ;
                    if (rendererMats[i].name.Contains("IndicatorRight"))
                    {
                        indicatorRight = new IndicatorRenderer()
                        {
                            renderer = child, materialIndex = i
                        }
                    }
                    ;
                    if (rendererMats[i].name.Contains("IndicatorReverse"))
                    {
                        indicatorReverse = new IndicatorRenderer()
                        {
                            renderer = child, materialIndex = i
                        }
                    }
                    ;
                }
            }
        }

        MainCollider.enabled          = true;
        MainCollider.gameObject.layer = LayerMask.NameToLayer("NPC");

        foreach (Light light in allLights)
        {
            if (light.name.Contains("Head"))
            {
                headLights.Add(light);
            }
            else if (light.name.Contains("Brake"))
            {
                brakeLights.Add(light);
            }
            else if (light.name.Contains("IndicatorLeft"))
            {
                indicatorLeftLights.Add(light);
            }
            else if (light.name.Contains("IndicatorRight"))
            {
                indicatorRightLights.Add(light);
            }
            else if (light.name.Contains("IndicatorReverse"))
            {
                indicatorReverseLight = light;
            }
        }

        Bounds = new Bounds(transform.position, Vector3.zero);
        foreach (Renderer renderer in allRenderers)
        {
            Bounds.Encapsulate(renderer.bounds); // renderer.bounds is world space
        }

        // centerOfMass is relative to the transform origin
        if (wheels.Count < 4 || name.Contains("Trailer"))
        {
            rb.centerOfMass = Bounds.center + new Vector3(0, -Bounds.extents.y * 0.15f, 0);
        }
        else
        {
            rb.centerOfMass = Bounds.center + new Vector3(0, 0, Bounds.extents.z * 0.3f);
        }

        // GroundTruth Box Collider
        var gtBox         = new GameObject("GroundTruthBox");
        var gtBoxCollider = gtBox.AddComponent <BoxCollider>();

        gtBoxCollider.isTrigger = true;
        gtBoxCollider.size      = Bounds.size;
        gtBoxCollider.center    = new Vector3(Bounds.center.x, Bounds.center.y, Bounds.center.z);
        gtBox.transform.parent  = transform;
        gtBox.layer             = LayerMask.NameToLayer("GroundTruth");

        // front transforms
        GameObject go = new GameObject("Front");

        go.transform.position = new Vector3(Bounds.center.x, Mathf.Clamp(Bounds.min.y + (Bounds.max.y / 2), 0.01f, 1f), Bounds.center.z + Bounds.max.z);
        go.transform.SetParent(transform, true);
        frontCenter           = go.transform;
        go                    = new GameObject("FrontHigh");
        go.transform.position = new Vector3(Bounds.center.x, Mathf.Clamp(Bounds.max.y, 0.01f, 1.5f), Bounds.center.z + Bounds.max.z);
        go.transform.SetParent(transform, true);
        frontCenterHigh = go.transform;
        go = new GameObject("Right");
        go.transform.position = new Vector3(Bounds.center.x + Bounds.max.x, Mathf.Clamp(Bounds.min.y + (Bounds.max.y / 2), 0.01f, 1f), Bounds.center.z + Bounds.max.z);
        go.transform.SetParent(transform, true);
        frontRight            = go.transform;
        go                    = new GameObject("Left");
        go.transform.position = new Vector3(Bounds.center.x - Bounds.max.x, Mathf.Clamp(Bounds.min.y + (Bounds.max.y / 2), 0.01f, 1f), Bounds.center.z + Bounds.max.z);
        go.transform.SetParent(transform, true);
        frontLeft = go.transform;

        // animation
        AgentAnimator = GetComponentInChildren <Animator>();
    }