1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class Graph : MonoBehaviour { public Transform pointPrefab; [Range(10, 100)] public int resolution = 10; //prefab의 개수 [Range(0.1f, 5)] public float height = .5f; [Range(1, 50)] public float length = 2f; [Range(0, 100)] public float rotationSpeed = 50f; [Range(.1f, 2f)] public float prefabScale = 1f; Transform[] points; private void Awake() { points = new Transform[resolution]; //배열은 미리 크기를 지정해주어야 한다. for(int i=0; i < points.Length; i++) { Transform point = Instantiate(pointPrefab); point.SetParent(transform, false); //생성자 하위 항목으로 prefab넣기 points[i] = point; //points배열에 생성한 point 넣기 } } private void Update() { float step = length / resolution; Vector3 scale = Vector3.one * prefabScale; for (int i=0; i<points.Length; i++) { //point wave animation Transform point = points[i]; Vector3 position = point.localPosition; //local position을 변수값으로 가져옴 position.x = (i + 0.5f) * step - (length / 2); position.y = height * Mathf.Sin(Mathf.PI * (position.x + Time.time)); //x축으로 이동하는 sin파(애니메이션) 파이를 곱한 이유는 주기를 2로 변경하기 위해서 position.z = 0; point.localPosition = position; //localposition을 수정한 position으로 변경 point.localScale = scale; } //x축으로 회전 Quaternion rotation = transform.localRotation; rotation = Quaternion.Euler(Time.time * rotationSpeed, 0, 0); transform.localRotation = rotation; } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | Shader "Custom/ColoredPoint" { Properties { _Glossiness ("Smoothness", Range(0,1)) = 1 _Metallic ("Metallic", Range(0,1)) = 0.0 } SubShader { Tags { "RenderType"="Opaque" } LOD 200 CGPROGRAM #pragma surface surf Standard fullforwardshadows #pragma target 3.0 struct Input { float3 worldPos; //input값으로 worldPos 사용 }; half _Glossiness; half _Metallic; UNITY_INSTANCING_BUFFER_START(Props) UNITY_INSTANCING_BUFFER_END(Props) void surf (Input IN, inout SurfaceOutputStandard o) { o.Albedo.rg = IN.worldPos.xy * 0.5 + 0.5; //worldPosition에 따라서 색상 가짐 o.Metallic = _Metallic; o.Smoothness = _Glossiness; o.Alpha = 0.5; } ENDCG } FallBack "Diffuse" } | cs |
'programming > c#' 카테고리의 다른 글
유니티 C# and Shader Study <Basics 01 _ Fractal> (0) | 2019.12.28 |
---|---|
유니티 C# and Shader Study <Basics 01 _ MathMatics Surface> static, enum, delegate (0) | 2019.12.17 |
유니티 C# and Shader Study <Basics 01 _ GameObjects and Scripts> (0) | 2019.12.16 |
Mathf (0) | 2018.11.17 |
object pooling + singleton (0) | 2018.11.17 |