Added autofocus and getting intrinsics

- Added computing indices offset in images blending for autofocus.
 - Added functions for getting intrinsics and distance between eyes.
 - Added function to changing distance between eyes.
This commit is contained in:
Max Barashev 2026-05-18 23:03:11 +03:00
parent acebaaec90
commit 55b7639cd8
2 changed files with 76 additions and 22 deletions

View File

@ -281,7 +281,7 @@ GameObject:
- component: {fileID: 73635655}
m_Layer: 0
m_Name: Camera
m_TagString: Untagged
m_TagString: Eye
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
@ -407,10 +407,9 @@ GameObject:
- component: {fileID: 146129747}
- component: {fileID: 146129744}
- component: {fileID: 146129745}
- component: {fileID: 146129748}
m_Layer: 0
m_Name: StreamCamera
m_TagString: Untagged
m_TagString: Eye
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
@ -528,19 +527,6 @@ Transform:
- {fileID: 73635657}
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &146129748
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 146129743}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 4fbe774de7f8cad4baf2baa9214f351b, type: 3}
m_Name:
m_EditorClassIdentifier:
Result: {fileID: 0}
--- !u!1 &365053774
GameObject:
m_ObjectHideFlags: 0
@ -762,7 +748,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
m_IsActive: 0
--- !u!4 &409312061
Transform:
m_ObjectHideFlags: 0
@ -1602,6 +1588,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 352843fcb82866541af2de5278408ab4, type: 3}
m_Name:
m_EditorClassIdentifier:
testDistance: 0.08
DepthShader: {fileID: 4800000, guid: 38575fbac906d53499789597d61d2fa4, type: 3}
BlendShader: {fileID: 7200000, guid: 5bf2857e309eda648b826d75af82d1ab, type: 3}
camera1: {fileID: 146129744}
@ -1609,7 +1596,7 @@ MonoBehaviour:
QuadResolution: 256
Multiplier: 2
Reciever: {fileID: 2021891225}
OffsetMultiplier: 408
OffsetMultiplier: 400
test: {fileID: 8400000, guid: 9238b2348d84d7441a39460751d49c8c, type: 2}
--- !u!4 &763126626
Transform:

View File

@ -1,3 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
@ -14,6 +15,19 @@ public class BlendShaderController : MonoBehaviour
private const string filename = "dsitMap";
private const string ext = ".txt";
private Vector3 center
{
get
{
if (camera1 && camera2)
return camera1.transform.position + camera1.transform.right * camera2.transform.localPosition.x * 0.5f;
else
return Vector3.zero;
}
}
[Range(0.0f, 2.0f)]
public float testDistance = 0.08f;
public Shader DepthShader;
public ComputeShader BlendShader;
public Camera camera1;
@ -40,6 +54,8 @@ public class BlendShaderController : MonoBehaviour
Task<string> task1;
Task task2;
private int uvOffset;
// Start is called before the first frame update
void Start()
{
@ -86,6 +102,9 @@ public class BlendShaderController : MonoBehaviour
// Update is called once per frame
void Update()
{
SetCameraDistance(testDistance);
ComputeUVOffset();
if (Input.GetKeyDown(KeyCode.F))
{
SetFlag();
@ -125,13 +144,31 @@ public class BlendShaderController : MonoBehaviour
BlendShader.SetTexture(0, "Camera1", tex1);
BlendShader.SetTexture(0, "Camera2", tex2);
BlendShader.SetTexture(0, "Result", Result);
BlendShader.SetFloat(Shader.PropertyToID("Offset"), (int)(camera2.transform.localPosition.x * OffsetMultiplier));
BlendShader.SetFloat(Shader.PropertyToID("Offset"), uvOffset);
BlendShader.SetFloat(Shader.PropertyToID("ResX"), Result.width);
BlendShader.SetBuffer(0, "MonoChannelResult", computeBuffer);
//BlendShader.SetTexture(0, "Test", test);
BlendShader.Dispatch(0, Result.width / 8, Result.height / 8, 1);
}
void ComputeUVOffset()
{
if (camera1 && camera2)
{
if (Physics.Raycast(center, camera1.transform.forward, out var hit, 1000))
{
Vector3 target = hit.point;
Vector3 clipPoint1 = camera1.WorldToScreenPoint(target);
Vector3 clipPoint2 = camera2.WorldToScreenPoint(target);
float diff = clipPoint1.x - clipPoint2.x;
uvOffset = (int)(diff * 0.5f);
return;
}
}
uvOffset = (int)(camera2.transform.localPosition.x * OffsetMultiplier);
}
void GenerateDepth()
{
//camera1.RenderWithShader(DepthShader, "RenderType");
@ -197,11 +234,9 @@ public class BlendShaderController : MonoBehaviour
resultArray = new float[currentRes * currentRes];
computeBuffer.GetData(resultArray);
int idx = (int)(currentRes * (0.5f + currentRes * 0.5f));
Matrix4x4 porjMat = camera1.projectionMatrix;
float zDepth = resultArray[idx];
float far = camera1.farClipPlane;
float near = camera1.nearClipPlane;
return zDepth * (far - near) + near;
return zDepth * far;
}
private float GetDistanceByRayCast()
@ -215,6 +250,38 @@ public class BlendShaderController : MonoBehaviour
return -1;
}
public double[] GetIntrinsicParameters(Camera camera)
{
int width = camera.targetTexture != null ? camera.targetTexture.width : camera.pixelWidth;
int height = camera.targetTexture != null ? camera.targetTexture.height : camera.pixelHeight;
double fovRad = camera.fieldOfView * Math.PI / 180.0;
double fy = (height * 0.5) / Math.Tan(fovRad * 0.5);
double aspect = (double)width / height;
double fx = fy * aspect;
double cx = width * 0.5;
double cy = height * 0.5;
return new double[] { fx, 0, cx, 0, fy, cy, 0, 0, 1 };
}
public float GetCameraDistance()
{
if (camera1 == null || camera2 == null)
return -1f;
return camera2.transform.localPosition.x;
}
public void SetCameraDistance(float distance)
{
if (camera1 == null || camera2 == null)
return;
Vector3 direction = camera1.transform.right;
distance = Mathf.Max(0, distance);
Vector3 camera2LocPos = camera2.transform.localPosition;
camera2.transform.localPosition = new Vector3(distance, camera2LocPos.y, camera2LocPos.z);
camera1.transform.position = center - direction * distance * 0.5f;
}
private void OnDisable()
{
computeBuffer.Release();