s
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// 视图相机,把内容渲染到渲染纹理上
|
||||
/// </summary>
|
||||
public abstract class ModuleViewCamera : MonoBehaviour {
|
||||
/// <summary> 默认图层遮罩 </summary>
|
||||
public static readonly LayerMask DefaultLayerMask = ~(1 << 0) | 1 << 0;
|
||||
/// <summary> 必须要初始化 </summary>
|
||||
protected abstract void Awake();
|
||||
/// <summary> 核心模块 </summary>
|
||||
protected virtual ModuleCore ModuleCore => ModuleCore.I;
|
||||
|
||||
/// <summary> 视图位置 </summary>
|
||||
public abstract Vector3 Position { get; set; }
|
||||
/// <summary> 视图旋转 </summary>
|
||||
public abstract Vector3 EulerAngles { get; set; }
|
||||
/// <summary> 视图缩放 </summary>
|
||||
public abstract float Scale { get; set; }
|
||||
/// <summary> 视图绿轴 </summary>
|
||||
public abstract Vector3 Up { get; }
|
||||
/// <summary> 视图红轴 </summary>
|
||||
public abstract Vector3 Right { get; }
|
||||
/// <summary> 视图蓝轴 </summary>
|
||||
public abstract Vector3 Forward { get; }
|
||||
/// <summary> 当前相机位置 </summary>
|
||||
public abstract Vector3 CameraPosition { get; }
|
||||
/// <summary> 渲染纹理 </summary>
|
||||
public abstract RenderTexture RenderTexture { get; }
|
||||
|
||||
/// <summary> 更新渲染纹理 </summary>
|
||||
public abstract void UpdateRenderTexture(int x, int y);
|
||||
/// <summary> 屏幕坐标转换视图坐标(0-1) </summary>
|
||||
public abstract Vector3 ScreenToViewPosition(Vector3 screenPosition);
|
||||
/// <summary> 屏幕坐标转换世界坐标 </summary>
|
||||
public abstract Vector3 ScreenToWorldPosition(Vector3 screenPosition);
|
||||
/// <summary> 视图坐标(0-1)转换屏幕坐标 </summary>
|
||||
//public abstract Vector3 ViewToScreenPosition(Vector3 screenPosition);
|
||||
///// <summary> 视图坐标(0-1)转换世界坐标 </summary>
|
||||
//public abstract Vector3 ViewToWorldPosition(Vector3 screenPosition);
|
||||
///// <summary> 世界坐标转换屏幕坐标 </summary>
|
||||
//public abstract Vector3 WorldToScreenPosition(Vector3 screenPosition);
|
||||
///// <summary> 世界坐标转换视图坐标(0-1) </summary>
|
||||
//public abstract Vector3 WorldToViewPosition(Vector3 screenPosition);
|
||||
|
||||
/// <summary> 屏幕坐标获取世界对象 </summary>
|
||||
public abstract bool ScreenToWorldObject<T>(Vector3 screenPosition, out T value) where T : Object;
|
||||
/// <summary> 屏幕坐标获取世界对象 </summary>
|
||||
public abstract bool ScreenToWorldObject<T>(Vector3 screenPosition, out T value, LayerMask planeLayerMask) where T : Object;
|
||||
/// <summary> 屏幕坐标获取世界对象的父对象 </summary>
|
||||
public abstract bool ScreenToWorldObjectParent<T>(Vector3 screenPosition, out T value) where T : Object;
|
||||
/// <summary> 屏幕坐标获取世界对象的父对象 </summary>
|
||||
public abstract bool ScreenToWorldObjectParent<T>(Vector3 screenPosition, out T value, LayerMask planeLayerMask) where T : Object;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 93bd8c351a140654da540f55d0d7091f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,92 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class ViewCameraBaking : ModuleViewCamera {
|
||||
public readonly Vector3 CameraOffset = new Vector3(0, 0, -1.5f);
|
||||
public Camera viewCamera;
|
||||
public Transform viewOrigin;
|
||||
public Transform viewSpace;
|
||||
private RaycastHit hitInfo;
|
||||
private RenderTexture renderTexture;
|
||||
|
||||
protected override void Awake() => ModuleCore.ViewCameraBaking = this;
|
||||
|
||||
public override Vector3 Position {
|
||||
get => viewOrigin.localPosition;
|
||||
set => viewOrigin.localPosition = value;
|
||||
}
|
||||
public override Vector3 EulerAngles {
|
||||
get => viewOrigin.eulerAngles;
|
||||
set => viewOrigin.eulerAngles = value;
|
||||
}
|
||||
public override float Scale {
|
||||
get => viewCamera.transform.localPosition.z;
|
||||
set => viewCamera.transform.localPosition = new Vector3(0, 0, value);
|
||||
}
|
||||
public override Vector3 Up {
|
||||
get => viewOrigin.up;
|
||||
}
|
||||
public override Vector3 Right {
|
||||
get => viewOrigin.right;
|
||||
}
|
||||
public override Vector3 Forward {
|
||||
get => viewOrigin.forward;
|
||||
}
|
||||
public override Vector3 CameraPosition {
|
||||
get => viewOrigin.localPosition;
|
||||
}
|
||||
public override RenderTexture RenderTexture {
|
||||
get => renderTexture;
|
||||
}
|
||||
|
||||
public override void UpdateRenderTexture(int x, int y) {
|
||||
renderTexture = new RenderTexture(x, y, 0);
|
||||
viewCamera.targetTexture = renderTexture;
|
||||
}
|
||||
public override Vector3 ScreenToViewPosition(Vector3 screenPosition) {
|
||||
float x = screenPosition.x / viewCamera.pixelWidth;
|
||||
float y = 1 - screenPosition.y / viewCamera.pixelHeight;
|
||||
Vector3 viewPosition = new Vector3(x, y);
|
||||
return viewPosition;
|
||||
}
|
||||
public override Vector3 ScreenToWorldPosition(Vector3 screenPosition) {
|
||||
Vector3 viewPosition = ScreenToViewPosition(screenPosition);
|
||||
float aspectRatio = (float)viewCamera.pixelWidth / viewCamera.pixelHeight;
|
||||
Vector3 ratio = viewPosition - new Vector3(0.5f, 0.5f);
|
||||
return new Vector3(ratio.x * aspectRatio, ratio.y) * Scale + viewOrigin.position;
|
||||
}
|
||||
//public override Vector3 ViewToScreenPosition(Vector3 screenPosition) {
|
||||
// throw new System.NotImplementedException();
|
||||
//}
|
||||
//public override Vector3 ViewToWorldPosition(Vector3 screenPosition) {
|
||||
// throw new System.NotImplementedException();
|
||||
//}
|
||||
//public override Vector3 WorldToScreenPosition(Vector3 screenPosition) {
|
||||
// throw new System.NotImplementedException();
|
||||
//}
|
||||
//public override Vector3 WorldToViewPosition(Vector3 screenPosition) {
|
||||
// throw new System.NotImplementedException();
|
||||
//}
|
||||
|
||||
public override bool ScreenToWorldObject<T>(Vector3 screenPosition, out T value) {
|
||||
return ScreenToWorldObject(screenPosition, out value, DefaultLayerMask);
|
||||
}
|
||||
public override bool ScreenToWorldObject<T>(Vector3 screenPosition, out T value, LayerMask planeLayerMask) {
|
||||
Vector3 viewPosition = ScreenToViewPosition(screenPosition);
|
||||
Ray ray = viewCamera.ViewportPointToRay(viewPosition);
|
||||
Physics.Raycast(ray, out hitInfo, 200, planeLayerMask);
|
||||
value = hitInfo.transform?.GetComponent<T>();
|
||||
return value != null;
|
||||
}
|
||||
public override bool ScreenToWorldObjectParent<T>(Vector3 screenPosition, out T value) {
|
||||
return ScreenToWorldObjectParent(screenPosition, out value, DefaultLayerMask);
|
||||
}
|
||||
public override bool ScreenToWorldObjectParent<T>(Vector3 screenPosition, out T value, LayerMask planeLayerMask) {
|
||||
Vector3 viewPosition = ScreenToViewPosition(screenPosition);
|
||||
Ray ray = viewCamera.ViewportPointToRay(viewPosition);
|
||||
Physics.Raycast(ray, out hitInfo, 200, planeLayerMask);
|
||||
value = hitInfo.transform?.GetComponentInParent<T>();
|
||||
return value != null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 802326651d2bc7442aa201a7859a30e2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,91 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class ViewCameraDesign : ModuleViewCamera {
|
||||
public Camera viewCamera;
|
||||
public Transform viewSpace;
|
||||
private RaycastHit hitInfo;
|
||||
private RenderTexture renderTexture;
|
||||
private readonly Vector3 CameraOffset = new Vector3(0, 0, -1.5f);
|
||||
|
||||
protected override void Awake() => ModuleCore.ViewCameraDesign = this;
|
||||
|
||||
public override Vector3 Position {
|
||||
get => viewCamera.transform.localPosition - CameraOffset;
|
||||
set => viewCamera.transform.localPosition = value + CameraOffset;
|
||||
}
|
||||
public override Vector3 EulerAngles {
|
||||
get => viewCamera.transform.eulerAngles;
|
||||
set => viewCamera.transform.eulerAngles = value;
|
||||
}
|
||||
public override float Scale {
|
||||
get => viewCamera.orthographicSize;
|
||||
set => viewCamera.orthographicSize = value;
|
||||
}
|
||||
public override Vector3 Up {
|
||||
get => viewCamera.transform.up;
|
||||
}
|
||||
public override Vector3 Right {
|
||||
get => viewCamera.transform.right;
|
||||
}
|
||||
public override Vector3 Forward {
|
||||
get => viewCamera.transform.forward;
|
||||
}
|
||||
public override Vector3 CameraPosition {
|
||||
get => viewCamera.transform.localPosition - CameraOffset;
|
||||
}
|
||||
public override RenderTexture RenderTexture {
|
||||
get => renderTexture;
|
||||
}
|
||||
|
||||
public override void UpdateRenderTexture(int x, int y) {
|
||||
renderTexture = new RenderTexture(x, y, 0);
|
||||
viewCamera.targetTexture = renderTexture;
|
||||
}
|
||||
public override Vector3 ScreenToViewPosition(Vector3 screenPosition) {
|
||||
float x = screenPosition.x / viewCamera.pixelWidth;
|
||||
float y = 1 - screenPosition.y / viewCamera.pixelHeight;
|
||||
Vector3 mouseRatio = new Vector3(x - 0.5f, y - 0.5f);
|
||||
float aspectRatio = (float)viewCamera.pixelWidth / viewCamera.pixelHeight;
|
||||
return new Vector3(mouseRatio.x * aspectRatio, mouseRatio.y) * 2;
|
||||
}
|
||||
public override Vector3 ScreenToWorldPosition(Vector3 screenPosition) {
|
||||
return ScreenToViewPosition(screenPosition) * Scale + Position;
|
||||
}
|
||||
|
||||
public override bool ScreenToWorldObject<T>(Vector3 screenPosition, out T value) {
|
||||
return ScreenToWorldObject(screenPosition, out value, DefaultLayerMask);
|
||||
}
|
||||
public override bool ScreenToWorldObject<T>(Vector3 screenPosition, out T value, LayerMask planeLayerMask) {
|
||||
Vector3 viewPosition = ScreenToViewPosition(screenPosition);
|
||||
Vector3 worldPosition = viewPosition * Scale + viewCamera.transform.position;
|
||||
Ray ray = new Ray(worldPosition, viewCamera.transform.forward);
|
||||
Physics.Raycast(ray, out hitInfo, 200, planeLayerMask);
|
||||
value = hitInfo.transform?.GetComponent<T>();
|
||||
return value != null;
|
||||
}
|
||||
public override bool ScreenToWorldObjectParent<T>(Vector3 screenPosition, out T value) {
|
||||
return ScreenToWorldObjectParent(screenPosition, out value, DefaultLayerMask);
|
||||
}
|
||||
public override bool ScreenToWorldObjectParent<T>(Vector3 screenPosition, out T value, LayerMask planeLayerMask) {
|
||||
Vector3 viewPosition = ScreenToViewPosition(screenPosition);
|
||||
Vector3 worldPosition = viewPosition * Scale + viewCamera.transform.position;
|
||||
Ray ray = new Ray(worldPosition, viewCamera.transform.forward);
|
||||
Physics.Raycast(ray, out hitInfo, 200, planeLayerMask);
|
||||
value = hitInfo.transform?.GetComponentInParent<T>();
|
||||
return value != null;
|
||||
}
|
||||
//public override Vector3 ViewToScreenPosition(Vector3 screenPosition) {
|
||||
// throw new System.NotImplementedException();
|
||||
//}
|
||||
//public override Vector3 ViewToWorldPosition(Vector3 screenPosition) {
|
||||
// throw new System.NotImplementedException();
|
||||
//}
|
||||
//public override Vector3 WorldToScreenPosition(Vector3 screenPosition) {
|
||||
// throw new System.NotImplementedException();
|
||||
//}
|
||||
//public override Vector3 WorldToViewPosition(Vector3 screenPosition) {
|
||||
// throw new System.NotImplementedException();
|
||||
//}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c639043240fd73545af704b1a6b52895
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user