Exemplo n.º 1
0
        private void CalculateVpMatrixOffsetsAndTileRects(Camera camera)
        {
            int   counter = 0;
            int   tileNum = drawInfo.tileX * drawInfo.tileY;
            float aspect  = camera.aspect;

            if (m_VpOffsetParam == null || m_VpOffsetParam.Length != tileNum)
            {
                m_VpOffsetParam = new Vector4[tileNum];
            }
            if (m_ScreenRectParam == null || m_ScreenRectParam.Length != tileNum)
            {
                m_ScreenRectParam = new Vector4[tileNum];
            }

            float width  = 1.0f / (float)drawInfo.tileX;
            float height = 1.0f / (float)drawInfo.tileY;

            for (int i = 0; i < drawInfo.tileY; ++i)
            {
                for (int j = 0; j < drawInfo.tileX; ++j)
                {
                    m_VpOffsetParam[counter] = LookingGlassUtil.GetVPMatrixOffsets(aspect, perCameraInfo.fov, perCameraInfo.size, counter, tileNum);

                    m_ScreenRectParam[counter] = new Vector4(
                        (j / (float)drawInfo.tileX) * 2.0f - 1.0f + width,
                        -((i / (float)drawInfo.tileY) * 2.0f - 1.0f + height),
                        width, height);
                    ++counter;
                }
            }
        }
Exemplo n.º 2
0
        public override void Execute(ScriptableRenderer renderer, ScriptableRenderContext context, ref RenderingData renderingData)
        {
            Camera camera = renderingData.cameraData.camera;

            LookingGlassUtil.SetupCameraInfo(camera, perCameraInfo.fov, perCameraInfo.size, perCameraInfo.nearClipFactor, perCameraInfo.farClipFactor);

            // clear Tile Texture
            commandBuffer.SetRenderTarget(dstTiledTexture);
            commandBuffer.ClearRenderTarget(true, true, Color.black);
            context.ExecuteCommandBuffer(commandBuffer);

            // setup
            CalculateVpMatrixOffsetsAndTileRects(camera);
            Shader.SetGlobalVector("LookingQuiltSize", new Vector4(dstTiledTexture.width, dstTiledTexture.height, 0, 0));

            MaterialPropertyBlock materialPropertyBlock = new MaterialPropertyBlock();

            materialPropertyBlock.SetVectorArray("LookingVPOffset", m_VpOffsetParam);
            materialPropertyBlock.SetVectorArray("LookingScreenRect", m_ScreenRectParam);
            DebugSetup(camera, materialPropertyBlock);


            Shader.EnableKeyword(LgInstancingShaderKeyword);
            // todo メッシュ一覧取得周りの仕組みつくる

            var meshFilters = Resources.FindObjectsOfTypeAll <MeshFilter>();

            foreach (var meshFilter in meshFilters)
            {
                var meshRenderer = meshFilter.GetComponent <MeshRenderer>();
                var mesh         = meshFilter.sharedMesh;
                if (mesh == null)
                {
                    continue;
                }
                if (meshRenderer == null || meshRenderer.sharedMaterial == null)
                {
                    continue;
                }
                var material = meshRenderer.sharedMaterial;

                if (!material.enableInstancing)
                {
                    continue;
                }

                var matrix = meshFilter.transform.localToWorldMatrix;
                this.CalculateInstancingMatrix(ref matrix);


                commandBuffer.DrawMeshInstanced(mesh, 0, material, 0, this.m_RenderMatrix,
                                                m_RenderMatrix.Length, materialPropertyBlock);
            }
            context.ExecuteCommandBuffer(commandBuffer);
            context.Submit();
            commandBuffer.Clear();

            Shader.DisableKeyword(LgInstancingShaderKeyword);
        }
Exemplo n.º 3
0
        public override void Execute(ScriptableRenderer renderer, ScriptableRenderContext context, ref RenderingData renderingData)
        {
            int w = drawInfo.renderTargetW / drawInfo.tileX;
            int h = drawInfo.renderTargetH / drawInfo.tileY;

            RenderTextureDescriptor renderTextureDesc = new RenderTextureDescriptor(w, h, RenderTextureFormat.ARGB32, 16);

            RenderTexture tempRenderTexture = RenderTexture.GetTemporary(renderTextureDesc);
            Camera        camera            = renderingData.cameraData.camera;

            LookingGlassUtil.SetupCameraInfo(camera, perCameraInfo.fov, perCameraInfo.size, perCameraInfo.nearClipFactor, perCameraInfo.farClipFactor);

            var opaqueSortFlag     = renderingData.cameraData.defaultOpaqueSortFlags;
            var opaquedrawSettings = CreateDrawRendererSettings(camera, opaqueSortFlag, rendererConfiguration, renderingData.supportsDynamicBatching);

            var transDrawSettings = CreateDrawRendererSettings(camera, SortFlags.CommonTransparent, rendererConfiguration, renderingData.supportsDynamicBatching);

            // clear Tile Texture
            commandBuffer.SetRenderTarget(dstTiledTexture);
            commandBuffer.ClearRenderTarget(true, true, Color.black);
            context.ExecuteCommandBuffer(commandBuffer);
            context.Submit();
            commandBuffer.Clear();

            var tileSize = new Vector2(w, h);
            int counter  = 0;
            int tileNum  = drawInfo.tileX * drawInfo.tileY;

            for (int i = 0; i < drawInfo.tileY; ++i)
            {
                for (int j = 0; j < drawInfo.tileX; ++j)
                {
                    SetupVPMatrices(context, commandBuffer, camera, counter, tileNum);

                    commandBuffer.SetRenderTarget(tempRenderTexture);
                    commandBuffer.ClearRenderTarget(true, true, Color.black);
                    context.ExecuteCommandBuffer(commandBuffer);
                    commandBuffer.Clear();

                    // opaque renderer draw
                    context.DrawRenderers(renderingData.cullResults.visibleRenderers, ref opaquedrawSettings, m_OpaqueFilterSettings);

                    // Render objects that did not match any shader pass with error shader
                    renderer.RenderObjectsWithError(context, ref renderingData.cullResults, camera, m_OpaqueFilterSettings, SortFlags.None);

                    // transparent renderer Draw
                    context.DrawRenderers(renderingData.cullResults.visibleRenderers, ref transDrawSettings, m_TransparentFilterSettings);

                    commandBuffer.CopyTexture(tempRenderTexture, 0, 0, 0, 0, tempRenderTexture.width, tempRenderTexture.height,
                                              dstTiledTexture, 0, 0, j * w, i * h);

                    context.ExecuteCommandBuffer(commandBuffer);
                    context.Submit();
                    ++counter;
                }
            }
            commandBuffer.Clear();
            RenderTexture.ReleaseTemporary(tempRenderTexture);
        }
