void Start()
    {
        if (isServer)
        {
            color = Color.HSVToRGB(Random.value, Random.value * 0.5f + 0.5f, Random.value * 0.5f + 0.5f);
        }

        if (isLocalPlayer)
        {
            Debug.Log("Initializing world '" + config.name + "'");

            // initialize the map
            GameObject map = FindObjectOfType <VtsMap>().gameObject;
            map.GetComponent <VtsMap>().GetVtsMap().SetMapconfigPath(config.mapconfigUrl);
            {
                VtsMapMakeLocal l = map.GetComponent <VtsMapMakeLocal>();
                l.x = config.position[0];
                l.y = config.position[1];
                l.z = config.position[2];
            }

            // initialize the car
            GetComponent <VtsRigidBodyActivate>().map      = map;
            GetComponent <VtsColliderProbe>().mapObject    = map;
            GetComponent <VtsColliderProbe>().collidersLod = config.collisionlod;

            // initialize other objects
            GameObject.Find("mainCamera").GetComponent <FollowingCamera>().target   = gameObject;
            GameObject.Find("mainCamera").GetComponent <FollowingCamera>().enabled  = true;
            GameObject.Find("minimapCamera").GetComponent <MinimapCamera>().target  = gameObject;
            GameObject.Find("minimapCamera").GetComponent <MinimapCamera>().enabled = true;
            GameObject.Find("sun").transform.rotation = Quaternion.Euler(VtsUtil.V2U3(config.sunDirection));

            // enable all components
            foreach (var c in gameObject.GetComponents <MonoBehaviour>())
            {
                c.enabled = true;
            }
        }
    }
Exemplo n.º 2
0
    private void PrepareShaderData()
    {
        var cel = draws.celestial;
        var atm = cel.atmosphere;

        shaderValueAtmSizes = new Vector4(
            (float)(atm.boundaryThickness / cel.majorRadius),
            (float)(cel.majorRadius / cel.minorRadius),
            (float)(1.0 / cel.majorRadius),
            0);
        shaderValueAtmCoefficients = new Vector4(
            (float)atm.horizontalExponent,
            (float)atm.colorGradientExponent,
            0,
            0);
        shaderValueAtmCameraPosition = VtsUtil.V2U3(draws.camera.eye) / (float)cel.majorRadius;
        shaderValueAtmViewInv        = VtsUtil.V2U44(Math.Inverse44(draws.camera.view));
        shaderValueAtmColorHorizon   = VtsUtil.V2U4(atm.colorHorizon);
        shaderValueAtmColorZenith    = VtsUtil.V2U4(atm.colorZenith);
        shaderValueAtmEnabled        = Shader.IsKeywordEnabled("VTS_ATMOSPHERE") && draws.celestial.atmosphere.densityTexture != null;
        shaderValueFrameIndex++;
    }
    private void FixedUpdate()
    {
        // update current colliders
        draws.Load(vmap, vcam);
        UpdateParts();

        // prepare for next frame
        vcam.SetViewportSize(1, 1);
        double[] Mu = Math.Mul44x44(VtsUtil.U2V44(mapTrans.localToWorldMatrix), VtsUtil.U2V44(VtsUtil.SwapYZ));
        double[] view = Math.Mul44x44(VtsUtil.U2V44(probTrans.localToWorldMatrix.inverse), Mu);
        vcam.SetView(view);

        // enforce fixed traversal mode
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("{ \"fixedTraversalDistance\":").Append(collidersDistance).Append(", \"fixedTraversalLod\":").Append(collidersLod).Append(", \"traverseModeSurfaces\":4, \"traverseModeGeodata\":0 }");
            vcam.SetOptions(builder.ToString());
        }

        // statistics
        Statistics = vcam.GetStatistics();
    }
