Created
September 29, 2015 04:45
-
-
Save davepape/31c65060a53fbea5d77f to your computer and use it in GitHub Desktop.
create a mesh in Unity, reading some data from a text file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma strict | |
// Script to create a mesh with 2 triangles, getting colors from a text file. | |
function Start () | |
{ | |
gameObject.AddComponent.<MeshFilter>(); | |
gameObject.AddComponent.<MeshRenderer>(); | |
var mat = new Material(Shader.Find("Custom/Vertex Colored")); | |
GetComponent.<Renderer>().material = mat; | |
var p0 : Vector3 = Vector3(-2,0,-1); | |
var p1 : Vector3 = Vector3(0,1,-1); | |
var p2 : Vector3 = Vector3(1,0,-1); | |
var p3 : Vector3 = Vector3(0,-1,-1); | |
var newVertices = [ p0, p1, p2, p3 ]; | |
var s = new System.IO.File.ReadAllLines('colors.txt'); | |
var newColors = new Color[4]; | |
for (var i=0; (i < s.Length) && (i < 4); i++) | |
{ | |
var vals = s[i].Split(" "[0]); | |
newColors[i] = Color(parseFloat(vals[0]),parseFloat(vals[1]),parseFloat(vals[2])); | |
} | |
var newTriangles = [ 0, 1, 2, 0, 2, 3 ]; | |
var m = GetComponent.<MeshFilter>().mesh; | |
m.vertices = newVertices; | |
m.colors = newColors; | |
m.triangles = newTriangles; | |
} | |
function Update() | |
{ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment