Pārlūkot izejas kodu

add block rotation control

shader
DemiSel 4 gadus atpakaļ
vecāks
revīzija
54ec177e4d

+ 51
- 2
FreeCam/Assets/BlockSpawner.cs Parādīt failu

@@ -6,6 +6,10 @@ using UnityEngine.InputSystem;
6 6
 public class BlockSpawner : MonoBehaviour
7 7
 {
8 8
 
9
+    private void Start()
10
+    {
11
+        _playerInput = GetComponent<PlayerInput>();
12
+    }
9 13
     [SerializeField]
10 14
     private GameObject _blockToPlace;
11 15
     public GameObject BlockToPlace
@@ -20,6 +24,7 @@ public class BlockSpawner : MonoBehaviour
20 24
             _blockToPlace = value;
21 25
         }
22 26
     }
27
+    PlayerInput _playerInput;
23 28
 
24 29
     public float spawnDistance = 3f;
25 30
 
@@ -28,6 +33,10 @@ public class BlockSpawner : MonoBehaviour
28 33
 
29 34
 
30 35
     private bool _isPreview = false;
36
+
37
+    public bool _blockRotating = false;
38
+    private Quaternion _lastRotationState;
39
+
31 40
     private Shader _initialShader;
32 41
 
33 42
     public void OnPlaceBlock()
@@ -43,8 +52,44 @@ public class BlockSpawner : MonoBehaviour
43 52
     {
44 53
         float valueAsFloat = value.Get<float>();
45 54
 
46
-        if (valueAsFloat == 1f) EnablePreviewMode();
47
-        else DisablePreviewMode();
55
+        if (valueAsFloat == 1f)
56
+        {
57
+            EnablePreviewMode();
58
+        }
59
+        else
60
+        {
61
+            DisablePreviewMode();
62
+        }
63
+    }
64
+
65
+    public void OnEnterRotateBlock(InputValue value)
66
+    {
67
+        float valueAsFloat = value.Get<float>();
68
+        Debug.Log(valueAsFloat);
69
+        _blockRotating = true;
70
+    }
71
+
72
+    public void OnExitRotateBlock(InputValue value)
73
+    {
74
+        float valueAsFloat = value.Get<float>();
75
+        Debug.Log(valueAsFloat);
76
+        _blockRotating = false;
77
+    }
78
+
79
+    public void OnRotateBlock(InputValue value)
80
+    {
81
+        if (_isPreview && _blockRotating)
82
+        {
83
+            Vector2 iMouseMovement = value.Get<Vector2>();
84
+
85
+            Transform vTransform = _blockInstance.transform;
86
+            //vTransform.Rotate(iMouseMovement.y / 10, iMouseMovement.x / 10, 0);
87
+            vTransform.RotateAround(vTransform.position, transform.right, iMouseMovement.y / 10.0f);
88
+            vTransform.RotateAround(vTransform.position, transform.up, iMouseMovement.x / 10.0f);
89
+            //vTransform.eulerAngles += vTransform.worldToLocalMatrix.MultiplyVector(new Vector3(iMouseMovement.y / 10, iMouseMovement.x / 10, 0));
90
+            //vTransform.Rotate(iMouseMovement.y/10, iMouseMovement.x/10, 0);
91
+            _lastRotationState = vTransform.rotation;
92
+        } 
48 93
     }
49 94
 
50 95
     private void EnablePreviewMode()
@@ -52,8 +97,10 @@ public class BlockSpawner : MonoBehaviour
52 97
         if (_blockToPlace == null) return;
53 98
 
54 99
         Vector3 spawnPos = Vector3.forward * spawnDistance;
100
+
55 101
         _blockInstance = Instantiate(_blockToPlace, spawnPos, Quaternion.identity);
56 102
         _blockInstance.transform.SetParent(this.transform, false);
103
+        _blockInstance.transform.rotation = _lastRotationState;
57 104
         _isPreview = true;
58 105
         SwitchToMeshRender();
59 106
     }
@@ -78,4 +125,6 @@ public class BlockSpawner : MonoBehaviour
78 125
         Material vMaterial = vRenderer.material;
79 126
         vMaterial.shader = _initialShader;
80 127
     }
128
+
129
+    
81 130
 }

+ 58
- 1
FreeCam/Assets/Controls.inputactions Parādīt failu

@@ -44,6 +44,30 @@
44 44
                     "expectedControlType": "Button",
45 45
                     "processors": "",
46 46
                     "interactions": ""
