//Methods private void Add(SurfaceData sd, SurfaceBlends.NormalizedBlends blendResults, Color tint, float weightMultiplier, ref float totalWeight) { if (weightMultiplier <= 0.0000000001f) { return; } for (int i = 0; i < blendResults.result.Count; i++) { var blend = blendResults.result[i]; blend.color *= tint; sd.AddBlend(blend, false, weightMultiplier, ref totalWeight); } }
internal bool TryAddBlends(SurfaceData sd, Mesh mesh, int submeshID, Vector3 point, int triangleID, out float totalWeight) { totalWeight = 0; //Finds Barycentric var triangle = triangleID * 3; var t0 = triangles[triangle + 0]; var t1 = triangles[triangle + 1]; var t2 = triangles[triangle + 2]; var a = vertices[t0]; var b = vertices[t1]; var c = vertices[t2]; point = transform.InverseTransformPoint(point); var bary = new Barycentric(a, b, c, point); #if UNITY_EDITOR lastSampledColors.Clear(); #endif float totalTotalWeight = 0; for (int i = 0; i < blendMaps.Length; i++) { var bm = blendMaps[i]; bm.sampled = false; for (int ii = 0; ii < bm.subMaterials.Length; ii++) { if (bm.subMaterials[ii].materialID == submeshID) { var uv = bary.Interpolate(bm.uvs[t0], bm.uvs[t1], bm.uvs[t2]); uv = uv * new Vector2(bm.uvScaleOffset.x, bm.uvScaleOffset.y) + new Vector2(bm.uvScaleOffset.z, bm.uvScaleOffset.w); //? Color color = bm.map.GetPixelBilinear(uv.x, uv.y); //this only works for clamp or repeat btw (not mirror etc.) bm.sampledColor = color; void SampleColor(BlendMap.SurfaceBlends2 sb2) { if (sb2.colorMap != null) { sb2.sampledColor = sb2.colorMap.GetPixelBilinear(uv.x, uv.y); } else { sb2.sampledColor = Color.white; } } SampleColor(bm.r); SampleColor(bm.g); SampleColor(bm.b); SampleColor(bm.a); totalTotalWeight += bm.weight * (color.r + color.g + color.b + color.a); #if UNITY_EDITOR lastSampledColors.Add(color); #endif bm.sampled = true; break; } } } if (totalTotalWeight > 0) { float invTotalTotal = 1f / totalTotalWeight; for (int i = 0; i < blendMaps.Length; i++) { var bm = blendMaps[i]; if (bm.sampled) { float invTotal = bm.weight * invTotalTotal; var color = bm.sampledColor; Add(sd, bm.r.result, bm.r.sampledColor, color.r * invTotal, ref totalWeight); Add(sd, bm.g.result, bm.g.sampledColor, color.g * invTotal, ref totalWeight); Add(sd, bm.b.result, bm.b.sampledColor, color.b * invTotal, ref totalWeight); Add(sd, bm.a.result, bm.a.sampledColor, color.a * invTotal, ref totalWeight); } } return(true); } return(false); }