Mercurial > hg > Game > Games
comparison Orchestland/Assets/LeapMotion/DemoResources/Scripts/CubeWave.cs @ 1:f7675884f2a1
Add Orchestland project
author | Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 17 Jul 2015 23:09:20 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:347d21cdfc22 | 1:f7675884f2a1 |
---|---|
1 /******************************************************************************\ | |
2 * Copyright (C) Leap Motion, Inc. 2011-2014. * | |
3 * Leap Motion proprietary. Licensed under Apache 2.0 * | |
4 * Available at http://www.apache.org/licenses/LICENSE-2.0.html * | |
5 \******************************************************************************/ | |
6 | |
7 using UnityEngine; | |
8 using System.Collections; | |
9 | |
10 public class CubeWave : MonoBehaviour { | |
11 | |
12 private const float BRIGHTNESS_SCALE = 5.0f; | |
13 private const float BASE_BRIGHTNESS = 0.2f; | |
14 | |
15 public int gridWidth = 25; | |
16 public int gridHeight = 25; | |
17 public float cellWidth = 1.0f; | |
18 public float cellHeight = 1.0f; | |
19 public float springConstant = 200.0f; | |
20 public float damping = 0.01f; | |
21 | |
22 public Transform model; | |
23 public Light lowGlow; | |
24 public Light highGlow; | |
25 public Color lowColor; | |
26 public Color highColor; | |
27 | |
28 Transform[,] cube_grid_; | |
29 | |
30 void Start() { | |
31 cube_grid_ = new Transform[gridHeight + 2, gridWidth + 2]; | |
32 for (int r = 0; r < gridHeight + 2; ++r) { | |
33 for (int c = 0; c < gridWidth + 2; ++c) { | |
34 cube_grid_[r, c] = transform; | |
35 } | |
36 } | |
37 | |
38 float center_x = gridWidth * cellWidth / 2.0f; | |
39 float center_y = gridHeight * cellHeight / 2.0f; | |
40 | |
41 // Setup the cube grid. | |
42 for (int r = 1; r <= gridHeight; ++r) { | |
43 for (int c = 1; c <= gridWidth; ++c) { | |
44 Vector3 location = new Vector3(c * cellWidth - center_x, 0, r * cellHeight - center_y); | |
45 cube_grid_[r, c] = Instantiate(model, transform.position + location, | |
46 transform.rotation * model.transform.rotation) as Transform; | |
47 cube_grid_[r, c].parent = transform; | |
48 } | |
49 } | |
50 } | |
51 | |
52 void Update() { | |
53 float low_total_light = 0.0f; | |
54 float high_total_light = 0.0f; | |
55 | |
56 for (int r = 1; r <= gridHeight; ++r) { | |
57 for (int c = 1; c <= gridWidth; ++c) { | |
58 | |
59 // Discrete wave equation with damping. | |
60 float neighbor_sum = (cube_grid_[r - 1, c].position.y + cube_grid_[r, c - 1].position.y + | |
61 cube_grid_[r + 1, c].position.y + cube_grid_[r, c + 1].position.y); | |
62 | |
63 float delta_from_rest = 0.25f * neighbor_sum - cube_grid_[r, c].position.y; | |
64 cube_grid_[r, c].GetComponent<Rigidbody>().AddForce(springConstant * Vector3.up * delta_from_rest); | |
65 cube_grid_[r, c].GetComponent<Rigidbody>().velocity *= (1 - damping); | |
66 | |
67 // Set color of cube and add to glow amount based on current height. | |
68 float delta_zero = transform.position.y - cube_grid_[r, c].position.y; | |
69 float brightness = BASE_BRIGHTNESS + | |
70 BRIGHTNESS_SCALE * Mathf.Log(1 + 0.2f * delta_zero); | |
71 | |
72 if (brightness < 0) { | |
73 cube_grid_[r, c].GetComponent<Renderer>().material.SetColor("_Color", highColor * (-brightness)); | |
74 high_total_light -= brightness / (gridHeight * gridWidth); | |
75 } | |
76 else { | |
77 cube_grid_[r, c].GetComponent<Renderer>().material.SetColor("_Color", lowColor * brightness); | |
78 low_total_light += brightness / (gridHeight * gridWidth); | |
79 } | |
80 } | |
81 } | |
82 | |
83 // Set glow amount. | |
84 if (lowGlow != null) | |
85 lowGlow.intensity = low_total_light; | |
86 if (highGlow != null) | |
87 highGlow.intensity = high_total_light; | |
88 } | |
89 } |