47
+                },
48
+                {
49
+                    "name": "Enter Rotate Block",
50
+                    "type": "Button",
51
+                    "id": "b22358ff-4b1a-4885-b252-f3f61eb2e897",
52
+                    "expectedControlType": "Button",
53
+                    "processors": "",
54
+                    "interactions": "Press"
55
+                },
56
+                {
57
+                    "name": "Exit Rotate Block",
58
+                    "type": "Button",
59
+                    "id": "38b2fbd1-ab2b-49f0-b20c-12ede099fd44",
60
+                    "expectedControlType": "Button",
61
+                    "processors": "",
62
+                    "interactions": "Press(behavior=1)"
63
+                },
64
+                {
65
+                    "name": "Rotate Block",
66
+                    "type": "PassThrough",
67
+                    "id": "83900c91-8eb5-4324-aed3-52d0adf00fad",
68
+                    "expectedControlType": "Vector2",
69
+                    "processors": "",
70
+                    "interactions": ""
47 71
                 }
48 72
             ],
49 73
             "bindings": [
@@ -214,7 +238,7 @@
214 238
                 },
215 239
                 {
216 240
                     "name": "",
217
-                    "id": "b00386e1-8b81-4b23-add0-14dfd7c1fc48",
241
+                    "id": "a13b0893-b67b-4b60-819c-a2a88e0a551c",
218 242
                     "path": "<Mouse>/delta",
219 243
                     "interactions": "",
220 244
                     "processors": "",
@@ -343,6 +367,39 @@
343 367
                     "action": "Sprint",
344 368
                     "isComposite": false,
345 369
                     "isPartOfComposite": false
370
+                },
371
+                {
372
+                    "name": "",
373
+                    "id": "14041b54-6b2c-4248-8d13-d3652f618c8f",
374
+                    "path": "<Mouse>/middleButton",
375
+                    "interactions": "Press",
376
+                    "processors": "",
377
+                    "groups": "",
378
+                    "action": "Enter Rotate Block",
379
+                    "isComposite": false,
380
+                    "isPartOfComposite": false
381
+                },
382
+                {
383
+                    "name": "",
384
+                    "id": "3ce3e2d9-ec9a-4062-971e-3f524008af63",
385
+                    "path": "<Mouse>/middleButton",
386
+                    "interactions": "Press(behavior=1)",
387
+                    "processors": "",
388
+                    "groups": "",
389
+                    "action": "Exit Rotate Block",
390
+                    "isComposite": false,
391
+                    "isPartOfComposite": false
392
+                },
393
+                {
394
+                    "name": "",
395
+                    "id": "ccc8db9a-4581-4a39-9c88-6834536d8888",
396
+                    "path": "<Mouse>/delta",
397
+                    "interactions": "",
398
+                    "processors": "",
399
+                    "groups": "",
400
+                    "action": "Rotate Block",
401
+                    "isComposite": false,
402
+                    "isPartOfComposite": false
346 403
                 }
347 404
             ]
348 405
         }

+ 31
- 129
FreeCam/Assets/Effects/wMaillageShader.shader Parādīt failu

@@ -1,134 +1,36 @@
1 1
 Shader "Custom/wMaillageShader"