Exemplo n.º 4
0
    protected override void CameraUpdate()
    {
        double[] conv = Math.Mul44x44(Math.Mul44x44(VtsUtil.U2V44(mapTrans.localToWorldMatrix), VtsUtil.U2V44(VtsUtil.SwapYZ)), Math.Inverse44(draws.camera.view));

        Dictionary <VtsMesh, List <DrawTask> > tasksByMesh = new Dictionary <VtsMesh, List <DrawTask> >();

        foreach (DrawTask t in draws.opaque)
        {
            VtsMesh k = t.mesh as VtsMesh;
            if (!tasksByMesh.ContainsKey(k))
            {
                tasksByMesh.Add(k, new List <DrawTask>());
            }
            tasksByMesh[k].Add(t);
        }

        HashSet <VtsMesh> partsToRemove = new HashSet <VtsMesh>(partsCache.Keys);

        foreach (KeyValuePair <VtsMesh, List <DrawTask> > tbm in tasksByMesh)
        {
            if (!partsCache.ContainsKey(tbm.Key))
            {
                partsCache.Add(tbm.Key, new List <GameObject>(tbm.Value.Count));
            }
            UpdateParts(tbm.Value, partsCache[tbm.Key], conv);
            partsToRemove.Remove(tbm.Key);
        }

        foreach (VtsMesh m in partsToRemove)
        {
            foreach (GameObject o in partsCache[m])
            {
                Destroy(o);
            }
            partsCache.Remove(m);
        }
    }
    private void UpdateParts()
    {
        double[] conv = Math.Mul44x44(Math.Mul44x44(VtsUtil.U2V44(mapTrans.localToWorldMatrix), VtsUtil.U2V44(VtsUtil.SwapYZ)), Math.Inverse44(draws.camera.view));

        Dictionary <VtsMesh, DrawTask> tasksByMesh = new Dictionary <VtsMesh, DrawTask>();

        foreach (DrawTask t in draws.colliders)
        {
            VtsMesh k = t.mesh as VtsMesh;
            if (!tasksByMesh.ContainsKey(k))
            {
                tasksByMesh.Add(k, t);
            }
        }

        HashSet <VtsMesh> partsToRemove = new HashSet <VtsMesh>(partsCache.Keys);

        foreach (KeyValuePair <VtsMesh, DrawTask> tbm in tasksByMesh)
        {
            if (!partsCache.ContainsKey(tbm.Key))
            {
                GameObject o = Instantiate(colliderPrefab);
                partsCache.Add(tbm.Key, o);
                UnityEngine.Mesh msh = (tbm.Value.mesh as VtsMesh).Get();
                o.GetComponent <MeshCollider>().sharedMesh = msh;
                VtsUtil.Matrix2Transform(o.transform, VtsUtil.V2U44(Math.Mul44x44(conv, System.Array.ConvertAll(tbm.Value.data.mv, System.Convert.ToDouble))));
            }
            partsToRemove.Remove(tbm.Key);
        }

        foreach (VtsMesh m in partsToRemove)
        {
            Destroy(partsCache[m]);
            partsCache.Remove(m);
        }
    }
Exemplo n.º 6
0
 protected override void CameraDraw()
 {
     conv = Math.Mul44x44(Math.Mul44x44(VtsUtil.U2V44(mapTrans.localToWorldMatrix), VtsUtil.U2V44(VtsUtil.SwapYZ)), Math.Inverse44(draws.camera.view));
     UpdateOpaqueDraws();
     UpdateTransparentDraws();
 }
Exemplo n.º 7
0
    private void Update()
    {
        // sync transformation etc.
        vcam.SetViewportSize((uint)ucam.pixelWidth, (uint)ucam.pixelHeight);
        double[] Mu = Math.Mul44x44(VtsUtil.U2V44(mapTrans.localToWorldMatrix), VtsUtil.U2V44(VtsUtil.SwapYZ));
        if (controlTransformation == VtsDataControl.Vts)
        {
            double[] view = vcam.GetView();
            VtsUtil.Matrix2Transform(camTrans, VtsUtil.V2U44(Math.Mul44x44(Math.Inverse44(Math.Mul44x44(view, Mu)), VtsUtil.U2V44(VtsUtil.InvertZ))));
        }
        else
        {
            double[] view = Math.Mul44x44(VtsUtil.U2V44(ucam.worldToCameraMatrix), Mu);
            vcam.SetView(view);
        }
        if (controlNearFar == VtsDataControl.Vts)
        {
            double n, f;
            vcam.SuggestedNearFar(out n, out f);
            ucam.nearClipPlane = (float)n;
            ucam.farClipPlane  = (float)f;
        }
        vcam.SetProj(ucam.fieldOfView, ucam.nearClipPlane, ucam.farClipPlane);
        Matrix4x4 proj = VtsUtil.V2U44(vcam.GetProj());

        if (proj[0] == proj[0] && proj[0] != 0)
        {
            ucam.projectionMatrix = proj;
        }

        // draw
        vcam.RenderUpdate();
        draws.Load(vmap, vcam);
        PrepareShaderData();
        CameraDraw();
        UpdateBackground();

        // statistics
        Statistics = vcam.GetStatistics();
    }
 protected override void CameraDraw()
 {
     shiftingOriginMap = mapObject.GetComponent <VtsMapShiftingOrigin>();
     conv = Math.Mul44x44(Math.Mul44x44(VtsUtil.U2V44(mapTrans.localToWorldMatrix), VtsUtil.U2V44(VtsUtil.SwapYZ)), Math.Inverse44(draws.camera.view));
     UpdateSurfacesCache(opaquePrefab, draws.opaque, opaquePartsCache);
     UpdateSurfacesCache(transparentPrefab, draws.transparent, transparentPartsCache);
     originHasShifted = false;
 }
 private void UpdateTransform(Part part, DrawSurfaceTask task)
 {
     VtsUtil.Matrix2Transform(part.go.transform, VtsUtil.V2U44(Math.Mul44x44(conv, System.Array.ConvertAll(task.data.mv, System.Convert.ToDouble))));
 }
