Exemplo n.º 1
0
    private void UpdatePart(GameObject o, DrawTask t)
    {
        o.layer = renderLayer;
        o.GetComponent <MeshFilter>().mesh = (t.mesh as VtsMesh).Get();
        Material mat = o.GetComponent <MeshRenderer>().material;

        UpdateMaterial(mat);
        bool monochromatic = false;

        if (t.texColor != null)
        {
            var tt = t.texColor as VtsTexture;
            mat.SetTexture(shaderPropertyMainTex, tt.Get());
            monochromatic = tt.monochromatic;
        }
        if (t.texMask != null)
        {
            var tt = t.texMask as VtsTexture;
            mat.SetTexture(shaderPropertyMaskTex, tt.Get());
        }
        mat.SetMatrix(shaderPropertyUvMat, VtsUtil.V2U33(t.data.uvm));
        mat.SetVector(shaderPropertyUvClip, VtsUtil.V2U4(t.data.uvClip));
        mat.SetVector(shaderPropertyColor, VtsUtil.V2U4(t.data.color));
        // flags: mask, monochromatic, flat shading, uv source
        mat.SetVector(shaderPropertyFlags, new Vector4(t.texMask == null ? 0 : 1, monochromatic ? 1 : 0, 0, t.data.externalUv ? 1 : 0));
        VtsUtil.Matrix2Transform(o.transform, VtsUtil.V2U44(Math.Mul44x44(conv, System.Array.ConvertAll(t.data.mv, System.Convert.ToDouble))));
    }
    protected void UpdateMaterial(Material mat)
    {
        VtsTexture tex = draws.celestial.atmosphere.densityTexture as VtsTexture;

        if (atmosphere && tex != null)
        {
            var cel = draws.celestial;
            var atm = cel.atmosphere;
            mat.SetVector(shaderPropertyAtmSizes, new Vector4(
                              (float)(atm.boundaryThickness / cel.majorRadius),
                              (float)(cel.majorRadius / cel.minorRadius),
                              (float)(1.0 / cel.majorRadius),
                              0));
            mat.SetVector(shaderPropertyAtmCoefficients, new Vector4(
                              (float)atm.horizontalExponent,
                              (float)atm.colorGradientExponent,
                              0,
                              0));
            mat.SetVector(shaderPropertyAtmCameraPosition, VtsUtil.V2U3(draws.camera.eye) / (float)cel.majorRadius);
            mat.SetMatrix(shaderPropertyAtmViewInv, VtsUtil.V2U44(Math.Inverse44(draws.camera.view)));
            mat.SetVector(shaderPropertyAtmColorHorizon, VtsUtil.V2U4(atm.colorHorizon));
            mat.SetVector(shaderPropertyAtmColorZenith, VtsUtil.V2U4(atm.colorZenith));
            mat.SetTexture("vtsTexAtmDensity", tex.Get());
            mat.EnableKeyword("VTS_ATMOSPHERE");
        }
        else
        {
            mat.DisableKeyword("VTS_ATMOSPHERE");
        }
    }
    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);
        }
    }
 private void RegenerateCommandBuffer(CommandBuffer buffer, List <DrawTask> tasks)
 {
     buffer.Clear();
     if (atmosphere && draws.celestial.atmosphere.densityTexture as VtsTexture != null)
     {
         buffer.EnableShaderKeyword("VTS_ATMOSPHERE");
     }
     else
     {
         buffer.DisableShaderKeyword("VTS_ATMOSPHERE");
     }
     buffer.SetViewMatrix(Matrix4x4.identity);
     buffer.SetProjectionMatrix(cam.projectionMatrix);
     foreach (DrawTask t in tasks)
     {
         if (t.mesh == null)
         {
             continue;
         }
         MaterialPropertyBlock mat    = new MaterialPropertyBlock();
         VtsTexture            atmTex = draws.celestial.atmosphere.densityTexture as VtsTexture;
         if (atmosphere && atmTex != null)
         {
             var cel = draws.celestial;
             var atm = cel.atmosphere;
             mat.SetVector(shaderPropertyAtmSizes, new Vector4(
                               (float)(atm.boundaryThickness / cel.majorRadius),
                               (float)(cel.majorRadius / cel.minorRadius),
                               (float)(1.0 / cel.majorRadius),
                               0));
             mat.SetVector(shaderPropertyAtmCoefficients, new Vector4(
                               (float)atm.horizontalExponent,
                               (float)atm.colorGradientExponent,
                               0,
                               0));
             mat.SetVector(shaderPropertyAtmCameraPosition, VtsUtil.V2U3(draws.camera.eye) / (float)cel.majorRadius);
             mat.SetMatrix(shaderPropertyAtmViewInv, VtsUtil.V2U44(Math.Inverse44(draws.camera.view)));
             mat.SetVector(shaderPropertyAtmColorHorizon, VtsUtil.V2U4(atm.colorHorizon));
             mat.SetVector(shaderPropertyAtmColorZenith, VtsUtil.V2U4(atm.colorZenith));
             mat.SetTexture("vtsTexAtmDensity", atmTex.Get());
         }
         bool monochromatic = false;
         if (t.texColor != null)
         {
             var tt = t.texColor as VtsTexture;
             mat.SetTexture(shaderPropertyMainTex, tt.Get());
             monochromatic = tt.monochromatic;
         }
         if (t.texMask != null)
         {
             mat.SetTexture(shaderPropertyMaskTex, (t.texMask as VtsTexture).Get());
         }
         mat.SetMatrix(shaderPropertyUvMat, VtsUtil.V2U33(t.data.uvm));
         mat.SetVector(shaderPropertyUvClip, VtsUtil.V2U4(t.data.uvClip));
         mat.SetVector(shaderPropertyColor, VtsUtil.V2U4(t.data.color));
         // flags: mask, monochromatic, flat shading, uv source
         mat.SetVector(shaderPropertyFlags, new Vector4(t.texMask == null ? 0 : 1, monochromatic ? 1 : 0, 0, t.data.externalUv ? 1 : 0));
         buffer.DrawMesh((t.mesh as VtsMesh).Get(), VtsUtil.V2U44(t.data.mv), renderMaterial, 0, -1, mat);
     }
 }
    private void Update()
    {
        // draw current frame
        draws.Load(vmap, vcam);
        CameraDraw();
        UpdateBackground();

        // prepare for next frame
        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);

        // statistics
        Statistics = vcam.GetStatistics();
    }
 private void RegenerateCommandBuffer(CommandBuffer buffer, List <DrawSurfaceTask> tasks, Material renderMaterial)
 {
     buffer.Clear();
     if (atmosphere && draws.celestial.atmosphere.densityTexture as VtsTexture != null)
     {
         buffer.EnableShaderKeyword("VTS_ATMOSPHERE");
     }
     else
     {
         buffer.DisableShaderKeyword("VTS_ATMOSPHERE");
     }
     buffer.SetViewMatrix(Matrix4x4.identity);
     buffer.SetProjectionMatrix(ucam.projectionMatrix);
     foreach (DrawSurfaceTask t in tasks)
     {
         if (t.mesh == null)
         {
             continue;
         }
         MaterialPropertyBlock mat    = new MaterialPropertyBlock();
         VtsTexture            atmTex = draws.celestial.atmosphere.densityTexture as VtsTexture;
         if (atmosphere && atmTex != null)
         {
             mat.SetVector(shaderPropertyAtmSizes, shaderValueAtmSizes);
             mat.SetVector(shaderPropertyAtmCoefficients, shaderValueAtmCoefficients);
             mat.SetVector(shaderPropertyAtmCameraPosition, shaderValueAtmCameraPosition);
             mat.SetMatrix(shaderPropertyAtmViewInv, shaderValueAtmViewInv);
             mat.SetVector(shaderPropertyAtmColorHorizon, shaderValueAtmColorHorizon);
             mat.SetVector(shaderPropertyAtmColorZenith, shaderValueAtmColorZenith);
             mat.SetTexture("vtsTexAtmDensity", atmTex.Get());
         }
         bool monochromatic = false;
         if (t.texColor != null)
         {
             var tt = t.texColor as VtsTexture;
             mat.SetTexture(shaderPropertyMainTex, tt.Get());
             monochromatic = tt.monochromatic;
         }
         if (t.texMask != null)
         {
             mat.SetTexture(shaderPropertyMaskTex, (t.texMask as VtsTexture).Get());
         }
         mat.SetMatrix(shaderPropertyUvMat, VtsUtil.V2U33(t.data.uvm));
         mat.SetVector(shaderPropertyUvClip, VtsUtil.V2U4(t.data.uvClip));
         mat.SetVector(shaderPropertyColor, VtsUtil.V2U4(t.data.color));
         // flags: mask, monochromatic, flat shading, uv source
         mat.SetVector(shaderPropertyFlags, new Vector4(t.texMask == null ? 0 : 1, monochromatic ? 1 : 0, 0, t.data.externalUv ? 1 : 0));
         buffer.DrawMesh((t.mesh as VtsMesh).Get(), VtsUtil.V2U44(t.data.mv), renderMaterial, 0, -1, mat);
     }
 }
    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;
        }
    }
 private void RegenerateCommandBuffer(CommandBuffer buffer, List <DrawSurfaceTask> tasks, Material renderMaterial)
 {
     buffer.Clear();
     buffer.SetViewMatrix(Matrix4x4.identity);
     buffer.SetProjectionMatrix(ucam.projectionMatrix);
     foreach (DrawSurfaceTask t in tasks)
     {
         if (t.mesh == null)
         {
             continue;
         }
         InitMaterial(propertyBlock, t);
         buffer.DrawMesh((t.mesh as VtsMesh).Get(), VtsUtil.V2U44(t.data.mv), renderMaterial, 0, -1, propertyBlock);
     }
 }