2 2
 {
3
-    Properties
4
-    {
5
-		_MainTex("Texture", 2D) = "white" {}
6
-		_Color("Color", Color) = (1,1,1,1)
7
-		_Threshold("Threshold", float) = 0.01
8
-
9
-		_EdgeColor("Edge color", Color) = (1,0,0,1)
10
-
11
-		_Glossiness("Smoothness", Range(0,1)) = 0.5
12
-		_Metallic("Metallic", Range(0,1)) = 0.0
13
-    }
14
-    SubShader
15
-    {
16
-        Cull Off ZWrite Off ZTest Always
17
-		Tags { "RenderType" = "Opaque" }
18
-        Pass
19
-
3
+	Properties
4
+	{
5
+	   _InnerColor("Inner Color", Color) = (0.0, 0.0, 0.0, 0.5)
6
+	   _RimColor("Rim Color", Color) = (1.0,1.0,1.0,0.5)
7
+	   _RimPower("Rim Power", Range(0.5,8.0)) = 2.47
8
+	}
9
+		SubShader
10
+	{
11
+	  Tags { "Queue" = "Transparent" }
12
+	  Cull Off ZWrite Off ZTest Less
13
+		//Blend based on output Alpha, not color value
14
+		Blend One OneMinusSrcAlpha
15
+		CGPROGRAM
16
+		#pragma surface surf Lambert alpha:fade
17
+		struct Input
20 18
 		{
21
-
22
-			CGPROGRAM
23
-
24
-			#pragma vertex vert
25
-
26
-			#pragma fragment frag 
27
-			
28
-
29
-			#include "UnityCG.cginc"
30
-
31
-			struct appdata
32
-
33
-			{
34
-
35
-				float4 vertex : POSITION;
36
-
37
-				float2 uv : TEXCOORD0;
38
-
39
-			};
40
-
41
-			struct v2f
42
-
43
-			{
44
-
45
-				float2 uv : TEXCOORD0;
46
-
47
-				float4 vertex : SV_POSITION;
48
-
49
-			};
50
-
51
-			sampler2D _CameraDepthNormalsTexture;
52
-			float4 _MainTex_ST;
53
-
54
-			v2f vert(appdata v)
55
-
56
-			{
57
-
58
-				v2f o;
59
-
60
-				o.vertex = UnityObjectToClipPos(v.vertex);
61
-
62
-				o.uv = TRANSFORM_TEX(v.uv, _MainTex);
63
-
64
-				return o;
65
-
66
-			}
67
-
68
-			sampler2D _MainTex;
69
-
70
-			float4 _MainTex_TexelSize;
71
-
72
-			float _Threshold;
73
-
74
-			fixed4 _EdgeColor;
75
-
76
-			float4 GetPixelValue(in float2 uv) {
77
-
78
-				half3 normal;
79
-
80
-				float depth;
81
-
82
-				DecodeDepthNormal(tex2D(_CameraDepthNormalsTexture, uv), depth, normal);
83
-
84
-				return fixed4(normal, depth);
85
-
86
-			}
87
-
88
-			fixed4 frag(v2f i) : SV_Target
89
-
90
-			{
91
-
92
-				fixed4 col = tex2D(_MainTex, i.uv);
93
-
94
-				fixed4 orValue = GetPixelValue(i.uv);
95
-
96
-				float2 offsets[8] = {
97
-
98
-					float2(-1, -1),
99
-
100
-					float2(-1, 0),
101
-
102
-					float2(-1, 1),
103
-
104
-					float2(0, -1),
105
-
106
-					float2(0, 1),
107
-
108
-					float2(1, -1),
109
-
110
-					float2(1, 0),
111
-
112
-					float2(1, 1)
113
-
114
-				};
115
-
116
-				fixed4 sampledValue = fixed4(0,0,0,0);
117
-
118
-				for (int j = 0; j < 8; j++) {
119
-
120
-					sampledValue += GetPixelValue(i.uv + offsets[j] * _MainTex_TexelSize.xy);
121
-
122
-				}
123
-
124
-				sampledValue /= 8;
125
-
126
-				return lerp(col, _EdgeColor, step(_Threshold, length(orValue - sampledValue)));
127
-
128
-			}
129
-
130
-			
131
-			ENDCG
19
+			float3 viewDir;
20
+		};
21
+		float4 _InnerColor;
22
+		float4 _RimColor;
23
+		float _RimPower;
24
+		void surf(Input IN, inout SurfaceOutput o)
25
+		{
26
+			half rim = 1.0 - saturate(dot(normalize(IN.viewDir), o.Normal));
27
+			rim = pow(rim, _RimPower);
28
+			//Shift inner color to rim color by rim value adjusted by _InnerColor transparency to reduce its effect
29
+			//on semi-transparent part of rim color the more transparent inner color becomes.
30
+			o.Emission = lerp(_InnerColor.rgb, _RimColor.rgb, saturate(rim + (1 - _InnerColor.a) - (1 - _RimColor.a)));
31
+			o.Alpha = lerp(_InnerColor.a, _RimColor.a, rim);
132 32
 		}
133
-    }
33
+		ENDCG
34
+	}
35
+		Fallback "Diffuse"
134 36
 }

+ 1
- 1
FreeCam/Assets/Effects/wMaillageShader.shader.meta Parādīt failu

@@ -3,7 +3,7 @@ guid: 167cdc0b24acc2f449b7fb1756fbc873
3 3
 ShaderImporter:
4 4
   externalObjects: {}
5 5
   defaultTextures:
6
-  - _MainTex: {fileID: 10300, guid: 0000000000000000f000000000000000, type: 0}
6
+  - _MainTex: {fileID: 10309, guid: 0000000000000000f000000000000000, type: 0}
7 7
   nonModifiableTextures: []
8 8
   userData: 
9 9
   assetBundleName: 

+ 8
- 2
FreeCam/Assets/Materials/New Material.mat Parādīt failu