Exemplo n.º 10
0
 protected override void CameraDraw()
 {
     shiftingOriginMap = mapObject.GetComponent <VtsMapShiftingOrigin>();
     conv = Math.Mul44x44(Math.Mul44x44(VtsUtil.U2V44(mapTrans.localToWorldMatrix), VtsUtil.U2V44(VtsUtil.SwapYZ)), Math.Inverse44(draws.camera.view));
     UpdateOpaqueDraws();
     UpdateTransparentDraws();
 }
Exemplo n.º 11
0
    private void PerformShift()
    {
        // the focus object must be moved
        Debug.Assert(focusObject.GetComponentInParent <VtsObjectShiftingOriginBase>());

        // compute the transformation change
        double[] originalNavigationPoint = umap.UnityToVtsNavigation(VtsUtil.ZeroV);
        double[] targetNavigationPoint   = umap.UnityToVtsNavigation(VtsUtil.U2V3(focusObject.transform.position));
        if (!VtsMapMakeLocal.MakeLocal(umap, targetNavigationPoint))
        {
            Debug.Assert(false, "failed shifting origin");
            return;
        }
        Vector3 move = -focusObject.transform.position;
        float   Yrot = (float)(targetNavigationPoint[0] - originalNavigationPoint[0]) * Mathf.Sign((float)originalNavigationPoint[1]);

        // find objects that will be transformed
        var objs = new System.Collections.Generic.List <VtsObjectShiftingOriginBase>();

        foreach (VtsObjectShiftingOriginBase obj in FindObjectsOfType <VtsObjectShiftingOriginBase>())
        {
            // ask if the object allows to be transformed by this map
            if (obj.enabled && obj.OnBeforeOriginShift(this))
            {
                objs.Add(obj);
            }
        }

        // actually transform the objects
        foreach (VtsObjectShiftingOriginBase obj in objs)
        {
            // only transform object's topmost ancestor - its childs will inherit the change
            // an object is shifted only once even if it has multiple VtsObjectShiftingOriginBase components
            if (!obj.transform.parent || !obj.transform.parent.GetComponentInParent <VtsObjectShiftingOriginBase>() &&
                obj == obj.GetComponents <VtsObjectShiftingOriginBase>()[0])
            {
                obj.transform.localPosition += move;
                obj.transform.RotateAround(Vector3.zero, Vector3.up, Yrot);
            }
        }

        // notify the object that it was transformed
        foreach (VtsObjectShiftingOriginBase obj in objs)
        {
            obj.OnAfterOriginShift();
        }

        // force all objects cameras to recompute positions -> improves precision
        foreach (VtsCameraBase cam in FindObjectsOfType <VtsCameraBase>())
        {
            cam.OriginShifted();
        }

        // force all collider probes to recompute positions -> improves precision
        // warning: this has big performance impact!
        if (updateColliders)
        {
            foreach (VtsColliderProbe col in FindObjectsOfType <VtsColliderProbe>())
            {
                col.OriginShifted();
            }
        }
    }
    private void CamOverrideView(ref double[] values)
    {
        double[] Mu = Math.Mul44x44(VtsUtil.U2V44(mapTrans.localToWorldMatrix), VtsUtil.U2V44(VtsUtil.SwapYZ));
        // view matrix
        switch (controlTransformation)
        {
        case VtsDataControl.Vts:
            VtsUtil.Matrix2Transform(camTrans, VtsUtil.V2U44(Math.Mul44x44(Math.Inverse44(Math.Mul44x44(values, Mu)), VtsUtil.U2V44(VtsUtil.InvertZ))));
            break;

        case VtsDataControl.Unity:
            values = Math.Mul44x44(VtsUtil.U2V44(cam.worldToCameraMatrix), Mu);
            break;
        }
    }