Exemplo n.º 4
0
        public static void SetupCameraInfo(Camera camera, float fov, float size, float nearClipFactor, float farClipFactor)
        {
            camera.fieldOfView = fov;
            float adjustedDistance = LookingGlassUtil.GetAdjustedDistance(fov, size);

            camera.nearClipPlane = adjustedDistance - nearClipFactor * size;
            camera.farClipPlane  = adjustedDistance + farClipFactor * size;

            camera.transform.position      = new Vector3(0, 0, -adjustedDistance);
            camera.transform.localRotation = Quaternion.identity;
        }
Exemplo n.º 5
0
        public static Vector4 GetVPMatrixOffsets(float aspect, float fov, float size, int view, int numViews)
        {
            float adjustedDistance = LookingGlassUtil.GetAdjustedDistance(fov, size);

            float verticalAngle   = 0.0f;
            float horizontalAngle = AngleAtView(view, numViews);

            float offsetX = adjustedDistance * Mathf.Tan(horizontalAngle * Mathf.Deg2Rad);
            float offsetY = adjustedDistance * Mathf.Tan(verticalAngle * Mathf.Deg2Rad);

            Vector4 result = new Vector4(offsetX, offsetY,
                                         offsetX / (size * aspect), offsetY / size);

            return(result);
        }
Exemplo n.º 6
0
        private void DebugSetup(Camera camera, MaterialPropertyBlock materialPropertyBlock)
        {
            int tileNum = drawInfo.tileX * drawInfo.tileY;

            Matrix4x4[] lookingView       = new Matrix4x4[tileNum];
            Matrix4x4[] lookingProjection = new Matrix4x4[tileNum];
            Matrix4x4[] lookingVp         = new Matrix4x4[tileNum];
            int         counter           = 0;

            for (int i = 0; i < drawInfo.tileY; ++i)
            {
                for (int j = 0; j < drawInfo.tileX; ++j)
                {
                    Matrix4x4 projMatrix = camera.projectionMatrix;
                    Matrix4x4 viewMatrix = camera.worldToCameraMatrix;

                    float adjustedDistance = LookingGlassUtil.GetAdjustedDistance(perCameraInfo.fov, perCameraInfo.size);

                    float verticalAngle   = 0.0f;
                    float horizontalAngle = LookingGlassUtil.AngleAtView(counter, tileNum);

                    float offsetX = adjustedDistance * Mathf.Tan(horizontalAngle * Mathf.Deg2Rad);
                    float offsetY = adjustedDistance * Mathf.Tan(verticalAngle * Mathf.Deg2Rad);

                    // view matrix
                    viewMatrix.m03 -= offsetX;
                    viewMatrix.m13 -= offsetY;

                    // proj matrix
                    projMatrix.m02 -= offsetX / (perCameraInfo.size * camera.aspect);
                    projMatrix.m12 -= offsetY / perCameraInfo.size;

                    lookingView[counter]       = viewMatrix;
                    lookingProjection[counter] = projMatrix;
                    lookingVp[counter]         = viewMatrix * projMatrix;
                    ++counter;
                }
            }
            materialPropertyBlock.SetMatrixArray("LookingView", lookingView);
            materialPropertyBlock.SetMatrixArray("LookingProjection", lookingProjection);
            materialPropertyBlock.SetMatrixArray("LookingVP", lookingVp);
        }
Exemplo n.º 7
0
        private void SetupVPMatrices(ScriptableRenderContext context, CommandBuffer cmd,
                                     Camera camera,
                                     int view, int numViews)
        {
            Matrix4x4 projMatrix = camera.projectionMatrix;
            Matrix4x4 viewMatrix = camera.worldToCameraMatrix;

            var vpOffset = LookingGlassUtil.GetVPMatrixOffsets(
                camera.aspect, perCameraInfo.fov, perCameraInfo.size,
                view, numViews);

            // view matrix
            viewMatrix.m03 -= vpOffset.x;
            viewMatrix.m13 -= vpOffset.y;

            // proj matrix
            projMatrix.m02 -= vpOffset.z;
            projMatrix.m12 -= vpOffset.w;


            cmd.SetViewProjectionMatrices(viewMatrix, projMatrix);
            context.ExecuteCommandBuffer(cmd);
            cmd.Clear();
        }