@@ -8,7 +8,7 @@ Material:
8 8
   m_PrefabInstance: {fileID: 0}
9 9
   m_PrefabAsset: {fileID: 0}
10 10
   m_Name: New Material
11
-  m_Shader: {fileID: 4800000, guid: b0c691a25e854aa488e55c7de961b502, type: 3}
11
+  m_Shader: {fileID: 4800000, guid: 167cdc0b24acc2f449b7fb1756fbc873, type: 3}
12 12
   m_ShaderKeywords: 
13 13
   m_LightmapFlags: 4
14 14
   m_EnableInstancingVariants: 0
@@ -40,7 +40,7 @@ Material:
40 40
         m_Scale: {x: 0.85, y: 0.85}
41 41
         m_Offset: {x: 0.075, y: 0.075}
42 42
     - _MainTex:
43
-        m_Texture: {fileID: 10906, guid: 0000000000000000f000000000000000, type: 0}
43
+        m_Texture: {fileID: 10305, guid: 0000000000000000f000000000000000, type: 0}
44 44
         m_Scale: {x: 0.85, y: 0.85}
45 45
         m_Offset: {x: 0.075, y: 0.075}
46 46
     - _MetallicGlossMap:
@@ -67,12 +67,18 @@ Material:
67 67
     - _Mode: 0
68 68
     - _OcclusionStrength: 1
69 69
     - _Parallax: 0.02
70
+    - _RimPower: 0.91
70 71
     - _SmoothnessTextureChannel: 0
71 72
     - _SpecularHighlights: 1
72 73
     - _SrcBlend: 1
74
+    - _Threshold: 0.01
73 75
     - _UVSec: 0
74 76
     - _ZWrite: 1
75 77
     m_Colors:
76 78
     - _Color: {r: 0.42955682, g: 0.4301132, b: 0.46226418, a: 1}
79
+    - _EdgeColor: {r: 1, g: 0, b: 0, a: 1}
77 80
     - _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
81
+    - _InnerColor: {r: 0, g: 0, b: 0, a: 1}
82
+    - _MainTex: {r: 0.5, g: 0.5, b: 1, a: 1}
83
+    - _RimColor: {r: 1, g: 1, b: 1, a: 1}
78 84
   m_BuildTextureStacks: []

+ 19
- 3
FreeCam/Assets/Scripts/FreeCam.cs Parādīt failu

@@ -23,6 +23,8 @@ public class FreeCam : MonoBehaviour
23 23
     private Vector2 _inputMouse;
24 24
     private Vector2 _velocity;
25 25
     private CharacterController _controller;
26
+    private BlockSpawner _blockSpawner;
27
+
26 28
 
27 29
     private float _rotationX;
28 30
     private bool _isSprinting;
@@ -37,7 +39,7 @@ public class FreeCam : MonoBehaviour
37 39
         _inputMouse = Vector2.zero;
38 40
         _velocity = Vector2.zero;
39 41
         _controller = gameObject.GetComponent<CharacterController>();
40
-
42
+        _blockSpawner = gameObject.GetComponent<BlockSpawner>();
41 43
         _rotationX = 0;
42 44
         _isSprinting = false;
43 45
     }
@@ -77,8 +79,17 @@ public class FreeCam : MonoBehaviour
77 79
     public void OnLook(InputValue value)
78 80
     {
79 81
         Vector2 valueAsVector2 = value.Get<Vector2>();
80
-        _inputMouse.x = valueAsVector2.x;
81
-        _inputMouse.y = valueAsVector2.y;
82
+        float xVelocity = valueAsVector2.x;
83
+        float yVelocity = valueAsVector2.y;
84
+
85
+        if (_blockSpawner._blockRotating)
86
+        {
87
+            xVelocity *= 0.05f;
88
+            yVelocity *= 0.05f;
89
+        }
90
+        _inputMouse.x = xVelocity;
91
+        _inputMouse.y = yVelocity;
92
+
82 93
     }
83 94
 
84 95
     public void OnMovement(InputValue value)
@@ -88,6 +99,11 @@ public class FreeCam : MonoBehaviour
88 99
         _velocity.y = valueAsVector2.y;
89 100
     }
90 101
 
102
+    public void OnMovementBlockRotating(InputValue value)
103
+    {
104
+        OnMovement(value);
105
+    }
106
+
91 107
     public void OnSprint(InputValue value)
92 108
     {
93 109
         float valueAsFloat = value.Get<float>();

Notiek ielāde…
Atcelt
Saglabāt