Added cw7 tasks
This commit is contained in:
parent
c050fdfc5f
commit
150570badf
8
cw7/Editor.meta
Normal file
8
cw7/Editor.meta
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: c84c0b9b0915a9c4b9dbfa25904c18e5
|
||||||
|
folderAsset: yes
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
23
cw7/Editor/GrassControllerEditor.cs
Normal file
23
cw7/Editor/GrassControllerEditor.cs
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
using UnityEditor;
|
||||||
|
using UnityEngine;
|
||||||
|
using System.Collections;
|
||||||
|
|
||||||
|
|
||||||
|
[CustomEditor(typeof(GrassController))]
|
||||||
|
public class GrassEditor : Editor {
|
||||||
|
|
||||||
|
|
||||||
|
public override void OnInspectorGUI() {
|
||||||
|
|
||||||
|
GrassController terrainController = (GrassController)target;
|
||||||
|
|
||||||
|
DrawDefaultInspector();
|
||||||
|
if (GUILayout.Button("Reset")) {
|
||||||
|
terrainController.resetGrass();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
11
cw7/Editor/GrassControllerEditor.cs.meta
Normal file
11
cw7/Editor/GrassControllerEditor.cs.meta
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: e6a133b553fb5e44fb2b246bc26e1663
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
22
cw7/Editor/ProceduralTextureEditor.cs
Normal file
22
cw7/Editor/ProceduralTextureEditor.cs
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
using UnityEditor;
|
||||||
|
using UnityEngine;
|
||||||
|
using System.Collections;
|
||||||
|
|
||||||
|
|
||||||
|
[CustomEditor(typeof(ProceduralTextureScript))]
|
||||||
|
public class ProceduralTextureEditor : Editor {
|
||||||
|
|
||||||
|
|
||||||
|
public override void OnInspectorGUI() {
|
||||||
|
|
||||||
|
|
||||||
|
DrawDefaultInspector();
|
||||||
|
if (GUILayout.Button("Run")) {
|
||||||
|
((ProceduralTextureScript)target).runProcedrualTexturing();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
11
cw7/Editor/ProceduralTextureEditor.cs.meta
Normal file
11
cw7/Editor/ProceduralTextureEditor.cs.meta
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: b9fd8063db74fd249b36f92aa2aace67
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
58
cw7/GrassCalculation.compute
Normal file
58
cw7/GrassCalculation.compute
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
// Each #kernel tells which function to compile; you can have many kernels
|
||||||
|
#pragma kernel CSMain
|
||||||
|
|
||||||
|
// Create a RenderTexture with enableRandomWrite flag and set it
|
||||||
|
// with cs.SetTexture
|
||||||
|
RWStructuredBuffer<float> oldVegetation;
|
||||||
|
RWStructuredBuffer<float> vegetation;
|
||||||
|
RWStructuredBuffer<float> oldWater;
|
||||||
|
RWStructuredBuffer<float> water;
|
||||||
|
RWStructuredBuffer<float> feedModifier;
|
||||||
|
RWStructuredBuffer<float> killModifier;
|
||||||
|
int sizeX;
|
||||||
|
int sizeY;
|
||||||
|
|
||||||
|
float DWater;
|
||||||
|
float DVegetation;
|
||||||
|
float feedRate;
|
||||||
|
float killRate;
|
||||||
|
float dT;
|
||||||
|
|
||||||
|
int index(int x, int y)
|
||||||
|
{
|
||||||
|
return x + sizeX * y;
|
||||||
|
}
|
||||||
|
|
||||||
|
float get(RWStructuredBuffer<float> array, int x, int y)
|
||||||
|
{
|
||||||
|
int idx = index(x, y);
|
||||||
|
if (idx < 0 || idx >= sizeX * sizeY)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return array[idx];
|
||||||
|
}
|
||||||
|
|
||||||
|
float laplace(RWStructuredBuffer<float> array, int x, int y)
|
||||||
|
{
|
||||||
|
return 0.2 * (get(array, x + 1, y) + get(array, x - 1, y) + get(array, x, y + 1) + get(array, x, y - 1))
|
||||||
|
+ 0.05 * (get(array, x + 1, y + 1) + get(array, x + 1, y - 1) + get(array, x - 1, y + 1) + get(array, x - 1, y - 1))
|
||||||
|
- get(array, x, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
[numthreads(8,8,1)]
|
||||||
|
void CSMain (uint3 id : SV_DispatchThreadID)
|
||||||
|
{
|
||||||
|
int x = id.x;
|
||||||
|
int y = id.y;
|
||||||
|
|
||||||
|
float w = oldWater[index(x, y)];
|
||||||
|
float r = oldVegetation[index(x, y)];
|
||||||
|
|
||||||
|
float feed = feedRate * feedModifier[index(x, y)];
|
||||||
|
float kill = killRate * killModifier[index(x, y)];
|
||||||
|
|
||||||
|
water[index(x, y)] = clamp(w + dT * DWater * laplace(oldWater, x, y) - w * w * r * r + feed, 1, 0);
|
||||||
|
vegetation[index(x, y)] = clamp(r + dT * (DVegetation * laplace(oldVegetation, x, y) + r * (w * r - kill)), 1, 0);
|
||||||
|
}
|
8
cw7/GrassCalculation.compute.meta
Normal file
8
cw7/GrassCalculation.compute.meta
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 88b7d14961b15af4ca0e47cfe8e10592
|
||||||
|
ComputeShaderImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
preprocessorOverride: 0
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
242
cw7/GrassController.cs
Normal file
242
cw7/GrassController.cs
Normal file
@ -0,0 +1,242 @@
|
|||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
struct DifferentialEquationData
|
||||||
|
{
|
||||||
|
public float water;
|
||||||
|
public float vegetation;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public class GrassController : MonoBehaviour
|
||||||
|
{
|
||||||
|
int _structSize()
|
||||||
|
{
|
||||||
|
return sizeof(float) + sizeof(float);
|
||||||
|
}
|
||||||
|
private GameObject cube;
|
||||||
|
|
||||||
|
public float updateInterval;
|
||||||
|
float timePassed;
|
||||||
|
Terrain terrain;
|
||||||
|
public ComputeShader computeShader;
|
||||||
|
public ComputeBuffer oldVegetation;
|
||||||
|
public ComputeBuffer vegetation;
|
||||||
|
|
||||||
|
public ComputeBuffer oldWater;
|
||||||
|
public ComputeBuffer water;
|
||||||
|
|
||||||
|
public ComputeBuffer feedModifier;
|
||||||
|
public ComputeBuffer killModifier;
|
||||||
|
|
||||||
|
[Header("equation coeffs")]
|
||||||
|
public float DWater = 1.0f;
|
||||||
|
public float DVegetation = 0.5f;
|
||||||
|
public float feedRate = 0.055f;
|
||||||
|
public float killRate = 0.0462f;
|
||||||
|
public float dT = 0.10f;
|
||||||
|
public int stepsNumber = 1;
|
||||||
|
|
||||||
|
|
||||||
|
int sizeX;
|
||||||
|
int sizeY;
|
||||||
|
// Start is called before the first frame update
|
||||||
|
void Start()
|
||||||
|
{
|
||||||
|
terrain = gameObject.GetComponent<Terrain>();
|
||||||
|
|
||||||
|
sizeX = terrain.terrainData.detailWidth;
|
||||||
|
sizeY = terrain.terrainData.detailWidth;
|
||||||
|
|
||||||
|
initComputeShader();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool doUpdate()
|
||||||
|
{
|
||||||
|
timePassed += Time.deltaTime;
|
||||||
|
if (timePassed < updateInterval)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
timePassed = 0;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
float transition(float start, float end, float value)
|
||||||
|
{
|
||||||
|
if (start > end)
|
||||||
|
{
|
||||||
|
return 1 - transition(end, start, value);
|
||||||
|
}
|
||||||
|
if (value < start)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (value >= end)
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
var scaledValue = (value - start) / (end - start);
|
||||||
|
return scaledValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
void initKillFeedBuffers()
|
||||||
|
{
|
||||||
|
feedModifier = new ComputeBuffer(sizeX * sizeY, sizeof(float));
|
||||||
|
killModifier = new ComputeBuffer(sizeX * sizeY, sizeof(float));
|
||||||
|
computeShader.SetBuffer(0, "feedModifier", feedModifier);
|
||||||
|
computeShader.SetBuffer(0, "killModifier", killModifier);
|
||||||
|
|
||||||
|
var feedValues = new float[sizeX * sizeY];
|
||||||
|
var killValues = new float[sizeX * sizeY];
|
||||||
|
|
||||||
|
var width = terrain.terrainData.alphamapWidth;
|
||||||
|
var height = terrain.terrainData.alphamapHeight;
|
||||||
|
|
||||||
|
var splatMap = terrain.terrainData.GetAlphamaps(0, 0, width, height);
|
||||||
|
|
||||||
|
for (var i = 0; i < sizeX; i++)
|
||||||
|
{
|
||||||
|
for (var j = 0; j < sizeY; j++)
|
||||||
|
{
|
||||||
|
var scale = height / (float)sizeX;
|
||||||
|
var x = Mathf.FloorToInt(scale * i);
|
||||||
|
var y = Mathf.FloorToInt(scale * j);
|
||||||
|
var terrainHeight = terrain.terrainData.GetInterpolatedHeight(x, y);
|
||||||
|
feedValues[i + j * sizeX] = transition(i, j, terrainHeight);
|
||||||
|
|
||||||
|
var snow = splatMap[x, y, 5];
|
||||||
|
if (snow > 0.5)
|
||||||
|
{
|
||||||
|
killValues[i + j * sizeX] = 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
feedModifier.SetData(killValues);
|
||||||
|
killModifier.SetData(killValues);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void initBufferValues()
|
||||||
|
{
|
||||||
|
var initValues = new float[sizeX * sizeY];
|
||||||
|
|
||||||
|
for (var i = 0; i < sizeX * sizeY; i++)
|
||||||
|
{
|
||||||
|
initValues[i] = Random.Range(0.0f, 1.0f);
|
||||||
|
}
|
||||||
|
|
||||||
|
oldVegetation.SetData(initValues);
|
||||||
|
vegetation.SetData(initValues);
|
||||||
|
|
||||||
|
oldWater.SetData(initValues);
|
||||||
|
water.SetData(initValues);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void resetGrass()
|
||||||
|
{
|
||||||
|
initBufferValues();
|
||||||
|
var detal = new int[sizeX, sizeY];
|
||||||
|
|
||||||
|
for (var i = 0; i < sizeX; i++)
|
||||||
|
{
|
||||||
|
detal[i, i] = 16;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var i = 0; i < sizeX; i++)
|
||||||
|
{
|
||||||
|
detal[i, sizeX - i] = 16;
|
||||||
|
}
|
||||||
|
|
||||||
|
terrain.terrainData.SetDetailLayer(0, 0, 1, detal);
|
||||||
|
}
|
||||||
|
void initParameters()
|
||||||
|
{
|
||||||
|
computeShader.SetInt("sizeX", sizeX);
|
||||||
|
computeShader.SetInt("sizeY", sizeY);
|
||||||
|
computeShader.SetFloat("DWater", DWater);
|
||||||
|
computeShader.SetFloat("DVegetation", DVegetation);
|
||||||
|
computeShader.SetFloat("feedRate", feedRate);
|
||||||
|
computeShader.SetFloat("killRate", killRate);
|
||||||
|
computeShader.SetFloat("dT", dT);
|
||||||
|
}
|
||||||
|
|
||||||
|
void initComputeShader()
|
||||||
|
{
|
||||||
|
oldVegetation = new ComputeBuffer(sizeX * sizeY, sizeof(float));
|
||||||
|
vegetation = new ComputeBuffer(sizeX * sizeY, sizeof(float));
|
||||||
|
|
||||||
|
computeShader.SetBuffer(0, "oldVegetation", oldVegetation);
|
||||||
|
computeShader.SetBuffer(0, "vegetation", vegetation);
|
||||||
|
|
||||||
|
oldWater = new ComputeBuffer(sizeX * sizeY, sizeof(float));
|
||||||
|
water = new ComputeBuffer(sizeX * sizeY, sizeof(float));
|
||||||
|
|
||||||
|
computeShader.SetBuffer(0, "oldWater", oldWater);
|
||||||
|
computeShader.SetBuffer(0, "water", water);
|
||||||
|
|
||||||
|
initKillFeedBuffers();
|
||||||
|
initBufferValues();
|
||||||
|
initParameters();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void swapBuffers()
|
||||||
|
{
|
||||||
|
var temp = vegetation;
|
||||||
|
vegetation = oldVegetation;
|
||||||
|
oldVegetation = temp;
|
||||||
|
computeShader.SetBuffer(0, "oldVegetation", oldVegetation);
|
||||||
|
computeShader.SetBuffer(0, "vegetation", vegetation);
|
||||||
|
|
||||||
|
var temp2 = water;
|
||||||
|
water = oldWater;
|
||||||
|
oldWater = temp2;
|
||||||
|
computeShader.SetBuffer(0, "oldWater", oldWater);
|
||||||
|
computeShader.SetBuffer(0, "water", water);
|
||||||
|
}
|
||||||
|
|
||||||
|
void doSimulationSteps(int steps)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < steps; i++)
|
||||||
|
{
|
||||||
|
swapBuffers();
|
||||||
|
computeShader.Dispatch(0, sizeX, sizeY, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void recoverData()
|
||||||
|
{
|
||||||
|
var result = new float[sizeX * sizeY];
|
||||||
|
vegetation.GetData(result);
|
||||||
|
var detailLayer = new int[sizeX, sizeY];
|
||||||
|
|
||||||
|
for (var i = 0; i < sizeX; i++)
|
||||||
|
{
|
||||||
|
for (var j = 0; j < sizeX; j++)
|
||||||
|
{
|
||||||
|
detailLayer[i, j] = Mathf.FloorToInt(result[i + j * sizeX] * 10);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
terrain.terrainData.SetDetailLayer(0, 0, 1, detailLayer);
|
||||||
|
}
|
||||||
|
// Update is called once per frame
|
||||||
|
void Update()
|
||||||
|
{
|
||||||
|
if (doUpdate())
|
||||||
|
{
|
||||||
|
terrain = gameObject.GetComponent<Terrain>();
|
||||||
|
initParameters();
|
||||||
|
doSimulationSteps(stepsNumber);
|
||||||
|
recoverData();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
11
cw7/GrassController.cs.meta
Normal file
11
cw7/GrassController.cs.meta
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 7edbfa0ffff914b40ab46ab9170e781d
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
77
cw7/ProceduralTextureScript.cs
Normal file
77
cw7/ProceduralTextureScript.cs
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using UnityEngine;
|
||||||
|
using System.Linq; // used for Sum of array
|
||||||
|
|
||||||
|
public class ProceduralTextureScript : MonoBehaviour {
|
||||||
|
public float snowHeight;
|
||||||
|
public float rockSlope;
|
||||||
|
|
||||||
|
// Start is called before the first frame update
|
||||||
|
void Start()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
float transition(float start, float end,float value) {
|
||||||
|
if (start > end) {
|
||||||
|
return 1 - transition(end, start, value);
|
||||||
|
}
|
||||||
|
if (value < start) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (value >= end) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
var scaledValue = (value - start) / (end - start);
|
||||||
|
return scaledValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void runProcedrualTexturing() {
|
||||||
|
var terrain = gameObject.GetComponent<Terrain>();
|
||||||
|
var layers = terrain.terrainData.alphamapLayers;
|
||||||
|
var height = terrain.terrainData.alphamapWidth;
|
||||||
|
var width = terrain.terrainData.alphamapHeight;
|
||||||
|
|
||||||
|
|
||||||
|
Debug.Log(layers);
|
||||||
|
Debug.Log(height);
|
||||||
|
Debug.Log(width);
|
||||||
|
|
||||||
|
Debug.Log(terrain.terrainData.GetInterpolatedHeight(0.5f,0.5f));
|
||||||
|
|
||||||
|
var newSplatMap = new float[width, height, layers];
|
||||||
|
//for (int i=0; i<)
|
||||||
|
|
||||||
|
for (int i=0; i < width;i++) {
|
||||||
|
for (int j = 0; j < width; j++) {
|
||||||
|
float x = j / (float)height;
|
||||||
|
float y = i / (float)width;
|
||||||
|
var splatWeights = new float[layers];
|
||||||
|
var terrainHeight = terrain.terrainData.GetInterpolatedHeight(x,y);
|
||||||
|
|
||||||
|
for (int k = 0; k < layers; k++) {
|
||||||
|
splatWeights[k] = Random.RandomRange(0.0f,1.0f);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
float sum = splatWeights.Sum();
|
||||||
|
|
||||||
|
for (int k = 0; k < layers; k++) {
|
||||||
|
newSplatMap[i,j,k]=splatWeights[k]/sum;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
terrain.terrainData.SetAlphamaps(0, 0, newSplatMap);
|
||||||
|
|
||||||
|
var a = terrain.terrainData.treeInstances;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update is called once per frame
|
||||||
|
void Update()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
11
cw7/ProceduralTextureScript.cs.meta
Normal file
11
cw7/ProceduralTextureScript.cs.meta
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: b8debef5f396b694885e8920a60b42df
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
294
cw7/SimpleCameraController.cs
Normal file
294
cw7/SimpleCameraController.cs
Normal file
@ -0,0 +1,294 @@
|
|||||||
|
#if ENABLE_INPUT_SYSTEM
|
||||||
|
using UnityEngine.InputSystem;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace UnityTemplateProjects
|
||||||
|
{
|
||||||
|
public class SimpleCameraController : MonoBehaviour
|
||||||
|
{
|
||||||
|
class CameraState
|
||||||
|
{
|
||||||
|
public float yaw;
|
||||||
|
public float pitch;
|
||||||
|
public float roll;
|
||||||
|
public float x;
|
||||||
|
public float y;
|
||||||
|
public float z;
|
||||||
|
|
||||||
|
public void SetFromTransform(Transform t)
|
||||||
|
{
|
||||||
|
pitch = t.eulerAngles.x;
|
||||||
|
yaw = t.eulerAngles.y;
|
||||||
|
roll = t.eulerAngles.z;
|
||||||
|
x = t.position.x;
|
||||||
|
y = t.position.y;
|
||||||
|
z = t.position.z;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Translate(Vector3 translation)
|
||||||
|
{
|
||||||
|
Vector3 rotatedTranslation = Quaternion.Euler(pitch, yaw, roll) * translation;
|
||||||
|
|
||||||
|
x += rotatedTranslation.x;
|
||||||
|
y += rotatedTranslation.y;
|
||||||
|
z += rotatedTranslation.z;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void LerpTowards(CameraState target, float positionLerpPct, float rotationLerpPct)
|
||||||
|
{
|
||||||
|
yaw = Mathf.Lerp(yaw, target.yaw, rotationLerpPct);
|
||||||
|
pitch = Mathf.Lerp(pitch, target.pitch, rotationLerpPct);
|
||||||
|
roll = Mathf.Lerp(roll, target.roll, rotationLerpPct);
|
||||||
|
|
||||||
|
x = Mathf.Lerp(x, target.x, positionLerpPct);
|
||||||
|
y = Mathf.Lerp(y, target.y, positionLerpPct);
|
||||||
|
z = Mathf.Lerp(z, target.z, positionLerpPct);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UpdateTransform(Transform t)
|
||||||
|
{
|
||||||
|
t.eulerAngles = new Vector3(pitch, yaw, roll);
|
||||||
|
t.position = new Vector3(x, y, z);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
CameraState m_TargetCameraState = new CameraState();
|
||||||
|
CameraState m_InterpolatingCameraState = new CameraState();
|
||||||
|
|
||||||
|
[Header("Movement Settings")]
|
||||||
|
[Tooltip("Exponential boost factor on translation, controllable by mouse wheel.")]
|
||||||
|
public float boost = 3.5f;
|
||||||
|
|
||||||
|
[Tooltip("Time it takes to interpolate camera position 99% of the way to the target."), Range(0.001f, 1f)]
|
||||||
|
public float positionLerpTime = 0.2f;
|
||||||
|
|
||||||
|
[Header("Rotation Settings")]
|
||||||
|
[Tooltip("X = Change in mouse position.\nY = Multiplicative factor for camera rotation.")]
|
||||||
|
public AnimationCurve mouseSensitivityCurve = new AnimationCurve(new Keyframe(0f, 0.5f, 0f, 5f), new Keyframe(1f, 2.5f, 0f, 0f));
|
||||||
|
|
||||||
|
[Tooltip("Time it takes to interpolate camera rotation 99% of the way to the target."), Range(0.001f, 1f)]
|
||||||
|
public float rotationLerpTime = 0.01f;
|
||||||
|
|
||||||
|
[Tooltip("Whether or not to invert our Y axis for mouse input to rotation.")]
|
||||||
|
public bool invertY = false;
|
||||||
|
|
||||||
|
#if ENABLE_INPUT_SYSTEM
|
||||||
|
InputAction movementAction;
|
||||||
|
InputAction verticalMovementAction;
|
||||||
|
InputAction lookAction;
|
||||||
|
InputAction boostFactorAction;
|
||||||
|
bool mouseRightButtonPressed;
|
||||||
|
|
||||||
|
void Start()
|
||||||
|
{
|
||||||
|
var map = new InputActionMap("Simple Camera Controller");
|
||||||
|
|
||||||
|
lookAction = map.AddAction("look", binding: "<Mouse>/delta");
|
||||||
|
movementAction = map.AddAction("move", binding: "<Gamepad>/leftStick");
|
||||||
|
verticalMovementAction = map.AddAction("Vertical Movement");
|
||||||
|
boostFactorAction = map.AddAction("Boost Factor", binding: "<Mouse>/scroll");
|
||||||
|
|
||||||
|
lookAction.AddBinding("<Gamepad>/rightStick").WithProcessor("scaleVector2(x=15, y=15)");
|
||||||
|
movementAction.AddCompositeBinding("Dpad")
|
||||||
|
.With("Up", "<Keyboard>/w")
|
||||||
|
.With("Up", "<Keyboard>/upArrow")
|
||||||
|
.With("Down", "<Keyboard>/s")
|
||||||
|
.With("Down", "<Keyboard>/downArrow")
|
||||||
|
.With("Left", "<Keyboard>/a")
|
||||||
|
.With("Left", "<Keyboard>/leftArrow")
|
||||||
|
.With("Right", "<Keyboard>/d")
|
||||||
|
.With("Right", "<Keyboard>/rightArrow");
|
||||||
|
verticalMovementAction.AddCompositeBinding("Dpad")
|
||||||
|
.With("Up", "<Keyboard>/pageUp")
|
||||||
|
.With("Down", "<Keyboard>/pageDown")
|
||||||
|
.With("Up", "<Keyboard>/e")
|
||||||
|
.With("Down", "<Keyboard>/q")
|
||||||
|
.With("Up", "<Gamepad>/rightshoulder")
|
||||||
|
.With("Down", "<Gamepad>/leftshoulder");
|
||||||
|
boostFactorAction.AddBinding("<Gamepad>/Dpad").WithProcessor("scaleVector2(x=1, y=4)");
|
||||||
|
|
||||||
|
movementAction.Enable();
|
||||||
|
lookAction.Enable();
|
||||||
|
verticalMovementAction.Enable();
|
||||||
|
boostFactorAction.Enable();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void OnEnable()
|
||||||
|
{
|
||||||
|
m_TargetCameraState.SetFromTransform(transform);
|
||||||
|
m_InterpolatingCameraState.SetFromTransform(transform);
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector3 GetInputTranslationDirection()
|
||||||
|
{
|
||||||
|
Vector3 direction = Vector3.zero;
|
||||||
|
#if ENABLE_INPUT_SYSTEM
|
||||||
|
var moveDelta = movementAction.ReadValue<Vector2>();
|
||||||
|
direction.x = moveDelta.x;
|
||||||
|
direction.z = moveDelta.y;
|
||||||
|
direction.y = verticalMovementAction.ReadValue<Vector2>().y;
|
||||||
|
#else
|
||||||
|
if (Input.GetKey(KeyCode.W))
|
||||||
|
{
|
||||||
|
direction += Vector3.forward;
|
||||||
|
}
|
||||||
|
if (Input.GetKey(KeyCode.S))
|
||||||
|
{
|
||||||
|
direction += Vector3.back;
|
||||||
|
}
|
||||||
|
if (Input.GetKey(KeyCode.A))
|
||||||
|
{
|
||||||
|
direction += Vector3.left;
|
||||||
|
}
|
||||||
|
if (Input.GetKey(KeyCode.D))
|
||||||
|
{
|
||||||
|
direction += Vector3.right;
|
||||||
|
}
|
||||||
|
if (Input.GetKey(KeyCode.Q))
|
||||||
|
{
|
||||||
|
direction += Vector3.down;
|
||||||
|
}
|
||||||
|
if (Input.GetKey(KeyCode.E))
|
||||||
|
{
|
||||||
|
direction += Vector3.up;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
return direction;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Update()
|
||||||
|
{
|
||||||
|
// Exit Sample
|
||||||
|
|
||||||
|
if (IsEscapePressed())
|
||||||
|
{
|
||||||
|
Application.Quit();
|
||||||
|
#if UNITY_EDITOR
|
||||||
|
UnityEditor.EditorApplication.isPlaying = false;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hide and lock cursor when right mouse button pressed
|
||||||
|
if (IsRightMouseButtonDown())
|
||||||
|
{
|
||||||
|
Cursor.lockState = CursorLockMode.Locked;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unlock and show cursor when right mouse button released
|
||||||
|
if (IsRightMouseButtonUp())
|
||||||
|
{
|
||||||
|
Cursor.visible = true;
|
||||||
|
Cursor.lockState = CursorLockMode.None;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rotation
|
||||||
|
if (IsCameraRotationAllowed())
|
||||||
|
{
|
||||||
|
var mouseMovement = GetInputLookRotation() * Time.deltaTime * 5;
|
||||||
|
if (invertY)
|
||||||
|
mouseMovement.y = -mouseMovement.y;
|
||||||
|
|
||||||
|
var mouseSensitivityFactor = mouseSensitivityCurve.Evaluate(mouseMovement.magnitude);
|
||||||
|
|
||||||
|
m_TargetCameraState.yaw += mouseMovement.x * mouseSensitivityFactor;
|
||||||
|
m_TargetCameraState.pitch += mouseMovement.y * mouseSensitivityFactor;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Translation
|
||||||
|
var translation = GetInputTranslationDirection() * Time.deltaTime;
|
||||||
|
|
||||||
|
// Speed up movement when shift key held
|
||||||
|
if (IsBoostPressed())
|
||||||
|
{
|
||||||
|
translation *= 10.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Modify movement by a boost factor (defined in Inspector and modified in play mode through the mouse scroll wheel)
|
||||||
|
boost += GetBoostFactor();
|
||||||
|
translation *= Mathf.Pow(2.0f, boost);
|
||||||
|
|
||||||
|
m_TargetCameraState.Translate(translation);
|
||||||
|
|
||||||
|
// Framerate-independent interpolation
|
||||||
|
// Calculate the lerp amount, such that we get 99% of the way to our target in the specified time
|
||||||
|
var positionLerpPct = 1f - Mathf.Exp((Mathf.Log(1f - 0.99f) / positionLerpTime) * Time.deltaTime);
|
||||||
|
var rotationLerpPct = 1f - Mathf.Exp((Mathf.Log(1f - 0.99f) / rotationLerpTime) * Time.deltaTime);
|
||||||
|
m_InterpolatingCameraState.LerpTowards(m_TargetCameraState, positionLerpPct, rotationLerpPct);
|
||||||
|
|
||||||
|
m_InterpolatingCameraState.UpdateTransform(transform);
|
||||||
|
}
|
||||||
|
|
||||||
|
float GetBoostFactor()
|
||||||
|
{
|
||||||
|
#if ENABLE_INPUT_SYSTEM
|
||||||
|
return boostFactorAction.ReadValue<Vector2>().y * 0.01f;
|
||||||
|
#else
|
||||||
|
return Input.mouseScrollDelta.y * 0.2f;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2 GetInputLookRotation()
|
||||||
|
{
|
||||||
|
#if ENABLE_INPUT_SYSTEM
|
||||||
|
return lookAction.ReadValue<Vector2>();
|
||||||
|
#else
|
||||||
|
return new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y")) * 10;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IsBoostPressed()
|
||||||
|
{
|
||||||
|
#if ENABLE_INPUT_SYSTEM
|
||||||
|
bool boost = Keyboard.current != null ? Keyboard.current.leftShiftKey.isPressed : false;
|
||||||
|
boost |= Gamepad.current != null ? Gamepad.current.xButton.isPressed : false;
|
||||||
|
return boost;
|
||||||
|
#else
|
||||||
|
return Input.GetKey(KeyCode.LeftShift);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IsEscapePressed()
|
||||||
|
{
|
||||||
|
#if ENABLE_INPUT_SYSTEM
|
||||||
|
return Keyboard.current != null ? Keyboard.current.escapeKey.isPressed : false;
|
||||||
|
#else
|
||||||
|
return Input.GetKey(KeyCode.Escape);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IsCameraRotationAllowed()
|
||||||
|
{
|
||||||
|
#if ENABLE_INPUT_SYSTEM
|
||||||
|
bool canRotate = Mouse.current != null ? Mouse.current.rightButton.isPressed : false;
|
||||||
|
canRotate |= Gamepad.current != null ? Gamepad.current.rightStick.ReadValue().magnitude > 0 : false;
|
||||||
|
return canRotate;
|
||||||
|
#else
|
||||||
|
return Input.GetMouseButton(1);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IsRightMouseButtonDown()
|
||||||
|
{
|
||||||
|
#if ENABLE_INPUT_SYSTEM
|
||||||
|
return Mouse.current != null ? Mouse.current.rightButton.isPressed : false;
|
||||||
|
#else
|
||||||
|
return Input.GetMouseButtonDown(1);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IsRightMouseButtonUp()
|
||||||
|
{
|
||||||
|
#if ENABLE_INPUT_SYSTEM
|
||||||
|
return Mouse.current != null ? !Mouse.current.rightButton.isPressed : false;
|
||||||
|
#else
|
||||||
|
return Input.GetMouseButtonUp(1);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
11
cw7/SimpleCameraController.cs.meta
Normal file
11
cw7/SimpleCameraController.cs.meta
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: ddeebdbedb9c0bc4e93debc0325462a5
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
Loading…
Reference in New Issue
Block a user