Vector3 CalculateSurfaceNormal(Vector3 p) { float H = 0.001f; float dx = DensityFunctions.Density_Func(p + new Vector3(H, 0.0f, 0.0f)) - DensityFunctions.Density_Func(p - new Vector3(H, 0.0f, 0.0f)); float dy = DensityFunctions.Density_Func(p + new Vector3(0.0f, H, 0.0f)) - DensityFunctions.Density_Func(p - new Vector3(0.0f, H, 0.0f)); float dz = DensityFunctions.Density_Func(p + new Vector3(0.0f, 0.0f, H)) - DensityFunctions.Density_Func(p - new Vector3(0.0f, 0.0f, H)); return(Vector3.Normalize(new Vector3(dx, dy, dz))); }
Vector3 ApproximateZeroCrossingPosition(Vector3 p0, Vector3 p1) { // approximate the zero crossing by finding the min value along the edge float minValue = 100000.0f; float t = 0.0f; float currentT = 0.0f; int steps = 8; float increment = 1.0f / (float)steps; while (currentT <= 1.0f) { Vector3 p = p0 + ((p1 - p0) * currentT); float density = Mathf.Abs(DensityFunctions.Density_Func(p)); if (density < minValue) { minValue = density; t = currentT; } currentT += increment; } return(p0 + ((p1 - p0) * t)); }
public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] properties) { Material targetMat = materialEditor.target as Material; EditorGUI.BeginChangeCheck(); // option resets every time the spector isn't being renderer if (targetMat.IsKeywordEnabled("FIXED")) { _option = DensityFunctions.Fixed; } else if (targetMat.IsKeywordEnabled("LIN")) { _option = DensityFunctions.Linear; } else if (targetMat.IsKeywordEnabled("POW2")) { _option = DensityFunctions.Pow2; } else if (targetMat.IsKeywordEnabled("POW3")) { _option = DensityFunctions.Pow3; } else if (targetMat.IsKeywordEnabled("SINE")) { _option = DensityFunctions.Sine; } _option = (DensityFunctions)EditorGUILayout.EnumPopup("Density Function", _option); if (EditorGUI.EndChangeCheck()) { targetMat.DisableKeyword("FIXED"); targetMat.DisableKeyword("LIN"); targetMat.DisableKeyword("POW2"); targetMat.DisableKeyword("POW3"); targetMat.DisableKeyword("SINE"); // enable or disable the keyword based on enum switch (_option) { case DensityFunctions.Fixed: targetMat.EnableKeyword("FIXED"); break; case DensityFunctions.Linear: targetMat.EnableKeyword("LIN"); break; case DensityFunctions.Pow2: targetMat.EnableKeyword("POW2"); break; case DensityFunctions.Pow3: targetMat.EnableKeyword("POW3"); break; case DensityFunctions.Sine: targetMat.EnableKeyword("SINE"); break; } } base.OnGUI(materialEditor, properties); }