Beispiel #1
0
    void updateObject(SpaceBody obj)
    {
        var diff = obj.position - originPosition;
        var dist = diff.magnitude;

        int numLayers = layerMasks.Length;

        if (numLayers != obj.spawnedObjects.Length)
        {
            obj.destroyObjects();
            obj.spawnedObjects = new GameObject[numLayers];
        }

        var radius = 1.0;

        if (obj.prefab)
        {
            radius = obj.radius;
        }
        var minDist = dist - radius;
        var maxDist = dist - radius;

        var absDiff = DVec3.abs(diff);
        var absMin  = absDiff - new DVec3(radius, radius, radius);
        var absMax  = absDiff + new DVec3(radius, radius, radius);

        double currentScale = 1.0;
        double farClip      = scaleFactor;
        double nearClip     = 0.0;

        for (int curIndex = 0; curIndex < layerMasks.Length; curIndex++)
        {
            var curLayer     = layerIndexes[curIndex];
            var curLayerMask = layerMasks[curIndex];
            var clipped      = ((absMax.x < nearClip) && (absMax.y < nearClip) && (absMax.z < nearClip));

            if (curIndex < (layerMasks.Length - 1))
            {
                clipped = clipped || (absMin.x > farClip) || (absMin.y > farClip) || (absMin.z > farClip);
            }

            if (obj.hidden)
            {
                clipped = true;
            }

            if (clipped)
            {
                if (obj.spawnedObjects[curIndex])
                {
                    Destroy(obj.spawnedObjects[curIndex]);
                    obj.spawnedObjects[curIndex] = null;
                }
            }
            else
            {
                if (obj.prefab)
                {
                    var    prefab   = obj.prefab;
                    double scale    = obj.radius / (prefab.prefabRadius * currentScale);
                    var    localPos = diff / currentScale;

                    if (!obj.spawnedObjects[curIndex])
                    {
                        var displayObj = Instantiate <GameObject>(obj.prefab.gameObject);
                        moveToLayer(displayObj, curLayer);
                        obj.spawnedObjects[curIndex] = displayObj;

                        var lights = displayObj.GetComponentsInChildren <Light>();
                        for (int i = 0; i < lights.Length; i++)
                        {
                            var curLight = lights[i];
                            if (!curLight)
                            {
                                continue;
                            }
                            curLight.cullingMask = curLayerMask;
                            curLight.range      *= (float)scale;
                        }

                        displayObj.name = string.Format("{0}: layer {1}", prefab.gameObject.name, curIndex);
                    }

                    var displayObject = obj.spawnedObjects[curIndex];
                    var spawnedData   = displayObject.GetComponent <SpawnedSpaceObjectData>();
                    if (!spawnedData)
                    {
                        spawnedData = displayObject.AddComponent <SpawnedSpaceObjectData>();
                    }
                    spawnedData.modelScale  = scale;
                    spawnedData.layerIndex  = curIndex;
                    spawnedData.scaleFactor = currentScale;
                    spawnedData.radius      = obj.radius;

                    displayObject.transform.localScale = new Vector3((float)scale, (float)scale, (float)scale);
                    displayObject.transform.position   = new Vector3((float)localPos.x, (float)localPos.y, (float)localPos.z);
                }
            }

            currentScale *= scaleFactor;
            nearClip      = farClip;
            farClip      *= scaleFactor;
        }
    }
    public void updateProxies(DoubleTransformManager manager)
    {
        var origin = manager.origin;
        var dtr    = GetComponent <DoubleTransform>();

        if (!dtr)
        {
            Debug.LogWarningFormat("Double precision transform not present on: {0}", gameObject.name);
            return;
        }

        var position = dtr.position;
        var rotation = dtr.rotation;

        var diff = position - origin;
        var dist = diff.magnitude;

        int numLayers = manager.numLevels;

        setNumProxies(numLayers);

        var minDist = dist - radius;
        var maxDist = dist - radius;

        var absDiff = DVec3.abs(diff);
        var absMin  = absDiff - new DVec3(radius, radius, radius);
        var absMax  = absDiff + new DVec3(radius, radius, radius);

        int numLevels = manager.numLevels;

        for (int levelIndex = 0; levelIndex < numLevels; levelIndex++)
        {
            double nearClip     = manager.getNearClip(levelIndex);
            double farClip      = manager.getFarClip(levelIndex);
            double levelScale   = manager.getScaleFactor(levelIndex);
            var    curLayer     = manager.getLayerIndex(levelIndex);
            var    curLayerMask = manager.getLayerMask(levelIndex);

            var clipped = ((absMax.x < nearClip) && (absMax.y < nearClip) && (absMax.z < nearClip));

            if (levelIndex < (numLevels - 1))
            {
                clipped = clipped || (absMin.x > farClip) || (absMin.y > farClip) || (absMin.z > farClip);
            }

            if (!gameObject.activeInHierarchy)
            {
                clipped = true;
            }

            if (clipped)
            {
                if (proxies[levelIndex])
                {
                    Destroy(proxies[levelIndex]);
                    proxies[levelIndex] = null;
                }
            }
            else
            {
                if (proxyPrefab)
                {
                    var    prefab   = proxyPrefab;
                    double scale    = radius / (prefabRadius * levelScale);
                    var    localPos = diff / levelScale;
                    var    localRot = dtr.rotation;

                    if (!proxies[levelIndex])
                    {
                        var displayObj = Instantiate <GameObject>(prefab);
                        moveToLayer(displayObj, curLayer);
                        proxies[levelIndex] = displayObj;

                        var lights = displayObj.GetComponentsInChildren <Light>();
                        for (int i = 0; i < lights.Length; i++)
                        {
                            var curLight = lights[i];
                            if (!curLight)
                            {
                                continue;
                            }
                            curLight.cullingMask = curLayerMask;
                            curLight.range      *= (float)scale;
                        }

                        displayObj.name = string.Format("{0}: layer {1}", prefab.gameObject.name, levelIndex);
                        displayObj.transform.SetParent(manager.gameObject.transform, false);
                    }

                    var displayObject = proxies[levelIndex];

                    displayObject.transform.localScale    = new Vector3((float)scale, (float)scale, (float)scale);
                    displayObject.transform.localPosition = localPos.toVector3();
                    displayObject.transform.localRotation = localRot.normalized.toQuaternion();
                }
            }
        }
    }