Exemplo n.º 9
0
 private void UpdateParts(List <DrawTask> tasks, List <GameObject> parts, double[] conv)
 {
     if (parts.Count == tasks.Count)
     {
         return;
     }
     if (parts.Count > 0)
     {
         foreach (GameObject p in parts)
         {
             Destroy(p);
         }
         parts.Clear();
     }
     foreach (DrawTask t in tasks)
     {
         GameObject o = Instantiate(renderPrefab);
         parts.Add(o);
         o.layer = renderLayer;
         UnityEngine.Mesh msh = (tasks[0].mesh as VtsMesh).Get();
         o.GetComponent <MeshFilter>().mesh = msh;
         Material mat = o.GetComponent <MeshRenderer>().material;
         UpdateMaterial(mat);
         bool monochromatic = false;
         if (t.texColor != null)
         {
             var tt = t.texColor as VtsTexture;
             mat.SetTexture(shaderPropertyMainTex, tt.Get());
             monochromatic = tt.monochromatic;
         }
         if (t.texMask != null)
         {
             var tt = t.texMask as VtsTexture;
             mat.SetTexture(shaderPropertyMaskTex, tt.Get());
         }
         mat.SetMatrix(shaderPropertyUvMat, VtsUtil.V2U33(t.data.uvm));
         mat.SetVector(shaderPropertyUvClip, VtsUtil.V2U4(t.data.uvClip));
         mat.SetVector(shaderPropertyColor, VtsUtil.V2U4(t.data.color));
         // flags: mask, monochromatic, flat shading, uv source
         mat.SetVector(shaderPropertyFlags, new Vector4(t.texMask == null ? 0 : 1, monochromatic ? 1 : 0, 0, t.data.externalUv ? 1 : 0));
         VtsUtil.Matrix2Transform(o.transform, VtsUtil.V2U44(Math.Mul44x44(conv, System.Array.ConvertAll(t.data.mv, System.Convert.ToDouble))));
     }
 }
Exemplo n.º 10
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);
    }
Exemplo n.º 11
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 UpdateTransform(Part part, DrawSurfaceTask task)
 {
     VtsUtil.Matrix2Transform(part.go.transform, VtsUtil.V2U44(Math.Mul44x44(conv, System.Array.ConvertAll(task.data.mv, System.Convert.ToDouble))));
 }