protected void SolveInstanceHelper(IGH_DataAccess data, int startIndex) { if (Rhino.RhinoApp.ExeVersion == 6) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, VersionErrorMessage); } if (!_conduitEnabled) { Rhino.Display.DisplayPipeline.PostDrawObjects += PostDrawObjects; _conduitEnabled = true; } if (!ActivateGlContext()) { return; } if (!OpenGL.IsAvailable) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Unable to access required OpenGL features"); } if (_majorVersion > 0 || _minorVersion > 1) { data.SetData(0, $"{InstanceGuid}:color"); data.SetData(1, $"{InstanceGuid}:depth"); } if (data.Iteration == 0) { if (!_model.CompileProgram()) { foreach (var err in _model.AllCompileErrors()) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, err.ToString()); } } _model.ClearData(); } var uniformsAndAttributes = _model.GetUniformsAndAttributes(data.Iteration); for (int i = startIndex; i < Params.Input.Count; i++) { List <IGH_Goo> destinationList = null; if (Params.Input[i].Access == GH_ParamAccess.item) { IGH_Goo destination = null; data.GetData(i, ref destination); destinationList = new List <IGH_Goo>(new[] { destination }); } else { destinationList = new List <IGH_Goo>(); data.GetDataList(i, destinationList); } string varname = Params.Input[i].NickName; if (varname.Contains("[")) { varname = varname.Substring(0, varname.IndexOf('[')); } string datatype; int arrayLength; if (_model.TryGetUniformType(varname, out datatype, out arrayLength)) { switch (datatype) { case "int": { int[] values = new int[destinationList.Count]; for (int j = 0; j < values.Length; j++) { IGH_Goo destination = destinationList[j]; int value; if (!destination.CastTo(out value)) { double dvalue; if (destination.CastTo(out dvalue)) { value = (int)dvalue; } else { bool bvalue; if (destination.CastTo(out bvalue)) { value = bvalue ? 1 : 0; } } } values[j] = value; } // hack for iterations if (arrayLength == 0 && data.Iteration < destinationList.Count) { values[0] = values[data.Iteration]; } uniformsAndAttributes.AddUniform(varname, values, arrayLength); break; } case "float": { float[] values = new float[destinationList.Count]; for (int j = 0; j < values.Length; j++) { IGH_Goo destination = destinationList[j]; double value; destination.CastTo(out value); if (!destination.CastTo(out value)) { int ival; if (destination.CastTo(out ival)) { value = ival; } } values[j] = (float)value; } // hack for iterations if (arrayLength == 0 && data.Iteration < destinationList.Count) { values[0] = values[data.Iteration]; } uniformsAndAttributes.AddUniform(varname, values, arrayLength); break; } case "vec3": { Point3f[] values = GooListToPoint3fArray(destinationList); // hack for iterations if (arrayLength == 0 && data.Iteration < destinationList.Count) { values[0] = values[data.Iteration]; } if (values != null) { uniformsAndAttributes.AddUniform(varname, values, arrayLength); } break; } case "vec4": { Vec4[] values = new Vec4[destinationList.Count]; for (int j = 0; j < values.Length; j++) { IGH_Goo destination = destinationList[j]; if (destination.TypeName == "Colour") { System.Drawing.Color color; if (destination.CastTo(out color)) { values[j] = new Vec4(color.R / 255.0f, color.G / 255.0f, color.B / 255.0f, color.A / 255.0f); } } } // hack for iterations if (arrayLength == 0 && data.Iteration < destinationList.Count) { values[0] = values[data.Iteration]; } uniformsAndAttributes.AddUniform(varname, values, arrayLength); break; } case "mat4": { Mat4[] values = new Mat4[destinationList.Count]; for (int j = 0; j < values.Length; j++) { IGH_Goo destination = destinationList[j]; Transform xform; if (destination.CastTo(out xform)) { values[j] = new Mat4(xform); } } // hack for iterations if (arrayLength == 0 && data.Iteration < destinationList.Count) { values[0] = values[data.Iteration]; } uniformsAndAttributes.AddUniform(varname, values, arrayLength); break; } case "bool": { int[] values = new int[destinationList.Count]; for (int j = 0; j < values.Length; j++) { IGH_Goo destination = destinationList[j]; bool value; if (!destination.CastTo(out value)) { int ivalue; if (destination.CastTo(out ivalue)) { value = ivalue != 0; } } values[j] = value ? 1 : 0; } // hack for iterations if (arrayLength == 0 && data.Iteration < destinationList.Count) { values[0] = values[data.Iteration]; } uniformsAndAttributes.AddUniform(varname, values, arrayLength); break; } case "sampler2D": { IGH_Goo destination = destinationList[0]; //Try casting to a string first. This will be interpreted as a path to an image file string path; if (destination.CastTo(out path)) { bool isComponentInput = false; // see if path refers to a component's output parameter if (path.EndsWith(":color") || path.EndsWith(":depth")) { string id = path.Substring(0, path.IndexOf(":")); isComponentInput = Guid.TryParse(id, out Guid compId); } if (!isComponentInput) { bool isUrl = path.StartsWith("http:/", StringComparison.InvariantCultureIgnoreCase) || path.StartsWith("https:/", StringComparison.InvariantCultureIgnoreCase); if (!isUrl && !System.IO.File.Exists(path)) { var ghdoc = OnPingDocument(); if (ghdoc != null) { string workingDirectory = System.IO.Path.GetDirectoryName(ghdoc.FilePath); path = System.IO.Path.GetFileName(path); path = System.IO.Path.Combine(workingDirectory, path); } } } uniformsAndAttributes.AddSampler2DUniform(varname, path); } else { System.Drawing.Bitmap bmp; if (destination.CastTo(out bmp)) { uniformsAndAttributes.AddSampler2DUniform(varname, bmp); } } break; } } continue; } int location; if (_model.TryGetAttributeType(varname, out datatype, out location)) { if (datatype == "int" || datatype == "float") { List <IGH_Goo> goo = new List <IGH_Goo>(); if (data.GetDataList(i, goo)) { int[] ints = datatype == "int" ? new int[goo.Count] : null; float[] floats = ints == null ? new float[goo.Count] : null; for (int index = 0; index < goo.Count; index++) { double dValue; int iValue; string sValue; if (goo[index].CastTo(out dValue)) { if (ints != null) { ints[index] = (int)dValue; } if (floats != null) { floats[index] = (float)dValue; } } else if (goo[index].CastTo(out iValue)) { if (ints != null) { ints[index] = iValue; } if (floats != null) { floats[index] = (float)iValue; } } else if (goo[index].CastTo(out sValue)) { if (double.TryParse(sValue, out dValue)) { if (ints != null) { ints[index] = (int)dValue; } if (floats != null) { floats[index] = (float)dValue; } } } } if (ints != null && ints.Length > 0) { uniformsAndAttributes.AddAttribute(varname, location, ints); } if (floats != null && floats.Length > 0) { uniformsAndAttributes.AddAttribute(varname, location, floats); } } if (goo.Count < 1 && datatype == "int") { List <int> int_destination = new List <int>(); if (data.GetDataList(i, int_destination) && int_destination.Count > 0) { int[] ints = int_destination.ToArray(); if (ints != null && ints.Length > 0) { uniformsAndAttributes.AddAttribute(varname, location, ints); } } } } if (datatype == "vec2") { //vec2 -> point2d Point2f[] vec2_array = GooListToPoint2fArray(destinationList); if (vec2_array != null) { uniformsAndAttributes.AddAttribute(varname, location, vec2_array); } } if (datatype == "vec3") { //vec3 -> point3d Point3f[] vec3_array = GooListToPoint3fArray(destinationList); if (vec3_array != null) { uniformsAndAttributes.AddAttribute(varname, location, vec3_array); } } if (datatype == "vec4") { List <System.Drawing.Color> destination = new List <System.Drawing.Color>(); if (data.GetDataList(i, destination)) { Vec4[] vec4_array = new Vec4[destination.Count]; for (int index = 0; index < destination.Count; index++) { Color4f color = new Color4f(destination[index]); vec4_array[index] = new Vec4(color.R, color.G, color.B, color.A); } uniformsAndAttributes.AddAttribute(varname, location, vec4_array); } } if (datatype == "mat4") { List <Transform> destination = new List <Transform>(); if (data.GetDataList(i, destination)) { Mat4[] mat4_array = new Mat4[destination.Count]; for (int index = 0; index < destination.Count; index++) { mat4_array[index] = new Mat4(destination[index]); } uniformsAndAttributes.AddAttribute(varname, location, mat4_array); } } continue; } // If we get here, we don't have a reference to this input in our code yet. // See if the input is an upstream texture since we know how to handle those if (destinationList.Count > 0 && destinationList[0].CastTo(out string upstreamSampler)) { // see if path refers to a component's output parameter if (upstreamSampler.EndsWith(":color") || upstreamSampler.EndsWith(":depth")) { string id = upstreamSampler.Substring(0, upstreamSampler.IndexOf(":")); bool isComponentInput = Guid.TryParse(id, out Guid compId); if (isComponentInput) { uniformsAndAttributes.AddSampler2DUniform(varname, upstreamSampler); } } } } }
protected void SolveInstanceHelper(IGH_DataAccess data, int startIndex) { if (!ActivateGlContext()) { return; } if (!_model.CompileProgram()) { foreach (var err in _model.AllCompileErrors()) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, err.ToString()); } } _model.ClearData(); for (int i = startIndex; i < Params.Input.Count; i++) { string varname = Params.Input[i].NickName; string datatype; if (_model.TryGetUniformType(varname, out datatype)) { IGH_Goo destination = null; if (Params.Input[i].Access == GH_ParamAccess.item) { data.GetData(i, ref destination); } else { List <IGH_Goo> list = new List <IGH_Goo>(); data.GetDataList(i, list); destination = list[0]; } switch (datatype) { case "int": { int value; if (!destination.CastTo(out value)) { double dvalue; if (destination.CastTo(out dvalue)) { value = (int)dvalue; } else { bool bvalue; if (destination.CastTo(out bvalue)) { value = bvalue ? 1 : 0; } } } _model.AddUniform(varname, value); break; } case "float": { double value; destination.CastTo(out value); if (!destination.CastTo(out value)) { int ival; if (destination.CastTo(out ival)) { value = ival; } } _model.AddUniform(varname, (float)value); break; } case "vec3": { Point3d point; if (destination.CastTo(out point)) { float x = (float)point.X; float y = (float)point.Y; float z = (float)point.Z; _model.AddUniform(varname, new Point3f(x, y, z)); } break; } case "vec4": { if (destination.TypeName == "Colour") { System.Drawing.Color color; if (destination.CastTo(out color)) { Vec4 v4 = new Vec4(color.R / 255.0f, color.G / 255.0f, color.B / 255.0f, color.A / 255.0f); _model.AddUniform(varname, v4); } } break; } case "bool": { bool value; if (!destination.CastTo(out value)) { int ivalue; if (destination.CastTo(out ivalue)) { value = ivalue != 0; } } _model.AddUniform(varname, value ? 1 : 0); break; } case "sampler2D": { //Try casting to a string first. This will be interpreted as a path to an image file string path; if (destination.CastTo(out path)) { _model.AddSampler2DUniform(varname, path); } break; } } continue; } int location; if (_model.TryGetAttributeType(varname, out datatype, out location)) { if (datatype == "int" || datatype == "float") { List <double> destination = new List <double>(); if (data.GetDataList(i, destination)) { int[] ints = datatype == "int" ? new int[destination.Count] : null; float[] floats = ints == null ? new float[destination.Count] : null; for (int index = 0; index < destination.Count; index++) { if (ints != null) { ints[index] = (int)destination[index]; } if (floats != null) { floats[index] = (float)destination[index]; } } if (ints != null && ints.Length > 0) { _model.AddAttribute(varname, location, ints); } if (floats != null && floats.Length > 0) { _model.AddAttribute(varname, location, floats); } } if (destination.Count < 1 && datatype == "int") { List <int> int_destination = new List <int>(); if (data.GetDataList(i, int_destination) && int_destination.Count > 0) { int[] ints = int_destination.ToArray(); if (ints != null && ints.Length > 0) { _model.AddAttribute(varname, location, ints); } } } } if (datatype == "vec3") { //vec3 -> point3d List <Point3d> destination = new List <Point3d>(); if (data.GetDataList(i, destination)) { Point3f[] vec3_array = new Point3f[destination.Count]; for (int index = 0; index < destination.Count; index++) { float x = (float)destination[index].X; float y = (float)destination[index].Y; float z = (float)destination[index].Z; vec3_array[index] = new Point3f(x, y, z); } _model.AddAttribute(varname, location, vec3_array); } } if (datatype == "vec4") { List <System.Drawing.Color> destination = new List <System.Drawing.Color>(); if (data.GetDataList(i, destination)) { Vec4[] vec4_array = new Vec4[destination.Count]; for (int index = 0; index < destination.Count; index++) { Color4f color = new Color4f(destination[index]); vec4_array[index] = new Vec4(color.R, color.G, color.B, color.A); } _model.AddAttribute(varname, location, vec4_array); } } } } }