﻿//////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//      Tree Script  v1.0.0
//      Deco Script for Photoshop
//      Daichi Ito, Technical Artist
//
//      (c) Adobe Systems Inc.  2013
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////

modelParameters = {
    mTreeType : 0,
    lightDirection : 0,
    tilt : 0,
    leavesAmount : 100,
    leavesColor : false,
    stemsColor : false,
    flatLeaves : false,
    noiseLeaves : false,
    flatStems : false,
    randShape : false,
    arrange : 1,
    stemColor : [0.5,0.3,0.1],
    leafColor : [0.2,0.9,0.2],
}

previousMTreeType = -1;
previousLightDirection = -1;
previousTilt = -1;
previousLeavesColor = false;
previousStemsColor = false;
previousStemColorR = 0;
previousStemColorG = 0;
previousStemColorB = 0;
previousleavesColorR = 0;
previousleavesColorG = 0;
previousleavesColorB = 0;

// get the size of the output area
var outputSize = RenderAPI.getParameter(kpsSize)
// get the location of the top left corner of the bounding rectangle around the selected area
var outputOrigin = RenderAPI.getParameter(kpsOrigin)

var outputSizeX = outputSize.x;
var outputSizeY = outputSize.y;


RenderAPI.setParameter (kpsUseOpenGL, 1)

// shapes

var leavesShape = new Array();
var leavesShapeN = new Array();

for(i = 0; i <= 11; i++){
    leavesShape[i] = new Array();
}

leavesShapeN[0] = 3;  // pepper tree
leavesShape[0][00] = [367.168,137.833,366.662,145.339,345.834,147.097,300.5,171.325,343.167,166.336,365.661,160.171,364.75,173.679,
	343.834,174.887,291.833,189.139,273.167,215.503,308.5,204.103,339.834,190.564,364.06,183.924,362.554,206.24,343.834,206.24,
	306.5,221.204,281.167,255.408,321.167,235.456,349.834,224.054,362.288,210.188,360.262,240.228,350.5,239.731,314.5,254.695,
	279.833,291.749,308.5,278.922,337.834,261.108,359.705,248.472,358.231,270.327,351.813,266.897,329.834,277.853,298.5,302.794,
	277.833,324.527,303.832,312.057,335.834,292.817,355.813,278.299,357.733,275.987,357.834,305.901,343.833,303.507,
	315.833,319.183,291.167,334.859,278.5,350.179,304.5,337.709,329.166,328.446,349.167,313.482,357.636,308.158,357.167,313.482,
	355.977,336.892,341.633,330.847,303.832,350.535,281.833,375.119,295.833,369.775,321.166,361.937,344.299,349.375,
	355.834,339.847,354.138,361.243,327.834,371.913,295.833,389.015,277.167,405.048,296.5,401.129,326.5,393.291,349.167,378.327,
	353.729,369.618,353.167,381.176,352.5,398.991,333.834,403.267,305.833,416.093,281.167,438.538,304.5,431.77,335.833,423.219,
	349.593,413.436,353.834,408.254,354.333,431.527,340.5,436.044,308.5,446.732,276.5,473.098,304.5,466.685,331.166,456.709,
	347.833,442.457,354.376,435.465,354.5,446.732,353.848,463.677,333.834,470.96,304.5,488.062,282.5,514.427,309.834,507.301,
	336.5,486.637,347.167,475.235,353.974,466.4,355.931,487.643,356.211,491.362,339.5,502.67,318.5,524.402,302.5,553.619,
	323.167,539.367,344.166,520.483,356.5,495.188,360.8,528.092,344.5,551.481,333.166,582.835,356.5,562.17,363.834,538.654,
	365.834,547.205,369.834,554.331,368.363,529.022,379.166,555.044,406.166,573.215,383.166,540.08,368.064,525.46,365.711,497.471,
	377.834,513.001,403.167,531.529,428.5,547.562,409.833,517.276,383.166,500.888,365.408,493.882,364.5,483.073,364.313,465.16,
	381.167,483.073,403.167,497.325,428.5,507.657,409.5,485.211,381.166,467.397,364.281,462.077,364.034,438.293,375.834,449.583,
	393.833,460.984,430.5,475.592,408.5,451.008,382.5,436.044,363.986,433.707,363.833,418.942,364.086,405.305,373.833,419.655,
	400.5,434.619,426.5,447.801,401.166,418.942,377.833,405.404,364.162,401.155,364.697,372.311,376.5,391.865,403.834,409.68,
	434.5,419.655,409.166,387.589,384.5,374.05,364.783,367.637,365.166,346.973,365.634,341.225,381.166,359.086,399.833,370.488,
	441.166,385.808,423.833,361.224,386.5,341.984,365.846,338.626,368.021,311.87,379.166,329.158,410.5,344.835,448.5,360.868,
	424.5,332.009,383.167,308.494,368.34,307.954,369.563,274.934,383.166,301.368,411.166,315.62,443.834,336.997,424.5,301.368,
	391.167,277.141,369.834,271.44,371.119,250.201,383.167,260.396,408.5,276.072,443.167,293.174,425.834,267.521,396.5,246.857,
	371.695,240.691,373.153,216.592,385.834,226.192,419.834,244.007,448.5,256.833,425.167,224.767,387.834,204.815,373.831,205.391,
	375.07,184.92,385.834,189.851,414.5,201.252,445.167,216.929,421.167,186.289,391.167,174.887,375.642,175.48,376.532,160.774,
	388.5,166.336,413.834,174.887,440.5,184.151,426.5,166.336,401.167,151.372,377.137,150.775,377.834,139.259];
leavesShape[0][01] = [386.5,36.5,386.5,48.5,436.5,91.833,470.419,131.806,484.056,151.005,485.827,146.179,499.037,139.494,
	523.888,137.157,541.914,141.392,525.54,146.181,504.19,151.75,489.234,156.427,487.08,155.264,498.73,171.669,505.074,165.118,
	524.657,159.103,542.398,158.103,554.052,161.668,537.677,166.458,522.017,170.802,507.072,174.7,499.937,173.367,502.781,177.373,
	513.198,192.909,520.029,186.732,546.364,179.604,564.411,182.278,557.944,188.628,542.621,194.309,525.542,198.764,
	514.714,195.172,527.659,214.478,545.976,208.458,567.315,203.669,581.491,204.116,572.856,213.361,555.031,220.603,
	538.646,226.172,531.957,220.888,537.89,229.736,545.363,241.937,556.262,234.527,575.141,228.179,595.658,231.633,583.491,239.988,
	564.603,247.117,553.224,249.568,548.425,246.936,559.716,265.365,568.347,258.703,588.634,253.023,609.858,256.812,
	597.701,264.387,583.418,271.739,568.526,271.738,561.611,268.458,567.033,277.309,573.879,289.11,591.758,284.329,616.989,280.207,
	629.356,283.328,613.634,292.351,595.135,296.917,583.093,295.913,575.14,291.282,587.132,311.958,588.793,314.958,600.246,312.292,
	620.43,314.41,637.708,321.429,624.172,325.996,606.421,327.778,590.502,318.043,604.691,343.684,620.345,346.941,638.571,362.65,
	619.115,359.306,606.56,347.06,606.862,347.608,610.575,361.535,602.854,347.386,603.488,361.311,591.531,380.362,594.054,350.949,
	601.279,344.501,588.909,321.834,588.289,331.342,581.748,343.15,567.655,362.757,568.644,342.036,578.088,325.324,587.324,318.926,
	582.547,310.174,573.922,296.108,570.188,306.273,559.02,319.419,548.221,331.562,550.585,313.849,562.847,298.476,572.437,293.688,
	560.984,275.011,559.829,285.664,549.807,292.682,531.529,307.163,537.08,289.784,546.86,274.411,558.774,271.409,551.668,259.816,
	544.889,249.24,543.527,258.703,533.389,274.299,520.138,284.101,525.396,262.265,534.44,248.898,542.826,246.021,528.49,223.65,
	528.56,237.87,517.75,250.79,500.238,260.929,505.834,240.429,515.593,226.615,526.167,220.026,515.896,204,512.846,199.662,
	510.092,213.803,502.51,223.941,482.075,240.539,485.124,224.72,499.196,206.672,511.467,197.702,497.268,177.512,496.588,189.739,
	484.294,207.452,465.995,223.493,473.755,200.098,487.857,179.712,495.189,174.557,482.723,156.833,480.905,169.35,469.706,184.834,
	455.329,199.206,459.137,179.822,471.839,157.987,480.876,154.194,467.896,134.926,427.167,93.167,385.833,59.833,385.833,95.833,
	381.833,133.167,380.522,156.751,395.833,155.167,431.166,167.167,452.5,181.833,421.166,177.167,393.166,169.833,380.252,160.019,
	378.34,181.995,392.5,182.5,428.5,192.5,458.5,208.5,420.5,204.5,389.166,195.833,378.021,185.658,375.846,210.691,399.833,211.833,
	433.833,219.833,451.166,231.833,409.833,228.5,391.166,223.833,375.634,213.122,375.166,218.5,374.783,237.833,394.5,237.833,
	419.166,244.5,443.833,259.167,411.833,259.833,386.5,254.5,374.697,242.206,374.162,269.192,387.833,267.167,411.166,273.833,
	436.5,289.833,410.5,288.5,383.833,280.5,374.086,273.074,373.833,285.833,373.986,299.646,392.5,295.833,418.5,303.833,
	440.5,315.833,403.833,313.167,384.5,312.5,374.034,303.938,374.281,326.19,391.166,325.167,420.5,331.833,438.5,345.833,
	413.833,341.167,387.833,335.833,374.313,329.074,374.5,345.833,375.408,355.945,393.166,356.5,419.833,365.833,438.5,383.167,
	403.166,373.833,384.5,367.167,375.711,359.303,378.064,385.489,393.166,387.167,421.166,411.168,389.166,401.167,378.363,388.822,
	379.833,405.167,373.166,391.167,373.115,390.532,366.5,407.834,343.166,421.168,354.5,397.834,372.799,386.618,370.402,356.892,
	357.166,373.833,331.833,381.833,309.833,385.167,327.166,369.833,352.5,357.167,370.113,353.313,369.833,349.833,368.64,325.568,
	361.833,333.833,346.5,341.167,317.833,347.167,289.166,347.833,303.166,338.5,340.5,328.5,368.514,323.019,367.833,309.167,
	367.709,298.624,361.166,305.167,341.166,313.167,315.832,314.5,293.166,314.5,318.5,299.833,350.5,293.833,367.666,294.941,
	367.407,272.987,363.166,277.834,345.833,281.834,314.5,285.834,291.167,285.167,315.833,271.167,346.5,266.5,367.337,267.035,
	367.166,252.5,367.729,241.686,363.166,249.834,336.5,253.834,306.5,257.167,287.167,253.834,305.833,245.834,338.5,238.5,
	368.137,233.851,369.367,210.254,357.832,219.167,331.166,224.5,305.833,227.833,291.833,225.833,313.832,213.833,355.166,205.833,
	369.51,207.489,370.5,188.5,370.969,183.519,362.5,188.5,339.166,193.167,314.5,197.833,288.5,202.5,301.167,193.167,325.833,184.5,
	357.166,179.167,371.167,181.407,373.088,161.005,371.166,163.167,347.166,167.167,313.832,173.833,287.833,178.5,308.5,165.167,
	343.832,153.833,367.166,152.5,373.586,155.71,375.833,131.833,376.5,99.833,377.167,57.833,340.5,75.167,315.833,95.167,
	295.165,116.523,303.875,120.994,314.664,143.9,314.5,156.5,303.645,148.188,293.8,131.785,293.085,119.049,279.047,136,
	285.931,141.72,293.833,158.5,293.167,171.833,287.054,169.915,276.311,151.189,276.708,138.826,260.717,158.136,272.208,168.055,
	284.702,187.246,286.36,203.397,267.371,185.208,260.676,174.451,259.165,160.01,255.729,164.16,244.039,179.56,254.042,186.958,
	262.592,201.574,266.386,222.622,249.753,211.15,240.072,197.356,241.395,183.042,225.077,204.538,233.217,208.041,241.092,222.155,
	244.43,244.526,232.032,233.697,223.259,217.259,222.731,207.631,215.014,217.794,206.879,228.957,218.54,232.839,226.973,249.027,
	230.999,266.931,213.982,251.028,204.569,243.237,204.351,232.427,191.245,250.41,200.419,255.923,211.338,272.29,212.145,290.3,
	202.406,277.292,192.386,263.248,189.546,252.741,179.674,266.285,174.123,274.756,182.802,281.865,190.782,299.375,
	189.945,320.316,177.569,299.554,172.063,287.189,172.279,277.569,157.903,299.506,164.567,306.521,164.5,336.325,154.212,316.276,
	156.072,302.298,147.099,315.99,152.042,302.232,152.393,301.703,138.749,313.131,118.981,315.097,138.606,300.589,154.56,298.438,
	171.02,273.638,154.231,282.293,136.621,279.219,123.477,273.644,141.388,267.819,161.774,267.142,173.001,270.653,174.929,267.749,
	188.752,247.793,180.384,251.883,168.244,252.026,150.131,246.093,135.191,235.873,147.844,233.622,172.732,239.591,
	190.204,245.696,198.095,234.304,204.302,225.78,197.091,228.586,182.187,227.513,168.542,219.08,157.042,210.574,178.617,208.288,
	198.419,215.472,206.471,222.804,219.393,205.056,214.359,207.361,203.187,204.072,184.912,195.531,173.47,186.239,194.309,184.239,
	212.644,191.995,222.897,200.243,231.452,188.494,238.169,180.01,231.008,184.848,215.101,178.059,197.899,169.481,190.072,159.547,
	204.299,160.119,225.234,166.48,243.035,173.864,257.69,155.354,246.539,158.191,229.838,152.473,215.002,145.647,209.089,138.786,
	227.387,137.393,253.114,146.47,259.407,153.184,271.201,138.289,274.401,134.459,267.142,135.287,252.529,130.283,237.239,124.779,
	221.273,118.775,233.249,116.024,250.917,118.313,269.985,125.781,275.757,132.835,288.862,117.153,286.603,118.17,272.048,112.38,
	251.172,105.232,235.205,99.229,253.62,96.264,278.285,100.41,290.916,108.094,292.264,113.083,307.604,94.729,335.167,71.167,
	375.833,51.167,377.167,39.167];
leavesShape[0][02] = [318.5,143.167,312.231,188.792,305.812,185.583,287.835,194.5,270.499,215.834,253.167,235.834,272.499,223.833,
	289.835,209.833,309.812,196.25,311.733,194.087,311.835,222.074,297.833,219.834,279.167,232.5,266.501,245.501,253.833,259.834,
	273.167,247.833,287.167,241.834,303.167,229.167,311.636,224.186,311.167,229.167,309.978,251.069,295.634,245.413,
	272.499,259.833,251.835,285.167,267.833,278.5,279.167,273.167,298.3,262.747,309.835,253.833,308.138,273.851,291.169,281.834,
	272.501,293.833,252.501,313.167,267.835,305.833,284.501,302.501,303.167,289.834,307.729,281.686,307.167,292.5,306.501,309.167,
	287.835,313.167,277.167,319.833,261.167,343.833,275.835,334.5,289.833,331.834,303.593,322.681,307.835,317.833,308.333,339.607,
	294.501,343.833,278.501,349.833,260.501,373.833,279.167,367.833,289.166,361.167,301.833,349.833,308.376,343.291,
	308.501,353.833,307.849,369.686,291.834,374.5,277.167,382.5,267.167,411.833,282.501,400.5,290.501,391.167,301.167,380.5,
	307.974,372.235,309.931,392.108,310.212,395.588,293.501,406.167,285.166,420.5,284.501,455.167,293.167,436.5,298.167,422.833,
	310.501,399.167,314.8,429.951,298.501,459.167,305.834,487.833,313.834,462.501,317.835,439.833,319.835,447.833,323.835,454.501,
	322.364,430.821,333.167,455.167,369.5,480.835,337.167,441.167,322.065,427.489,319.712,401.304,331.835,415.833,366.5,441.833,
	391.834,456.833,373.166,428.499,337.167,404.5,319.409,397.945,318.501,387.833,318.313,371.074,335.167,387.833,366.5,409.833,
	391.834,419.499,372.834,398.5,335.167,373.167,318.282,368.19,318.034,345.938,329.835,356.5,357.166,375.833,393.834,389.5,
	371.834,366.5,336.501,343.833,317.987,341.646,317.833,327.833,318.087,315.074,327.833,328.5,363.834,351.167,389.834,363.5,
	364.5,336.5,331.833,315.167,318.163,311.192,318.698,284.206,330.501,302.5,367.168,327.833,397.834,337.167,372.5,307.167,
	338.501,285.833,318.784,279.833,319.167,260.5,319.634,255.122,335.167,271.833,363.166,291.167,373.5,296.167,368.167,283.5,
	340.501,255.833,319.847,252.691,322.022,227.658,333.167,243.833,350.167,254.5,355.5,255.167,344.833,239.833,333.833,228.167,
	322.341,223.995,323.563,193.102,335.5,205.833,333.167,196.167,323.835,189.833,329.833,164.167,331.833,156.833,335.5,162.833,
	352.655,191.488,346.07,191.505,337.5,207.833,340.5,231.167,345.475,219.654,354.418,199.051,354.859,196.323,368.645,220.679,
	358.615,226.2,357.833,263.167,362.5,250.5,367.833,234.5,369.523,222.607,371.598,227.156,381.395,246.773,369.5,253.167,
	367.167,280.833,371.833,279.167,377.997,262.075,382.638,249.245,391.115,267.443,381.777,281.845,378.833,310.167,
	386.694,302.787,395.062,283.558,394.635,274.453,399.491,284.128,407.134,298.951,396.167,327.833,405.223,326.023,
	411.482,312.009,412.421,305.922,423.473,324.688,414.742,334.438,414.524,362.088,419.066,351.891,423.405,336.454,425.31,327.88,
	430.57,337.018,437.824,351.125,429.634,365.453,433.128,389.107,434.801,377.461,437.905,363.484,439.171,353.292,450.43,369.76,
	452.355,372.672,444.489,389.223,447.606,400.854,459.084,415.817,456.295,401.707,454.332,375.664,472.767,400.619,
	477.212,417.485,487.952,429.421,479.975,407.904,485.455,414.002,491.844,418.062,479.098,398.062,499.456,414.549,
	540.392,420.994,495.721,400.59,477.232,395.287,462.57,373.488,479.151,380.84,518.948,388.305,546.073,390.274,517.621,373.757,
	477.764,368.622,460.691,370.693,455.028,362.274,446.675,347.746,468.042,354.967,503.275,360.406,527.789,357.723,
	502.672,347.749,460.858,342.179,445.238,345.245,434.146,325.953,448.531,329.987,479.339,334.857,514.659,330.692,
	486.219,320.286,447.532,316.019,432.009,322.231,425.122,310.256,419.072,299.021,433.257,306.452,472.465,310.426,
	498.802,309.777,465.8,297.347,429.85,293.073,417.23,295.602,404.432,271.84,422.606,282.614,463.64,288.621,492.152,283.31,
	457.681,268.264,420.689,264.574,402.357,267.989,393.188,250.965,390.919,246.072,411.229,253.83,442.557,258.407,475.377,258.323,
	451.653,244.123,407.559,237.541,389.895,243.859,379.333,221.08,395.955,230.294,433.735,236.601,465.9,235.355,438.787,219.999,
	389.609,211.685,377.788,217.746,363.613,190.275,386.855,199.468,420.532,205.785,447.228,204.553,419.196,185.125,
	384.675,178.519,362.224,187.307,351.697,166.17,339.5,141.833];


leavesShapeN[1] = 3;  // maple tree
leavesShape[1][00] = [377.75,268.25,382.25,263.938,384.938,259.688,380.5,259.438,377.125,258.188,377.75,257.313,374.375,255.813,
	371.25,252.813,369.688,248.563,372.125,250.063,374.5,251.063,372.313,247.188,370.813,242.75,372.25,243.375,372.313,239.75,
	373.063,234.563,377.313,237.625,377.813,235.938,383.188,240.813,384.813,235.438,386.813,230.875,388.75,232.938,389.767,234.617,
	391.03,231.184,393.085,228.818,393.66,230.863,395.544,228.357,397.669,226.295,397.794,229.107,397.364,232.06,399.75,231.688,
	398.37,237.399,402.375,237.063,400.438,241.75,398.125,245.188,402.063,244.25,405.875,243.875,404.625,245.25,410,245.125,
	408.938,246.375,413.938,246.375,409.313,250.125,410.688,250.938,407.688,253.313,409.25,254.375,404.375,257.125,399.5,258.625,
	403.375,260.375,406.375,263,400.375,264.375,396.625,264.125,397.75,264.875,392.625,264.5,388.875,262.375,386,260.375,
	383,264.75,378.688,268.813];
leavesShape[1][01] = [371.75,269.25,368.5,264.25,365,261.5,368,260.75,363.5,259,365.75,257.75,362.5,256.25,360,253.75,359,254.5,
	352.25,251.5,353,253,345.75,251,338.5,249.75,331.5,251.25,328.5,253,333.25,254.5,331.25,256,336.25,257,340.25,257.5,343,259,
	337,260.25,333.5,262,338,262.75,330.75,266,335.25,267.25,339.5,267.5,338.25,270.75,343.75,268.75,344.5,271,350.25,268.25,
	353.5,265.25,351.5,271.25,355.75,269,359.75,266.25,362.75,262.75,367.25,265.25,370.25,270];
leavesShape[1][02] = [401.641,258.26,402.899,258.372,407.209,258.415,411.878,256.572,410.53,256.475,413.935,255.108,418.524,251.587,
	414.63,250.857,410.392,251.149,413.936,248.001,416.876,243.913,414.994,243.763,416.475,240.733,414.878,240.693,417.116,235.957,
	412.738,238.03,413.064,236.651,408.418,238.973,408.849,237.42,405.691,239.283,402.697,241.621,403.063,238.078,402.494,233.752,
	399.149,235.666,397.6,230.801,395.69,232.07,394.64,229.671,393.172,227.61,392.308,230.041,391.868,232.706,390.377,231.407,
	389.721,234.037,390.272,237.141,388.571,236.3,387.109,235.895,384.785,233.799,380.315,231.223,382.225,228.535,384.226,225.046,
	384.438,220.366,383.724,221.417,383.854,217.995,382.288,212.58,379.761,215.404,378.135,218.993,376.48,214.606,373.576,210.265,
	372.564,211.726,370.095,209.079,369.325,210.359,365.431,206.279,365.591,210.831,364.307,209.909,364.603,214.797,
	363.191,213.707,363.688,217.164,364.751,220.717,361.241,218.732,356.494,217.133,356.958,220.768,351.208,219.711,351.655,221.87,
	348.689,221.583,345.883,221.796,348.014,223.658,350.577,225.286,348.552,225.881,350.981,227.668,354.454,228.698,
	352.808,229.683,350.809,231.52,355.433,233.179,360.856,234.47,356.156,239.536,357.857,239.931,354.936,243.908,360.142,244.409,
	363.768,244.337,363.188,245.669,367.575,244.147,371.377,242.02,370.454,244.218,369.033,246.488,373.23,244.917,376.129,241.967,
	377.522,238.841,378.414,239.379,379.555,236.263,379.665,232.214,383.997,234.512,385.951,236.243,386.331,239.812,
	387.504,244.525,380.443,243.09,380.82,244.565,375.62,244.025,377.469,248.235,378.301,249.584,379.365,257.929,376.481,253.472,
	374.237,249.653,370.242,247.132,370.52,250.131,369.214,249.579,369.636,252.794,368.195,252.137,368.779,254.399,369.933,256.697,
	366.377,255.542,361.599,254.697,362.154,257.077,356.385,256.636,356.889,258.041,353.92,257.983,351.123,258.25,353.297,259.383,
	355.897,260.344,353.892,260.828,356.364,261.9,359.856,262.425,358.239,263.149,356.289,264.451,360.949,265.341,366.396,265.953,
	361.833,269.505,363.544,269.692,360.726,272.448,365.938,272.546,369.555,272.339,369.011,273.242,373.354,272.042,
	377.095,270.468,376.229,271.961,375.95,272.281,373.284,273.104,374.617,273.3,371.089,274.411,366.164,277.581,369.972,278.594,
	374.224,278.617,370.377,281.492,367.039,285.347,368.899,285.637,367.121,288.545,368.708,288.703,374.384,284.801,
	377.661,282.572,376.088,286.374,375.433,290.044,375.957,295.288,377.719,290.785,378.841,294.894,381.855,302.89,382.773,299.482,
	382.511,292.01,383.56,287.947,385.264,284.276,387.492,281.524,386.574,285.063,387.622,287.422,388.278,293.714,392.188,299.761,
	393.105,297.188,392.473,292.404,394.478,295.018,397.086,295.961,395.881,289.258,396.274,282.834,398.765,292.01,404.795,294.501,
	404.532,289.389,403.746,284.932,403.615,280.868,402.697,276.149,403.222,270.644,406.873,278.17,408.096,275.241,409.256,276.145,
	409.261,272.124,408.433,268.372,410.361,269.838,412.188,271.73,412.085,268.263,415.711,269.624,413.708,271.168,411.218,274.708,
	412.862,278.976,413.429,278.037,413.611,280.86,415.538,285.135,417.623,282.546,418.807,279.426,420.725,282.848,423.79,286.099,
	424.597,284.792,427.111,286.699,427.71,285.565,431.67,288.495,431.118,284.777,432.386,285.396,431.681,281.415,433.082,282.158,
	432.316,279.375,431.02,276.573,434.438,277.825,438.964,278.627,438.214,275.693,443.62,275.943,443.017,274.219,445.781,274.136,
	448.355,273.66,446.223,272.361,443.711,271.3,445.528,270.594,443.125,269.389,439.826,268.917,441.26,267.932,442.945,266.21,
	438.525,265.344,433.4,264.868,437.296,260.206,435.688,260.064,438.037,256.487,433.182,256.635,429.839,257.083,430.257,255.927,
	426.338,257.649,423.013,259.803,423.672,257.9,424.783,255.884,421.046,257.624,418.628,260.357,417.618,263.071,415.806,261.468,
	415.02,265.007,416.227,268.74,412.019,267.32,411.598,267.06,410.076,264.545,407.361,262.395,408.234,261.856,405.439,259.91,
	401.317,258.484];

leavesShapeN[2] = 2;  // ginko tree
leavesShape[2][00] = [206.373,228.281,204.473,230.655,202.099,233.386,200.555,236.829,199.487,240.153,199.724,243.478,
	200.199,247.158,201.149,251.194,202.573,255.35,203.998,259.743,204.71,264.254,204.117,267.341,201.98,269.478,199.724,269.834,
	195.806,269.478,191.888,268.766,185.478,267.341,182.391,265.441,182.034,262.117,183.459,259.03,181.441,260.811,179.185,262.117,
	176.217,261.879,172.655,258.199,169.212,251.551,168.5,244.665,168.975,241.578,170.162,239.678,172.655,238.966,176.455,238.966,
	181.56,239.56,187.496,239.797,192.482,239.441,195.45,238.373,196.875,236.354,199.012,233.267,201.861,229.824,204.829,226.5];
leavesShape[2][01] = [265.377,219.167,256.953,222.976,251.645,225.976,245.297,229.785,239.873,234.285,236.18,240.633,
	235.833,245.018,237.68,251.25,244.95,255.174,247.49,253.904,248.759,250.096,248.297,244.672,248.875,239.94,250.606,234.055,
	252.567,228.977,258.107,225.399,265.955,222.052];

leavesShapeN[3] = 1;  // pine tree
leavesShape[3][00] = [346.667,240.833,324.667,263.833,330.864,260.187,320.294,270.188,310.432,281.267,297.355,292.839,
	295.014,295.249,304.32,278.107,310.627,262.795,315.292,251.829,319.64,245.113,318.014,243.795,308.689,249.934,296.403,257.725,
	284.601,266.711,269.551,275.566,264.136,279.327,264.382,281.853,284.291,269.523,294.878,263.333,278.496,277.578,265.532,288.25,
	267.219,290.874,275.67,283.98,298.961,264.962,274.354,289,267.328,296.364,269.013,298.989,278.454,290.257,299.277,269.863,
	277.9,294.401,269.885,303.603,271.779,305.638,281.114,295.271,299.679,272.91,284.328,296.146,276.833,305.801,279.491,306.96,
	289.95,291.795,304.28,271.137,291.708,296.794,284.492,308.235,287.133,309.765,293.113,298.75,293.485,300,310.664,284.086,
	319.872,275.983,306.52,293.102,290.84,309.225,292.997,311.478,304.973,299.926,322.385,278.974,320.399,282.289,304.642,305.104,
	293.488,319.512,295.645,321.766,308.906,305.554,325.438,281.55,309.156,309.728,306.384,314.96,308.633,316.595,312.477,309.967,
	326.417,284.464,315.799,310.209,301.958,334.621,304.789,335.25,320.483,304.863,325.096,294.361,325.386,294.579,329.521,284.284,
	330.318,282.469,330.554,281.988,324.659,303.887,316.476,317.725,306.397,338.409,307.834,340.5,320.995,315.032,312.275,336.709,
	315.16,337.706,327.056,304.293,330.034,299.289,330.566,299.407,331.367,297.05,332.726,294.767,325.278,315.16,314.255,345.186,
	317,346.667,325.901,322.129,335.64,297.829,335.997,297.841,327.178,327.159,324.59,337.004,327.335,338.486,331.374,326.277,
	339.733,298.354,332.894,330.171,330.329,342.102,332.972,342.968,336.128,329.379,338.79,315.386,339.968,316.499,340.44,314.966,
	339.363,328.589,337.474,340.665,340.361,340.394,342.18,322.062,344.114,303.077,344.9,300.533,345.694,316.63,345.038,320.567,
	345.901,320.818,346.137,325.587,345.354,339.09,348.406,339.152,348.263,320.096,349.876,310.688,353.018,292.324,353.793,291.953,
	354.373,285.398,354.39,284.303,354.946,281.05,352.182,315.337,351.721,325.506,354.716,326.378,356.099,313.593,358.403,284.537,
	358.403,317.081,358.403,329.284,361.169,329.575,361.397,315.627,360.708,286.571,364.396,314.175,365.087,326.378,
	367.853,325.506,365.776,307.201,363.013,282.212,370.386,309.816,378.628,340.349,381.624,339.768,369.493,295.837,387.239,338.49,
	389.983,337.399,376.218,307.599,367.157,280.567,378.528,302.979,393.815,332.422,396.532,331.698,383.444,308.101,
	371.399,283.011,386.665,307.742,400.282,328.652,402.364,326.952,389.884,307.384,372.159,280.091,389.853,303.247,
	397.937,313.241,399.888,310.933,393.978,302.647,373.062,275.348,393.312,297.536,400.661,305.581,402.614,303.273,
	391.353,290.839,377.735,274.99,377.972,274.995,386.72,282.068,404.507,297.194,405.052,294.761,400.263,290.249,386.761,279.27,
	382.697,275.102,387.713,275.215,394.296,274.867,395.417,272.592,380.25,272.592,377.181,269.444,381.167,268.796,397.808,266.546,
	397.755,263.428,386.939,264.84,373.974,266.54,372.556,265.309,390.701,261.264,400.551,258.703,400.501,255.583,387.878,258.038,
	369.046,262.261,368.353,261.66,390.53,254.805,402.188,251.202,401.65,248.475,388.257,252.375,366.182,259.775,365.66,259.322,
	385.983,249.938,397.438,245.676,395.788,243.29,378.913,250.678,365.964,256.305,364.697,255.145,380.051,245.502,392.208,239.574,
	390.769,236.883,362.43,253.071,350.333,242];

leavesShapeN[4] = 2;  // spruce
leavesShape[4][00] = [394.5,183.5,346.5,213.5,315.611,222.066,274.597,208.884,309.223,225.864,260.726,235.049,299.749,245.933,
	262.611,243.75,283.872,256.716,251.777,272.038,261.129,270.368,265.856,274.096,243.796,278.543,254.661,279.649,239.701,286.894,
	251.952,287.244,250.021,290.637,216.562,298.256,239.35,299.146,259.336,300.147,216.99,313.298,238.771,312.963,194.575,326.127,
	234.992,320.242,238.56,323.159,208.431,333.62,231.095,333.921,193.589,341.907,211.438,344.765,192.47,360.852,223.848,351.691,
	220.033,355.927,188.99,372.647,214.901,367.002,181.976,384.566,206.586,380.169,186.793,388.785,196.181,390.156,170.588,415.469,
	187.321,405.958,174.999,427.969,202.634,414.906,170.412,436.406,197.131,429.602,172.013,453.898,198.801,438.954,178.17,473.672,
	200.068,448.886,197.766,470.123,199.535,491.797,210.178,462.826,216.628,473.355,217.717,453.861,229.777,458.816,223.887,425.84,
	237.072,442.785,226.522,412.162,250.49,440.926,232.112,397.959,255.75,422.148,243.637,397.945,256.459,406.307,252.186,385.231,
	259.816,390.978,264.967,388.534,257.441,363.466,267.059,377.494,270.785,372.765,261.537,355.116,278.449,367.333,
	276.937,350.334,285.955,355.327,274.632,328.906,300.504,354.92,279.676,317.339,296.432,337.203,299.575,303.625,307.715,317.917,
	311.881,315.649,305.939,285.221,316.928,304.083,313.408,284.394,307.625,274.761,321.707,291.038,330.303,287.979,
	325.756,250.585,343.75,277.958,339.512,259.921,335.086,246.154,350.078,256.511,350.832,235.543,391.5,210.5,380.221,247.997,
	343.261,265.466,379.442,258.536,352.809,285.548,367.772,287.066,348.457,311.153,375.444,299.932,348.765,319.292,
	375.274,319.734,335.419,349.694,357.268,343.837,380.085,332.903,368.52,345.065,349.181,359.619,365.119,355.641,333.326,386.141,
	383.689,354.185,348.948,386.492,371.025,377.331,342.715,404.864,381.39,382.587,356.353,406.425,384.896,390.279,354.276,427.963,
	376.742,413.837,355.295,441.776,390.353,416.896,365.089,447.599,383.759,434.823,363.937,475.575,390.096,446.604,
	378.938,468.636,394.903,460.475,379.446,496.294,398.938,475.752,396.768,496.992,406.078,482.59,404.55,523.122,413.21,490.14,
	424.029,509.063,418.952,482.804,442.655,501.24,418.284,472.185,431.184,469.571,409.35,454.351,440.775,466.342,414.353,444.887,
	432.433,441.915,410.692,429.969,444.868,440.811,420.871,423.537,404.952,413.363,437.26,423.341,403.355,402.261,430.949,408.733,
	412.195,387.198,410.758,377.979,431.165,383.272,395.39,358.977,423.803,358.392,395.132,345.177,442.148,359.402,403.916,336.482,
	430.748,343.654,403.292,320.911,418.834,319.404,398.844,309.52,431.353,317.137,398.947,297.497,420.123,301.384,411.436,284.02,
	437.411,301.039,409.506,256.987,438.352,274.48,402.893,237.739,410.5,190.5];
leavesShape[4][01] = [363,197.5,342,239,304.5,235,297.5,216,288.5,195,281.5,198,288,207,297.5,232.5,279,216,266.5,199,259.5,201,
	269.5,217,276,238,262.5,223,254,208,250.5,210,256.5,226.5,257.569,232.646,250.661,228.188,237.749,222.337,228.207,214.26,
	225.301,217.694,232.551,223.832,232.694,229.851,215.2,215.694,221.277,224.768,211.032,216.75,206.156,217.864,213.699,227.525,
	209.854,228.2,182.649,207.285,196.495,225.406,208.468,241.441,171.335,217.207,178.379,225.137,185.482,233.771,174.797,224.725,
	147.159,208.121,165.636,225.486,177.466,235.502,177.494,240.111,165.665,230.095,153.247,221.548,150.223,223.572,155.3,229.182,
	164.444,241.223,149.604,228.945,134.375,217.425,143.555,232.996,134.1,230.231,119.069,228.643,128.227,236.396,146.134,246.972,
	140.438,246.735,122.854,235.777,107.763,233.484,119.152,242.471,128.635,249.844,108.115,237.712,94.109,235.681,105.323,242.555,
	113.189,251.835,101.916,244.257,93.931,242.082,98.862,250.188,91.165,251.453,65.971,243.545,63.043,246.615,81.039,253.44,
	60.368,252.318,56.227,257.984,83.911,270.942,46.802,259.831,69.082,276.074,34.349,272.218,62.943,283.324,23.044,289.57,
	43.621,289.897,56.1,290.633,38.273,302.402,22.705,317.586,63.938,295.304,51.808,307.31,47.811,318.993,63.522,307.401,
	72.216,301.003,67.395,319.851,78.262,305.468,89.042,294.285,79.553,312.456,84.396,315.247,93.824,304.884,101.258,287.594,
	94.385,324.397,105.911,304.12,113.067,289.448,115.763,282.842,112.203,316.475,123.123,291.711,124.858,306.92,138.368,290.189,
	141.042,292.451,138.807,299.731,143.974,302.14,152.229,286.2,158.485,280.358,153.812,296.712,159.831,296.566,167.529,278.188,
	168.903,299.006,181.032,287.002,182.937,297.132,196.069,271.562,192.528,308.08,208.196,268.072,203.579,293.645,231.449,274.655,
	225.63,290.039,230.034,291.802,249.684,267.821,242.161,288.313,255.084,273.048,258.816,262.449,255.258,283.677,263.095,288.348,
	289,261,279.389,292.316,290.581,277.55,298.363,265.361,299.945,283.514,316.577,270.724,326.215,276.942,300.171,345.251,
	279.495,360.391,260.516,373.393,287.477,367.121,246.411,399.336,268.639,394.963,254.359,409.671,269.635,407.453,
	247.113,428.742,267.025,419.91,231.851,444.69,284.343,421.732,246.111,447.316,264.879,442.219,231.782,467.433,270.238,447.676,
	243.159,482.099,253.204,475.534,278.333,457.318,241.804,493.735,260.236,479.896,276.415,469.921,234.413,513.499,
	278.085,484.358,248.867,510.025,268.505,500.774,256.19,513.005,239.366,532.96,238.479,538.915,265.059,513.779,253.35,550.7,
	264.231,543.588,252.43,560.261,281.118,531.954,260.749,575.73,279.553,551.494,267.715,587.304,284.605,560.139,296.816,589.789,
	291.318,553.959,309.421,576.175,292.801,543.433,317.311,562.655,288.121,524.391,316.27,540.041,294.577,515.99,319.248,532.719,
	294.162,500.731,320.991,508.881,299.154,485.521,327.045,498.951,330.41,494.96,292.896,472.29,320.82,482.114,325.553,480.216,
	295.522,458.03,318.313,468.224,296.62,444.172,327.682,459.71,329.517,456.122,294.128,428.479,330.982,447.398,332.964,443.117,
	296.609,414.91,332.9,434.796,329.797,425.476,306.381,407.927,329.572,419.648,332.1,416.205,308.879,392.557,329.381,410.217,
	332.617,405.115,307.046,380.613,325.618,391.215,322.098,372.568,316.729,347.585,316.241,347.336,344.5,273,382.5,300,
	367.524,338.038,389.149,308.214,388.233,346.137,400.179,336.998,402.594,367.778,414.579,341.122,408.391,373.499,428.015,355.67,
	419.48,404.795,431.394,385.566,440.537,361.974,440.436,378.757,436.306,402.605,445.198,388.793,442.91,432.791,457.735,375.016,
	454.536,422.351,464.354,400.558,462.573,440.008,475.507,397.291,473.581,431.809,483.329,400.496,486.815,448.928,
	493.517,423.249,497.015,458.297,505.53,416.159,508.141,455.834,513,433.74,526.455,477.014,525.685,437.986,532.639,461.684,
	538.686,444.805,551.946,481.494,552.087,453.176,565.049,470.143,561.973,453.271,588.611,483.859,572.339,453.891,
	593.182,460.273,571.502,444.611,601.4,441.82,563.744,437.33,571.356,426.593,545.021,430.451,576.136,417.672,542.188,420.127,
	553.33,405.582,529.306,411.761,561.637,396.262,532.32,400.104,513.752,403.589,544.13,388.739,504.986,396.59,529.528,382.414,
	501.115,379.559,493.756,373.825,512.253,363.709,469.544,370.498,489.852,350.617,459.907,360.617,503.914,338.792,
	460.356,348.266,484.822,335.12,449.239,337.344,459.535,325.604,438.198,332.088,467.106,315.381,430.041,323.254,448.137,311.589,
	429.915,304.881,460.5,299.5,410,286.5,443,279.5,392,277,353.5,253.5,376,205];


leavesShapeN[5] = 4;  // aspen
leavesShape[5][00] = [263.75,247.5,260.5,241,257.75,232.25,257,224.25,262.75,226,261.25,217.5,261.5,207.25,266.75,207.25,
	269.5,200.75,272.25,194.75,277.25,194.25,284.75,185,289.25,187.5,297.25,182.5,300,186.25,307.5,183.75,311.25,187,317,187.25,
	326,187,327.5,197,328,203.5,331.75,205,330.5,214.25,333.5,217.25,329.25,223.5,332.75,227.5,328.5,235.25,329.75,239.25,
	322.75,246,321.75,251.5,314,254.75,313.25,259.25,303,258.75,300.5,263,291.5,260.75,286,259,284.5,261.5,274.75,256.75,269,253.5];
leavesShape[5][01] = [356.5,91.75,344.25,91.5,344.75,94.25,333.5,94.75,329.75,97.25,324.5,99.5,318.75,93.25,316,94.75,304.25,91.25,
	295.5,93.5,282.75,94.25,282.75,95.75,269.75,100.25,266,103.25,257.25,110.5,262,118.75,262.75,121.25,269.25,123.25,269.5,127.5,
	278.75,135.75,280.5,139.25,289.75,144.25,291.5,147,306.25,148.75,306.75,154,309.5,156.75,312,165,320,161.75,321.75,163.75,
	336.25,157.5,339.75,159,351.5,149.5,353.75,151,363,139.75,365.75,135.75,367.25,122,368.75,121.25,365.25,107.25,365.75,104.75,
	359.5,94.25];
leavesShape[5][02] = [429.25,234,420.25,227.5,418,228.5,402.75,223.75,400.5,225.5,381.5,225,380,228.25,366.75,237,364.5,242.25,
	358.25,252.25,360.75,252.5,358.25,267,371.5,259.5,399.25,247.75,418.25,241.5,432.5,238];
    leavesShape[5][03] = [301,313.604,307.906,299.729,310.976,300.243,324.276,292.535,325.299,294.59,346.272,291.25,349.599,295.361,
	364.178,296.132,365.201,299.986,378.246,302.556,379.013,306.41,389.5,312.576,383.105,317.972,385.919,322.597,373.13,327.993,
	377.479,330.819,356.76,334.417,356.249,337.5,333.483,334.16,327.601,336.215,312.254,327.479,309.952,329.535,301.768,318.743];

leavesShapeN[6] = 3;  // redwood
leavesShape[6][00] = [413.07,151.778,406.391,147.325,396.373,142.426,391.594,141.18,391.271,135.442,388.135,130.181,384.795,126.842,
	384.572,131.517,384.994,136.16,387.467,139.755,388.15,140.282,384.098,139.224,382.791,134.189,379.006,127.509,373.439,121.276,
	372.258,126.474,374.41,131.675,377.461,135.801,380.928,138.406,375.018,137.52,374.775,136.193,370.768,129.068,365.98,118.94,
	362.977,118.604,362.213,124.142,366.16,131.317,370.322,135.302,371.916,137.055,366.516,136.245,366.094,134.411,362.309,127.509,
	355.576,116.07,352.066,116.378,352.9,124.881,355.756,129.343,359.164,133.29,363.199,135.747,356.619,135.172,354.293,130.849,
	350.063,123.279,344.814,115.532,341.379,115.264,341.824,121.053,344.994,126.474,349.479,131.675,353.682,134.915,
	347.482,134.372,345.164,130.627,341.156,123.502,332.438,114.635,329.135,114.819,331.807,120.385,333.873,125.397,
	339.254,130.779,344.08,134.173,338.496,134.105,334.479,129.736,330.285,123.783,324.904,119.272,317.111,115.487,315.038,117.326,
	322.213,127.371,328.672,131.675,333.625,134.045,327.131,133.966,323.289,130.061,317.111,124.17,311.768,119.94,305.089,117.268,
	303.2,119.837,307.146,126.653,312.527,131.137,316.115,133.11,321.898,134.562,314.137,135.446,310.877,133.076,306.07,127.729,
	299.078,123.057,292.398,121.276,290.644,123.066,297.46,131.317,303.753,134.857,308.275,136.17,301.332,137.266,298.855,134.634,
	292.621,130.404,288.168,127.064,279.708,125.506,278.087,127.371,287.057,134.187,290.823,136.339,297.606,137.855,
	292.621,138.642,290.066,139.266,287.774,136.519,282.931,132.931,277.926,132.185,269.689,131.072,268.939,133.111,
	277.704,138.642,283.938,140.2,285.113,140.477,276.92,142.48,275.218,140.106,269.658,137.057,262.564,135.97,261.585,138.313,
	268.402,142.438,273.032,143.43,272.583,143.54,268.504,145.076,265.904,142.649,261.005,140.868,255.439,139.755,254.411,141.721,
	260.51,145.846,265.611,146.166,260.063,148.255,257.666,146.657,254.77,144.949,250.096,144.653,248.67,146.384,255.439,149.997,
	252.079,151.227,247.647,149.551,241.413,149.774,243.639,152,246.756,152.891,247.6,153.059,235.397,158.582,239.73,158.321,
	244.982,156.22,244.53,156.899,243.862,160.461,247.87,157.567,251.182,153.917,255.374,152.064,253.436,154.449,250.987,160.015,
	254.411,159.299,260.51,150.51,263.954,149.091,262.341,151.778,260.115,155.785,259.225,160.591,262.841,159.837,268.402,151.406,
	268.64,147.501,275.064,145.321,270.579,150.442,266.794,156.899,264.791,162.242,267.325,162.348,275.218,154.097,279.344,148.178,
	281.966,143.753,287.652,142.463,285.719,144.653,280.821,149.774,275.478,156.231,273.251,163.578,275.935,163.604,
	284.366,154.456,289.747,147.46,292.976,141.182,295.846,140.823,300.183,140.183,298.187,141.759,291.541,147.281,287.236,155.173,
	286.832,161.574,289.926,161.631,297.819,152.841,303.2,144.231,303.91,139.587,309.73,138.654,312.715,138.321,308.76,140.285,
	305.532,143.334,301.765,148.357,297.742,154.895,295.293,160.906,299.433,160.914,306.896,153.559,313.424,143.334,
	314.934,138.074,324.674,136.987,320.674,139.532,313.783,145.667,309.318,152,307.094,159.57,310.913,159.658,318.805,151.944,
	325.621,142.976,328.617,136.639,336.252,136.648,335.369,137.974,330.105,141.182,324.188,146.922,320.006,153.781,
	317.111,159.347,320.599,158.761,330.396,149.571,336.926,140.868,338.951,136.651,341.73,136.654,345.398,136.941,343.828,138.642,
	338.357,142.796,333.156,147.46,329.568,152.662,329.58,158.457,334.479,154.227,340.896,147.77,346.018,141.314,347.678,137.119,
	354.094,137.62,350.375,140.106,345.174,144.052,341.156,150.664,338.039,156.453,340.688,157.146,348.76,149.792,354.738,141.536,
	357.381,137.877,363.064,138.321,363.199,138.34,358.984,140.465,353.244,145.128,346.608,154.635,348.504,155.785,355.184,150.887,
	363.422,141.981,366.1,138.73,371.756,139.491,368.541,140.868,363.289,143.693,360.527,148.216,357.188,154.672,359.882,154.635,
	371.721,144.77,375.557,140.003,380.396,140.654,380.6,140.71,376.922,142.079,372.977,144.949,369.432,149.551,366.762,153.559,
	369.747,153.2,380.33,144.77,383.146,141.404,387.119,142.488,382.662,143.873,379.971,147.281,379.896,150.219,386.354,146.657,
	390.584,143.433,395.064,144.654,404.73,148.988,409.166,152.38,400.381,154.672,390.467,158.343,386.967,156.07,381.232,155.34,
	379.006,155.785,380.868,158.94,386.82,159.827,382.168,161.954,376.779,159.347,370.768,157.789,363.199,158.011,365.084,161.989,
	371.002,163.783,378.221,163.758,372.771,166.249,369.926,165.039,364.188,162.527,354.516,162.465,348.504,163.578,
	355.184,167.585,362.977,169.144,368.76,168.584,361.449,172.838,360.582,170.381,352.123,168.377,336.742,169.523,335.592,171.816,
	342.27,172.929,349.617,174.042,355.629,174.71,357.99,175.032,351.797,179.078,349.395,178.717,344.193,175.631,338.182,175.409,
	331.361,177.604,323.346,181.167,329.58,182.28,335.176,183.133,340.52,182.911,347.344,181.986,343.828,184.283,338.443,188.322,
	336.086,185.47,327.625,184.581,316.652,187.819,311.768,190.74,310.195,192.483,323.346,192.744,329.357,192.299,334.383,191.368,
	330.471,194.302,328.17,196.158,325.621,193.888,316.049,194.111,304.635,196.788,303.308,199.646,308.428,200.314,312.381,201.074,
	319.504,200.406,323.568,199.868,316.686,205.42,315.164,202.614,308.262,202.614,298.41,207.884,298.715,209.703,308.873,208.774,
	312.516,208.774,306.895,213.296,305.094,211.347,299.307,212.46,294.179,216.567,298.187,217.903,302.417,217.235,303,217.097,
	299.746,220.352,298.197,222.232,295.737,222.355,291.953,224.582,294.624,224.804,295.86,225.07,290.395,231.706,290.172,233.488,
	293.734,231.261,297.857,226.708,297.742,229.258,296.406,235.046,296.628,240.835,299.746,234.378,302.842,228.357,
	302.483,221.721,304.421,219.461,307.6,216.893,307.314,218.793,307.094,222.356,306.646,229.703,306.967,234.994,309.318,234.378,
	310.654,228.812,312.348,218.671,311.631,213.828,315.998,210.11,316.369,209.817,316.221,211.001,315.998,216.789,314.439,226.14,
	315.107,233.488,317.334,231.706,321.676,223.873,322.752,215.443,321.75,210.593,319.373,207.443,327.354,201.136,327.354,202.54,
	326.463,208.329,324.904,216.789,324.236,223.691,326.018,230.816,329.209,230.51,334.231,210.779,333.515,202.887,330.615,198.583,
	339.658,191.724,338.707,194.302,337.148,200.536,336.48,212.337,335.666,224.59,338.707,225.027,342.304,215.981,346.03,207.167,
	345.585,194.698,343.918,191.048,342.715,189.404,352.951,182.221,352.512,183.615,350.953,187.401,348.402,193.021,
	348.269,200.406,349.657,216.877,351.844,217.903,354.738,206.993,357.157,200.091,356.933,188.291,355.406,180.499,
	363.117,176.077,361.641,178.495,360.599,183.694,359.343,191.945,360.082,200.314,360.527,208.329,363.867,205.212,365.426,198.31,
	367.235,191.048,366.339,180.465,364.227,175.442,369.567,172.393,371.361,171.496,371.361,173.648,370.755,177.078,
	369.864,183.757,370.464,195.891,373.217,196.084,375.666,187.178,377.64,179.748,376.779,173.597,375.08,169.346,382.357,165.486,
	381.678,166.917,379.971,172.393,379.612,182.438,381.678,182.947,384.35,175.155,385.531,169.882,384.17,164.573,389.633,161.916,
	387.863,164.859,386.967,167.729,387.912,172.038,391.475,167.363,392.207,160.768,401.27,157.344,410.031,154.841,407.123,166.924,
	405.277,168.03,403.273,168.03,398.375,169.812,394.369,171.816,393.033,173.597,393.962,174.904,401.938,172.533,406.406,169.901,
	406.064,171.321,405.387,173.524,400.381,175.378,395.26,177.382,389.916,179.608,384.795,183.17,383.682,184.729,384.456,187.281,
	392.527,184.052,400.688,179.756,404.721,175.688,402.098,184.21,399.268,184.061,390.584,186.287,382.568,190.295,377.225,193.412,
	381.232,193.857,385.729,194.501,394.636,191.161,401.27,186.51,401.443,186.336,399.611,192.293,397.709,191.853,389.248,194.302,
	381.678,197.197,374.107,201.65,370.768,204.989,372.617,206.833,383.014,203.623,390.807,200.728,395.705,196.974,398.533,195.717,
	395.949,203.876,392.811,203.876,385.018,205.435,378.338,208.774,372.104,211.891,366.539,215.231,364.758,217.68,367.595,218.492,
	376.779,216.314,386.799,212.083,391.697,207.884,395.156,206.38,393.064,212.988,392.061,215.842,388.58,215.676,377.225,218.793,
	367.652,223.023,361.008,228.014,362.034,230.33,370.105,227.819,379.074,224.59,386.576,220.574,391.135,218.47,388.582,225.726,
	385.24,226.363,375.221,228.59,365.203,233.265,359.191,238.831,360.6,240.733,371.003,237.505,379.972,233.379,384.635,231.406,
	387.689,228.266,386.73,230.988,384.098,237.571,376.563,238.94,369.567,240.913,358.984,247.191,358.746,250.186,364.313,248.404,
	372.771,245.51,379.006,242.616,382.754,240.929,380.064,247.654,379.881,248.057,375.889,248.404,367.652,252.19,360.973,255.752,
	358.078,259.759,361.535,262.233,367.99,259.784,374.59,254.904,378.508,251.064,374.209,260.479,369.877,261.986,364.313,266.216,
	360.527,270.669,365.871,269.779,370.545,266.661,372.08,265.042,368.246,273.044,364.535,273.786,361.195,276.012,363.422,277.126,
	366.771,276.121,365.396,278.988,367.064,278.988,368.49,276.134,369.209,277.571,370.768,278.462,370.768,275.344,370.141,272.835,
	372.963,267.189,373.662,271.559,375.443,276.903,376.384,275.353,376.563,266.922,374.83,263.457,379.047,255.022,378.783,256.865,
	379.229,261.541,380.342,267.775,382.123,273.118,384.635,272.662,384.456,263.514,384.097,256.339,383.2,251.675,381.73,249.654,
	384.719,242.459,384.572,245.956,385.018,251.522,386.576,257.756,388.357,263.989,389.693,258.646,390.195,248.805,
	388.135,242.171,386.369,238.48,389.064,231.988,389.479,239.657,391.92,246.624,394.592,251.744,394.592,242.616,393.256,235.491,
	391.92,228.59,390.896,226.999,393.672,219.443,393.924,222.578,394.321,230.868,396.832,241.272,399.49,241.503,400.381,235.937,
	399.268,229.035,397.264,220.574,395.064,215.654,396.957,209.407,397.264,211.446,398.154,217.235,401.854,231.406,
	404.164,230.148,403.051,220.352,400.778,209.523,398.6,206.993,398.066,205.749,400.301,198.374,400.824,201.204,402.16,207.661,
	405.621,221.182,407.727,219.684,407.059,211.446,405.621,201.989,403.469,195.532,401.586,194.132,401.73,193.654,403.076,189.418,
	403.273,190.74,405.723,196.306,407.504,203.208,411.066,209.665,413.514,207.191,409.747,194.097,406.877,188.178,404.26,185.691,
	406.107,179.869,406.836,182.057,408.84,188.737,412.402,196.084,414.949,194.276,410.465,179.926,407.595,174.904,408.17,173.37,
	409.508,175.155,412.402,178.94,412.976,177.057,409.023,170.196,413.064,154.321];
leavesShape[6][01] = [523.25,185.75,515.75,194.75,507.5,202.5,501.25,207.75,495,212,488,217.5,495.25,215.5,500.589,211.264,
	496.031,216.119,491.5,217.75,483.75,222,478,226.75,478.5,228.25,489.473,222.279,478.5,230,472.75,232.5,464.75,238.25,
	464.25,241.25,477.6,234.15,471.25,240.5,468.729,242.904,463.25,244,457.75,246.5,452.25,250,448.5,254.5,457,251.75,460.5,250.75,
	457.418,253.833,453.157,254.697,447.263,257.947,442.204,261.849,439.25,266,452.378,258.572,452.951,258.299,449.5,261.75,
	446.936,264.434,444.25,264.75,435.5,270.25,433.25,273.25,439.25,271.25,441.495,270.128,438.75,273,428.25,282.25,423.25,285.75,
	423.75,287.25,430,283.5,429,287,428,292.5,432,293.25,436.25,287.75,438.5,280.75,441.75,274.25,443.846,271.339,442.75,273.75,
	441.5,278.5,438.75,286.25,438.75,293,445.25,286.25,447.75,276,450.25,269,451.25,263.75,454.984,260.282,453.75,265.75,
	451.75,273.25,449,282.75,448.75,290.25,454.75,286,460.25,273.5,462,260.75,462.75,253.25,465.25,250.75,468.52,247.714,468,250,
	466,257,464.25,265,461.75,275.5,462.75,284.75,468.5,279.25,472,264.5,473.75,251.5,475.5,241,479.219,237.779,477.75,243,
	476.5,252.25,475.25,265.25,476,274.75,479,274.5,484.75,252.25,486.25,238.75,487.5,230.25,489.854,227.686,489.5,230.75,
	488.75,237.25,487.25,248.5,486.25,260.75,487,267.25,491,267,494.25,254.25,494.75,243,496,231.5,496.5,224.25,496.75,220.75,
	498.916,218.241,499,219.5,497.75,225.5,496.75,235.75,496.75,245.75,498,253.5,502.25,244.75,502.75,234.25,503,221,503.25,213.75,
	506.196,210.082,505,216.75,504.5,224.75,504.25,240.75,506.75,240.5,508.25,235,509.25,225.75,509.25,215.25,508.478,207.525,
	508.5,207.5,510.5,205.25,511,210.25,510.75,214.75,511,226.25,513.5,226.25,515.5,217.75,515.75,209.5,514.086,201.179,518,196.75,
	526,185.75];
leavesShape[6][02] = [131.167,153.833,133.833,161.833,137.5,173.5,139.408,179.476,135.833,184.167,130.833,192.167,130.167,198.167,
	134.5,195.5,138.167,189.5,140.833,185.5,141.025,184.54,143.874,193.46,141.5,195.833,137.167,202.167,133.5,211.5,
	131.167,221.167,135.5,216.5,140.5,209.167,144.167,202.5,145.712,199.216,148.095,206.679,146.167,209.833,141.167,219.5,
	136.167,232.5,135.167,239.833,140.167,235.167,144.5,227.167,147.167,218.5,149.608,211.418,150.167,213.167,152.18,219.271,
	147.833,228.833,143.167,244.5,142.167,253.833,144.167,259.833,146.833,251.167,149.833,240.167,152.5,231.5,153.167,225.833,
	153.74,224,158.187,237.485,154.5,244.167,151.833,254.5,149.833,265.5,149.833,274.5,154.167,267.5,157.5,257.167,159.5,248.167,
	159.5,242.833,159.783,242.325,160.5,244.5,163.262,254.167,161.833,259.167,158.5,268.167,156.167,278.5,156.167,287.167,
	160.167,277.833,162.167,268.833,163.833,262.833,164.859,259.757,167.833,270.167,167.905,270.44,165.167,277.833,162.167,287.167,
	160.833,294.5,165.5,289.5,167.5,281.833,169.87,277.955,171.897,285.706,169.5,290.5,166.833,297.833,166.5,303.167,169.5,299.5,
	171.833,293.833,173.173,290.582,173.5,291.833,175.144,299.699,173.167,304.5,171.5,311.833,171.167,316.833,174.5,310.833,
	176.396,305.689,178.167,314.167,175.5,317.5,174.167,323.5,177.167,321.5,178.761,318.768,179.5,324.5,182.167,324.5,
	181.477,319.325,183.833,321.5,186.833,324.5,184.5,318.5,181.052,316.141,180.833,314.5,179.552,307.773,181.5,310.5,
	187.167,315.167,185.5,309.833,182.833,305.5,178.527,302.39,176.833,293.5,177.5,294.833,180.5,298.5,187.167,303.5,184.833,297.5,
	180.167,291.833,175.514,287.646,173.291,277.78,175.167,279.5,177.833,284.167,184.167,289.833,188.167,295.167,187.833,290.833,
	182.833,283.833,177.167,276.833,172.332,273.525,171.5,269.833,169.322,262.211,171.5,265.167,174.833,269.167,179.5,275.833,
	185.5,279.167,184.167,273.833,179.833,266.833,171.833,258.833,167.256,254.979,164.167,244.167,167.833,247.5,173.167,253.5,
	180.5,261.833,185.833,266.167,184.833,260.167,179.167,251.167,168.833,241.5,161.603,235.876,157.719,223.319,161.833,225.833,
	166.167,231.833,172.5,239.167,182.5,248.167,187.833,252.833,184.167,242.833,174.833,233.167,164.167,222.167,155.856,217.295,
	154.167,211.833,152.908,207.965,154.5,208.833,158.5,212.167,168.167,221.167,177.167,230.5,183.167,234.833,181.167,228.833,
	176.167,221.833,169.167,214.167,159.167,207.167,151.791,204.533,147.787,192.228,150.5,195.167,154.833,197.833,161.167,202.5,
	167.5,206.833,170.833,207.167,166.167,200.833,159.833,195.167,152.833,190.833,146.729,188.976,144.131,180.991,145.5,181.833,
	149.167,183.833,154.833,187.833,158.833,187.5,155.5,183.167,149.167,179.5,143.066,177.721,140.5,169.833,134.833,152.167];
        
leavesShapeN[7] = 5;  // oak tree leaves
leavesShape[7][00] = [394.333,241.167,393.167,237.167,389.892,232.745,386.999,230.018,386.663,225.812,385.836,222.411,
	384.931,218.686,383.968,217.548,382.957,217.622,381.128,219.268,379.981,215.258,378.997,211.209,378.201,209.344,
	377.272,209.055,376.34,209.454,376.13,210.706,375.754,212.684,373.474,208.949,370.555,203.999,369.063,203.504,368.489,204.673,
	368.384,208.473,364.777,203.517,362.244,201.56,359.828,200.089,358.891,201.174,358.677,203.113,358.991,204.408,355.411,201.677,
	351.95,199.431,350.453,199.623,350.724,201.444,347.136,200.086,344.111,198.935,342.172,198.72,341.196,199.644,342.109,201.994,
	343.151,203.455,339.003,201.206,336.298,200.663,334.315,200.972,333.982,202.426,334.704,203.28,331.798,202.613,329.574,202.639,
	328.761,203.523,329.272,205.629,327.412,205.737,325.223,206.613,324.208,207.375,325.813,209.042,327.179,210.425,325.472,211.87,
	326.632,213.817,328.452,213.547,330.755,213.844,328.557,216.094,329.028,218.038,330.802,218.979,334.803,219.207,
	337.104,219.505,335.226,222.362,335.095,223.938,336.381,224.998,340.627,224.823,343.214,224.881,341.423,226.688,
	341.532,228.548,343.226,229.166,349.778,228.602,348.513,230.453,348.745,232.112,352.789,231.815,359.547,230.688,
	356.946,232.691,357.139,234.188,358.793,234.644,364.134,234.032,369.802,232.654,367.968,234.987,368.725,236.69,371.197,235.574,
	375.731,234.472,378.77,233.562,380.253,235.431,382.884,234.963,385.274,234.211,386.897,233.13,389.471,235.25,391.333,238.333,
	392,242,388.333,243.667,384.167,245,381,245.833,379.667,244.167,377.167,242,374.833,241.167,373.667,242.5,373.167,244,
	371.167,243,368.333,241.5,367,241.833,366.833,243.667,366.833,246,363.167,244.667,359.333,243.667,358.833,244.833,
	359.667,248.5,353.5,246.667,350.167,245.5,348.667,246.333,349.5,248.167,349.833,250.333,345,249.667,340.167,250,
	339.167,250.667,339.667,252.167,341,253.833,334.5,254.5,331.5,254.833,331.167,256.5,332.833,258.667,329.5,259.833,
	325.333,261.333,325.167,262.5,325.667,264.333,322.833,265.333,321,267.333,321.667,269.5,319.333,271.5,318.333,273.333,
	319.667,274.667,324.5,274.833,324.5,277,326.833,276.833,334,275.333,334.167,277.5,336.5,277.667,342.333,277,345.833,276,
	345.667,279,347.167,279.5,351.167,277,354.5,273,354.167,277,356.167,279,358.833,276.833,362.833,270.5,364.667,271.833,
	366.833,269.833,369.333,264.667,370.5,266.333,371.5,265.5,375.333,258.833,376.833,259.667,378.167,258.833,380,253.833,
	381.167,251.667,382.167,248,385.667,247,390,245.333,392.5,244,393.122,248.612,392.563,252.237,388.19,255.44,386.087,258.152,
	384.78,261.155,385.211,262.739,379.9,267.654,378.253,269.593,378.848,271.202,381.863,271.33,377.187,276.511,374.626,279.995,
	373.915,282.415,375.815,283.214,378.362,283.101,376.029,289.486,374.875,293.693,375.445,295.467,377.574,294.782,
	381.033,293.124,379.371,300.625,379.383,303.831,380.422,305.846,381.588,304.846,383.324,303.427,383.398,309.509,
	385.501,313.374,387.148,313.628,388.2,312.272,390.328,315.973,393.014,320.434,394.636,320.853,396.183,317.381,398.045,317.331,
	397.882,312.922,397.883,308.537,400.518,311.136,402.165,311.39,402.964,307.297,402.357,300.289,404.65,301.823,405.74,301.316,
	404.564,294.728,407.414,294.829,408.721,294.019,407.354,289.761,404.592,283.603,407.722,284.085,408.748,282.893,405.62,278.026,
	402.707,272.855,405.178,273.236,406.787,272.642,405.8,270.297,402.785,265.785,400.43,263.567,403.584,263.885,404.864,263.239,
	402.939,260.412,400.254,255.951,395.934,252.25,395.479,248.639,394.667,242.833,397.667,242,403.5,242.833,410.699,247.609,
	410.163,251.742,409.036,255.763,410.931,256.447,409.815,261.411,410.446,264.585,411.968,263.741,412.04,269.397,411.984,274.23,
	412.723,276.578,414.844,276.55,418.462,273.792,416.903,280.885,417.557,285.945,418.888,288.638,420.636,287.083,424.008,283.386,
	423.722,288.693,424.599,292.808,426.522,295.612,428.395,294.645,430.721,292.257,431.271,298.379,432.149,302.493,
	434.252,301.051,437.048,298.423,436.734,301.61,438.301,304.3,439.715,304.282,441.344,302.611,442.814,307.07,444.238,307.759,
	445.86,305.617,446.238,307.38,448.01,307.711,449.29,306.397,450.838,307.674,451.492,303.659,452.259,299.052,454.282,300.558,
	454.967,298.899,453.024,294.445,450.727,289.878,452.495,289.974,455.547,288.992,454.804,286.172,451.463,282.915,448.84,280.592,
	451.539,279.732,452.464,278.305,449.581,274.1,445.178,270.739,440.319,268.797,445.38,268.143,443.564,264.513,440.128,263.024,
	435.269,260.848,438.308,258.923,436.048,257.419,431.904,255.941,427.522,254.466,429.492,251.729,428.053,249.863,
	422.027,248.644,416.944,247.412,412.572,246.643,404,241,397.833,239.667,399.167,235.833,399.992,230.604,404.659,228.801,
	407.901,226.965,408.51,225.165,415.241,220.171,417.827,216.447,418.01,214.44,414.479,214.437,419.186,211.737,421.613,208.067,
	423.726,204.507,420.988,204.755,418.989,205.098,421.794,202.003,423.64,198.183,424.28,194.959,422.173,194.987,424.532,190.106,
	424.913,187.149,422.955,186.597,421.586,186.72,424.028,184.104,425.552,179.867,423.854,179.046,419.133,180.691,421.601,176.125,
	422.188,172.743,420.291,172.875,417.451,173.335,418.831,170.206,418.898,167.359,416.476,167.498,415.913,164.87,413.497,165.536,
	413.312,163.482,412.913,161.326,410.882,163.094,409.494,165.695,407.515,163.561,405.545,166.013,404.116,169.51,401.925,167.274,
	400.099,168.617,399.204,172.635,399.021,174.64,396.529,173.567,395.394,175.374,395.185,179.331,395.377,181.911,393.247,180.358,
	392.702,182.844,392.342,187.381,393.255,192.534,392.248,192.179,389.599,191.161,388.088,192.394,388.87,195.65,392.646,201.925,
	390.639,201.741,388.743,201.872,388.245,203.988,390.164,209.496,392.446,214.525,390.271,213.343,390.628,216.395,
	393.253,223.422,395.608,226.131,398.458,230.256,397.5,234.667,396,238.667];
leavesShape[7][01] = [392.5,242.667,393.667,248.833,393.667,252.5,389.833,256.333,388.167,259.333,387.333,262.5,388,264,
	383.5,269.667,382.167,271.833,383,273.333,386,273,382.167,278.833,380.167,282.667,379.833,285.167,381.833,285.667,
	384.333,285.167,383,291.833,382.5,296.167,383.333,297.833,385.333,296.833,388.5,294.667,388,302.333,388.5,305.5,
	389.833,307.333,390.833,306.167,392.333,304.5,393.333,310.5,396,314,397.667,314,398.5,312.5,401.167,315.833,404.5,319.833,
	406.167,320,407.167,316.333,409,316,408.167,311.667,407.5,307.333,410.5,309.5,412.167,309.5,412.333,305.333,410.667,298.5,
	413.167,299.667,414.167,299,412,292.667,414.833,292.333,416,291.333,414,287.333,410.333,281.667,413.5,281.667,414.333,280.333,
	410.5,276,406.833,271.333,409.333,271.333,410.833,270.5,409.5,268.333,405.833,264.333,403.167,262.5,406.333,262.333,
	407.5,261.5,405.167,259,401.833,255,397,252,396,248.5,394.5,241.5,392.604,235.361,392.866,231.202,393.725,227.115,
	391.788,226.559,392.573,221.532,391.734,218.406,390.272,219.349,389.826,213.71,389.563,208.884,388.672,206.589,386.557,206.757,
	383.129,209.747,384.216,202.568,383.229,197.563,381.723,194.963,380.081,196.629,376.963,200.542,376.896,195.227,375.749,191.18,
	373.646,188.508,371.841,189.598,369.677,192.133,368.724,186.061,367.575,182.014,365.572,183.591,362.957,186.399,
	363.059,183.199,361.317,180.618,359.907,180.729,358.393,182.504,356.63,178.152,355.165,177.559,353.687,179.803,353.195,178.069,
	351.405,177.856,350.215,179.25,348.585,178.079,348.196,182.129,347.736,186.776,345.618,185.407,345.044,187.107,347.277,191.423,
	349.872,195.828,348.101,195.851,345.12,197.032,346.048,199.796,349.598,202.825,352.368,204.97,349.731,206.006,348.902,207.49,
	352.057,211.497,356.674,214.56,361.648,216.176,356.644,217.163,358.693,220.666,362.222,221.923,367.215,223.774,364.31,225.896,
	366.663,227.247,370.896,228.449,375.362,229.631,373.579,232.492,375.138,234.26,381.231,235.078,386.386,235.971,390.798,236.449,
	391.833,240.667];
leavesShape[7][02] = [392.167,242.5,384.5,243.667,374.167,243.167,366.5,243.333,366.167,245.333,359.167,245.333,354.833,246.833,
	355.333,248.167,348,249.167,340.167,252,342.333,253.167,334.833,255.333,328.333,259.333,330.167,260.333,324.833,263.833,
	321.667,268.167,324.333,267.667,321,271.333,318,277.333,322.5,275.5,330.167,270.667,338.833,265.333,353.5,257.667,
	369.5,251.167,381.167,247.167,393.333,244,374.333,256.833,369.167,261,366,265.833,359.5,274.5,357.667,279.5,360.833,278,
	358,284,356.333,293.167,358.333,297.167,359.5,294.667,360,301.667,364.333,296.5,368.833,288.5,370.333,289.5,371,285.5,
	375.5,280.167,376.167,275.333,382,265.333,388.333,257.833,390.833,253,391.333,248,396,244.167,394.667,252.667,393.667,258.667,
	390.667,261.333,387.667,265.5,388.667,266.833,386.833,272,385.667,280.333,388.167,279.167,387.167,284,387.167,292.833,
	389.833,291.333,391.167,299.5,393.5,305.167,395.833,302,397,309.833,400.667,315.167,406.167,320.167,408.5,315,411.5,306.5,
	411.833,299.667,414.667,299.667,414.333,297,411.833,290.167,414.167,290.833,413.667,286.667,411.333,280,414,280.167,412,275.5,
	409,271,411,271.167,408.667,266.833,405.833,262.667,401.5,259,399.833,256.167,398,255.667,396.5,252.833,397,248.167,
	398.167,243.333,397,241.833,394.833,242.333];
leavesShape[7][03] = [372.244,247.637,368.948,246.168,360.648,243.477,362.801,247.428,355.872,245.509,347.943,246.981,
		350.988,250.827,344.588,253.364,338.335,255.821,342.03,259.123,342.455,259.467,334.5,262.833,334.5,264,330.5,267.833,
		333,268.667,327.333,274.167,329.333,275.167,326.333,281.167,330.833,279.333,340.166,278.5,351.166,273.667,353.076,271.487,
		357.228,271.342,363.44,269.855,361.722,274.018,362.68,274.455,362,276.167,361.666,282.167,359.5,286.5,364,288.167,364.666,297,
		366.666,301,367.833,298.5,368.333,305.5,372.666,300.333,377.166,292.333,377.833,296.833,381.833,290.5,383.833,284,387,279.333,
		389.166,281.5,391.166,271.667,394.667,268.167,400.833,266.333,403.839,264.93,409.833,269.5,413.382,267.518,414.333,268.333,
		418.666,269.5,417.5,261.167,418.166,258.167,421.833,260.5,419.333,254.333,417,250.167,421.5,251.167,415.666,246.333,
		409,243.333,412,242.333,406.333,240.5,403,239.833,403.5,238.167,397.166,238.833,391.405,241.899,391.833,237.333,389.666,238,
		389.166,231.833,386,225.333,384.5,226.5,383.333,222.167,380.333,217.5,378.5,215,377.5,217.167,375.833,214.167,371.666,211.167,
		371,214.167,368.5,209.5,365.5,207.333,363.333,211.833,363.5,215.5,362.333,215.667,362.666,220,364.5,224.167,362.333,224.333,
		362.5,226.833,365,231.167,367.833,234.333,364.666,234.167,367.333,238.333,370.166,241.333,374.166,244.333,370.333,244.833];
leavesShape[7][04] = [407.383,234.688,407.533,232.896,410.307,229.995,412.959,225.599,412.346,224.906,415.933,222.245,
	418.519,218.521,418.701,216.514,415.17,216.51,419.877,213.811,422.305,210.141,424.417,206.581,421.68,206.829,419.681,207.172,
	422.485,204.077,424.331,200.256,424.972,197.033,422.864,197.061,425.224,192.18,425.604,189.223,423.646,188.67,422.277,188.793,
	424.72,186.178,426.243,181.94,424.546,181.12,419.824,182.765,422.292,178.199,422.879,174.817,420.982,174.949,418.143,175.409,
	419.522,172.28,419.59,169.433,417.167,169.572,416.604,166.944,414.188,167.61,414.003,165.556,413.604,163.4,411.573,165.167,
	410.186,167.769,408.206,165.634,406.236,168.086,404.808,171.584,402.616,169.348,400.79,170.69,399.896,174.708,399.712,176.713,
	397.221,175.641,396.141,177.359,395.971,177.151,390.084,172.614,388.178,177.953,385.877,186.667,386.1,193.505,383.275,193.736,
	383.824,196.367,386.871,202.974,384.49,202.5,385.327,206.612,388.194,213.067,385.522,213.117,387.477,216.815,386.652,217.454,
	383.718,218.367,383.921,214.025,380.848,217.941,380.673,217.332,379.688,213.283,378.893,211.417,377.964,211.129,
	377.031,211.528,376.821,212.78,376.445,214.757,374.165,211.023,371.246,206.073,369.755,205.578,369.181,206.747,369.075,210.546,
	365.469,205.59,362.936,203.633,360.52,202.163,359.582,203.248,359.368,205.187,359.683,206.482,356.103,203.75,352.642,201.504,
	351.145,201.696,351.415,203.518,347.827,202.16,344.803,201.009,342.863,200.794,341.888,201.717,342.801,204.068,343.843,205.529,
	339.694,203.28,336.989,202.736,335.007,203.046,334.674,204.5,335.396,205.354,332.489,204.687,330.266,204.713,329.452,205.597,
	329.964,207.703,328.104,207.811,325.914,208.687,324.899,209.448,326.505,211.116,327.87,212.499,326.163,213.943,327.323,215.891,
	329.144,215.621,331.446,215.918,329.248,218.168,329.72,220.111,331.493,221.053,335.494,221.281,337.796,221.579,335.917,224.436,
	335.786,226.012,337.072,227.072,341.318,226.897,343.905,226.955,342.114,228.762,342.224,230.622,343.917,231.239,350.47,230.676,
	349.204,232.527,349.437,234.186,353.48,233.889,360.238,232.761,357.638,234.765,357.83,236.262,359.484,236.718,364.825,236.106,
	370.493,234.728,368.659,237.061,369.416,238.764,371.889,237.648,374.198,237.086,374.268,237.415,375.347,240.639,
	373.651,241.033,375.37,243.417,374.358,244.574,373.858,246.074,371.858,245.074,369.024,243.574,367.691,243.907,367.524,245.741,
	367.524,248.074,363.858,246.741,360.024,245.741,359.524,246.907,360.358,250.574,354.191,248.741,350.858,247.574,
	349.358,248.407,350.191,250.241,350.524,252.407,345.691,251.741,340.858,252.074,339.858,252.741,340.358,254.241,
	341.691,255.907,335.191,256.574,332.191,256.907,331.858,258.574,333.524,260.741,330.191,261.907,326.024,263.407,
	325.858,264.574,326.358,266.407,323.524,267.407,321.691,269.407,322.358,271.574,320.024,273.574,319.024,275.407,
	320.358,276.741,325.191,276.907,325.191,279.074,327.524,278.907,334.691,277.407,334.858,279.574,337.191,279.741,
	343.024,279.074,346.524,278.074,346.358,281.074,347.858,281.574,351.858,279.074,355.191,275.074,354.858,279.074,
	356.858,281.074,359.524,278.907,363.524,272.574,365.358,273.907,366.649,272.714,365.972,274.24,368.35,274.031,366.574,276.969,
	366.041,282.076,368.977,281.163,366.169,285.651,365.78,289.331,370.762,288.976,373.86,287.008,374.585,287.938,378.18,285.496,
	378.396,285.204,379.054,285.174,376.721,291.56,375.566,295.767,376.137,297.541,378.266,296.856,381.725,295.198,380.063,302.699,
	380.074,305.905,381.113,307.919,382.279,306.919,384.016,305.5,384.09,311.583,386.192,315.448,387.84,315.702,388.892,314.346,
	391.02,318.046,393.705,322.507,395.327,322.926,396.874,319.455,398.736,319.405,398.573,314.996,398.574,310.611,401.209,313.209,
	402.856,313.463,403.655,309.371,403.049,302.363,405.342,303.897,406.432,303.39,405.256,296.801,408.105,296.903,409.412,296.092,
	408.045,291.834,405.283,285.676,408.413,286.159,409.439,284.966,409.088,284.419,410.734,280.31,416.116,284.603,418.002,286.116,
	418.248,288.019,419.579,290.712,421.327,289.157,424.699,285.459,424.413,290.767,425.29,294.881,427.214,297.686,429.086,296.718,
	431.412,294.331,431.962,300.453,432.841,304.566,434.943,303.125,437.739,300.497,437.426,303.684,438.992,306.374,
	440.406,306.356,442.035,304.685,443.506,309.144,444.93,309.833,446.552,307.691,446.93,309.454,448.701,309.785,449.981,308.471,
	451.529,309.748,452.184,305.733,452.95,301.126,454.974,302.632,455.658,300.973,453.716,296.519,451.418,291.952,453.187,292.048,
	456.238,291.065,455.495,288.246,452.154,284.989,449.531,282.666,452.23,281.806,453.155,280.379,450.272,276.174,445.869,272.813,
	441.011,270.871,446.071,270.216,444.256,266.586,440.819,265.098,435.96,262.922,438.999,260.997,436.739,259.493,432.596,258.015,
	429.573,256.997,432.623,255.469,437.46,255.195,436.67,250.462,444.004,245.494,446.4,241.891,447.199,241.54,446.594,240.25,
	453.82,238.659,461.397,235.199,459.145,234.212,466.444,231.444,472.598,226.93,470.688,226.082,475.721,222.161,478.525,217.584,
	475.908,218.299,478.933,214.375,481.436,208.15,477.1,210.343,469.85,215.783,461.646,221.802,447.648,230.633,435.113,236.956,
	433.743,236.966,437.316,234.151,429.832,233.828,424.656,234.994,426.952,233.155,431.763,228.582,434.527,223.508,
	440.303,214.343,441.724,209.21,438.689,210.962,441.027,204.752,441.944,195.48,439.627,191.656,438.666,194.242,437.6,187.306,
	433.7,192.807,429.864,201.146,428.288,200.271,427.947,204.312,423.896,209.993,423.623,214.865,418.621,225.306,412.918,233.294,
	412.7,233.814,408.852,235.087];

leavesShapeN[8] = 2;  // ash tree
leavesShape[8][00] = [334.5,178.5,330.5,187.5,331.23,192.248,299.5,199.5,282.5,231.5,286.5,281.5,303.5,259.5,318.5,222.5,
	331.893,196.552,333.665,208.074,323.5,225.5,316.5,267.5,315.5,301.5,338.5,351.5,357.5,333.5,369.5,283.5,362.5,240.5,
	343.234,210.446,342.45,201.424,357.109,220.56,381.622,257.127,396.329,291.5,404.5,262.978,398.78,224.948,379.171,199.351,
	346.488,193.5,341.827,194.259,341.5,190.5,340.5,178.5];
leavesShape[8][01] = [247.554,210.214,232.429,200.471,216.192,198.354,196.426,204.707,170.84,219.735,148.193,251.833,
	144.497,290.746,180.637,241.402,204.911,218.648,228.463,203.867,241.205,213.551,197.856,236.331,172.602,286.256,
	180.908,352.216,218.385,314.195,233.004,268.786,246.109,216.03,285.159,216.692,269.302,246.46,256.963,283.099,258.003,323.717,
	271.305,369.164,288.308,348.383,299.892,296.458,300.947,255.135,286.257,216.687,322.682,212.484,323.054,226.292,
	329.557,263.664,348.138,313.929,379.053,354.87,388.066,313.697,388.22,258.08,364.396,223.234,326.526,210.364,346.746,198.958,
	373.492,221.12,410.302,255.841,444.512,281.078,445.204,255.888,421.017,209.86,387.312,194.815,355.262,192,343.967,192.706,
	320.671,208.236,286.08,212.472];


leavesShapeN[9] = 2;  // willow tree
leavesShape[9][00] = [279.246,50.243,278.408,84.847,261.912,116.243,227.912,142.243,188.579,216.243,170.5,263.5,139.5,314.5,
	195.5,260.5,232.5,209.5,253.5,165.5,268.579,130.909,277.855,107.719,277.246,132.909,278.175,173.034,265.246,204.243,
	238.5,259.5,212.579,305.576,192.5,361.5,170.5,428.5,210.5,384.5,241.5,323.5,259.5,268.5,275.246,208.243,278.719,196.52,
	279.167,298.5,280.5,323.833,253.833,364.5,232.5,410.5,212.5,463.167,186.5,536.5,239.5,472.5,268.5,415.834,281.246,372.243,
	284.978,355.59,287.053,444.295,271.833,484.5,238.245,527.057,198.912,601.057,176.5,658.5,244.5,570.5,278.5,505.167,
	286.5,467.167,287.579,517.724,288.508,557.849,275.579,589.057,244.912,631.724,222.912,690.391,208.5,737.5,183.5,801.5,
	220.5,757.5,266.5,652.5,285.579,593.057,289.052,581.334,291.579,690.391,293.268,707.286,268.245,755.058,242.912,804.391,
	228.912,851.723,232.245,921.723,265.579,850.391,278.912,795.723,291.579,757.058,296.188,736.489,301.579,743.724,
	301.825,724.014,307.578,765.058,325.579,828.391,358.245,897.057,352.912,827.723,335.579,765.058,318.245,719.058,
	302.133,699.307,302.145,698.411,302.245,690.391,300.692,555.807,305.579,573.724,316.912,637.057,331.5,681.5,363.5,736.5,
	395.5,753.5,376.5,709.5,355.5,644.5,314.912,545.724,300.341,525.398,300.245,517.057,299.167,464.5,313.167,499.834,330.5,543.5,
	352.5,596.5,378.5,643.5,428.5,693.5,401.579,625.057,370.912,542.391,322.5,479.167,298.5,437.834,293.833,355.899,303.5,383.5,
	327.5,431.5,375.5,476.5,356.5,427.167,325.834,364.5,307.912,334.243,295.167,317.833,292.5,303.833,291.834,279.833,
	290.359,170.993,295.246,188.909,315.579,248.243,337.5,298.5,363.912,367.576,390.912,411.242,379.5,362.5,372.579,330.909,
	356.579,253.576,327.912,197.576,302.579,172.909,290.008,140.584,289.912,132.243,290.534,94.599,302.579,118.243,325.5,165.5,
	349.5,211.5,375.5,243.5,433.5,314.5,398.5,207.5,360.579,157.576,313.246,108.243,290.888,73.195,291.246,51.576];
leavesShape[9][01] = [326.5,24.5,330.869,59.087,302.5,122.5,270.5,175.5,324.5,127.5,335.457,95.411,343.059,155.589,319.5,198.5,
	291.5,246.5,243.5,305.5,309.5,250.5,329.5,221.5,347.314,189.283,350.5,214.5,349.899,231.553,302.5,310.5,279.5,359.5,
	348.655,266.852,346.499,328.03,300.5,423.5,271.5,462.5,207.5,555.5,293.5,482.5,333.5,419.5,344.458,385.941,340.5,467.5,
	260.5,597.5,319.5,539.5,343.5,485.5,337.406,580.499,307.5,632.5,269.5,682.5,232.5,730.5,288.5,683.5,315.5,655.5,
	335.641,635.734,338.199,694.586,306.5,736.5,266.5,779.5,314.5,753.5,338.5,726.5,341.42,768.66,325.5,800.5,316.5,871.5,
	336.5,826.5,342.974,804.383,356.431,778.31,360.5,789.5,404.5,834.5,379.5,783.5,354.798,737.625,352.5,670.5,417.5,720.5,
	384.5,664.5,351.14,613.678,353.5,539.5,408.5,633.5,382.5,550.5,354.911,514.551,357.5,446.5,357.576,442.936,374.5,482.5,
	411.5,530.5,469.5,561.5,424.5,497.5,376.5,430.5,358.708,389.727,359.976,330.164,363.5,342.5,411.5,388.5,381.5,333.5,
	360.839,289.594,361.802,244.305,376.5,283.5,404.5,326.5,435.5,361.5,418.5,311.5,388.5,257.5,362.278,221.914,362.5,211.5,
	360.41,193.447,412.5,220.5,366.5,160.5,352.808,127.79,346.048,69.416,351.5,82.5,376.5,130.5,414.5,184.5,445.5,203.5,
	422.5,166.5,397.5,117.5,369.5,58.5,341.998,34.436,340.5,21.5];
    

leavesShapeN[10] = 2;  // robinia
leavesShape[10][00] = [403.833,168.333,400.954,183.036,390.833,176.833,379.489,175.763,372.724,169.281,358.267,165.31,
	371.867,148.708,364.493,145.563,355.321,157.41,349.062,147.324,332.167,134.833,318.5,137.5,312.834,149.167,318.834,160.167,
	334.034,172.789,344.737,171.076,335.056,183.581,324.861,175.455,309.89,169.613,293.335,173.608,276.1,183.416,279.658,194.155,
	261.979,201.502,257.833,207.833,263.61,218.64,286.401,227.85,300.833,224.833,312.5,220.167,303.515,232.436,298.816,241.661,
	289.196,236.434,271.761,233.272,254.088,237.265,243.833,245.167,244.167,257.5,257.167,264.5,275.72,268.833,286.455,265.938,
	279.05,280.482,268.551,276.422,253.352,273.264,233.167,273.167,219.507,283.986,221.5,294.5,235.167,298.833,250.833,298.5,
	264.263,298.328,273.827,290.742,263.412,311.195,258.5,316.5,243.834,311.167,226.303,315.373,220.5,324.5,221.834,331.833,
	233.834,336.5,249.5,335.167,256.258,330.693,248.067,354.264,244.044,349.721,231.525,347.909,218.167,349.833,208.98,360.326,
	212.833,369.5,223.5,373.5,233.833,372.5,246.5,363.5,240.867,374.982,241.049,375.073,227.433,385.021,216.222,401.77,
	213.275,423.23,221.087,431.072,235.625,428.863,247.287,410.102,252.237,393.563,248.232,382.598,244.865,374.326,248.012,366.279,
	248.91,379.022,258.064,386.417,269.5,387.167,275.833,378.5,274.5,361.833,263.711,355.123,253.253,352.875,263.204,327.425,
	264.417,335.894,271.992,350.89,284.283,355.384,289.22,345.554,286.577,329.11,275.167,321.833,266.167,317.5,281.321,289.973,
	281.688,307.081,285.915,318.716,297.311,322.761,303.136,316.735,304.503,302.873,314.569,300.656,318.407,280.762,
	308.378,262.855,298.323,258.365,307.834,236.833,310.5,252.833,316.5,264.167,329.371,266.41,334.309,256.58,329.878,244.558,
	324.312,231.132,317.247,223.854,326.196,209.604,323.659,221.292,328.329,235.388,335.501,237.833,342.501,234.5,348.501,228.833,
	351.501,222.167,344.716,200.535,336.997,192.41,340.044,187.558,345.251,181.201,344.507,192.037,349.382,197.867,348.334,205.833,
	366.334,210.333,388.834,206.333,397.63,199.999,394.591,215.519,381.834,212.833,365.834,214.333,352.834,225.333,341.834,241.833,
	349.834,249.833,340.5,260.833,339.834,271.833,347.834,278.833,372.334,276.833,385.334,271.833,388.937,267.662,388.334,273.333,
	388.276,283.686,377.334,283.333,360.334,288.333,352.167,298.167,346.667,309.167,352.667,320.667,363.833,320.5,379.834,318.333,
	388.127,310.928,387.834,324.833,376.834,328.333,361.834,332.333,339.167,342.5,335.834,349.833,341.167,356.5,358.167,358.167,
	372.834,354.833,382.834,346.5,388.167,335.5,387.849,361.713,380.834,361.167,359.167,366.833,340.334,380.335,334.834,393.334,
	343.834,400.834,366.167,399.834,380.501,392.5,388.5,372.167,393.468,407.086,387.834,404.832,375.834,408.832,362.167,417.998,
	358.667,431.498,366.667,442.5,380.167,440.5,390.333,431.332,395.188,420.133,396.333,428.832,396.536,428.832,388.834,443.834,
	386.334,463.834,393.333,484.334,403.833,487.834,415.833,479.334,417.833,457.334,414.833,440.332,406.333,432.332,
	399.612,426.451,398.812,417.85,407.333,425.832,418.833,428.332,428.5,423.167,430.333,414.332,421.333,403.832,407.833,400.832,
	397.479,403.518,394.948,376.31,399.833,383.333,420.167,390.833,433.167,389.333,433.167,378.333,424.167,363.333,407.833,361.333,
	393.897,360.462,394.327,334.71,401.5,341.5,408.834,346.5,419.834,347.5,422.334,339.5,416.668,327.834,425.668,323.5,
	417.167,306.833,406.333,298.333,395.333,298.833,395.13,286.541,403.333,293.833,418.167,296.833,425.833,294.833,426.833,283.5,
	417.333,272.333,406.333,262.833,396.753,259.5,398.354,242.749,404.167,249.167,414.167,262.5,434.333,271.333,444.833,266.333,
	445.5,256,433.167,240.833,410.833,226.333,400.288,222.537,400.833,216.833,402.634,208.816,406.833,218.833,421.833,225.333,
	437.333,228.833,450.833,224.833,453.833,210.333,447.833,193.833,421.833,185.833,407.132,188.773,411.833,167.833];
leavesShape[10][01] = [388.167,335.5,382.834,346.5,372.834,354.833,358.167,358.167,341.167,356.5,335.834,349.833,339.167,342.5,
	361.834,332.333,376.834,328.333,387.834,324.833,388.127,310.928,379.834,318.333,363.833,320.5,352.667,320.667,346.667,309.167,
	352.167,298.167,360.334,288.333,377.334,283.333,388.276,283.686,390.167,236.5,378.5,245.167,362.5,248.167,357.167,241.833,
	359.5,227.833,369.167,219.833,381.834,212.833,394.591,215.519,397.63,199.999,388.834,206.333,366.334,210.333,348.334,205.833,
	349.382,197.867,344.507,192.037,345.251,181.201,338.928,187.404,335.034,191.607,341.113,201.021,343.771,223.536,
	339.589,229.531,332.643,233.987,325.146,235.966,318.552,232.234,316.574,217.517,321.234,206.502,309.799,218.846,
	315.393,227.308,318.375,241.532,320.5,254.167,313.826,262.912,301.594,258.323,297.799,246.073,298.144,229.856,284.807,249.253,
	293.855,255.528,300.393,274.983,293.457,285.281,281.912,281.747,274.79,258.355,242.249,301.404,250.29,307.33,260.153,316.596,
	259.703,333.244,253.03,341.989,241.785,335.295,237.12,319.155,237.498,310.608,223.003,333.772,231.51,339.7,226.659,346.292,
	215.369,345.974,210.785,353.298,212.56,362.051,214.463,373.567,206.535,388.902,197.529,395.357,182.833,394.833,176.61,385.68,
	177.551,374.957,191.672,360.576,206.896,353.325,206.734,353.201,214.398,342.962,200.283,349.459,189.943,348.526,
	180.202,342.619,178.116,332.89,189.088,324.282,202.572,324.866,214.538,328.967,217.649,334.176,230.066,312.532,222.597,315.676,
	206.955,314.083,196.027,307.272,196.076,299.819,203.47,291.926,221.476,291.042,234.9,299,240.709,294.697,254.735,276.528,
	243.93,282.211,230.702,279.892,215.245,277.315,202.619,270.524,202.608,259.823,218.037,251.724,237.854,255.56,252.205,261.479,
	261.769,267.415,271.741,254.495,260.656,255.351,243.227,247.654,231.749,238.366,233.708,226.185,245.249,220.319,
	263.355,219.672,279.903,226.011,288.388,232.93,294.715,224.734,315.52,198.874,288.525,179.185,287.019,167.972,295.613,165.04,
	312.621,164.182,326.252,172.696,334.763,182.571,344.737,171.076,334.034,172.789,318.834,160.167,312.834,149.167,318.5,137.5,
	332.167,134.833,349.062,147.324,355.321,157.41,364.493,145.563,371.867,148.708,358.267,165.31,372.724,169.281,379.489,175.763,
	390.833,176.833,400.954,183.036,403.833,168.333,411.833,167.833,407.132,188.773,421.833,185.833,447.833,193.833,
	453.833,210.333,450.833,224.833,437.333,228.833,421.833,225.333,406.833,218.833,402.634,208.816,400.833,216.833,
	400.288,222.537,410.833,226.333,433.167,240.833,445.5,256,444.833,266.333,434.333,271.333,414.167,262.5,404.167,249.167,
	398.354,242.749,396.753,259.5,406.333,262.833,417.333,272.333,426.833,283.5,425.833,294.833,418.167,296.833,403.333,293.833,
	395.13,286.541,395.333,298.833,406.333,298.333,417.167,306.833,425.668,323.5,416.668,327.834,422.334,339.5,419.834,347.5,
	408.834,346.5,401.5,341.5,394.327,334.71,397.5,348.833,407.5,370.5,396.5,385.833,378.5,377.833,380.5,357.167,386.5,346.167];

leavesShapeN[11] = 2;  // robinia
leavesShape[11][00] = [409.833,184.833,395.833,162.833,377.833,154.5,367.833,165.5,376.5,188.5,387.5,200,376.5,210.5,372.5,220,
	367.167,237.833,377.167,241.5,390.096,239.702,386.5,248,385.346,253.312,377.833,245.5,369.167,246.167,365.424,260.112,
	356.5,260.5,337.167,266.5,336.167,278.167,348,287.5,361.279,290.317,356,298.5,346.833,325.833,353.167,331.5,372.397,326.39,
	375,335.5,380.5,345,359,355.833,358.5,364.833,376.5,368.833,394.167,366.167,398,379,409.5,389.167,416.833,379.167,
	416.833,356.5,434.167,356.5,449.167,347.167,439,332,423.5,326.5,431.167,316.833,427.5,311,414.349,311.47,409.5,304.5,
	408.69,299.106,417.5,306,437.833,308.5,434,286.5,426.5,271.5,445.5,269,460.5,264.5,462.167,250.167,440.833,241.833,428.855,241,
	430.348,234.7,442.5,236.5,450.5,225.5,445.5,210.5,431.833,192.5,444,176.5,440.833,153.5,430.166,151,416.5,166.5];

leavesShape[11][01] = [229,167,230,152,220,128.5,211,134,203.5,127.5,194,145.5,190,163.5,191.5,175.5,179.5,168.5,165.5,168.5,
	146.5,178,153,184,144,187.5,155.5,196,172.5,203.5,182,205.5,168.5,215.5,158.5,229.5,156,246.5,166,249,166,257,181.5,253.5,
	195,244,203.5,231.5,211.5,245.5,225,256,238.5,260,240.5,250,248,248,242,230,235,218.5,228.5,211,238.5,209.5,251.5,206.5,
	268,194,262.5,186,265.5,177.5,244,174,229.5,175.5,223.5,177];
    
var leaves = new Array;  // points of leaves

for (k = 0; k <= 11; k++)
{
    leaves[k] = new Array;
    for (j = 0; j < leavesShapeN[k]; j++)
    {
        leaves[k][j] = new Array;
        for (i = 0; i < leavesShape[k][j].length/2; i++)
        {
            var x = (leavesShape[k][j][i*2] - leavesShape[k][j][0])/4;
            var y = (leavesShape[k][j][i*2+1] - leavesShape[k][j][1])/4;
            leaves[k][j].push(new Vector3(x,y,0));

        }
    }
}


var sunShape = new Array();

sunShape[0] = [-4.396,10.641,-10.607,4.43,-10.607,-4.354,-4.396,-10.565,4.387,-10.565,10.599,-4.354,10.599,4.43,4.387,10.641];
sunShape[1] = [2.313,-25.375,-2.438,-25.375,-2.438,-15.25,2.438,-15.25];
sunShape[2] = [-16.313,-19.75,-19.688,-16.375,-12.5,-9.063,-9.125,-12.438];
sunShape[3] = [-15.25,-2.313,-25.5,-2.313,-25.5,2.625,-15.313,2.625];
sunShape[4] = [-12.5,9.125,-19.813,16.5,-16.375,19.813,-8.938,12.625];
sunShape[5] = [-2.313,15.313,-2.313,25.625,2.313,25.625,2.313,15.188];
sunShape[6] = [9.125,12.563,16.313,19.75,19.875,16.313,12.5,9.125];
sunShape[7] = [15.375,-2.25,15.375,2.375,25.438,2.375,25.438,-2.375];
sunShape[8] = [16.25,-19.688,9.125,-12.5,12.563,-9,19.688,-16.375];

var sunIcon = new Array();
for ( j=0; j <= 8; j++)
{
    sunIcon[j] = new Array();
    for ( i=0; i<sunShape[j].length/2; i++)
            sunIcon[j].push(new Vector3(sunShape[j][i*2]/4, sunShape[j][i*2+1]/4, 0));
}


var iconFrameShape = [3.669,60.534,1.986,59.836,0.697,58.548,0,56.865,0,3.669,
	0.697,1.986,1.986,0.698,3.669,0,56.788,0,58.472,0.698,59.76,1.986,60.457,3.669,60.457,56.865,59.76,58.548,58.472,59.836,
	56.788,60.534];
var iconFrame = new Array();
for ( i=0; i<iconFrameShape.length/2; i++)
    iconFrame.push(new Vector3(iconFrameShape[i*2], iconFrameShape[i*2+1],0));


var inputPaths = RenderAPI.getParameter (kpsSelectedPaths);

var totalCalls = 0;

var frameNum = 1;

function StartEachRender (api)
{
    frameNum++
 }

function EndEachRender (api)
{

}





function exRender(symmetrical, lPoints, angle, lr, lg, lb)
{
    RenderAPI.pushMatrix()
    
    RenderAPI.Color (kStrokeColor, lr,lg,lb);
    RenderAPI.lineWidth (0.1);
    RenderAPI.Color (kFillColor, lr,lg,lb);
    RenderAPI.Polygon( lPoints );
  
    RenderAPI.scale(-1,1);
    RenderAPI.translate (outputSizeX, 0);
    RenderAPI.Polygon( lPoints );
        
    RenderAPI.popMatrix()
}





function line (x,y, x1,y1, size, cr,cg,cb)
{

   var lenX = Math.abs(x1-x);
   var lenY = Math.abs(y1-y);

   var exX = 0;
   var exY =0;

   if (lenX == 0){
       exX = 0;
       exY = 1;
   }else if (lenY == 0){
       exX = 1;
       exY = 0;
   }else if (lenX > lenY){
       exX = 1;
       exY = lenY/lenX;
   }else{
       exY = 1;
       exX = lenX/lenY;
   }
   
    var exX0 = 0;
    var exY0 = 0;
    var exX1 = 0;
    var exY1 = 0;
    
    if (x1 > x){
        exX0 = -exX;
        exX1 = exX;
    }else{
        exX0 = exX;
        exX1 = -exX;
   }

    if (y1 > y){
        exY0 = -exY;
        exY1 = exY;
    }else{
        exY0 = exY;
        exY1 = -exY;
    }
        
    tLine(x + exX0, y + exY0, x1 + exX1, y1 + exY1, size, cr,cg,cb);
        
}

function tLine(x0,y0,x1,y1, w, cr,cg,cb)
{
    
    
    var len = Math.sqrt (Math.pow(x1-x0,2) + Math.pow(y1-y0,2));
    if (len < 0.05)
        len = 0.05;
        
    var sx = (y1-y0)*w/len;
    var sy = (x1-x0)*w/len;

    var t = new Array;
    t = [ new Vector3(x0-sx,y0+sy), 
                                    new Vector3(x0+sx,y0-sy),
                                    new Vector3(x1+sx,y1-sy),
                                    new Vector3(x1-sx,y1+sy)
                                    ];
    
    RenderAPI.Color (kStrokeColor, cr,cg,cb)
    RenderAPI.lineWidth (0.01)
    RenderAPI.Color (kFillColor, cr,cg,cb)
    RenderAPI.Polygon( t )    

}

function ModuleStem(api,tree, tx, ty, treeSize, depth, path, pathOffset, pathlength, stumpHeight, alongPath, power, offsetX, offsetY)
{

    this.power = power;
    this.offsetX = offsetX;
    this.offsetY = offsetY;
    
    totalCalls++; // count number of module calls

    this.depth = depth;

    sLength = tree.sLength * treeSize;
    var adjust_sLength = sLength * Math.cos(tree.viewAngle);

    var sWidth = tree.sInitialWidth * treeSize;

    var stemAngleSegmentRandom = tree.stemAngleSegmentRandom * treeSize * Math.cos(tree.viewAngle);
    var stemStraightSegmentRandom = tree.stemStraightSegmentRandom * treeSize * Math.cos(tree.viewAngle);
    var stemHeightBeforeAngleChange = tree.stemHeightBeforeAngleChange * treeSize * Math.cos(tree.viewAngle);
    
    stemHeightBeforeAngleChange += (Engine.rand()*2-1)*tree.stemHeightBeforeAngleChangeRandom;


    var stemAngleSegment    = (adjust_sLength / tree.stemSegmentNumber) * tree.stemAngleSegmentRatio;
    var stemStraightSegment = (adjust_sLength / tree.stemSegmentNumber) * (1-tree.stemAngleSegmentRatio);
   
   
    this.points = new Array;
    this.pointsX = new Array;
    this.pointsY = new Array;

    for (i = 0; i <=16; i++){
        this.points[i] = new Array; 
        this.pointsX[i] = new Array;
        this.pointsY[i] = new Array;
    }


    this.surfaceNoiseL = {v : Engine.rand(), c : Engine.rand(), offset : Engine.rand()*100};
    this.surfaceNoiseR = {v : Engine.rand(), c : Engine.rand(), offset : Engine.rand()*100};

    
    var stem_step = (10/tree.d) * treeSize * Math.cos(tree.viewAngle);
    
    var height = 0;
    var stemAngle = Math.PI/2 + (1-Math.sin(Engine.rand()*Math.PI/2) -0.5)*tree.stemInitialAngleRandom;

    var direction = Engine.rand()*2*Math.PI;
    var exeDirection = direction;

    var x;
    var y;
    

    if (alongPath){
        var pt = path.getValue(kGetPointAlongGeometry, pathOffset);
        var x1 = pt.x + offsetX;
        var y1 = pt.y + offsetY;
    }else{
        var x1 = tx; 
        var y1 = ty;    
    }
    
    var skipFirst = false;
    
    var nextStemAngle = 1;       // which direction the stem goes
    if (Engine.rand()>0.5)
        nextStemAngle = -1;

    api.command(kpsStartTask,0.8);   // progress bar start

   
    var CounterForRoundedRoot = 1.5;
    do 
    {  
            var iMax = stemStraightSegment + (Engine.rand()-0.5)*stemStraightSegmentRandom;

            for(var i = 0; i < iMax; i+= stem_step)
            {
                    if (alongPath){
                        var pt = path.getValue(kGetPointAlongGeometry, pathOffset+(pathlength-1)*(height+i)/(adjust_sLength*tree.sLengthStop));
                        x = pt.x;
                        y = pt.y;
                    }else{
                        x = x1 + Math.cos(stemAngle)*stem_step;
                        y = y1 - Math.sin(stemAngle)*stem_step;
                    }
                    
                    var w = treeWidth(height+i, adjust_sLength, sWidth, stumpHeight, tree.stumpShape, tree.stumpShapeRatio*treeSize);

                    var len = Math.sqrt (Math.pow(x1-x,2) + Math.pow(y1-y,2));
                    if (len < 0.05)
                        len = 0.05;

                    var tempA = (tree.stemHeightbeforePalmBump * treeSize * Math.cos(tree.viewAngle));
                    if(height > tempA){
                        var m = (Math.abs(Math.sin(Math.PI*(height - tempA)/ ((adjust_sLength*tree.sLengthStop) - tempA)))+2);
                        var sxL = (y1-y)*(m*w*noise(height+i, treeSize, this.surfaceNoiseL, tree.stemSurfaceNoiseLevel, tree.d))/len;
                        var syL = (x1-x)*(m*w*noise(height+i,  treeSize, this.surfaceNoiseL, tree.stemSurfaceNoiseLevel, tree.d))/len;
                        var sxR = (y1-y)*(m*w*noise(height+i, treeSize, this.surfaceNoiseR, tree.stemSurfaceNoiseLevel, tree.d))/len;
                        var syR = (x1-x)*(m*w*noise(height+i,  treeSize, this.surfaceNoiseR, tree.stemSurfaceNoiseLevel, tree.d))/len;
                    }else{
                        var sxL = (y1-y)*(w*noise(height+i, treeSize, this.surfaceNoiseL, tree.stemSurfaceNoiseLevel, tree.d))/len;
                        var syL = (x1-x)*(w*noise(height+i,  treeSize, this.surfaceNoiseL, tree.stemSurfaceNoiseLevel, tree.d))/len;
                        var sxR = (y1-y)*(w*noise(height+i, treeSize, this.surfaceNoiseR, tree.stemSurfaceNoiseLevel, tree.d))/len;
                        var syR = (x1-x)*(w*noise(height+i,  treeSize, this.surfaceNoiseR, tree.stemSurfaceNoiseLevel, tree.d))/len;
                    }
                
                    var rrY = 0;   // rounded Root Y
                    
                    for (var j = 0; j <=16; j++){
                        if (CounterForRoundedRoot < stumpHeight){
                                rrY = (Math.sqrt(Math.abs(w*w - Math.pow(((j-8)*w/8),2)))* Math.sin(tree.viewAngle))*(stumpHeight*CounterForRoundedRoot/50);

                      //          if (this.points[j].length > 85)
                      //      Engine.message(this.points[j].length, "  ", Math.sqrt(w*w - Math.pow(((j-8)*w/8),2)),"  ", w*w - Math.pow(((j-8)*w/8),2),"  ", w*w, "  ", Math.pow(((j-8)*w/8),2), "  ", j,"  ", w);
                        }
                        if(skipFirst){
                            var xx = offsetX + power * (x1+(-sxL*(16-j)+sxR*j)/(16+(Engine.rand()-0.5)*tree.stemSurfaceNoiseLevel/5));
                            var yy = offsetY + power * (y1+(syL*(16-j)-syR*j)/(16+(Engine.rand()-0.5)*tree.stemSurfaceNoiseLevel/5) + rrY);
                            this.pointsX[j].push(xx);
                            this.pointsY[j].push(yy);
                            this.points[j].push(new Vector3(xx,yy, depth*10 +Math.PI/2));
                            
                        }
                    }
                    skipFirst = true;
                    CounterForRoundedRoot -= 0.05;
                    if (CounterForRoundedRoot < 0)
                        CounterForRoundedRoot = 0;
                               
                    x1 = x;
                    y1 = y;

            }
            height += i;

            if (adjust_sLength*tree.sLengthStop - height <= 0)
                var pbarStep = 1;
            else
                var pbarStep = iMax / (adjust_sLength*tree.sLengthStop - height);
            
            api.command(kpsStartTask, pbarStep);  // progress bar
            api.command(kpsFinishTask);     // progress bar



            var Brate = tree.b1Rate;
            for (j=0; j<tree.b1Rate; j++)
            {

                if (height > (tree.stemHeightBeforeBranch * treeSize * Math.cos(tree.viewAngle)) && Engine.rand() < Brate && tree.branchGeneration >= 0){
                    
                    direction += (137.5 * Math.PI/180)+(Engine.rand()*tree.b1DirectionRandom);         // phyllotactic
                    if (Engine.rand()<tree.b1CenterForce  * (height/sLength)){

                        Engine.addModule(new ModuleB1(tree, x, y, w, height, treeSize, stemAngle, direction, stemAngleSegment, stemStraightSegment, 0, depth, sLength, power, offsetX,offsetY));
                        exeDirection = Math.PI*(0.495+Engine.rand()*0.01);
                        Engine.addModule(new ModuleB1(tree, x, y, w, height, treeSize, stemAngle, exeDirection, stemAngleSegment, stemStraightSegment, 0, depth, sLength, power, offsetX,offsetY));
                    }else{

                        exeDirection = direction;                
                        Engine.addModule(new ModuleB1(tree, x, y, w, height, treeSize, stemAngle, exeDirection, stemAngleSegment, stemStraightSegment, 0, depth, sLength, power, offsetX,offsetY));
                    }
                
                Brate -= 1;
                }


            }

            var rand = Engine.rand()-0.5;
            if (  (stemAngle - Math.PI/2 > Math.abs(tree.stemMaxAngle))  || (tree.stemAngleChangeRate > Engine.rand() && height > stemHeightBeforeAngleChange ))        // changing angle?
            {
                if (stemAngle - Math.PI/2 > tree.stemMaxAngle)
                    var base = -(1-Math.sin(Engine.rand()*Math.PI/2))*tree.stemRandomAngle;
                else if( stemAngle - Math.PI/2 < -tree.stemMaxAngle)
                    var base = (1-Math.sin(Engine.rand()*Math.PI/2))*tree.stemRandomAngle;
                else
                    var base = (1-Math.sin(Engine.rand()*Math.PI/2))*tree.stemRandomAngle*nextStemAngle;
                
                var sbase = Math.floor((stemAngleSegment + rand*stemAngleSegmentRandom)/stem_step);
                if (sbase == 0)
                    sbase = 1;
                var eachStemAngle =  base / sbase ;
            }
            else
                var eachStemAngle = 0;
            nextStemAngle *= -1;
            
            var iMax = stemAngleSegment + rand*stemAngleSegmentRandom;

            for(var i = 0; i < iMax; i+= stem_step)
            {
                    stemAngle += eachStemAngle;
                    if(alongPath){
                        var pt = path.getValue(kGetPointAlongGeometry, pathOffset + (pathlength-1)*(height+i)/(adjust_sLength*tree.sLengthStop));
                        x = pt.x;
                        y = pt.y;
                    }else{
                        x = x1 + Math.cos(stemAngle)*stem_step ;
                        y = y1 - Math.sin(stemAngle)*stem_step ;
                    }
                    
                    var w = treeWidth(height+i, adjust_sLength, sWidth, stumpHeight, tree.stumpShape, tree.stumpShapeRatio*treeSize);
                    
                    var len = Math.sqrt (Math.pow(x1-x,2) + Math.pow(y1-y,2));
                    if (len < 0.05)
                        len = 0.05;
                        
                    var tempA = (tree.stemHeightbeforePalmBump * treeSize * Math.cos(tree.viewAngle));
                    if(height > tempA){
                        var m = (Math.abs(Math.sin(Math.PI*(height - tempA)/ ((adjust_sLength*tree.sLengthStop) - tempA)))+1);
                        var sxL = (y1-y)*(m*w*noise(height+i, treeSize, this.surfaceNoiseL, tree.stemSurfaceNoiseLevel, tree.d))/len;
                        var syL = (x1-x)*(m*w*noise(height+i,  treeSize, this.surfaceNoiseL, tree.stemSurfaceNoiseLevel, tree.d))/len;
                        var sxR = (y1-y)*(m*w*noise(height+i, treeSize, this.surfaceNoiseR, tree.stemSurfaceNoiseLevel, tree.d))/len;
                        var syR = (x1-x)*(m*w*noise(height+i,  treeSize, this.surfaceNoiseR, tree.stemSurfaceNoiseLevel, tree.d))/len;                   
                    }else{
                        var sxL = (y1-y)*(w*noise(height+i, treeSize, this.surfaceNoiseL, tree.stemSurfaceNoiseLevel, tree.d))/len;
                        var syL = (x1-x)*(w*noise(height+i,  treeSize, this.surfaceNoiseL, tree.stemSurfaceNoiseLevel, tree.d))/len;
                        var sxR = (y1-y)*(w*noise(height+i, treeSize, this.surfaceNoiseR, tree.stemSurfaceNoiseLevel, tree.d))/len;
                        var syR = (x1-x)*(w*noise(height+i,  treeSize, this.surfaceNoiseR, tree.stemSurfaceNoiseLevel, tree.d))/len;
                    }



                    var rrY = 0;   // rounded Root Y

                    for (var j = 0; j <=16; j++){
                        if (CounterForRoundedRoot < stumpHeight){
                                rrY = (Math.sqrt(Math.abs(w*w - Math.pow(((j-8)*w/8),2)))* Math.sin(tree.viewAngle))*(stumpHeight*CounterForRoundedRoot/50);
                        }
                    
                        var xx = offsetX + power * (x1+(-sxL*(16-j)+sxR*j)/(16+(Engine.rand()-0.5)*tree.stemSurfaceNoiseLevel/5));
                        var yy = offsetY + power * (y1+(syL*(16-j)-syR*j)/(16+(Engine.rand()-0.5)*tree.stemSurfaceNoiseLevel/5) + rrY);
                        this.pointsX[j].push(xx);
                        this.pointsY[j].push(yy);
                        this.points[j].push(new Vector3(xx,yy, depth*10+ Math.PI/2));
                        

                    }
                    CounterForRoundedRoot -= 0.05;
                    if (CounterForRoundedRoot < 0)
                        CounterForRoundedRoot = 0;
                
                    var oldX1 = x1;
                    var oldY1 = y1;
                
                    x1 = x;
                    y1 = y;

            }

            height += i;

            if (adjust_sLength*tree.sLengthStop - height <= 0)
                var pbarStep = 1;
            else
                var pbarStep = iMax / (adjust_sLength*tree.sLengthStop - height);

            api.command(kpsStartTask, pbarStep);  // progress bar

            api.command(kpsFinishTask);     // progress bar


    }while ( height < adjust_sLength*tree.sLengthStop )


    if (tree.sLengthStop < 1 && (!tree.makeBranchLeaves || tree.leavesRate > 0)){
                if(tree.sLengthStopDiverse >=0){
                    Engine.addModule(new ModuleB1(tree, oldX1, oldY1, w, height, treeSize, stemAngle, exeDirection, stemAngleSegment, stemStraightSegment, 1, depth, sLength, power, offsetX,offsetY));
                    if(tree.sLengthStopDiverse > 0){
                        Engine.addModule(new ModuleB1(tree, oldX1, oldY1, w, height, treeSize, stemAngle+tree.sLengthStopDiverse, exeDirection, stemAngleSegment, stemStraightSegment, 1, depth, sLength, power, offsetX,offsetY));
                        Engine.addModule(new ModuleB1(tree, oldX1 , oldY1, w, height, treeSize, stemAngle-tree.sLengthStopDiverse, exeDirection, stemAngleSegment, stemStraightSegment, 1, depth, sLength, power, offsetX,offsetY));
                    }
                }else{
                    Engine.addModule(new ModuleB1(tree, oldX1, oldY1, w, height, treeSize, stemAngle, exeDirection, stemAngleSegment, stemStraightSegment, 2, depth, sLength, power, offsetX,offsetY));
                    for ( j=0; j<2; j++){
                        Engine.addModule(new ModuleB1(tree, oldX1, oldY1, w, height, treeSize, stemAngle+tree.sLengthStopDiverse*Engine.rand(), exeDirection, stemAngleSegment, stemStraightSegment, 2, depth, sLength, power, offsetX,offsetY));
                        Engine.addModule(new ModuleB1(tree, oldX1 , oldY1, w, height, treeSize, stemAngle-tree.sLengthStopDiverse*Engine.rand(), exeDirection, stemAngleSegment, stemStraightSegment, 2, depth, sLength, power, offsetX,offsetY));
                    }
                }
    }

    for( var j = 0; j < 16; j++){
        for (var i = this.points[j+1].length-1; i >= 0 ; i--)
            this.points[j].push(this.points[j+1][i]);
    }


    api.command(kpsFinishTask);     // progress bar

}



ModuleStem.prototype.produce = function(system)
{
}

ModuleStem.prototype.render = function(api, env)
{

    api.command(kpsStartTask,1/totalCalls)
    totalCalls--


    var r = tree.stemR;
    var g = tree.stemG;
    var b = tree.stemB;


    var dapples = 1;

    for (var i =0; i < 16; i++){
        if(tree.stemTexShading)
            var m = Math.sin((Math.PI/2)*((tree.lightDirection - Math.PI)/Math.PI)+(Math.PI/2)*(i/16))*((Engine.rand()-0.5)*tree.stemTexRandomness+1);
        else{
            if(tree.stemsColor)
                var m = 1;
            else
                var m = 0.5;
        }
    
        m = 1-((1-m)*0.8);
            
        api.lineWidth (0.001);
        if(Engine.rand()<tree.stemTexStripeRatio && tree.stemTexShading)
            api.Color (kStrokeColor,  0,0,0);
        else
            api.Color (kStrokeColor,  m*r,m*g,m*b);
        api.Color (kFillColor, m*r,m*g,m*b);

        api.Polygon( this.points[i] );
    } 


    if(tree.stemTexShading)
    {
        var d = this.depth*10 +Math.PI/2+0.01;
        var tTop = this.pointsX[0].length-1-tree.stemTexPatternLength*2-2;
        for (var i=2; i < tTop; i++){
            
            if (tree.stemTexDapples)
                dapples = i/16;
            else
                dapples = 1;
            
            for (var j=0; j < 16; j++){
                
                if (Engine.rand()<tree.stemTexRatio){
                    
                    if (i/tTop > tree.stemHeightOfShadow)
                        var m = Math.sin((Math.PI/2)*((tree.lightDirection - Math.PI)/Math.PI)+(Math.PI/2)*(j/16)*dapples)*((Engine.rand()-0.5)*tree.stemTexRandomness+1)*(1-tree.leavesRate*0.25);
                    else
                        var m = Math.sin((Math.PI/2)*((tree.lightDirection - Math.PI)/Math.PI)+(Math.PI/2)*(j/16)*dapples)*((Engine.rand()-0.5)*tree.stemTexRandomness+1);
                        
                    
                    api.lineWidth (0);
                    api.Color (kStrokeColor,  m*r,m*g,m*b);
                    api.Color (kFillColor, m*r,m*g,m*b);
                    var n = Math.floor(Engine.rand()*tree.stemTexPatternLength*0.9999999)+tree.stemTexPatternLength;
                    var points = new Array();
                    var temp1 = this.power * tree.stemTexRoughness;
                    for (var k = 0; k < n; k++)
                        points.push(new Vector3(this.pointsX[j][i+k]*(1+(Engine.rand()-0.5) * temp1), this.pointsY[j][i+k]*(1+(Engine.rand()-0.5) * temp1), d));

                    points.push(new Vector3(this.pointsX[j][i+k+1]*0.75*(1+(Engine.rand()-0.5) * temp1)+this.pointsX[j+1][i+k+1]*0.25*(1+(Engine.rand()-0.5) * temp1), this.pointsY[j][i+k+1]*0.75*(1+(Engine.rand()-0.5) * temp1)+this.pointsY[j+1][i+k+1]*0.25*(1+(Engine.rand()-0.5) * temp1), d));
                    points.push(new Vector3(this.pointsX[j][i+k+2]*0.5*(1+(Engine.rand()-0.5) * temp1)+this.pointsX[j+1][i+k+2]*0.5*(1+(Engine.rand()-0.5) * temp1), this.pointsY[j][i+k+2]*0.5*(1+(Engine.rand()-0.5) * temp1)+this.pointsY[j+1][i+k+2]*0.5*(1+(Engine.rand()-0.5) * temp1), d));          
                    points.push(new Vector3(this.pointsX[j][i+k+1]*0.25*(1+(Engine.rand()-0.5) * temp1)+this.pointsX[j+1][i+k+1]*0.75*(1+(Engine.rand()-0.5) * temp1), this.pointsY[j][i+k+1]*0.25*(1+(Engine.rand()-0.5) * temp1)+this.pointsY[j+1][i+k+1]*0.75*(1+(Engine.rand()-0.5) * temp1), d));    

                    for (var k = n-1; k >= 0; k--)
                        points.push(new Vector3(this.pointsX[j+1][i+k]*(1+(Engine.rand()-0.5) * temp1), this.pointsY[j+1][i+k]*(1+(Engine.rand()-0.5) * temp1), d));  

                    points.push(new Vector3(this.pointsX[j][i-1]*0.25*(1+(Engine.rand()-0.5) * temp1)+this.pointsX[j+1][i-1]*0.75*(1+(Engine.rand()-0.5) * temp1), this.pointsY[j][i-1]*0.25*(1+(Engine.rand()-0.5) * temp1)+this.pointsY[j+1][i-1]*0.75*(1+(Engine.rand()-0.5) * temp1), d));   
                    points.push(new Vector3(this.pointsX[j][i-2]*0.5*(1+(Engine.rand()-0.5) * temp1)+this.pointsX[j+1][i-2]*0.5*(1+(Engine.rand()-0.5) * temp1), this.pointsY[j][i-2]*0.5*(1+(Engine.rand()-0.5) * temp1)+this.pointsY[j+1][i-2]*0.5*(1+(Engine.rand()-0.5) * temp1), d));                     
                    points.push(new Vector3(this.pointsX[j][i-1]*0.75*(1+(Engine.rand()-0.5) * temp1)+this.pointsX[j+1][i-1]*0.25*(1+(Engine.rand()-0.5) * temp1), this.pointsY[j][i-1]*0.75*(1+(Engine.rand()-0.5) * temp1)+this.pointsY[j+1][i-1]*0.25*(1+(Engine.rand()-0.5) * temp1), d));

                    api.Polygon(points);
                }
            }

            if (i%2==0 && Engine.rand()<tree.stemHTexRate && tree.stemHTexOpacity > 0){

                    api.lineWidth (0);
                    api.Color (kStrokeColor,  m*r,m*g,m*b, tree.stemHTexOpacity);
                    api.Color (kFillColor, m*r,m*g,m*b, tree.stemHTexOpacity);
                    
                    var jStart = Math.floor(Engine.rand()*(15-tree.stemHTexSize));
                    
                    var points = new Array();
                    for(var j=jStart; j<=jStart+tree.stemHTexSize; j++){
                        points.push(new Vector3(this.pointsX[j][i]*(1+Engine.rand()*tree.stemHTexRoughness*2-tree.stemHTexRoughness), this.pointsY[j][i]*(1+(tree.viewAngle/200)*Math.sin(j*Math.PI/15))*(1+Engine.rand()*tree.stemHTexRoughness*2-tree.stemHTexRoughness), d));
                    }
                    for(var j=jStart+tree.stemHTexSize; j>=jStart; j--){
                        points.push(new Vector3(this.pointsX[j][i]*(1+Engine.rand()*tree.stemHTexRoughness*2-tree.stemHTexRoughness), this.pointsY[j][i]*(1.0015+((tree.viewAngle/200)+0.001)*Math.sin(j*Math.PI/15))*(1+Engine.rand()*tree.stemHTexRoughness*2-tree.stemHTexRoughness), d));
                    }
                    api.Polygon(points);
            }
        }
    }

    api.command(kpsFinishTask)

}





// branch 1
function ModuleB1(tree, bx, by, width, height, treeSize, stemAngle, direction, stemAngleSegment, stemStraightSegment, LengthStop, treeDepth, sLength, power, offsetX,offsetY)
{
    totalCalls++; // count number of module calls
    
    var adjust_sLength = sLength * Math.cos(tree.viewAngle);    


    if (height > adjust_sLength)
        height = adjust_sLength;

    this.branchDirection = direction;

    this.treeDepth = treeDepth;

    stemAngle = (stemAngle - Math.PI/2) ;

    var x1 = bx;
    var y1 = by;
    
    var b1Angle = tree.b1InitialAngle - (height/sLength)*tree.b1InitialAngleChangeRate;

    var b1Ratio = 0;
    switch (tree.b1ReduceRatioType)
    {
        case 0:
            b1Ratio = Math.cos((Math.PI/2)*(height/adjust_sLength));
            break;
        case 1:
            b1Ratio = -Math.sqrt(height/adjust_sLength)+1;
            break;
        case 2:
            if (height/adjust_sLength < 2/3)
                b1Ratio = 1;
            else
                b1Ratio = Math.cos((Math.PI/2)*(((height/adjust_sLength)-2/3)*3));
            break;
        case 3:
                b1Ratio = 1;
            break;
        case 4:
                b1Ratio = Engine.rand();
            break;
        case 5:
                b1Ratio = Math.sin(Math.PI*height/adjust_sLength);
            break;
        case 6:
                if(height/adjust_sLength<3/8){
                    b1Ratio = Math.sin(1.5*Math.PI*height/adjust_sLength);
                }else{
                    b1Ratio = Math.sin((Math.PI)*((3/4)*(height/adjust_sLength+1/3)));   
                }
            break;     
    }
   
   if(tree.b1ShapeType == 0)
        var b1Length = tree.b1LengthRate * adjust_sLength * b1Ratio;    
    else
        var b1Length = tree.b1LengthRate * sLength * b1Ratio;               // when tilted, looks short   this is adjusting

   var rx =  b1Length*Math.sin(b1Angle) * Math.cos(direction);
   var ry = - b1Length*Math.cos(b1Angle) - b1Length*Math.sin(b1Angle) * Math.sin(direction) * Math.sin(tree.viewAngle);
    
    x = rx*Math.cos(stemAngle) - ry*Math.sin(stemAngle);
    y = rx*Math.sin(stemAngle) + ry*Math.cos(stemAngle);

    if (tree.b1ShapeType == 5 || tree.b1ShapeType == 6)
        var b1LengthOnScreen = (Math.sqrt(x*x + y*y) + b1Length) /2;                                // with tree.b1ShapeType = 5, the branch changes the direction to bottom, so the length should be longer 
    else
        var b1LengthOnScreen = Math.sqrt(x*x + y*y) * (1+Math.sin(tree.viewAngle)/4);

    if (x > 0){
        b1InitialAngleOnScreen = Math.atan(y/x)+Math.PI + (Engine.rand()*2-1)*tree.b1InitialAngleRandom;
    }else{
        b1InitialAngleOnScreen = Math.PI - Math.atan(-y/x)+Math.PI + (Engine.rand()*2-1)*tree.b1InitialAngleRandom;
    }


    side = Math.cos(direction);

    rightLeft = 1;
    if (side < 0)
        rightLeft = -1;


    this.surfaceNoiseL = {v : Engine.rand(), c : Engine.rand(), offset : Engine.rand()*100};
    this.surfaceNoiseR = {v : Engine.rand(), c : Engine.rand(), offset : Engine.rand()*100};

    var b1Width = width * tree.b1InitialWidth;

    switch (LengthStop)             // if stem stops, 1st branch continues on the end
    {
        case 1:
            b1Width = width;            // don't put "break" here.  when it is "1", both cases excutes
        case 2:                                         
            b1LengthOnScreen = b1Length;
            b1InitialAngleOnScreen = stemAngle + Math.PI/2;
    }


    var depth = Math.abs((this.branchDirection%(Math.PI*2))-Math.PI/2);
    if (depth < Math.PI/2)
        depth = Math.PI-(depth + Math.PI/2);
    else
        depth = depth - Math.PI/2;

    if (depth > Math.PI/2)
        this.depth = Math.PI/2;
    else
        this.depth = depth - 0.01;
                    
    var b1AngleSegment    = stemAngleSegment * tree.b1AngleSegmentRate;
    var b1StraightSegment = stemStraightSegment * tree.b1StraightSegmentRate;

    this.points = new Array;
    this.pointsX = new Array;
    this.pointsY = new Array;

    for (i = 0; i <=8; i++){
        this.points[i] = new Array;
        this.pointsX[i] = new Array;
        this.pointsY[i] = new Array;
    }

    this.surfaceNoiseL = {v : Engine.rand(), c : Engine.rand(), offset : Engine.rand()*100};
    this.surfaceNoiseR = {v : Engine.rand(), c : Engine.rand(), offset : Engine.rand()*100};

    var b1_step = (10/tree.d) * treeSize * b1LengthOnScreen/b1Length;

    var length = 0;
    var b1Angle = b1InitialAngleOnScreen; 

    var x = bx;
    var y = by;
    
    var x1 = x;
    var y1 = y;



    var nextb1Angle = 1;       // which direction the b1 goes
    if (Engine.rand()>0.5)
        nextb1Angle = -1;
        


    do {  

//        straight part

        var iMax = b1StraightSegment + (Engine.rand()-0.5)*tree.b1StraightSegmentRandom*treeSize;

        for(var i = 0; i < iMax; i+= b1_step)
        {
            
                switch (tree.b1ShapeType)
                {
                    case 0:
                        break;
                    case 1:
                        if (length / b1LengthOnScreen < tree.b1ShapeBeginningPart)
                            b1Angle -= -side*tree.b1ShapeBeginningMagnitude* (1+(Engine.rand()-0.5)*tree.b1ShapeMagnitudeRandom)/(100*tree.d);
                        else
                            b1Angle += -side*tree.b1ShapeMagnitude* (1+(Engine.rand()-0.5)*tree.b1ShapeMagnitudeRandom)/(100*tree.d);
                        break;
                    case 2:
                        var ran = Math.floor((b1Angle+Math.PI/2-stemAngle)/(Math.PI));
                        b1Angle = (b1Angle - ran*Math.PI)/((1+(tree.b1ShapeMagnitude-1)/tree.d) * (1+(Engine.rand()-0.5)*tree.b1ShapeMagnitudeRandom)) + ran*Math.PI;
                        break;
                    case 3:
                        b1Angle += -side*tree.b1ShapeMagnitude* (1+(Engine.rand()-0.5)*tree.b1ShapeMagnitudeRandom)/(100*tree.d);
                        break;
                    case 4:
                        if (length / b1LengthOnScreen < tree.b1ShapeBeginningPart)
                            b1Angle += side*tree.b1ShapeBeginningMagnitude*(1-height/sLength)* (1+(Engine.rand()-0.5)*tree.b1ShapeMagnitudeRandom)/(100*tree.d);
                        else
                            b1Angle -= side*tree.b1ShapeMagnitude*(2+Math.cos(direction))* (1+(Engine.rand()-0.5)*tree.b1ShapeMagnitudeRandom)/(100*tree.d);
                        break;
                    case 5:
                        b1Angle += Math.PI/2;
                        var ran = Math.floor((b1Angle)/(Math.PI));
                        if (length / b1LengthOnScreen < tree.b1ShapeBeginningPart)
                            b1Angle -= -side*tree.b1ShapeBeginningMagnitude* (1+(Engine.rand()-0.5)*tree.b1ShapeMagnitudeRandom)/(100*tree.d);
                        else
                        {
                            var m = Math.pow((Math.abs(Math.sin(direction))*2),4)+7;
                            if (ran%2 == 0)
                                b1Angle += (0.01*tree.b1ShapeMagnitude*m+((Engine.rand()-0.5)*tree.b1ShapeMagnitudeRandom)*0.001)/tree.d;
                            else
                                b1Angle -= (0.01*tree.b1ShapeMagnitude*m+((Engine.rand()-0.5)*tree.b1ShapeMagnitudeRandom)*0.001)/tree.d;
                        }
                        b1Angle -= Math.PI/2;
                        break;
                    case 6:
                        b1Angle += Math.PI/2;
                        var ran = Math.floor((b1Angle)/(Math.PI));
                        if (length / b1LengthOnScreen < tree.b1ShapeBeginningPart)
                            b1Angle -= -side*tree.b1ShapeBeginningMagnitude* (1+(Engine.rand()-0.5)*tree.b1ShapeMagnitudeRandom)/(100*tree.d);
                        else
                        {
                            var m = Math.pow((Math.abs(Math.sin(direction))*2),4)+7;
                            if (ran%2 == 0)
                                b1Angle -= (0.01*tree.b1ShapeMagnitude*m+((Engine.rand()-0.5)*tree.b1ShapeMagnitudeRandom)*0.001)/tree.d;
                            else
                                b1Angle += (0.01*tree.b1ShapeMagnitude*m+((Engine.rand()-0.5)*tree.b1ShapeMagnitudeRandom)*0.001)/tree.d;
                        }
                        b1Angle -= Math.PI/2;
                        break;
                }

                var w = branchWidth(length+i, b1LengthOnScreen, b1Width);

                if (length > tree.b1HeightBeforeBranchHappens * b1LengthOnScreen*tree.b1LengthStop+ tree.b1HeightBeforeBranchHappensRandom*b1LengthOnScreen*tree.b1LengthStop*(Engine.rand()-0.5)*2  &&Engine.rand() < tree.b2Rate && tree.branchGeneration > 0 && length < b1LengthOnScreen)          
                    Engine.addModule(new ModuleB2(tree, 0, x1, y1, w, height, treeSize, rightLeft, length/b1LengthOnScreen , length, direction, b1Length, b1Angle, b1AngleSegment, b1StraightSegment, 1, treeDepth, this.depth, false, sLength, false, power, offsetX,offsetY));


                x = x1 + Math.cos(b1Angle)*b1_step;
                y = y1 - Math.sin(b1Angle)*b1_step;

                var len = Math.sqrt (Math.pow(x1-x,2) + Math.pow(y1-y,2));
                if (len < 0.05)
                    len = 0.05;
    
                var sxL = (y1-y)*(w*noise(length+i, treeSize, this.surfaceNoiseL, tree.stemSurfaceNoiseLevel, tree.d))/len;
                var syL = (x1-x)*(w*noise(length+i,  treeSize, this.surfaceNoiseL, tree.stemSurfaceNoiseLevel, tree.d))/len;
                var sxR = (y1-y)*(w*noise(length+i, treeSize, this.surfaceNoiseR, tree.stemSurfaceNoiseLevel, tree.d))/len;
                var syR = (x1-x)*(w*noise(length+i,  treeSize, this.surfaceNoiseR, tree.stemSurfaceNoiseLevel, tree.d))/len;
                          
                for (var j = 0; j <=4; j++){

                    var xx = offsetX + power * (x1+(-sxL*(4-j)+sxR*j)/(4+(Engine.rand()-0.5)*tree.stemSurfaceNoiseLevel/5));
                    var yy = offsetY + power * (y1+(syL*(4-j)-syR*j)/(4+(Engine.rand()-0.5)*tree.stemSurfaceNoiseLevel/5));
                    this.pointsX[j].push(xx);
                    this.pointsY[j].push(yy);
                    this.points[j].push(new Vector3(xx,yy, -10));

                }
         
                x1 = x;
                y1 = y;
        }
        length += i

      
        var rand = Engine.rand()-0.5;
        if ( (tree.b1AngleChangeRate > Engine.rand()  ))        // changing angle?
        {
            var base = (1-Math.sin(Engine.rand()*Math.PI/2))*tree.b1RandomAngle*nextb1Angle;
            
            var sbase = Math.floor((b1AngleSegment + rand*tree.b1AngleSegmentRandom)/b1_step);
            if (sbase == 0)
                sbase = 1;
            var eachb1Angle =  base / sbase;
        }
        else
            var eachb1Angle = 0;
        nextb1Angle *= -1;
        
        var iMax = b1AngleSegment + rand*tree.b1AngleSegmentRandom * treeSize;
        
        for(var i = 0; i < iMax; i+= b1_step)
        {
 
                switch (tree.b1ShapeType)
                {
                    case 0:
                        break;
                    case 1:
                        if (length / b1LengthOnScreen < tree.b1ShapeBeginningPart)
                            b1Angle -= -side*tree.b1ShapeBeginningMagnitude* (1+(Engine.rand()-0.5)*tree.b1ShapeMagnitudeRandom)/(100*tree.d);
                        else
                            b1Angle += -side*tree.b1ShapeMagnitude* (1+(Engine.rand()-0.5)*tree.b1ShapeMagnitudeRandom)/(100*tree.d);
                        break;
                    case 2:
                        var ran = Math.floor((b1Angle+Math.PI/2)/(Math.PI));
                        b1Angle = (b1Angle - ran*Math.PI)/((1+(tree.b1ShapeMagnitude-1)/tree.d) * (1+(Engine.rand()-0.5)*tree.b1ShapeMagnitudeRandom)) + ran*Math.PI;
                        break;
                    case 3:
                        b1Angle += -side*tree.b1ShapeMagnitude* (1+(Engine.rand()-0.5)*tree.b1ShapeMagnitudeRandom)/(100*tree.d);
                        break;
                    case 4:
                        if (length / b1LengthOnScreen < tree.b1ShapeBeginningPart)
                            b1Angle -= -side*tree.b1ShapeBeginningMagnitude*(1-height/sLength)* (1+(Engine.rand()-0.5)*tree.b1ShapeMagnitudeRandom)/(100*tree.d);
                        else
                            b1Angle += -side*tree.b1ShapeMagnitude*(2+Math.sin(direction))* (1+(Engine.rand()-0.5)*tree.b1ShapeMagnitudeRandom)/(100*tree.d);
                        break;
                    case 5:
                        b1Angle += Math.PI/2;
                        var ran = Math.floor((b1Angle)/(Math.PI));
                        if (length / b1LengthOnScreen < tree.b1ShapeBeginningPart)
                            b1Angle -= -side*tree.b1ShapeBeginningMagnitude* (1+(Engine.rand()-0.5)*tree.b1ShapeMagnitudeRandom)/(100*tree.d);
                        else
                        {
                            var m = Math.pow((Math.abs(Math.sin(direction))*2),4)+7;
                            if (ran%2 == 0)
                                b1Angle += (0.01*tree.b1ShapeMagnitude*m+((Engine.rand()-0.5)*tree.b1ShapeMagnitudeRandom)*0.001)/tree.d;
                            else
                                b1Angle -= (0.01*tree.b1ShapeMagnitude*m+((Engine.rand()-0.5)*tree.b1ShapeMagnitudeRandom)*0.001)/tree.d;
            
                        }
                        b1Angle -= Math.PI/2;
                        break;
                    case 6:
                        b1Angle += Math.PI/2;
                        var ran = Math.floor((b1Angle)/(Math.PI));
                        if (length / b1LengthOnScreen < tree.b1ShapeBeginningPart)
                            b1Angle -= -side*tree.b1ShapeBeginningMagnitude* (1+(Engine.rand()-0.5)*tree.b1ShapeMagnitudeRandom)/(100*tree.d);
                        else
                        {
                            var m = Math.pow((Math.abs(Math.sin(direction))*2),4)+7;
                            if (ran%2 == 0)
                                b1Angle -= (0.01*tree.b1ShapeMagnitude*m+((Engine.rand()-0.5)*tree.b1ShapeMagnitudeRandom)*0.001)/tree.d;
                            else
                                b1Angle += (0.01*tree.b1ShapeMagnitude*m+((Engine.rand()-0.5)*tree.b1ShapeMagnitudeRandom)*0.001)/tree.d;
                        }
                        b1Angle -= Math.PI/2;
                        break;
                        
                }
            
                b1Angle += eachb1Angle;
                
                var ba = b1Angle%Math.PI;
                var bb = Math.floor(b1Angle/Math.PI);

                x = x1 + Math.cos(b1Angle)*b1_step;
                y = y1 - Math.sin(b1Angle)*b1_step;

                
                var w = branchWidth(length+i, b1LengthOnScreen, b1Width);

                var len = Math.sqrt (Math.pow(x1-x,2) + Math.pow(y1-y,2));
                if (len < 0.05)
                    len = 0.05;
                    
                var sxL = (y1-y)*(w*noise(length+i, treeSize, this.surfaceNoiseL, tree.stemSurfaceNoiseLevel, tree.d))/len;
                var syL = (x1-x)*(w*noise(length+i,  treeSize, this.surfaceNoiseL, tree.stemSurfaceNoiseLevel, tree.d))/len;
                var sxR = (y1-y)*(w*noise(length+i, treeSize, this.surfaceNoiseR, tree.stemSurfaceNoiseLevel, tree.d))/len;
                var syR = (x1-x)*(w*noise(length+i,  treeSize, this.surfaceNoiseR, tree.stemSurfaceNoiseLevel, tree.d))/len;

                for (var j = 0; j <=4; j++){

                    var xx = offsetX + power * (x1+(-sxL*(4-j)+sxR*j)/(4+(Engine.rand()-0.5)*tree.stemSurfaceNoiseLevel/5));
                    var yy = offsetY + power * (y1+(syL*(4-j)-syR*j)/(4+(Engine.rand()-0.5)*tree.stemSurfaceNoiseLevel/5));
                    this.pointsX[j].push(xx);
                    this.pointsY[j].push(yy);
                    this.points[j].push(new Vector3(xx,yy, -10));
                }

                var oldX1 = x1;
                var oldY1 = y1;

                x1 = x;
                y1 = y;
        }

        length += i;



    }while ( length < b1LengthOnScreen*tree.b1LengthStop)

    if(tree.b1LengthStop < 1){
        Engine.addModule(new ModuleB2(tree, 0, oldX1, oldY1, w, height, treeSize, rightLeft, length/b1LengthOnScreen , length, direction, b1Length, b1Angle, b1AngleSegment, b1StraightSegment, 1, treeDepth, this.depth, true, sLength, true, power, offsetX,offsetY));
        if(tree.b1LengthStopDiverse > 0){
            Engine.addModule(new ModuleB2(tree, 0, oldX1, oldY1, w, height, treeSize, rightLeft, length/b1LengthOnScreen , length, direction, b1Length, b1Angle+tree.b1LengthStopDiverse, b1AngleSegment, b1StraightSegment, 1, treeDepth, this.depth, true, sLength, false, power, offsetX,offsetY));
            Engine.addModule(new ModuleB2(tree, 0, oldX1, oldY1, w, height, treeSize, rightLeft, length/b1LengthOnScreen , length, direction, b1Length, b1Angle -tree.b1LengthStopDiverse, b1AngleSegment, b1StraightSegment, 1, treeDepth, this.depth, true, sLength, false, power, offsetX,offsetY));
        }
    }


    if (this.pointsX[0].length < 2){
        this.draw = false;
    }else{
        this.draw = true;
        for( var j = 0; j < 8; j++){
            for (var i = this.points[j+1].length-1; i >= 0 ; i--)
                this.points[j].push(this.points[j+1][i]);
        }
    }

}




ModuleB1.prototype.produce = function(system)
{
}



ModuleB1.prototype.render = function(api, env)
{ 
    
   api.command(kpsStartTask,1/totalCalls)
   totalCalls--;
   
   
   if (!this.draw)
   {
        api.command(kpsFinishTask)
        return
   }

    if(tree.makeBranchLeaves){
        var r = tree.leavesR;
        var g = tree.leavesG;
        var b = tree.leavesB;
        
    }else if(tree.stemTexShading){
        var r = tree.stemR*(1-tree.leavesRate*0.25);
        var g = tree.stemG*(1-tree.leavesRate*0.25);
        var b = tree.stemB*(1-tree.leavesRate*0.25);
    }else{
        var r = tree.stemR;
        var g = tree.stemG;
        var b = tree.stemB;
    }

    var dapples = 1;

    var l = Math.floor((this.branchDirection+Math.PI/2)/Math.PI)%2;

    

    for (var i =0; i < 4; i++){
        if(tree.makeBranchLeaves)
        {
            if(tree.leavesGroupShading)
                var m = Math.sin((Math.PI/2)*l+(Math.PI/2)*(i/3));
            else{
                if(tree.leafColor)
                    var m = 1;
                else
                    var m = 0.5;
            }
        } else if(tree.stemTexShading)
                var m = Math.sin((Math.PI/2)*l+(Math.PI/2)*(i/3));
            else{
                if(tree.stemsColor)
                    var m = 1;
                else
                    var m = 0.5;
        }
        
        api.lineWidth (0.001);
        
        m = 1-((1-m)*0.8);
        
            api.Color (kStrokeColor,  m*r,m*g,m*b);
        api.Color (kFillColor, m*r,m*g,m*b);
        api.Polygon( this.points[i] );
    } 




    if((tree.stemTexShading  && !tree.makeBranchLeaves) || (tree.makeBranchLeaves && tree.leavesGroupShading))
    {
        var d = this.depth*10 +Math.PI/2+0.01;
        for (var i=2; i < this.pointsX[0].length-1-tree.stemTexPatternLength*2-2; i++){
            
            if (tree.stemTexDapples)
                dapples = i/4;
            else
                dapples = 1;
            for (var j=0; j < 4; j++){
                
       
                if (Engine.rand()<tree.stemTexRatio){
                    var m = Math.sin((Math.PI/2)*l+(Math.PI/2)*(j/4)*dapples)*(Engine.rand()*0.5+0.5);
                    m = 1-((1-m)*0.8);
                    api.lineWidth (0);
                    api.Color (kStrokeColor,  m*r,m*g, m*b);
                    api.Color (kFillColor, m*r,m*g, m*b);
                    var n = Math.floor(Engine.rand()*tree.stemTexPatternLength*0.99999999)+tree.stemTexPatternLength;
                    var points = new Array();


                    for (var k = 0; k < n; k++)           
                        points.push(new Vector3(this.pointsX[j][i+k]*(1+Engine.rand()*tree.stemTexRoughness-tree.stemTexRoughness), this.pointsY[j][i+k]*(1+Engine.rand()*tree.stemTexRoughness-tree.stemTexRoughness), d));

                    points.push(new Vector3(this.pointsX[j][i+k+1]*0.75*(1+Engine.rand()*tree.stemTexRoughness-tree.stemTexRoughness)+this.pointsX[j+1][i+k+1]*0.25*(1+Engine.rand()*tree.stemTexRoughness-tree.stemTexRoughness), this.pointsY[j][i+k+1]*0.75*(1+Engine.rand()*tree.stemTexRoughness-tree.stemTexRoughness)+this.pointsY[j+1][i+k+1]*0.25*(1+Engine.rand()*tree.stemTexRoughness-tree.stemTexRoughness), d));
                    points.push(new Vector3(this.pointsX[j][i+k+2]*0.5*(1+Engine.rand()*tree.stemTexRoughness-tree.stemTexRoughness)+this.pointsX[j+1][i+k+2]*0.5*(1+Engine.rand()*tree.stemTexRoughness-tree.stemTexRoughness), this.pointsY[j][i+k+2]*0.5*(1+Engine.rand()*tree.stemTexRoughness-tree.stemTexRoughness)+this.pointsY[j+1][i+k+2]*0.5*(1+Engine.rand()*tree.stemTexRoughness-tree.stemTexRoughness), d));          
                    points.push(new Vector3(this.pointsX[j][i+k+1]*0.25*(1+Engine.rand()*tree.stemTexRoughness-tree.stemTexRoughness)+this.pointsX[j+1][i+k+1]*0.75*(1+Engine.rand()*tree.stemTexRoughness-tree.stemTexRoughness), this.pointsY[j][i+k+1]*0.25*(1+Engine.rand()*tree.stemTexRoughness-tree.stemTexRoughness)+this.pointsY[j+1][i+k+1]*0.75*(1+Engine.rand()*tree.stemTexRoughness-tree.stemTexRoughness), d));    

                    for (var k = n-1; k >= 0; k--)
                        points.push(new Vector3(this.pointsX[j+1][i+k]*(1+Engine.rand()*tree.stemTexRoughness-tree.stemTexRoughness), this.pointsY[j+1][i+k]*(1+Engine.rand()*tree.stemTexRoughness-tree.stemTexRoughness), d));  

                    points.push(new Vector3(this.pointsX[j][i-1]*0.25*(1+Engine.rand()*tree.stemTexRoughness-tree.stemTexRoughness)+this.pointsX[j+1][i-1]*0.75*(1+Engine.rand()*tree.stemTexRoughness-tree.stemTexRoughness), this.pointsY[j][i-1]*0.25*(1+Engine.rand()*tree.stemTexRoughness-tree.stemTexRoughness)+this.pointsY[j+1][i-1]*0.75*(1+Engine.rand()*tree.stemTexRoughness-tree.stemTexRoughness), d));   
                    points.push(new Vector3(this.pointsX[j][i-2]*0.5*(1+Engine.rand()*tree.stemTexRoughness-tree.stemTexRoughness)+this.pointsX[j+1][i-2]*0.5*(1+Engine.rand()*tree.stemTexRoughness-tree.stemTexRoughness), this.pointsY[j][i-2]*0.5*(1+Engine.rand()*tree.stemTexRoughness-tree.stemTexRoughness)+this.pointsY[j+1][i-2]*0.5*(1+Engine.rand()*tree.stemTexRoughness-tree.stemTexRoughness), d));                     
                    points.push(new Vector3(this.pointsX[j][i-1]*0.75*(1+Engine.rand()*tree.stemTexRoughness-tree.stemTexRoughness)+this.pointsX[j+1][i-1]*0.25*(1+Engine.rand()*tree.stemTexRoughness-tree.stemTexRoughness), this.pointsY[j][i-1]*0.75*(1+Engine.rand()*tree.stemTexRoughness-tree.stemTexRoughness)+this.pointsY[j+1][i-1]*0.25*(1+Engine.rand()*tree.stemTexRoughness-tree.stemTexRoughness), d));

                    api.Polygon(points);
                }
          
            }
        }
    }
    api.command(kpsFinishTask);
    
}




function ModuleB2(tree, light, bx, by, width, height, treeSize, topBottom, distanceFromCenter ,bHeight, branchDirection, b1Length, branchAngle, b1AngleSegment, b1StraightSegment, generation, treeDepth, B1Depth, b1stop, sLength, b1stopped, power, offsetX,offsetY)
{
    this.power = power;

    this.treeSize = treeSize;
    
    totalCalls++; // count number of module calls

    if(b1Length < 10)
        b1Length = 10;

    this.treeDepth = treeDepth;

// how much light the place get
    if (light == 0)
    {
        var light = Math.abs((branchDirection%(Math.PI*2)) - tree.lightDirection);
        if (light > Math.PI){
            light = Math.PI*2 - light;
        }

        light = Math.PI - light;
        light = (light/Math.PI)* height/sLength * 3;
    }

     this.light = light;

    this.distanceFromCenter = distanceFromCenter;
    this.generation = generation;
    generation++;
    
    this.height = height;

    this.branchDirection = branchDirection;

    this.r = tree.leavesR;
    this.g = tree.leavesG;
    this.b = tree.leavesB;
   

    branchAngle = (branchAngle - Math.PI/2) ;


    var direction = 0;
    
    if (tree.b2LowBranchForceUpDown >= 0){
        if (height/sLength < tree.b2LowBranchForceUpDown){
            if (Math.floor((branchAngle+Math.PI)/Math.PI)%2 == 0){
                direction = Engine.rand()*Math.PI + Math.PI*3/2;
            }else{
                direction = Engine.rand()*Math.PI + Math.PI/2;
            }
        }else{ 
            direction = Engine.rand()*Math.PI*2;
        }
    }else{
        if (height/sLength < -tree.b2LowBranchForceUpDown){
            if (Math.floor((branchAngle+Math.PI)/Math.PI)%2 == 0){
                direction = Engine.rand()*Math.PI + Math.PI/2;
            }else{
                direction = Engine.rand()*Math.PI + Math.PI*3/2;
            }
        }else{ 
            direction = Engine.rand()*Math.PI*2;
        }
    }


    var x1 = bx;
    var y1 = by;
    
    if (b1stopped)
        var b2Angle = 0;
    else
        var b2Angle = tree.b2InitialAngle;

    var b2Ratio = 0;
    switch (tree.b2ReduceRatioType)
    {
        case 0:
            b2Ratio = Math.cos((Math.PI/2)*(bHeight/b1Length));
            break;
        case 1:
            b2Ratio = -Math.sqrt(Math.abs(bHeight/b1Length))+1;
            break;
        case 2:
            if (bHeight/b1Length < 2/3)
                b2Ratio = 1;
            else
                b2Ratio = Math.cos((Math.PI/2)*(((bHeight/b1Length)-2/3)*3));
            break;
        case 3:
            b2Ratio = 1;
            break;
        case 4:
            b2Ratio = Engine.rand();
            break;
        case 5:
            b2Ratio = Math.sin(Math.PI*bHeight/b1Length);
            break;
        case 6:
                if(bHeight/b1Length<0.125){
                    b2Ratio = Math.sin(4*Math.PI*bHeight/b1Length);
                }else{
                    b2Ratio = Math.sin((Math.PI)*((7/8)*(bHeight/b1Length+1/7)));   
                }
            break;     
    }

    if(b2Ratio < 0.1)
        b2Ratio = 0.1;
    
    var b2Length = tree.b2LengthRate * b1Length * b2Ratio;
    
    x = x1 + b2Length*Math.sin(b2Angle) * Math.cos(direction);
    y = y1 - b2Length*Math.cos(b2Angle) - b2Length*Math.sin(b2Angle) * Math.sin(direction) * Math.sin(tree.viewAngle);
    var rx = x-x1;
    var ry = y-y1;
    x = rx*Math.cos(branchAngle) - ry*Math.sin(branchAngle);
    y = rx*Math.sin(branchAngle) + ry*Math.cos(branchAngle);

    if (tree.b2ShapeType == 5 || tree.b2ShapeType == 6)
        var b2LengthOnScreen = (Math.sqrt(x*x + y*y) + b2Length) /2;                                // with b2ShapeType = 5, the branch changes the direction to bottom, so the length should be longer 
    else
        var b2LengthOnScreen = Math.sqrt(x*x + y*y) * (1+Math.sin(tree.viewAngle)/4);

    if (x > 0){
        b2InitialAngleOnScreen = Math.atan(y/x)+Math.PI + (Engine.rand()*2-1)*tree.b2InitialAngleRandom;
    }else{
        b2InitialAngleOnScreen = Math.PI - Math.atan(-y/x)+Math.PI + (Engine.rand()*2-1)*tree.b2InitialAngleRandom;
    }


    var side = Math.cos(direction);
    if (this.generation > 1)
        this.topBottom = topBottom;
    else
        this.topBottom = side*topBottom;

    var b2Width = width * tree.b2InitialWidth;
    if (b1stop)
        b2Width = width;

    var depth = Math.abs((this.branchDirection%(Math.PI*2))-Math.PI/2)
    if (depth < Math.PI/2)
        depth = Math.PI-(depth + Math.PI/2);
    else
        depth = depth - Math.PI/2;

    var b2AngleSegment    = b1AngleSegment * tree.b2AngleSegmentRate;
    var b2StraightSegment = b1StraightSegment * tree.b2StraightSegmentRate;

    this.pointsL = new Array;  // points of leaves

    this.points = new Array;
    this.pointsX = new Array;
    this.pointsY = new Array;
    
    this.leafAngle = new Array;

    for (i = 0; i <=3; i++){
        this.points[i] = new Array;
        this.pointsX[i] = new Array;
        this.pointsY[i] = new Array;
    }

    if(this.generation == 1)
        this.branchDetail = 3;
    else
        this.branchDetail = 1;

    var b2_step = (10/tree.d) * this.treeSize * b2LengthOnScreen/b2Length;
    
    var length = 0;
    var b2Angle = b2InitialAngleOnScreen; //Math.PI/2 + (1-Math.sin(Engine.rand()*Math.PI/2) -0.5)*tree.b2InitialAngleRandom;

    var x = bx;
    var y = by;
    
    var x1 = x;
    var y1 = y;

    var nextb2Angle = 1;       // which direction the b2 goes
    if (Engine.rand()>0.5)
        nextb2Angle = -1;
        
    do {  

//        straight part

        var iMax = b2StraightSegment + (Engine.rand()-0.5)*tree.b2StraightSegmentRandom*this.treeSize;

        for(var i = 0; i < iMax; i+= b2_step)
        {
                switch (tree.b2ShapeType)
                {
                    case 0:
                        break;
                    case 1:
                        if (length / b2LengthOnScreen < tree.b2ShapeBeginningPart)
                            b2Angle -= -side*tree.b2ShapeBeginningMagnitude* (1+(Engine.rand()-0.5)*tree.b2ShapeMagnitudeRandom)/(100*tree.d);
                        else
                            b2Angle += -side*tree.b2ShapeMagnitude* (1+(Engine.rand()-0.5)*tree.b2ShapeMagnitudeRandom)/(100*tree.d);
                        break;
                    case 2:
                        var ran = Math.floor((b2Angle+Math.PI/2)/(Math.PI));
                        b2Angle = (b2Angle - ran*Math.PI)/((1+(tree.b2ShapeMagnitude-1)/tree.d) * (1+(Engine.rand()-0.5)*tree.b2ShapeMagnitudeRandom)) + ran*Math.PI;
                        break;
                    case 3:
                        b2Angle += -side*tree.b2ShapeMagnitude* (1+(Engine.rand()-0.5)*tree.b2ShapeMagnitudeRandom)/(100*tree.d);
                        break;
                    case 4:
                        if (length / b2LengthOnScreen < tree.b2ShapeBeginningPart)
                            b2Angle -= -side*tree.b2ShapeBeginningMagnitude*(1-bHeight/b1Length)* (1+(Engine.rand()-0.5)*tree.b2ShapeMagnitudeRandom)/(100*tree.d);
                        else
                            b2Angle += -side*tree.b2ShapeMagnitude*(2+Math.sin(direction))* (1+(Engine.rand()-0.5)*tree.b2ShapeMagnitudeRandom)/(100*tree.d);
                        break;
                    case 5:
                        var b2Angle1 = b2Angle - Math.floor(b2Angle/(Math.PI*2))*(Math.PI*2);
                        b2Angle += Math.PI/2;
                        var ran = Math.floor((b2Angle)/(Math.PI));

                        if (length / b2LengthOnScreen < tree.b2ShapeBeginningPart){
                            b2Angle -= -side*tree.b2ShapeBeginningMagnitude* (1+(Engine.rand()-0.5)*tree.b2ShapeMagnitudeRandom)/(100*tree.d);
                        }else {
                            var m = Math.pow((Math.abs(Math.sin(direction))*4.5),4)+3;

                            var b2AngleAdd = 0.1;
                            if (branchDirection > Math.PI*0.495 && branchDirection < Math.PI*0.505)
                                b2AngleAdd = 0.5;
                            
                            if ((b2Angle1 > 0 && b2Angle1 < Math.PI/2) || (b2Angle1>Math.PI*25/16 && b2Angle1 < Math.PI*2)){

                                b2Angle -= b2AngleAdd;
                                
                            } else if (b2Angle1 >= Math.PI/2 && b2Angle1 < Math.PI * 23/16){

                                b2Angle += b2AngleAdd;
                                
                            }else if (ran%2 == 0){
                                b2Angle += (0.02*tree.b2ShapeMagnitude*m+((Engine.rand()-0.5)*tree.b2ShapeMagnitudeRandom)*0.001)/tree.d;
                            }else{
                                b2Angle -= (0.02*tree.b2ShapeMagnitude*m+((Engine.rand()-0.5)*tree.b2ShapeMagnitudeRandom)*0.001)/tree.d;
                            }
                        }
                        b2Angle -= Math.PI/2;
                        break;
                    case 6:
                        b2Angle += Math.PI/2;
                        var ran = Math.floor((b2Angle)/(Math.PI));
                        if (length / b2LengthOnScreen < tree.b2ShapeBeginningPart)
                            b2Angle -= -side*tree.b2ShapeBeginningMagnitude* (1+(Engine.rand()-0.5)*tree.b2ShapeMagnitudeRandom)/(100*tree.d);
                        else
                        {
                            var m = Math.pow((Math.abs(Math.sin(direction))*4.5),4)+3;
                            if (ran%2 == 0)
                                b2Angle -= (0.01*tree.b2ShapeMagnitude*m+((Engine.rand()-0.5)*tree.b2ShapeMagnitudeRandom)*0.001)/tree.d;
                            else
                                b2Angle += (0.01*tree.b2ShapeMagnitude*m+((Engine.rand()-0.5)*tree.b2ShapeMagnitudeRandom)*0.001)/tree.d;
                        }
                        b2Angle -= Math.PI/2;
                        break;
                }

                var w = branchWidth(length+i, b2LengthOnScreen, b2Width);

                if (length > tree.b2HeightBeforeBranchHappens * b2LengthOnScreen+ tree.b2HeightBeforeBranchHappensRandom * b2LengthOnScreen *(Engine.rand()-0.5)*2   &&Engine.rand() < tree.b2Rate && this.generation < tree.branchGeneration  && length < b2LengthOnScreen)
                    Engine.addModule(new ModuleB2(tree, light, x1,        y1, w, height, treeSize, this.topBottom, distanceFromCenter,length, branchDirection, b2Length, b2Angle,b2AngleSegment, b2StraightSegment, generation, treeDepth, B1Depth,false,  sLength, false, power, offsetX,offsetY));
                x = x1 + Math.cos(b2Angle)*b2_step;
                y = y1 - Math.sin(b2Angle)*b2_step;
                
                var len = Math.sqrt (Math.pow(x1-x,2) + Math.pow(y1-y,2));
                if (len < 0.05)
                    len = 0.05;


   


                var sxL = (y1-y)*w/len;
                var syL = (x1-x)*w/len;
                var sxR = (y1-y)*w/len;
                var syR = (x1-x)*w/len;                   

                for (var j = 0; j <=this.branchDetail; j++){

                    var xx = offsetX + power * ( x1+(-sxL*(this.branchDetail-j)+sxR*j)/(this.branchDetail+(Engine.rand()-0.5)*tree.stemSurfaceNoiseLevel/5));
                    var yy = offsetY + power * (y1+(syL*(this.branchDetail-j)-syR*j)/(this.branchDetail+(Engine.rand()-0.5)*tree.stemSurfaceNoiseLevel/5));
                    this.pointsX[j].push(xx);
                    this.pointsY[j].push(yy);
                    this.points[j].push(new Vector3(xx,yy, B1Depth*10+Math.PI/2 + 0.01 + 0.02*(Engine.rand()-0.5)));

                }
            

                if (tree.leavesStartsGeneration <= this.generation){
                    
                    var c1 = (w/len)*2*this.treeSize*Math.sin(Math.PI*(length+i)/b2LengthOnScreen)*tree.leavesVolume/this.generation;
                    var c2 = (Engine.rand()-0.5)*tree.leavesRandom*this.treeSize/this.generation;
            
                    var sxL = (y1-y)* c1+ c2;
                    var syL = (x1-x)* c1 + c2;
                    var sxR = (y1-y)* c1 + c2;
                    var syR = (x1-x)* c1 + c2;
                    this.pointsL.push(offsetX + power * (x1-sxL), offsetY + power * (y1+syL), B1Depth*10+Math.PI/2 + 0.01 + 0.02*(Engine.rand()-0.5));
                    this.leafAngle.push(b2Angle);
                    if (tree.leavesVolume > 0){  // extra leaves
                        this.pointsL.push(offsetX + power * (x1+sxR),offsetY + power * (y1-syR), B1Depth*10+Math.PI/2 + 0.01 + 0.02*(Engine.rand()-0.5));
                        this.leafAngle.push(b2Angle);
                    }
                    if (tree.leavesVolume > 20){  // extra leaves
                        var sxL = (y1-y)* c1 *0.5 + c2 *0.5;
                        var syL = (x1-x)* c1 *0.5 + c2 *0.5;
                        var sxR = (y1-y)* c1 *0.5 + c2 *0.5;
                        var syR = (x1-x)* c1 *0.5 + c2 *0.5;
                        this.pointsL.push(offsetX + power * (x1-sxL),offsetY + power * (y1+syL), B1Depth*10+Math.PI/2 + 0.01 + 0.02*(Engine.rand()-0.5));
                        this.leafAngle.push(b2Angle);
                        this.pointsL.push(offsetX + power * (x1+sxR),offsetY + power * (y1-syR), B1Depth*10+Math.PI/2 + 0.01 + 0.02*(Engine.rand()-0.5));
                        this.leafAngle.push(b2Angle);
                    }
                            
                }

                
                x1 = x;
                y1 = y;
        }
        length += i;
    
        var rand = Engine.rand()-0.5;
        if ( (tree.b2AngleChangeRate > Engine.rand()  ))        // changing angle?
        {
            var base = (1-Math.sin(Engine.rand()*Math.PI/2))*tree.b2RandomAngle*nextb2Angle;
            
            var sbase = Math.floor((b2AngleSegment + rand*tree.b2AngleSegmentRandom)/b2_step);
            if (sbase == 0)
                sbase = 1;
            var eachb2Angle =  base / sbase;
        }
        else
            var eachb2Angle = 0;
        nextb2Angle *= -1;
        
        var iMax = b2AngleSegment + rand*tree.b2AngleSegmentRandom * this.treeSize;
        if (length + iMax >= b2LengthOnScreen * 0.9)
            break;        



        for(var i = 0; i < iMax; i+= b2_step)
        {
            
                switch (tree.b2ShapeType)
                {
                    case 0:
                        break;
                    case 1:
                        if (length / b2LengthOnScreen < tree.b2ShapeBeginningPart)
                            b2Angle -= -side*tree.b2ShapeBeginningMagnitude* (1+(Engine.rand()-0.5)*tree.b2ShapeMagnitudeRandom)/(100*tree.d);
                        else
                            b2Angle += -side*tree.b2ShapeMagnitude* (1+(Engine.rand()-0.5)*tree.b2ShapeMagnitudeRandom)/(100*tree.d);
                        break;
                    case 2:
                        var ran = Math.floor((b2Angle+Math.PI/2)/(Math.PI));
                        b2Angle = (b2Angle - ran*Math.PI)/((1+(tree.b2ShapeMagnitude-1)/tree.d) * (1+(Engine.rand()-0.5)*tree.b2ShapeMagnitudeRandom)) + ran*Math.PI;
                        break;
                    case 3:
                        b2Angle += -side*tree.b2ShapeMagnitude* (1+(Engine.rand()-0.5)*tree.b2ShapeMagnitudeRandom)/(100*tree.d);
                        break;
                    case 4:
                        if (length / b2LengthOnScreen < tree.b2ShapeBeginningPart)
                            b2Angle -= -side*tree.b2ShapeBeginningMagnitude*(1-bHeight/b1Length)* (1+(Engine.rand()-0.5)*tree.b2ShapeMagnitudeRandom)/(100*tree.d);
                        else
                            b2Angle += -side*tree.b2ShapeMagnitude*(2+Math.sin(direction))* (1+(Engine.rand()-0.5)*tree.b2ShapeMagnitudeRandom)/(100*tree.d);
                        break;
                    case 5:
                        var b2Angle1 = b2Angle - Math.floor(b2Angle/(Math.PI*2))*(Math.PI*2);                    
                        b2Angle += Math.PI/2;
                        var ran = Math.floor((b2Angle)/(Math.PI));

                        if (length / b2LengthOnScreen < tree.b2ShapeBeginningPart){
                            b2Angle -= -side*tree.b2ShapeBeginningMagnitude* (1+(Engine.rand()-0.5)*tree.b2ShapeMagnitudeRandom)/(100*tree.d);
                        }else {
                            var m = Math.pow((Math.abs(Math.sin(direction))*4.5),4)+3;
                            
                            var b2AngleAdd = 0.1;
                            if (branchDirection > Math.PI*0.495 && branchDirection < Math.PI*0.505)
                                b2AngleAdd = 0.5;
                                
                            if (b2Angle1 > 0 && b2Angle1 < Math.PI/2|| (b2Angle1>Math.PI*25/16 && b2Angle1 < Math.PI*2)){

                                b2Angle -= b2AngleAdd;
                            } else if (b2Angle1 >= Math.PI/2 && b2Angle1 < Math.PI * 23/16){

                                b2Angle += b2AngleAdd;
                            }else if (ran%2 == 0){
                                b2Angle += (0.02*tree.b2ShapeMagnitude*m+((Engine.rand()-0.5)*tree.b2ShapeMagnitudeRandom)*0.001)/tree.d;
                            }else{
                                b2Angle -= (0.02*tree.b2ShapeMagnitude*m+((Engine.rand()-0.5)*tree.b2ShapeMagnitudeRandom)*0.001)/tree.d;
                            }
                        }
                        b2Angle -= Math.PI/2;
                        break;
                    case 6:
                        b2Angle += Math.PI/2;
                        var ran = Math.floor((b2Angle)/(Math.PI));
                        if (length / b2LengthOnScreen < tree.b2ShapeBeginningPart)
                            b2Angle -= -side*tree.b2ShapeBeginningMagnitude* (1+(Engine.rand()-0.5)*tree.b2ShapeMagnitudeRandom)/(100*tree.d);
                        else
                        {
                            var m = Math.pow((Math.abs(Math.sin(direction))*4.5),4)+3;
                            if (ran%2 == 0)
                                b2Angle -= (0.01*tree.b1ShapeMagnitude*m+((Engine.rand()-0.5)*tree.b2ShapeMagnitudeRandom)*0.001)/tree.d;
                            else
                                b2Angle += (0.01*tree.b1ShapeMagnitude*m+((Engine.rand()-0.5)*tree.b2ShapeMagnitudeRandom)*0.001)/tree.d;
                        }
                        b2Angle -= Math.PI/2;
                        break;
                        
                }

                
                b2Angle += eachb2Angle;
                x = x1 + Math.cos(b2Angle)*b2_step;
                y = y1 - Math.sin(b2Angle)*b2_step;
                
                var w = branchWidth(length+i, b2LengthOnScreen, b2Width);

                var len = Math.sqrt (Math.pow(x1-x,2) + Math.pow(y1-y,2));
                if (len < 0.05)
                    len = 0.05;
                    

                   
                    
                var sxL = (y1-y)*w/len;
                var syL = (x1-x)*w/len;
                var sxR = (y1-y)*w/len;
                var syR = (x1-x)*w/len;                  

                for (var j = 0; j <=this.branchDetail; j++){

                    var xx = offsetX + power * (x1+(-sxL*(this.branchDetail-j)+sxR*j)/(this.branchDetail+(Engine.rand()-0.5)*tree.stemSurfaceNoiseLevel/5));
                    var yy = offsetY + power * (y1+(syL*(this.branchDetail-j)-syR*j)/(this.branchDetail+(Engine.rand()-0.5)*tree.stemSurfaceNoiseLevel/5));
                    this.pointsX[j].push(xx);
                    this.pointsY[j].push(yy);
                    this.points[j].push(new Vector3(xx,yy, B1Depth*10+Math.PI/2 + 0.01 + 0.02*(Engine.rand()-0.5)));
                }        
            
            
            
                if (tree.leavesStartsGeneration <= this.generation){                    

                    var c1 = (w/len)*2*this.treeSize*Math.sin(Math.PI*(length+i)/b2LengthOnScreen)*tree.leavesVolume/this.generation;
                    var c2 = (Engine.rand()-0.5)*tree.leavesRandom*this.treeSize/this.generation;
            
                    var sxL = (y1-y)* c1 + c2;
                    var syL = (x1-x)* c1 + c2;
                    var sxR = (y1-y)* c1 + c2;
                    var syR = (x1-x)* c1 + c2;
                    this.leafAngle.push(b2Angle);
                    this.pointsL.push(offsetX + power * (x1-sxL), offsetY + power * (y1+syL), B1Depth*10+Math.PI/2 + 0.01 + 0.02*(Engine.rand()-0.5));
                    if (tree.leavesVolume > 0){  // extra leaves
                        this.pointsL.push(offsetX + power * (x1+sxR), offsetY + power * (y1-syR), B1Depth*10+Math.PI/2 + 0.01 + 0.02*(Engine.rand()-0.5));
                        this.leafAngle.push(b2Angle);
                    }
                    if (tree.leavesVolume > 20){  // extra leaves
                        var sxL = (y1-y)*(w/len)* c1*0.5 + c2*0.5;
                        var syL = (x1-x)*(w/len)* c1*0.5 + c2*0.5;
                        var sxR = (y1-y)*(w/len)* c1*0.5 + c2*0.5;
                        var syR = (x1-x)*(w/len)* c1*0.5 + c2*0.5;
                        this.pointsL.push(offsetX + power * (x1-sxL), offsetY + power * (y1+syL), B1Depth*10+Math.PI/2 + 0.01 + 0.02*(Engine.rand()-0.5));
                        this.leafAngle.push(b2Angle);
                        this.pointsL.push(offsetX + power * (x1+sxR), offsetY + power * (y1-syR), B1Depth*10+Math.PI/2 + 0.01 + 0.02*(Engine.rand()-0.5));  
                        this.leafAngle.push(b2Angle);
                    }
                    
                }



                x1 = x;
                y1 = y;
        }

        length += i;

    }while ( length < b2LengthOnScreen)

    if (this.pointsX[0].length < 2)
        this.draw = false;
    else{
        this.draw = true;    
        for( var j = 0; j < this.branchDetail; j++){
            for (var i = this.points[j+1].length-1; i >= 0 ; i--)
                this.points[j].push(this.points[j+1][i]);
        }
    }

}




ModuleB2.prototype.produce = function(system)
{
}

ModuleB2.prototype.render = function(api, env)
{

    api.command(kpsStartTask,1/totalCalls );
    totalCalls--;
    
    if (!this.draw)
    {
        api.command(kpsFinishTask)
        return
    }
     
    for(var j=0; j< leavesShapeN[tree.leavesPattern]; j++){
        api.pushMatrix();
        frame = new Frame3();
        api.setFrame(frame);

        if (!api.instanceExists("leaf"+j))
        {

            if (api.defineInstance("leaf"+j)) 
            {
                api.Polygon(leaves[tree.leavesPattern][j]);                    
                api.endInstance();
            }
        }
        api.popMatrix();
    }
    

    var darken = (((this.light*((this.distanceFromCenter+3)/4)+(Engine.rand()-0.5)*0.05)*((this.topBottom+3)/4))*0.9+0.1);
    darken = 1-(1-darken)/1.25;
    
    var lr = this.r*darken;
    var lg = this.g*darken;
    var lb = this.b*darken;

    if(tree.makeBranchLeaves){

        if(tree.noiseLeaves)
            darken *= 1+(Engine.rand()-0.5)/2.5;   
                    
        var r =lr*darken;
        var g = lg*darken;
        var b = lb*darken;  
    }else if(tree.stemTexShading){
        var r = tree.stemR*(1-tree.leavesRate*0.25);
        var g = tree.stemG*(1-tree.leavesRate*0.25);
        var b = tree.stemB*(1-tree.leavesRate*0.25);
    }else{
        var r = tree.stemR;
        var g = tree.stemG;
        var b = tree.stemB;
    }

    var dapples = 1;
    
    var l = Math.floor((this.branchDirection+Math.PI/2)/Math.PI)%2;
    


    for (var i =0; i < this.branchDetail; i++){



        if((tree.stemTexShading  && !tree.makeBranchLeaves) || (tree.makeBranchLeaves && tree.leavesGroupShading)){
            if(this.branchDetail != 1)
                var m = Math.abs((i-2*l)/(this.branchDetail-1));
            else
                var m = 0.5;
        }else{
            if(tree.stemsColor)
                var m = 1;
            else
                var m = 0.5;
        }
    
        if(tree.makeBranchLeaves)
            m = 1-((1-m)*0.4);
        else
            m = 1-((1-m)*0.8);
    
    
        api.lineWidth (0.001);
        api.Color (kStrokeColor,  m*r,m*g,m*b);
        api.Color (kFillColor, m*r,m*g,m*b);
        api.Polygon( this.points[i] );

    } 

    
    if (tree.leavesStartsGeneration <= this.generation){

        api.lineWidth (0.01);
        api.Color(kStrokeColor, lr,lg,lb);
        api.Color (kFillColor, lr,lg,lb);
        
        var maxY = -10000;
        var minY = 10000;
        
        if (tree.leavesGroupShading)
        {
            for(i = 0; i < this.pointsL.length/3; i++)
            {
                if(maxY < this.pointsL[i*3+1])
                    maxY = this.pointsL[i*3+1];
                if(minY > this.pointsL[i*3+1])
                    minY = this.pointsL[i*3+1];
            }
            var rate = 1/(maxY-minY);
            var middleY = (maxY+minY)/2;
        }


        for(i = 0; i < this.pointsL.length/3; i++)
        {
            
            if (tree.leavesGroupShading)
            {
                var pointY = this.pointsL[i*3+1];

                var darken = ((maxY-pointY)*rate);
                darken = (1-(1-darken)/tree.leavesShade)*(0.5+tree.leavesHighlight)+0.5;;
         
                if(tree.noiseLeaves)
                    darken *= 1+(Engine.rand()-0.5)/2.5;         
         
                lr = lr*darken;
                lg = lg*darken;
                lb = lb*darken;

                api.Color(kStrokeColor, lr,lg,lb);
                api.Color (kFillColor, lr,lg,lb);

            }


            if (Engine.rand()<tree.leavesRate)
            {         
                
                api.pushMatrix();
                api.translate(this.pointsL[i*3], this.pointsL[i*3+1], this.pointsL[i*3+2]+0.01)

                api.rotate(this.leafAngle[Math.floor(i)]*180/Math.PI+90 +(Engine.rand()-0.5)*tree.leavesDirectionRandomness, (Engine.rand()-0.5)*tree.leavesDirectionRandomness, (Engine.rand()-0.5)*tree.leavesDirectionRandomness,1);

                if(tree.usePath)
                    api.scale(tree.leavesSize * this.treeSize);
                else
                    api.scale(tree.leavesSize * this.power * 1.7);

                api.drawInstance("leaf"+Math.floor(Engine.rand()*leavesShapeN[tree.leavesPattern]*0.9999)); ///////////////////////////////////////////////////   instancing
                api.popMatrix();
            }
        }
    api.command(kpsFinishTask);     // progress bar
    }
}








function noise(n,s, surfaceNoise, level, d)
{

    var m = Math.sin(n/27+surfaceNoise.offset)*20;
    var m1 = Math.sin((n+m)/(40*s)+surfaceNoise.offset)/7;
    
    surfaceNoise.v += surfaceNoise.c;
    if (surfaceNoise.v >= 1)
    {
        surfaceNoise.v = 1;
        surfaceNoise.c = -Engine.rand()/(4*d);
    } else if (surfaceNoise.v <= -1) {
        surfaceNoise.v = -1;
        surfaceNoise.c = Engine.rand()/(4*d);
    } else if (Engine.rand() > 0.98){
        surfaceNoise.c = (Engine.rand()-0.5)/(1*d);
    }
    
    return((m1*level+1)*(level*surfaceNoise.v/12+1));
}

function treeWidth(height, treeLength, initialWidth, stumpHeight, stumpShape, stumpShapeRatio)
{

    if (height <= stumpHeight){

        switch (stumpShape)
        {
            case 1:
                var w = initialWidth+stumpShapeRatio*Math.pow((stumpHeight-height)/stumpHeight,2);
                break;
            case 2:
                var w = initialWidth+stumpShapeRatio*(-(Math.cos(Math.PI*(stumpHeight-height)/stumpHeight)-1)/2);
                break;
            case 3:
                var w = initialWidth+stumpShapeRatio*(stumpHeight-height)/stumpHeight;
                break;
            
        }    
        return (w);
        
    }else{
        var w = ((treeLength-height)/(treeLength-stumpHeight))*initialWidth;
        if (w<0.2)
            w = 0.2;
        return (w);
    }
}


function branchWidth(currentLength, branchLength, initialWidth)
{
    if(branchLength < 0.05)
        branchLength = 0.05;

    var w = ((branchLength-currentLength)/(branchLength))*initialWidth;
    if (w<0.2)
        w = 0.2;
    return (w);

}


var previewSize = 512;


function run (api, parameters, scale)
{

    api.command(kpsStartTask,0.7);   // progress bar start

    // get the size of the output area  
    var outputSize = api.getParameter(kpsSize);
    var outputOrigin = api.getParameter(kpsOrigin);
    var visibleSize = api.getParameter(kpsVisibleSize);
    var visibleOrigin = api.getParameter(kpsVisibleOrigin);
    
    // get the location of the top left corner of the bounding rectangle around the selected area

    switch (parameters.mTreeType )
    {

        case 0:                 //Oak Tree
            tree = {
                
                name : "oak",
                stemIntensity : 2,
                leavesIntensity : 2,

                viewAngle : 5    *Math.PI/180,    //   0... from side      90... from top

                s : 1.05,    // scale      it effects everything
                d : 2,    // detail     how much detail
                
                dimensionRatio : 11.5/12,       //   y/x
                
                lightDirection : (180 + 180)      *Math.PI/180,      // light direction

                sLength : 1000,                 // if s = 1, this is pixel
                sLengthStop : 0.5,              // If this is less than 1, it stops growing.  , then 1st branch continues.
                sLengthStopDiverse : 0.5,    // if this is not zero, 3 branches happen.  and it splits based on this number.
                sInitialWidth : 22,                 // stump initial width

                stumpHeight : 50,       // stump part
                stumpShape : 2,          //   stump shape  1-3
                stumpShapeRatio : 4,    //  how much it is thicker on the bottom

                stemSegmentNumber : 400,    // how many segments on the tree?
                stemAngleSegmentRatio : 0.5,  // stem angle segment : stem straight segment

                stemAngleSegmentRandom : 0.5,      // how much change on stemAngleSegment (longer/shorter)
                stemStraightSegmentRandom : 0.5,   // how much change on stemStraightSegment (longer/shorter)

                stemInitialAngleRandom : 0         *Math.PI/180,            //

                stemRandomAngle : 10     *Math.PI/180,
                stemMaxAngle : 5           *Math.PI/180,                     // if stem angle becomes bigger than this, next angle will change to go up
                stemAngleChangeRate : 0.5,                                           // how often the angle changes?   0... no change   1.. always change

                stemHeightBeforeAngleChange : 50,                               // Stem goes straight before this height
                stemHeightBeforeAngleChangeRandom : 100,                     // plus minus this value
                
                stemSurfaceNoiseLevel : 1,                                               // less or more bumps

                stemHeightBeforeBranch : 120,
                stemHeightbeforePalmBump : 100000,
                
                stemHeightOfShadow : 0.25,                                          // leaves cast shadow on the stem higher than this height
                
                
                b1LengthRate : 0.4,                                                          // If this is 0.5, length of the first branch is 50% of sLength
                b1LengthStop : 0.8,                                                     // If this is less than 1, it stops growing. , then 2nd branch continues.
                b1LengthStopDiverse : 0.5,                                          // if this is not zero, 3 second branches happen.  and it splits based on this number.
                b1ReduceRatioType : 0,                                                      // How the first branches change their length based on the stem height 0 - 6
                b1InitialWidth : 0.5,                                                    //  based on the stem width
                
                b1Rate : 1,                                                              // 
                b1AngleSegmentRate : 0.2,
                b1StraightSegmentRate : 0.2,
                b1AngleSegmentRandom : 0.5,
                b1StraightSegmentRandom : 0.5,

                b1ShapeType : 4,                                                        // branch shape type 0-6
                b1ShapeMagnitude : 0.75,                                                // how strongly the branch changes its shape angle
                b1ShapeMagnitudeRandom : 0.0,
                b1ShapeBeginningPart : 0.13,
                b1ShapeBeginningMagnitude : 28,

                b1RandomAngle : 15    *Math.PI/180,
                b1AngleChangeRate : 0.5,                                              // 1 everytime, 0 non

                b1DirectionRandom : 0.02 ,                                            // direction randomness from golden angle

                b1InitialAngle : 3                  *Math.PI/180,          // branch initial angle when the branch is low
                b1InitialAngleChangeRate : 0,                                    // if this is >0, branch angle will change based on its height
                b1InitialAngleRandom : 0       *Math.PI/180,          // branch initial angle randomize
                
                b1HeightBeforeBranchHappens : 0,
                b1HeightBeforeBranchHappensRandom : 0,

                b1AngleMax : 5 *Math.PI/180,                            // 0 is holizontal angle.  if b1 branch is corrected if it becomes more than this number.  the number of willow trees should be 90

                b1CenterForce : 0,                                       // when it needs more leaves to cover the stem

                b2LengthRate : 0.475,
                b2ReduceRatioType : 2,                                                      // How the first branches change their length based on the stem height 0 - 6    
                b2InitialWidth : 0.5,   
                
                b2Rate : 0.20,
                b2AngleSegmentRate : 0.1,
                b2StraightSegmentRate : 0.1,
                b2AngleSegmentRandom : 0,
                b2StraightSegmentRandom : 0,

                b2ShapeType : 4,                                                        // branch shape type 0-6
                b2ShapeMagnitude : 1,                                                // how strongly the branch changes its shape angle
                b2ShapeMagnitudeRandom : 0,
                b2ShapeBeginningPart : 0.01,
                b2ShapeBeginningMagnitude : 28,

                b2RandomAngle : 30    *Math.PI/180,
                b2AngleChangeRate : 0.6,                                              // 1 everytime, 0 non

                b2InitialAngle : 25                  *Math.PI/180,          // branch initial angle when the branch is low
                b2InitialAngleRandom : 0       *Math.PI/180,          // branch initial angle randomize

                b2HeightBeforeBranchHappens : 0,
                b2HeightBeforeBranchHappensRandom : 0,

                b2LowBranchForceUpDown : 0.4,                       // 0.. normal    if >0, some lower part b2 branch goes up  (if =1, every branch goes up),  if <0, some lower part b2 branch goes down

                branchGeneration : 3,
                
                leavesStartsGeneration : 2,
                
                leavesVolume : 75,
                leavesRandom : 5,
                leavesRate : 1,               // how much leaves
                
                leavesPattern : 7,
                
                leavesSize : 0.4,                 // leaves size
                
                leavesGroupShading : true,                  //  in a small group, it shades accordingly
                leavesHighlight : 0.09,              // if this is >0, some leaves would be brighter than the original color
                leavesShade : 3.75,                // leaves brightness (brighter -> less contrast)  default: 3.75

                leavesDirectionRandomness : 360,     // leaves direction   if 0, leaves don't rotate.  if 360, totally random

                leavesR : 108/256,
                leavesG : 131/256,
                leavesB : 59/256,
                
                makeBranchLeaves : false,            // branches become leaves color

                stemTexShading : true,
                stemTexRandomness : 0.5,
                stemTexStripeRatio : 0.5,               // texture stripe ratio  0.. non,   1.. max    the stripes would be covered by the other texture
                stemTexRatio : 0.25,                         // rough texture on the stem
                stemTexDapples : true,                   // dapples on the texture
                stemTexRoughness : 0.002,            // texture roughness    default should be 0.002
                stemTexPatternLength : 3,               // vertical texture length

                stemHTexRate : 0.4,
                stemHTexRoughness : 0.001,           // holizontal texture roughness    default should be 0.001
                stemHTexOpacity : 0,                   // holizontal texture opacity
                stemHTexSize : 15,                          // 3 - 15
          
                stemR : 137/256,                        // stem color
                stemG : 130/256,
                stemB : 101/256,
                
                baseArrangement : 0,
                
            };
            break;

        case 1:                 //Redwood
            tree = {
                
                name : "redwood",
                stemIntensity : 3,
                leavesIntensity : 4.5,

                viewAngle : 5    *Math.PI/180,    //   0... from side      90... from top

                s : 0.9,    // scale      it effects everything
                d : 3.1,    // detail     how much detail

                dimensionRatio : 11.3/5,       //   y/x

                lightDirection : (180 + 180)      *Math.PI/180,      // light direction

                sLength : 1000,                 // if s = 1, this is pixel
                sLengthStop : 0.98,              // If this is less than 1, it stops growing.  , then 1st branch continues.
                sLengthStopDiverse : 0,    // if this is not zero, 3 branches happen.  and it splits based on this number.
                sInitialWidth : 18,                 // stump initial width

                stumpHeight : 70,       // stump part
                stumpShape : 1,          //   stump shape  1-3
                stumpShapeRatio : 15,    //  how much it is thicker on the bottom

                stemSegmentNumber : 400,    // how many segments on the tree?
                stemAngleSegmentRatio : 0.5,  // stem angle segment : stem straight segment

                stemAngleSegmentRandom : 0.5,      // how much change on stemAngleSegment (longer/shorter)
                stemStraightSegmentRandom : 0.5,   // how much change on stemStraightSegment (longer/shorter)

                stemInitialAngleRandom : 0         *Math.PI/180,            //

                stemRandomAngle : 0     *Math.PI/180,
                stemMaxAngle : 0           *Math.PI/180,                     // if stem angle becomes bigger than this, next angle will change to go up
                stemAngleChangeRate : 0.5,                                           // how often the angle changes?   0... no change   1.. always change

                stemHeightBeforeAngleChange : 250,                               // Stem goes straight before this height
                stemHeightBeforeAngleChangeRandom : 100,                     // plus minus this value
                
                stemSurfaceNoiseLevel : 0.2,                                               // less or more bumps

                stemHeightBeforeBranch : 250,
                stemHeightbeforePalmBump : 100000,

                stemHeightOfShadow : 0.25,                                          // leaves cast shadow on the stem higher than this height


                b1LengthRate : 0.275,                                                          // If this is 0.5, length of the first branch is 50% of sLength
                b1LengthStop : 0.4,                                                     // If this is less than 1, it stops growing. , then 2nd branch continues.
                b1LengthStopDiverse : 0,                                          // if this is not zero, 3 second branches happen.  and it splits based on this number.
                b1ReduceRatioType : 0,                                                      // How the first branches change their length based on the stem height 0 - 6
                b1InitialWidth : 0.3,                                                    //  based on the stem width
                
                b1Rate : 1.5,                                                              // 
                b1AngleSegmentRate : 0.2,
                b1StraightSegmentRate : 0.2,
                b1AngleSegmentRandom : 0.5,
                b1StraightSegmentRandom : 0.5,

                b1ShapeType : 3,                                                        // branch shape type 0-6
                b1ShapeMagnitude : 0.75,                                                // how strongly the branch changes its shape angle
                b1ShapeMagnitudeRandom : 0.0,
                b1ShapeBeginningPart : 0.2,
                b1ShapeBeginningMagnitude : 28,

                b1RandomAngle : 5    *Math.PI/180,
                b1AngleChangeRate : 0.5,                                              // 1 everytime, 0 non

                b1DirectionRandom : 0.1 ,                                            // direction randomness from golden angle

                b1InitialAngle : 130                  *Math.PI/180,          // branch initial angle when the branch is low
                b1InitialAngleChangeRate : 0.7,                                    // if this is >0, branch angle will change based on its height
                b1InitialAngleRandom : 0       *Math.PI/180,          // branch initial angle randomize
                
                b1HeightBeforeBranchHappens : 0,
                b1HeightBeforeBranchHappensRandom : 0,

                b1AngleMax : 5 *Math.PI/180,                            // 0 is holizontal angle.  if b1 branch is corrected if it becomes more than this number.  the number of willow trees should be 90

                b1CenterForce : 0.2,                                       // when it needs more leaves to cover the stem

                b2LengthRate : 0.5,
                b2ReduceRatioType : 2,                                                      // How the first branches change their length based on the stem height 0 - 6    
                b2InitialWidth : 0.5,   
                
                b2Rate : 0.2,
                b2AngleSegmentRate : 0.1,
                b2StraightSegmentRate : 0.1,
                b2AngleSegmentRandom : 0,
                b2StraightSegmentRandom : 0,

                b2ShapeType : 4,                                                        // branch shape type 0-6
                b2ShapeMagnitude : 1,                                                // how strongly the branch changes its shape angle
                b2ShapeMagnitudeRandom : 0,
                b2ShapeBeginningPart : 0.01,
                b2ShapeBeginningMagnitude : 28,

                b2RandomAngle : 30    *Math.PI/180,
                b2AngleChangeRate : 0.6,                                              // 1 everytime, 0 non

                b2InitialAngle : 25                  *Math.PI/180,          // branch initial angle when the branch is low
                b2InitialAngleRandom : 0       *Math.PI/180,          // branch initial angle randomize

                b2HeightBeforeBranchHappens : 0,
                b2HeightBeforeBranchHappensRandom : 0,

                b2LowBranchForceUpDown : 0.3,                       // 0.. normal    if >0, some lower part b2 branch goes up  (if =1, every branch goes up),  if <0, some lower part b2 branch goes down

                branchGeneration : 2,
                
                leavesStartsGeneration : 0,
                
                leavesVolume : 15,
                leavesRandom : 5,
                leavesRate : 1,               // how much leaves
                
                leavesPattern : 6,
                
                leavesSize : 0.225,                 // leaves size
                
                leavesGroupShading : true,                  //  in a small group, it shades accordingly
                leavesHighlight : 0.08,              // if this is >0, some leaves would be brighter than the original color
                leavesShade : 3.75,                // leaves brightness (brighter -> less contrast)  default: 3.75

                leavesDirectionRandomness : 360,     // leaves direction   if 0, leaves don't rotate.  if 360, totally random

                leavesR : 52/256,
                leavesG : 56/256,
                leavesB : 37/256,
                
                makeBranchLeaves : false,            // branches become leaves color

                stemTexShading : true,
                stemTexRandomness : 0.5,
                stemTexStripeRatio : 0.5,               // texture stripe ratio  0.. non,   1.. max    the stripes would be covered by the other texture
                stemTexRatio : 0.25,                         // rough texture on the stem
                stemTexDapples : true,                   // dapples on the texture
                stemTexRoughness : 0.002,            // texture roughness    default should be 0.002
                stemTexPatternLength : 3,               // vertical texture length

                stemHTexRate : 0.4,
                stemHTexRoughness : 0.001,           // holizontal texture roughness    default should be 0.001
                stemHTexOpacity : 0.0,                   // holizontal texture opacity
                stemHTexSize : 4,                          // 3 - 15
          
                stemR : 103/256,                        // stem color
                stemG : 81/256,
                stemB : 58/256,
                
                baseArrangement : 2,                
                
            };
            break;

        case 2:                 // ginko tree
            tree = {

                name : "ginko",
                stemIntensity : 3,
                leavesIntensity : 3.4,

                viewAngle : 5    *Math.PI/180,    //   0... from side      90... from top

                s : 0.95,    // scale      it effects everything
                d : 1.8,    // detail     how much detail

                dimensionRatio : 11.3/9.5,       //   y/x

                lightDirection : (  180 + 180)      *Math.PI/180,      // light direction

                sLength : 900,       // if s = 1, this is pixel
                sLengthStop : 0.99,        // If this is less than 1, it stops growing.  (not very spercific) , then 1st branch continues on the end.
                sLengthStopDiverse : 0,    // if this is not zero, 3 branches happen.  and it splits based on this number.
                sInitialWidth : 18,     // stump initial width

                stumpHeight : 80,       // stump part
                stumpShape : 1,          //   stump shape  1-3
                stumpShapeRatio : 10,    //  how much it is thicker on the bottom

                stemSegmentNumber : 400,    // how many segments on the tree?
                stemAngleSegmentRatio : 0.5,  // stem angle segment : stem straight segment

                stemAngleSegmentRandom : 0.5,      // how much change on stemAngleSegment (longer/shorter)
                stemStraightSegmentRandom : 0.5,   // how much change on stemStraightSegment (longer/shorter)

                stemInitialAngleRandom : 0         *Math.PI/180,            //

                stemRandomAngle : 5     *Math.PI/180,
                stemMaxAngle : 2           *Math.PI/180,                     // if stem angle becomes bigger than this, next angle will change to go up
                stemAngleChangeRate : 0.5,                                           // how often the angle changes?   0... no change   1.. always change

                stemHeightBeforeAngleChange : 50,                               // Stem goes straight before this height
                stemHeightBeforeAngleChangeRandom : 100,                     // plus minus this value
                
                stemSurfaceNoiseLevel : 0.25,                                               // less or more bumps

                stemHeightBeforeBranch : 120,
                stemHeightbeforePalmBump : 100000,
                
                stemHeightOfShadow : 0.25,                                          // leaves cast shadow on the stem higher than this height

                
                b1LengthRate : 0.5,                                                          // If this is 0.5, length of the first branch is 50% of sLength
                b1LengthStop : 0.8,                                                     // If this is less than 1, it stops growing. , then 2nd branch continues.
                b1LengthStopDiverse : 0.5,                                          // if this is not zero, 3 second branches happen.  and it splits based on this number.
                b1ReduceRatioType : 6,                                                      // How the first branches change their length based on the stem height 0 - 6
                b1InitialWidth : 0.3,                                                    //  based on the stem width
                
                b1Rate : 1.2,                                                              // 
                b1AngleSegmentRate : 0.2,
                b1StraightSegmentRate : 0.2,
                b1AngleSegmentRandom : 1,
                b1StraightSegmentRandom : 1,

                b1ShapeType : 0,                                                        // branch shape type 0-6
                b1ShapeMagnitude : 2,                                                // how strongly the branch changes its shape angle
                b1ShapeMagnitudeRandom : 0.0,
                b1ShapeBeginningPart : 0.05,
                b1ShapeBeginningMagnitude : 28,

                b1RandomAngle : 20    *Math.PI/180,
                b1AngleChangeRate : 0.5,                                              // 1 everytime, 0 non

                b1DirectionRandom : 2 ,                                            // direction randomness from golden angle

                b1InitialAngle : 75                  *Math.PI/180,          // branch initial angle when the branch is low
                b1InitialAngleChangeRate : 0.7,                                    // if this is >0, branch angle will change based on its height
                b1InitialAngleRandom : 0       *Math.PI/180,          // branch initial angle randomize
                
                b1HeightBeforeBranchHappens : 0.1,
                b1HeightBeforeBranchHappensRandom : 0.2,

                b1AngleMax : 5 *Math.PI/180,                            // 0 is holizontal angle.  if b1 branch is corrected if it becomes more than this number.  the number of willow trees should be 90

                b1CenterForce : 0.3,                                       // when it needs more leaves to cover the stem

                b2LengthRate : 0.375,
                b2ReduceRatioType : 0,                                                      // How the first branches change their length based on the stem height 0 - 6    
                b2InitialWidth : 0.5,     
                
                b2Rate : 0.4,
                b2AngleSegmentRate : 0.15,
                b2StraightSegmentRate : 0.15,
                b2AngleSegmentRandom : 0.05,
                b2StraightSegmentRandom : 0.05,

                b2ShapeType : 0,                                                        // branch shape type 0-6
                b2ShapeMagnitude : 2,                                                // how strongly the branch changes its shape angle
                b2ShapeMagnitudeRandom : 0,
                b2ShapeBeginningPart : 0.2,
                b2ShapeBeginningMagnitude : 28,

                b2RandomAngle : 30    *Math.PI/180,
                b2AngleChangeRate : 0.6,                                              // 1 everytime, 0 non

                b2InitialAngle : 35                  *Math.PI/180,          // branch initial angle when the branch is low
                b2InitialAngleRandom : 0       *Math.PI/180,          // branch initial angle randomize

                b2HeightBeforeBranchHappens : 0.1,
                b2HeightBeforeBranchHappensRandom : 0.1,

                b2LowBranchForceUpDown : 0.3,                       // 0.. normal    if >0, some lower part b2 branch goes up  (if =1, every branch goes up),  if <0, some lower part b2 branch goes down

                branchGeneration : 3,
                
                leavesStartsGeneration : 2,
                
                leavesVolume : 30,
                leavesRandom : 20,
                leavesRate : 0,               // how much leaves
                
                leavesPattern : 2,
                
                leavesSize : 0.8,                 // leaves size 
                
                leavesGroupShading : true,                  //  in a small group, it shades accordingly
                leavesHighlight : 0.09,              // if this is >0, some leaves would be brighter than the original color
                leavesShade : 3.9,                // leaves brightness (brighter -> less contrast)  default: 3.75

                leavesDirectionRandomness : 360,     // leaves direction   if 0, leaves don't rotate.  if 360, totally random

                leavesR : 108/256,
                leavesG : 131/256,
                leavesB : 59/256,
                
                makeBranchLeaves : false,            // branches become leaves color
                
                stemTexShading : true,
                stemTexRandomness : 0.5,
                stemTexStripeRatio : 0.25,               // texture stripe ratio  0.. non,   1.. max    the stripes would be covered by the other texture
                stemTexRatio : 0.5,                         // rough texture on the stem
                stemTexDapples : false,                   // dapples on the texture
                stemTexRoughness : 0.002,            // texture roughness    default should be 0.002
                stemTexPatternLength : 3,               // vertical texture length

                stemHTexRate : 0,
                stemHTexRoughness : 0.001,           // holizontal texture roughness    default should be 0.001
                stemHTexOpacity : 0,                   // holizontal texture opacity
                stemHTexSize : 15,                          // 3 - 15

                stemR : 133/256,                        // stem color
                stemG : 116/256,
                stemB : 76/256,   
                
                baseArrangement : 1,
                
            };
            break;

        case 3:                 // maple tree
            tree = {
                
                name : "maple",
                stemIntensity : 2,
                leavesIntensity : 2.6,
                
                viewAngle : 5    *Math.PI/180,    //   0... from side      90... from top

                s : 1.05,    // scale      it effects everything
                d : 1.5,    // detail     how much detail

                dimensionRatio : 11/9,       //   y/x

                lightDirection : (  180 + 180)      *Math.PI/180,      // light direction

                sLength : 900,       // if s = 1, this is pixel
                sLengthStop : 0.7,        // If this is less than 1, it stops growing.  (not very spercific) , then 1st branch continues on the end.
                sLengthStopDiverse : 0,    // if this is not zero, 3 branches happen.  and it splits based on this number.
                sInitialWidth : 16,     // stump initial width

                stumpHeight : 80,       // stump part
                stumpShape : 1,          //   stump shape  1-3
                stumpShapeRatio : 10,    //  how much it is thicker on the bottom

                stemSegmentNumber : 400,    // how many segments on the tree?
                stemAngleSegmentRatio : 0.5,  // stem angle segment : stem straight segment

                stemAngleSegmentRandom : 0.5,      // how much change on stemAngleSegment (longer/shorter)
                stemStraightSegmentRandom : 0.5,   // how much change on stemStraightSegment (longer/shorter)

                stemInitialAngleRandom : 0         *Math.PI/180,            //

                stemRandomAngle : 5     *Math.PI/180,
                stemMaxAngle : 2           *Math.PI/180,                     // if stem angle becomes bigger than this, next angle will change to go up
                stemAngleChangeRate : 0.5,                                           // how often the angle changes?   0... no change   1.. always change

                stemHeightBeforeAngleChange : 50,                               // Stem goes straight before this height
                stemHeightBeforeAngleChangeRandom : 100,                     // plus minus this value
                
                stemSurfaceNoiseLevel : 0.25,                                               // less or more bumps

                stemHeightBeforeBranch : 140,
                stemHeightbeforePalmBump : 100000,
                
                stemHeightOfShadow : 0.25,                                          // leaves cast shadow on the stem higher than this height

                
                b1LengthRate : 0.5,                                                          // If this is 0.5, length of the first branch is 50% of sLength
                b1LengthStop : 0.8,                                                     // If this is less than 1, it stops growing. , then 2nd branch continues.
                b1LengthStopDiverse : 0.5,                                          // if this is not zero, 3 second branches happen.  and it splits based on this number.
                b1ReduceRatioType : 0,                                                      // How the first branches change their length based on the stem height 0 - 6
                b1InitialWidth : 0.3,                                                    //  based on the stem width
                
                b1Rate : 1.1,                                                              // 
                b1AngleSegmentRate : 0.2,
                b1StraightSegmentRate : 0.2,
                b1AngleSegmentRandom : 1,
                b1StraightSegmentRandom : 1,

                b1ShapeType : 3,                                                        // branch shape type 0-6
                b1ShapeMagnitude : 2,                                                // how strongly the branch changes its shape angle
                b1ShapeMagnitudeRandom : 0.0,
                b1ShapeBeginningPart : 0.05,
                b1ShapeBeginningMagnitude : 14,

                b1RandomAngle : 15    *Math.PI/180,
                b1AngleChangeRate : 1,                                              // 1 everytime, 0 non

                b1DirectionRandom : 2 ,                                            // direction randomness from golden angle

                b1InitialAngle : 70                  *Math.PI/180,          // branch initial angle when the branch is low
                b1InitialAngleChangeRate : 0.7,                                    // if this is >0, branch angle will change based on its height
                b1InitialAngleRandom : 0       *Math.PI/180,          // branch initial angle randomize
                
                b1HeightBeforeBranchHappens : 0.05,
                b1HeightBeforeBranchHappensRandom : 0.1,

                b1AngleMax : 5 *Math.PI/180,                            // 0 is holizontal angle.  if b1 branch is corrected if it becomes more than this number.  the number of willow trees should be 90

                b1CenterForce : 1,                                       // when it needs more leaves to cover the stem

                b2LengthRate : 0.5,
                b2ReduceRatioType : 0,                                                      // How the first branches change their length based on the stem height 0 - 6    
                b2InitialWidth : 0.5,     
                
                b2Rate : 0.4,
                b2AngleSegmentRate : 0.15,
                b2StraightSegmentRate : 0.15,
                b2AngleSegmentRandom : 0.05,
                b2StraightSegmentRandom : 0.05,

                b2ShapeType : 1,                                                        // branch shape type 0-6
                b2ShapeMagnitude : 1,                                                // how strongly the branch changes its shape angle
                b2ShapeMagnitudeRandom : 0,
                b2ShapeBeginningPart : 0.1,
                b2ShapeBeginningMagnitude : 5,

                b2RandomAngle : 15    *Math.PI/180,
                b2AngleChangeRate : 0.5,                                              // 1 everytime, 0 non

                b2InitialAngle : 25                  *Math.PI/180,          // branch initial angle when the branch is low
                b2InitialAngleRandom : 0       *Math.PI/180,          // branch initial angle randomize

                b2HeightBeforeBranchHappens : 0.2,
                b2HeightBeforeBranchHappensRandom : 0.1,

                b2LowBranchForceUpDown : 0,                       // 0.. normal    if >0, some lower part b2 branch goes up  (if =1, every branch goes up),  if <0, some lower part b2 branch goes down

                branchGeneration : 3,
                
                leavesStartsGeneration : 2,
                
                leavesVolume : 30,
                leavesRandom : 20,
                leavesRate : 0,               // how much leaves
                
                leavesPattern : 1,
                
                leavesSize : 0.8,                 // leaves size 
                
                leavesGroupShading : true,                  //  in a small group, it shades accordingly
                leavesHighlight : 0.1,              // if this is >0, some leaves would be brighter than the original color
                leavesShade : 3.75,                // leaves brightness (brighter -> less contrast)  default: 3.75

                leavesDirectionRandomness : 360,     // leaves direction   if 0, leaves don't rotate.  if 360, totally random

                leavesR : 108/256,
                leavesG : 131/256,
                leavesB : 59/256,
                
                makeBranchLeaves : false,            // branches become leaves color
                
                stemTexShading : true,
                stemTexRandomness : 0.5,
                stemTexStripeRatio : 0.25,               // texture stripe ratio  0.. non,   1.. max    the stripes would be covered by the other texture
                stemTexRatio : 0.5,                         // rough texture on the stem
                stemTexDapples : false,                   // dapples on the texture
                stemTexRoughness : 0.002,            // texture roughness    default should be 0.002
                stemTexPatternLength : 3,               // vertical texture length

                stemHTexRate : 0,
                stemHTexRoughness : 0.001,           // holizontal texture roughness    default should be 0.001
                stemHTexOpacity : 0,                   // holizontal texture opacity
                stemHTexSize : 15,                          // 3 - 15

                stemR : 133/256,                        // stem color
                stemG : 116/256,
                stemB : 76/256,   
                
                baseArrangement : 0,
                
            };
            break;

        case 4:                 // young maple tree
            tree = {

                name : "youngMaple",
                stemIntensity : 2.5,
                leavesIntensity : 2.4,
                
                viewAngle : 5    *Math.PI/180,    //   0... from side      90... from top

                s : 1.1,    // scale      it effects everything
                d : 1.3,    // detail     how much detail

                dimensionRatio : 10.8/8.5,       //   y/x

                lightDirection : (  180 + 180)      *Math.PI/180,      // light direction

                sLength : 900,       // if s = 1, this is pixel
                sLengthStop : 0.6,        // If this is less than 1, it stops growing.  (not very spercific) , then 1st branch continues on the end.
                sLengthStopDiverse : 0,    // if this is not zero, 3 branches happen.  and it splits based on this number.
                sInitialWidth : 11,     // stump initial width

                stumpHeight : 80,       // stump part
                stumpShape : 1,          //   stump shape  1-3
                stumpShapeRatio : 2,    //  how much it is thicker on the bottom

                stemSegmentNumber : 400,    // how many segments on the tree?
                stemAngleSegmentRatio : 0.5,  // stem angle segment : stem straight segment

                stemAngleSegmentRandom : 0.5,      // how much change on stemAngleSegment (longer/shorter)
                stemStraightSegmentRandom : 0.5,   // how much change on stemStraightSegment (longer/shorter)

                stemInitialAngleRandom : 0         *Math.PI/180,            //

                stemRandomAngle : 5     *Math.PI/180,
                stemMaxAngle : 2           *Math.PI/180,                     // if stem angle becomes bigger than this, next angle will change to go up
                stemAngleChangeRate : 0.5,                                           // how often the angle changes?   0... no change   1.. always change

                stemHeightBeforeAngleChange : 50,                               // Stem goes straight before this height
                stemHeightBeforeAngleChangeRandom : 100,                     // plus minus this value
                
                stemSurfaceNoiseLevel : 0.25,                                               // less or more bumps

                stemHeightBeforeBranch : 220,
                stemHeightbeforePalmBump : 100000,
                
                stemHeightOfShadow : 0.25,                                          // leaves cast shadow on the stem higher than this height

                
                b1LengthRate : 0.5,                                                          // If this is 0.5, length of the first branch is 50% of sLength
                b1LengthStop : 0.8,                                                     // If this is less than 1, it stops growing. , then 2nd branch continues.
                b1LengthStopDiverse : 0.5,                                          // if this is not zero, 3 second branches happen.  and it splits based on this number.
                b1ReduceRatioType : 0,                                                      // How the first branches change their length based on the stem height 0 - 6
                b1InitialWidth : 0.3,                                                    //  based on the stem width
                
                b1Rate : 1.1,                                                              // 
                b1AngleSegmentRate : 0.2,
                b1StraightSegmentRate : 0.2,
                b1AngleSegmentRandom : 1,
                b1StraightSegmentRandom : 1,

                b1ShapeType : 3,                                                        // branch shape type 0-6
                b1ShapeMagnitude : 2,                                                // how strongly the branch changes its shape angle
                b1ShapeMagnitudeRandom : 0.0,
                b1ShapeBeginningPart : 0.05,
                b1ShapeBeginningMagnitude : 14,

                b1RandomAngle : 15    *Math.PI/180,
                b1AngleChangeRate : 1,                                              // 1 everytime, 0 non

                b1DirectionRandom : 2 ,                                            // direction randomness from golden angle

                b1InitialAngle : 70                  *Math.PI/180,          // branch initial angle when the branch is low
                b1InitialAngleChangeRate : 0.7,                                    // if this is >0, branch angle will change based on its height
                b1InitialAngleRandom : 0       *Math.PI/180,          // branch initial angle randomize
                
                b1HeightBeforeBranchHappens : 0.05,
                b1HeightBeforeBranchHappensRandom : 0.1,

                b1AngleMax : 5 *Math.PI/180,                            // 0 is holizontal angle.  if b1 branch is corrected if it becomes more than this number.  the number of willow trees should be 90

                b1CenterForce : 1,                                       // when it needs more leaves to cover the stem

                b2LengthRate : 0.5,
                b2ReduceRatioType : 0,                                                      // How the first branches change their length based on the stem height 0 - 6    
                b2InitialWidth : 0.5,     
                
                b2Rate : 0.4,
                b2AngleSegmentRate : 0.15,
                b2StraightSegmentRate : 0.15,
                b2AngleSegmentRandom : 0.05,
                b2StraightSegmentRandom : 0.05,

                b2ShapeType : 1,                                                        // branch shape type 0-6
                b2ShapeMagnitude : 1,                                                // how strongly the branch changes its shape angle
                b2ShapeMagnitudeRandom : 0,
                b2ShapeBeginningPart : 0.1,
                b2ShapeBeginningMagnitude : 10,

                b2RandomAngle : 15    *Math.PI/180,
                b2AngleChangeRate : 0.5,                                              // 1 everytime, 0 non

                b2InitialAngle : 25                  *Math.PI/180,          // branch initial angle when the branch is low
                b2InitialAngleRandom : 0       *Math.PI/180,          // branch initial angle randomize

                b2HeightBeforeBranchHappens : 0.2,
                b2HeightBeforeBranchHappensRandom : 0.1,

                b2LowBranchForceUpDown : 0,                       // 0.. normal    if >0, some lower part b2 branch goes up  (if =1, every branch goes up),  if <0, some lower part b2 branch goes down

                branchGeneration : 3,
                
                leavesStartsGeneration : 2,
                
                leavesVolume : 30,
                leavesRandom : 20,
                leavesRate : 0,               // how much leaves
                
                leavesPattern : 1,
                
                leavesSize : 1.2,                 // leaves size 
                
                leavesGroupShading : true,                  //  in a small group, it shades accordingly
                leavesHighlight : 0.1,              // if this is >0, some leaves would be brighter than the original color
                leavesShade : 3.75,                // leaves brightness (brighter -> less contrast)  default: 3.75

                leavesDirectionRandomness : 360,     // leaves direction   if 0, leaves don't rotate.  if 360, totally random

                leavesR : 108/256,
                leavesG : 131/256,
                leavesB : 59/256,
                
                makeBranchLeaves : false,            // branches become leaves color
                
                stemTexShading : true,
                stemTexRandomness : 0.5,
                stemTexStripeRatio : 0.25,               // texture stripe ratio  0.. non,   1.. max    the stripes would be covered by the other texture
                stemTexRatio : 0.5,                         // rough texture on the stem
                stemTexDapples : false,                   // dapples on the texture
                stemTexRoughness : 0.002,            // texture roughness    default should be 0.002
                stemTexPatternLength : 3,               // vertical texture length

                stemHTexRate : 0,
                stemHTexRoughness : 0.001,           // holizontal texture roughness    default should be 0.001
                stemHTexOpacity : 0,                   // holizontal texture opacity
                stemHTexSize : 15,                          // 3 - 15

                stemR : 133/256,                        // stem color
                stemG : 116/256,
                stemB : 76/256,   
                
                baseArrangement : 1,
                
            };
            break;

        case 5:         //spruce
            tree = {
                
                name : "spruce",
                stemIntensity : 3,
                leavesIntensity : 3.4,
                
                viewAngle : 5    *Math.PI/180,    //   0... from side      90... from top

                s : 0.92,    // scale      it effects everything
                d : 1.35,    // detail     how much detail

                dimensionRatio : 11.8/7.5,       //   y/x

                lightDirection : (  180 + 180)      *Math.PI/180,      // light direction

                sLength : 1000,       // if s = 1, this is pixel
                sLengthStop : 0.9,        // If this is less than 1, it stops growing.  (not very spercific) , then 1st branch continues on the end.
                sLengthStopDiverse : 0,    // if this is not zero, 3 branches happen.  and it splits based on this number.
                sInitialWidth : 12,     // stump initial width

                stumpHeight : 120,       // stump part
                stumpShape : 1,          //   stump shape  1-3
                stumpShapeRatio : 3,    //  how much it is thicker on the bottom

                stemSegmentNumber : 500,    // how many segments on the tree?
                stemAngleSegmentRatio : 0.5,  // stem angle segment : stem straight segment

                stemAngleSegmentRandom : 0.5,      // how much change on stemAngleSegment (longer/shorter)
                stemStraightSegmentRandom : 0.5,   // how much change on stemStraightSegment (longer/shorter)

                stemInitialAngleRandom : 0         *Math.PI/180,            //

                stemRandomAngle : 2     *Math.PI/180,
                stemMaxAngle : 2           *Math.PI/180,                     // if stem angle becomes bigger than this, next angle will change to go up
                stemAngleChangeRate : 0.5,                                           // how often the angle changes?   0... no change   1.. always change

                stemHeightBeforeAngleChange : 50,                               // Stem goes straight before this height
                stemHeightBeforeAngleChangeRandom : 100,                     // plus minus this value
                
                stemSurfaceNoiseLevel : 0.5,                                               // less or more bumps

                stemHeightBeforeBranch : 130,
                stemHeightbeforePalmBump : 100000,
                
                stemHeightOfShadow : 0.25,                                          // leaves cast shadow on the stem higher than this height

                
                b1LengthRate : 0.32,                                                          // If this is 0.5, length of the first branch is 50% of sLength
                b1LengthStop : 0.9,
                b1LengthStopDiverse : 0,                                          // if this is not zero, 3 second branches happen.  and it splits based on this number.
                b1ReduceRatioType : 0,                                                      // How the first branches change their length based on the stem height 0 - 6
                b1InitialWidth : 0.4,                                                    //  based on the stem width
                
                b1Rate : 2.5,                                                              // 
                b1AngleSegmentRate : 0.4,
                b1StraightSegmentRate : 0.4,
                b1AngleSegmentRandom : 1,
                b1StraightSegmentRandom : 1,

                b1ShapeType : 4,                                                        // branch shape type 0-6
                b1ShapeMagnitude : 1,                                                // how strongly the branch changes its shape angle
                b1ShapeMagnitudeRandom : 0.4,
                b1ShapeBeginningPart : 0.1,
                b1ShapeBeginningMagnitude : 3,

                b1RandomAngle : 5    *Math.PI/180,
                b1AngleChangeRate : 0.5,                                              // 1 everytime, 0 non

                b1DirectionRandom : 1 ,                                            // direction randomness from golden angle

                b1InitialAngle : 100                  *Math.PI/180,          // branch initial angle when the branch is low
                b1InitialAngleChangeRate : 0.25,                                    // if this is >0, branch angle will change based on its height
                b1InitialAngleRandom : 5       *Math.PI/180,          // branch initial angle randomize
                
                b1HeightBeforeBranchHappens : 0,
                b1HeightBeforeBranchHappensRandom : 0,

                b1AngleMax : 5 *Math.PI/180,                            // 0 is holizontal angle.  if b1 branch is corrected if it becomes more than this number.  the number of willow trees should be 90

                b1CenterForce : 0.55,                                       // when it needs more leaves to cover the stem

                b2LengthRate : 0.25,
                b2ReduceRatioType : 0,                                                      // How the first branches change their length based on the stem height 0 - 6    
                b2InitialWidth : 0.3,   
                
                b2Rate : 1,
                b2AngleSegmentRate : 0.3,
                b2StraightSegmentRate : 0.3,
                b2AngleSegmentRandom : 0,
                b2StraightSegmentRandom : 0,

                b2ShapeType : 0,                                                        // branch shape type 0-6
                b2ShapeMagnitude : 1,                                                // how strongly the branch changes its shape angle
                b2ShapeMagnitudeRandom : 0,
                b2ShapeBeginningPart : 0.01,
                b2ShapeBeginningMagnitude : 28,

                b2RandomAngle : 10    *Math.PI/180,
                b2AngleChangeRate : 0.5,                                              // 1 everytime, 0 non

                b2InitialAngle : 45                  *Math.PI/180,          // branch initial angle when the branch is low
                b2InitialAngleRandom : 30       *Math.PI/180,          // branch initial angle randomize

                b2HeightBeforeBranchHappens : 0,
                b2HeightBeforeBranchHappensRandom : 0,

                b2LowBranchForceUpDown : 0,                       // 0.. normal    if >0, some lower part b2 branch goes up  (if =1, every branch goes up),  if <0, some lower part b2 branch goes down

                branchGeneration : 3,
                
                leavesStartsGeneration : 1,
                
                leavesVolume : 11,
                leavesRandom : 2,
                leavesRate : 0.5,               // how much leaves
                
                leavesPattern : 4,
                
                leavesSize : 0.175,                 // leaves size
                
                leavesGroupShading : true,                  //in a small group, it shades accordingly
                leavesHighlight : 0.18,              // if this is >0, some leaves would be brighter than the original color
                leavesShade : 3.75,                // leaves brightness (brighter -> less contrast)  default: 3.75

                leavesDirectionRandomness : 360,     // leaves direction   if 0, leaves don't rotate.  if 360, totally random

                leavesR : 52/256,
                leavesG : 68/256,
                leavesB : 40/256,
                
                makeBranchLeaves : false,            // branches become leaves color

                stemTexShading : true,
                stemTexRandomness : 0.5,
                stemTexStripeRatio : 0.25,               // texture stripe ratio  0.. non,   1.. max    the stripes would be covered by the other texture
                stemTexRatio : 0.5,                         // rough texture on the stem
                stemTexDapples : false,                   // dapples on the texture
                stemTexRoughness : 0.002,            // texture roughness    default should be 0.002
                stemTexPatternLength : 3,               // vertical texture length

                stemHTexRate : 0.0,
                stemHTexRoughness : 0.001,           // holizontal texture roughness    default should be 0.001
                stemHTexOpacity : 0.25,                   // holizontal texture opacity
                stemHTexSize : 15,                          // 3 - 15
                
                stemR : 92/256,                        // stem color
                stemG : 77/256,
                stemB : 54/256,
                
                baseArrangement : 2,
                
            };
            break;
            
        case 6:  // aspen
            tree = {

                name : "aspen",
                stemIntensity : 1,
                leavesIntensity : 2.4,
                
                viewAngle : 5    *Math.PI/180,    //   0... from side      90... from top

                s : 0.85,    // scale      it effects everything
                d : 1.95,    // detail     how much detail

                dimensionRatio : 11.3/7,       //   y/x

                lightDirection : (  180 + 180)      *Math.PI/180,      // light direction

                sLength : 1000,       // if s = 1, this is pixel
                sLengthStop : 0.95,        // If this is less than 1, it stops growing.  (not very spercific) , then 1st branch continues on the end.
                sLengthStopDiverse : 0,    // if this is not zero, 3 branches happen.  and it splits based on this number.
                sInitialWidth : 12,     // stump initial width  ( >= 15)

                stumpHeight : 50,       // stump part
                stumpShape : 1,          //   stump shape  1-3
                stumpShapeRatio : 2,    //  how much it is thicker on the bottom

                stemSegmentNumber : 800,    // how many segments on the tree?
                stemAngleSegmentRatio : 0.5,  // stem angle segment : stem straight segment

                stemAngleSegmentRandom : 0.5,      // how much change on stemAngleSegment (longer/shorter)
                stemStraightSegmentRandom : 0.5,   // how much change on stemStraightSegment (longer/shorter)

                stemInitialAngleRandom : 0         *Math.PI/180,            //

                stemRandomAngle : 3     *Math.PI/180,
                stemMaxAngle : 3           *Math.PI/180,                     // if stem angle becomes bigger than this, next angle will change to go up
                stemAngleChangeRate : 0.5,                                           // how often the angle changes?   0... no change   1.. always change

                stemHeightBeforeAngleChange : 50,                               // Stem goes straight before this height
                stemHeightBeforeAngleChangeRandom : 100,                     // plus minus this value
                
                stemSurfaceNoiseLevel : 0.5,                                               // less or more bumps

                stemHeightBeforeBranch : 150,
                stemHeightbeforePalmBump : 100000,
                
                stemHeightOfShadow : 0.25,                                          // leaves cast shadow on the stem higher than this height

                
                b1LengthRate : 0.4,                                                          // If this is 0.5, length of the first branch is 50% of sLength
                b1LengthStop : 0.5,
                b1LengthStopDiverse : 0.25,                                          // if this is not zero, 3 second branches happen.  and it splits based on this number.
                b1ReduceRatioType : 6,                                                      // How the first branches change their length based on the stem height 0 - 6
                b1InitialWidth : 0.3,                                                    //  based on the stem width
                
                b1Rate : 0.9,                                                              // 
                b1AngleSegmentRate : 0.1,
                b1StraightSegmentRate : 0.1,
                b1AngleSegmentRandom : 2,
                b1StraightSegmentRandom : 2,

                b1ShapeType : 3,                                                        // branch shape type 0-6
                b1ShapeMagnitude : -2,                                                // how strongly the branch changes its shape angle
                b1ShapeMagnitudeRandom : 0.0,
                b1ShapeBeginningPart : 0.1,
                b1ShapeBeginningMagnitude : 28,

                b1RandomAngle : 15    *Math.PI/180,
                b1AngleChangeRate : 0.5,                                              // 1 everytime, 0 non

                b1DirectionRandom : 2 ,                                            // direction randomness from golden angle

                b1InitialAngle : 70                  *Math.PI/180,          // branch initial angle when the branch is low
                b1InitialAngleChangeRate : 0.7,                                    // if this is >0, branch angle will change based on its height
                b1InitialAngleRandom : 0       *Math.PI/180,          // branch initial angle randomize
                
                b1HeightBeforeBranchHappens : 0.1,
                b1HeightBeforeBranchHappensRandom : 0.05,

                b1AngleMax : 5 *Math.PI/180,                            // 0 is holizontal angle.  if b1 branch is corrected if it becomes more than this number.  the number of willow trees should be 90

                b1CenterForce : 0.2,                                       // when it needs more leaves to cover the stem

                b2LengthRate : 0.5,
                b2ReduceRatioType : 0,                                                      // How the first branches change their length based on the stem height 0 - 6    
                b2InitialWidth : 0.5,   
                
                b2Rate : 0.25,
                b2AngleSegmentRate : 0.05,
                b2StraightSegmentRate : 0.05,
                b2AngleSegmentRandom : 2,
                b2StraightSegmentRandom : 0,

                b2ShapeType : 4,                                                        // branch shape type 0-6
                b2ShapeMagnitude : 1,                                                // how strongly the branch changes its shape angle
                b2ShapeMagnitudeRandom : 0,
                b2ShapeBeginningPart : 0.01,
                b2ShapeBeginningMagnitude : 28,

                b2RandomAngle : 15    *Math.PI/180,
                b2AngleChangeRate : 0.3,                                              // 1 everytime, 0 non

                b2InitialAngle : 35                  *Math.PI/180,          // branch initial angle when the branch is low
                b2InitialAngleRandom : 0       *Math.PI/180,          // branch initial angle randomize

                b2HeightBeforeBranchHappens : 0,
                b2HeightBeforeBranchHappensRandom : 0,

                b2LowBranchForceUpDown : 0.3,                       // 0.. normal    if >0, some lower part b2 branch goes up  (if =1, every branch goes up),  if <0, some lower part b2 branch goes down

                branchGeneration : 3,
                
                leavesStartsGeneration : 2,
                
                leavesVolume : 120,
                leavesRandom : 20,
                leavesRate : 0.5,               // how much leaves
                
                leavesPattern : 5,
                
                leavesSize : 0.24,                 // leaves size
                
                leavesGroupShading : true,                  //  in a small group, it shades accordingly
                leavesHighlight : 0.08,              // if this is >0, some leaves would be brighter than the original color
                leavesShade : 3.75,                // leaves brightness (brighter -> less contrast)  default: 3.75

                leavesDirectionRandomness : 360,     // leaves direction   if 0, leaves don't rotate.  if 360, totally random

                leavesR : 114/256,
                leavesG : 141/256,
                leavesB : 78/256,
                
                makeBranchLeaves : false,            // branches become leaves color
       
                stemTexShading : true,
                stemTexRandomness : 0.2, 
                stemTexStripeRatio : 0,               // texture stripe ratio  0.. non,   1.. max    the stripes would be covered by the other texture
                stemTexRatio : 0.45,                         // rough texture on the stem
                stemTexDapples : false,                   // dapples on the texture
                stemTexRoughness : 0.001,            // texture roughness    default should be 0.002
                stemTexPatternLength : 1,               // vertical texture length
                
                stemHTexRate : 0.4,
                stemHTexRoughness : 0.0015,           // holizontal texture roughness    default should be 0.001
                stemHTexOpacity : 0.6,                   // holizontal texture opacity
                stemHTexSize : 4,                          // 3 - 15
          
                stemR : 242/256,                        // stem color
                stemG : 242/256,
                stemB : 213/256,
                
                baseArrangement : 0,
                
            };
            break;

        case 7:                 //Pine tree
            tree = {

                name : "pine",
                stemIntensity : 2.5,
                leavesIntensity : 2.4,
                
                viewAngle : 5    *Math.PI/180,    //   0... from side      90... from top

                s : 0.975,    // scale      it effects everything
                d : 1.45,    // detail     how much detail

                dimensionRatio : 11.3/7.2,       //   y/x

                lightDirection : (180 + 180)      *Math.PI/180,      // light direction

                sLength : 1000,                 // if s = 1, this is pixel
                sLengthStop : 0.8,              // If this is less than 1, it stops growing.  , then 1st branch continues.
                sLengthStopDiverse : 0,    // if this is not zero, 3 branches happen.  and it splits based on this number.
                sInitialWidth : 18,                 // stump initial width

                stumpHeight : 50,       // stump part
                stumpShape : 2,          //   stump shape  1-3
                stumpShapeRatio : 4,    //  how much it is thicker on the bottom

                stemSegmentNumber : 800,    // how many segments on the tree?
                stemAngleSegmentRatio : 0.5,  // stem angle segment : stem straight segment

                stemAngleSegmentRandom : 0.5,      // how much change on stemAngleSegment (longer/shorter)
                stemStraightSegmentRandom : 0.5,   // how much change on stemStraightSegment (longer/shorter)

                stemInitialAngleRandom : 0         *Math.PI/180,            //

                stemRandomAngle : 5     *Math.PI/180,
                stemMaxAngle : 2           *Math.PI/180,                     // if stem angle becomes bigger than this, next angle will change to go up
                stemAngleChangeRate : 0.1,                                           // how often the angle changes?   0... no change   1.. always change

                stemHeightBeforeAngleChange : 50,                               // Stem goes straight before this height
                stemHeightBeforeAngleChangeRandom : 100,                     // plus minus this value
                
                stemSurfaceNoiseLevel : 1,                                               // less or more bumps

                stemHeightBeforeBranch : 100,
                stemHeightbeforePalmBump : 100000,
                
                stemHeightOfShadow : 0.25,                                          // leaves cast shadow on the stem higher than this height

                
                b1LengthRate : 0.35,                                                          // If this is 0.5, length of the first branch is 50% of sLength
                b1LengthStop : 0.8,                                                     // If this is less than 1, it stops growing. , then 2nd branch continues.
                b1LengthStopDiverse : 0.5,                                          // if this is not zero, 3 second branches happen.  and it splits based on this number.
                b1ReduceRatioType : 0,                                                      // How the first branches change their length based on the stem height 0 - 6
                b1InitialWidth : 0.3,                                                    //  based on the stem width
                
                b1Rate : 1.4,                                                              // 
                b1AngleSegmentRate : 0.2,
                b1StraightSegmentRate : 0.2,
                b1AngleSegmentRandom : 0.5,
                b1StraightSegmentRandom : 0.5,

                b1ShapeType : 4,                                                        // branch shape type 0-6
                b1ShapeMagnitude : 0.8,                                                // how strongly the branch changes its shape angle
                b1ShapeMagnitudeRandom : 0.0,
                b1ShapeBeginningPart : 0.1,
                b1ShapeBeginningMagnitude : 20,

                b1RandomAngle : 10    *Math.PI/180,
                b1AngleChangeRate : 2,                                              // 1 everytime, 0 non

                b1DirectionRandom : 0.02 ,                                            // direction randomness from golden angle

                b1InitialAngle : 55                  *Math.PI/180,          // branch initial angle when the branch is low
                b1InitialAngleChangeRate : -0.4,                                    // if this is >0, branch angle will change based on its height
                b1InitialAngleRandom : 0       *Math.PI/180,          // branch initial angle randomize
                
                b1HeightBeforeBranchHappens : 0.05,
                b1HeightBeforeBranchHappensRandom : 0.1,

                b1AngleMax : 5 *Math.PI/180,                            // 0 is holizontal angle.  if b1 branch is corrected if it becomes more than this number.  the number of willow trees should be 90

                b1CenterForce : 0.32,                                       // when it needs more leaves to cover the stem

                b2LengthRate : 0.25,
                b2ReduceRatioType : 0,                                                      // How the first branches change their length based on the stem height 0 - 6    
                b2InitialWidth : 0.5,   
                
                b2Rate : 1,
                b2AngleSegmentRate : 0.1,
                b2StraightSegmentRate : 0.1,
                b2AngleSegmentRandom : 0,
                b2StraightSegmentRandom : 0,

                b2ShapeType : 0,                                                        // branch shape type 0-6
                b2ShapeMagnitude : 1,                                                // how strongly the branch changes its shape angle
                b2ShapeMagnitudeRandom : 0,
                b2ShapeBeginningPart : 0.01,
                b2ShapeBeginningMagnitude : 28,

                b2RandomAngle : 5    *Math.PI/180,
                b2AngleChangeRate : 0.6,                                              // 1 everytime, 0 non

                b2InitialAngle : 45                  *Math.PI/180,          // branch initial angle when the branch is low
                b2InitialAngleRandom : 10       *Math.PI/180,          // branch initial angle randomize

                b2HeightBeforeBranchHappens : 0,
                b2HeightBeforeBranchHappensRandom : 0,

                b2LowBranchForceUpDown : 0.1,                       // 0.. normal    if >0, some lower part b2 branch goes up  (if =1, every branch goes up),  if <0, some lower part b2 branch goes down

                branchGeneration : 3,
                
                leavesStartsGeneration : 1,
                
                leavesVolume : 7,
                leavesRandom : 2,
                leavesRate : 1,               // how much leaves
                
                leavesPattern : 3,
                
                leavesSize : 0.3,                 // leaves size
                
                leavesGroupShading : true,                  //  in a small group, it shades accordingly
                leavesHighlight : 0.13,              // if this is >0, some leaves would be brighter than the original color
                leavesShade : 3.75,                // leaves brightness (brighter -> less contrast)  default: 3.75

                leavesDirectionRandomness : 360,     // leaves direction   if 0, leaves don't rotate.  if 360, totally random

                leavesR : 98/256,
                leavesG : 111/256,
                leavesB : 67/256,
                
                makeBranchLeaves : false,            // branches become leaves color

                stemTexShading : true,
                stemTexRandomness : 0.5,
                stemTexStripeRatio : 0.1,               // texture stripe ratio  0.. non,   1.. max    the stripes would be covered by the other texture
                stemTexRatio : 0.25,                         // rough texture on the stem
                stemTexDapples : true,                   // dapples on the texture
                stemTexRoughness : 0.002,            // texture roughness    default should be 0.002
                stemTexPatternLength : 3,               // vertical texture length

                stemHTexRate : 0.4,
                stemHTexRoughness : 0.001,           // holizontal texture roughness    default should be 0.001
                stemHTexOpacity : 0,                   // holizontal texture opacity
                stemHTexSize : 15,                          // 3 - 15
          
                stemR : 93/256,                        // stem color
                stemG : 77/256,
                stemB : 70/256,
                
                baseArrangement : 3,
                
            };
            break;

        case 8:                 //Elm Tree
            tree = {
                
                name : "elm",
                stemIntensity : 2.1,
                leavesIntensity : 2.4,
                
                viewAngle : 5    *Math.PI/180,    //   0... from side      90... from top

                s : 0.9,    // scale      it effects everything
                d : 0.95,    // detail     how much detail

                dimensionRatio : 11/12.5,       //   y/x

                lightDirection : (180 + 180)      *Math.PI/180,      // light direction

                sLength : 1000,                 // if s = 1, this is pixel
                sLengthStop : 0.4,              // If this is less than 1, it stops growing.  , then 1st branch continues.
                sLengthStopDiverse : 0.5,    // if this is not zero, 3 branches happen.  and it splits based on this number.
                sInitialWidth : 27,                 // stump initial width

                stumpHeight : 50,       // stump part
                stumpShape : 2,          //   stump shape  1-3
                stumpShapeRatio : 4,    //  how much it is thicker on the bottom

                stemSegmentNumber : 400,    // how many segments on the tree?
                stemAngleSegmentRatio : 0.5,  // stem angle segment : stem straight segment

                stemAngleSegmentRandom : 0.5,      // how much change on stemAngleSegment (longer/shorter)
                stemStraightSegmentRandom : 0.5,   // how much change on stemStraightSegment (longer/shorter)

                stemInitialAngleRandom : 0         *Math.PI/180,            //

                stemRandomAngle : 1     *Math.PI/180,
                stemMaxAngle : 2           *Math.PI/180,                     // if stem angle becomes bigger than this, next angle will change to go up
                stemAngleChangeRate : 0.2,                                           // how often the angle changes?   0... no change   1.. always change

                stemHeightBeforeAngleChange : 50,                               // Stem goes straight before this height
                stemHeightBeforeAngleChangeRandom : 100,                     // plus minus this value
                
                stemSurfaceNoiseLevel : 1,                                               // less or more bumps

                stemHeightBeforeBranch : 75,
                stemHeightbeforePalmBump : 100000,
                
                stemHeightOfShadow : 0.25,                                          // leaves cast shadow on the stem higher than this height

                
                b1LengthRate : 0.6,                                                          // If this is 0.5, length of the first branch is 50% of sLength
                b1LengthStop : 0.8,                                                     // If this is less than 1, it stops growing. , then 2nd branch continues.
                b1LengthStopDiverse : 0.5,                                          // if this is not zero, 3 second branches happen.  and it splits based on this number.
                b1ReduceRatioType : 0,                                                      // How the first branches change their length based on the stem height 0 - 6
                b1InitialWidth : 0.5,                                                    //  based on the stem width
                
                b1Rate : 0.9,                                                              // 
                b1AngleSegmentRate : 0.2,
                b1StraightSegmentRate : 0.2,
                b1AngleSegmentRandom : 0.5,
                b1StraightSegmentRandom : 0.5,

                b1ShapeType : 3,                                                        // branch shape type 0-6
                b1ShapeMagnitude : -2.75,                                                // how strongly the branch changes its shape angle
                b1ShapeMagnitudeRandom : 0.0,
                b1ShapeBeginningPart : 0.14,
                b1ShapeBeginningMagnitude : 28,

                b1RandomAngle : 15    *Math.PI/180,
                b1AngleChangeRate : 0.5,                                              // 1 everytime, 0 non

                b1DirectionRandom : 0.0002 ,                                            // direction randomness from golden angle

                b1InitialAngle : 10                  *Math.PI/180,          // branch initial angle when the branch is low
                b1InitialAngleChangeRate : 0,                                    // if this is >0, branch angle will change based on its height
                b1InitialAngleRandom : 5       *Math.PI/180,          // branch initial angle randomize
                
                b1HeightBeforeBranchHappens : 0.4,
                b1HeightBeforeBranchHappensRandom : 0,

                b1AngleMax : 5 *Math.PI/180,                            // 0 is holizontal angle.  if b1 branch is corrected if it becomes more than this number.  the number of willow trees should be 90

                b1CenterForce : 0.1,                                       // when it needs more leaves to cover the stem

                b2LengthRate : 0.45,
                b2ReduceRatioType : 2,                                                      // How the first branches change their length based on the stem height 0 - 6    
                b2InitialWidth : 0.5,   
                
                b2Rate : 0.75,
                b2AngleSegmentRate : 0.1,
                b2StraightSegmentRate : 0.1,
                b2AngleSegmentRandom : 0,
                b2StraightSegmentRandom : 0,

                b2ShapeType : 4,                                                        // branch shape type 0-6
                b2ShapeMagnitude : 1,                                                // how strongly the branch changes its shape angle
                b2ShapeMagnitudeRandom : 0,
                b2ShapeBeginningPart : 0.01,
                b2ShapeBeginningMagnitude : 28,

                b2RandomAngle : 30    *Math.PI/180,
                b2AngleChangeRate : 0.6,                                              // 1 everytime, 0 non

                b2InitialAngle : 25                  *Math.PI/180,          // branch initial angle when the branch is low
                b2InitialAngleRandom : 0       *Math.PI/180,          // branch initial angle randomize

                b2HeightBeforeBranchHappens : 0,
                b2HeightBeforeBranchHappensRandom : 0,

                b2LowBranchForceUpDown : 0,                       // 0.. normal    if >0, some lower part b2 branch goes up  (if =1, every branch goes up),  if <0, some lower part b2 branch goes down

                branchGeneration : 3,
                
                leavesStartsGeneration : 3,
                
                leavesVolume : 90,
                leavesRandom : 15,
                leavesRate : 1.1,               // how much leaves
                
                leavesPattern : 7,
                
                leavesSize : 0.4,                 // leaves size
                
                leavesGroupShading : true,                  //  in a small group, it shades accordingly
                leavesHighlight : 0.12,              // if this is >0, some leaves would be brighter than the original color
                leavesShade : 3.75,                // leaves brightness (brighter -> less contrast)  default: 3.75

                leavesDirectionRandomness : 360,     // leaves direction   if 0, leaves don't rotate.  if 360, totally random

                leavesR : 108/256,
                leavesG : 131/256,
                leavesB : 59/256,
                
                makeBranchLeaves : false,            // branches become leaves color

                stemTexShading : true,
                stemTexRandomness : 0.5,
                stemTexStripeRatio : 0.5,               // texture stripe ratio  0.. non,   1.. max    the stripes would be covered by the other texture
                stemTexRatio : 0.25,                         // rough texture on the stem
                stemTexDapples : true,                   // dapples on the texture
                stemTexRoughness : 0.002,            // texture roughness    default should be 0.002
                stemTexPatternLength : 3,               // vertical texture length

                stemHTexRate : 0.4,
                stemHTexRoughness : 0.001,           // holizontal texture roughness    default should be 0.001
                stemHTexOpacity : 0,                   // holizontal texture opacity
                stemHTexSize : 15,                          // 3 - 15
          
                stemR : 137/256,                        // stem color
                stemG : 130/256,
                stemB : 101/256,
                
                baseArrangement : 3,
                
            };
            break;

        case 9:                 // Populus Nigra
            tree = {
                
                name : "populusNigra",
                stemIntensity : 2.5,
                leavesIntensity : 3.5,

                viewAngle : 5    *Math.PI/180,    //   0... from side      90... from top

                s : 1.05,    // scale      it effects everything
                d : 1.5,    // detail     how much detail

                dimensionRatio : 11.5/4.5,       //   y/x

                lightDirection : (180 + 180)      *Math.PI/180,      // light direction

                sLength : 1000,                 // if s = 1, this is pixel
                sLengthStop : 0.7,              // If this is less than 1, it stops growing.  , then 1st branch continues.
                sLengthStopDiverse : 0.0,    // if this is not zero, 3 branches happen.  and it splits based on this number.
                sInitialWidth : 12,                 // stump initial width

                stumpHeight : 50,       // stump part
                stumpShape : 2,          //   stump shape  1-3
                stumpShapeRatio : 1,    //  how much it is thicker on the bottom

                stemSegmentNumber : 400,    // how many segments on the tree?
                stemAngleSegmentRatio : 0.5,  // stem angle segment : stem straight segment

                stemAngleSegmentRandom : 0.5,      // how much change on stemAngleSegment (longer/shorter)
                stemStraightSegmentRandom : 0.5,   // how much change on stemStraightSegment (longer/shorter)

                stemInitialAngleRandom : 0         *Math.PI/180,            //

                stemRandomAngle : 0.1     *Math.PI/180,
                stemMaxAngle : 0.2           *Math.PI/180,                     // if stem angle becomes bigger than this, next angle will change to go up
                stemAngleChangeRate : 0.2,                                           // how often the angle changes?   0... no change   1.. always change

                stemHeightBeforeAngleChange : 50,                               // Stem goes straight before this height
                stemHeightBeforeAngleChangeRandom : 100,                     // plus minus this value
                
                stemSurfaceNoiseLevel : 1,                                               // less or more bumps

                stemHeightBeforeBranch : 50,
                stemHeightbeforePalmBump : 100000,
                
                stemHeightOfShadow : 0.25,                                          // leaves cast shadow on the stem higher than this height

                
                b1LengthRate : 0.3,                                                          // If this is 0.5, length of the first branch is 50% of sLength
                b1LengthStop : 0.8,                                                     // If this is less than 1, it stops growing. , then 2nd branch continues.
                b1LengthStopDiverse : 0.5,                                          // if this is not zero, 3 second branches happen.  and it splits based on this number.
                b1ReduceRatioType : 0,                                                      // How the first branches change their length based on the stem height 0 - 6
                b1InitialWidth : 0.3,                                                    //  based on the stem width
                
                b1Rate : 1,                                                              // 
                b1AngleSegmentRate : 0.2,
                b1StraightSegmentRate : 0.2,
                b1AngleSegmentRandom : 0.5,
                b1StraightSegmentRandom : 0.5,

                b1ShapeType : 3,                                                        // branch shape type 0-6
                b1ShapeMagnitude : 2,                                                // how strongly the branch changes its shape angle
                b1ShapeMagnitudeRandom : 0.0,
                b1ShapeBeginningPart : 0.14,
                b1ShapeBeginningMagnitude : 28,

                b1RandomAngle : 15    *Math.PI/180,
                b1AngleChangeRate : 0.5,                                              // 1 everytime, 0 non

                b1DirectionRandom : 0.0002 ,                                            // direction randomness from golden angle

                b1InitialAngle : 45                  *Math.PI/180,          // branch initial angle when the branch is low
                b1InitialAngleChangeRate : 0.4,                                    // if this is >0, branch angle will change based on its height
                b1InitialAngleRandom : 0.1       *Math.PI/180,          // branch initial angle randomize
                
                b1HeightBeforeBranchHappens : 0,
                b1HeightBeforeBranchHappensRandom : 0,

                b1AngleMax : 5 *Math.PI/180,                            // 0 is holizontal angle.  if b1 branch is corrected if it becomes more than this number.  the number of willow trees should be 90

                b1CenterForce : 0.1,                                       // when it needs more leaves to cover the stem

                b2LengthRate : 0.4,
                b2ReduceRatioType : 0,                                                      // How the first branches change their length based on the stem height 0 - 6    
                b2InitialWidth : 0.5,   
                
                b2Rate : 1,
                b2AngleSegmentRate : 0.1,
                b2StraightSegmentRate : 0.1,
                b2AngleSegmentRandom : 0,
                b2StraightSegmentRandom : 0,

                b2ShapeType : 4,                                                        // branch shape type 0-6
                b2ShapeMagnitude : 1,                                                // how strongly the branch changes its shape angle
                b2ShapeMagnitudeRandom : 0,
                b2ShapeBeginningPart : 0.01,
                b2ShapeBeginningMagnitude : 28,

                b2RandomAngle : 30    *Math.PI/180,
                b2AngleChangeRate : 0.6,                                              // 1 everytime, 0 non

                b2InitialAngle : 25                  *Math.PI/180,          // branch initial angle when the branch is low
                b2InitialAngleRandom : 0       *Math.PI/180,          // branch initial angle randomize

                b2HeightBeforeBranchHappens : 0,
                b2HeightBeforeBranchHappensRandom : 0,

                b2LowBranchForceUpDown : 0,                       // 0.. normal    if >0, some lower part b2 branch goes up  (if =1, every branch goes up),  if <0, some lower part b2 branch goes down

                branchGeneration : 2,
                
                leavesStartsGeneration : 2,
                
                leavesVolume : 10,
                leavesRandom : 15,
                leavesRate : 1,               // how much leaves
                
                leavesPattern : 7,
                
                leavesSize : 0.3,                 // leaves size
                
                leavesGroupShading : true,                  //  in a small group, it shades accordingly
                leavesHighlight : 0.2,              // if this is >0, some leaves would be brighter than the original color
                leavesShade : 3.75,                // leaves brightness (brighter -> less contrast)  default: 3.75

                leavesDirectionRandomness : 360,     // leaves direction   if 0, leaves don't rotate.  if 360, totally random

                leavesR : 77/256,
                leavesG : 86/256,
                leavesB : 46/256,
                
                makeBranchLeaves : false,            // branches become leaves color

                stemTexShading : true,
                stemTexRandomness : 0.5,
                stemTexStripeRatio : 0.5,               // texture stripe ratio  0.. non,   1.. max    the stripes would be covered by the other texture
                stemTexRatio : 0.25,                         // rough texture on the stem
                stemTexDapples : true,                   // dapples on the texture
                stemTexRoughness : 0.002,            // texture roughness    default should be 0.002
                stemTexPatternLength : 3,               // vertical texture length

                stemHTexRate : 0.4,
                stemHTexRoughness : 0.001,           // holizontal texture roughness    default should be 0.001
                stemHTexOpacity : 0,                   // holizontal texture opacity
                stemHTexSize : 15,                          // 3 - 15
          
                stemR : 109/256,                        // stem color
                stemG : 96/256,
                stemB : 81/256,
                
                baseArrangement : 6,
                
            };
            break;

        case 10:                 //Pepper Tree
            tree = {
                
                name : "pepper",
                stemIntensity : 2.2,
                leavesIntensity : 2.4,
                
                viewAngle : 5    *Math.PI/180,    //   0... from side      90... from top

                s : 0.65,    // scale      it effects everything
                d : 0.75,    // detail     how much detail

                dimensionRatio : 11/11,       //   y/x

                lightDirection : (180 + 180)      *Math.PI/180,      // light direction

                sLength : 1000,                 // if s = 1, this is pixel
                sLengthStop : 0.45,              // If this is less than 1, it stops growing.  , then 1st branch continues.
                sLengthStopDiverse : 0.5,    // if this is not zero, 3 branches happen.  and it splits based on this number.
                sInitialWidth : 25,                 // stump initial width

                stumpHeight : 50,       // stump part
                stumpShape : 2,          //   stump shape  1-3
                stumpShapeRatio : 4,    //  how much it is thicker on the bottom

                stemSegmentNumber : 400,    // how many segments on the tree?
                stemAngleSegmentRatio : 0.05,  // stem angle segment : stem straight segment

                stemAngleSegmentRandom : 0.15,      // how much change on stemAngleSegment (longer/shorter)
                stemStraightSegmentRandom : 2.25,   // how much change on stemStraightSegment (longer/shorter)

                stemInitialAngleRandom : 0         *Math.PI/180,            //

                stemRandomAngle : 10     *Math.PI/180,
                stemMaxAngle : 5           *Math.PI/180,                     // if stem angle becomes bigger than this, next angle will change to go up
                stemAngleChangeRate : 0.8,                                           // how often the angle changes?   0... no change   1.. always change

                stemHeightBeforeAngleChange : 100,                               // Stem goes straight before this height
                stemHeightBeforeAngleChangeRandom : 5,                     // plus minus this value
                
                stemSurfaceNoiseLevel : 0.5,                                               // less or more bumps

                stemHeightBeforeBranch : 150,
                stemHeightbeforePalmBump : 100000,
                
                stemHeightOfShadow : 0.25,                                          // leaves cast shadow on the stem higher than this height

                
                b1LengthRate : 0.95,                                                          // If this is 0.5, length of the first branch is 50% of sLength
                b1LengthStop : 0.7,                                                     // If this is less than 1, it stops growing. , then 2nd branch continues.
                b1LengthStopDiverse : 0,                                          // if this is not zero, 3 second branches happen.  and it splits based on this number.
                b1ReduceRatioType : 0,                                                      // How the first branches change their length based on the stem height 0 - 6
                b1InitialWidth : 0.5,                                                    //  based on the stem width
                
                b1Rate : 1,                                                              // 
                b1AngleSegmentRate : 0.2,
                b1StraightSegmentRate : 1,
                b1AngleSegmentRandom : 0.5,
                b1StraightSegmentRandom : 0.5,

                b1ShapeType : 3,                                                        // branch shape type 0-6
                b1ShapeMagnitude : -0.6,                                                // how strongly the branch changes its shape angle
                b1ShapeMagnitudeRandom : 0.5,
                b1ShapeBeginningPart : 0.14,
                b1ShapeBeginningMagnitude : 28,

                b1RandomAngle : 20    *Math.PI/180,
                b1AngleChangeRate : 5,                                              // 1 everytime, 0 non

                b1DirectionRandom : 0.0002 ,                                            // direction randomness from golden angle

                b1InitialAngle : 10                  *Math.PI/180,          // branch initial angle when the branch is low
                b1InitialAngleChangeRate : 0,                                    // if this is >0, branch angle will change based on its height
                b1InitialAngleRandom : 2       *Math.PI/180,          // branch initial angle randomize
                
                b1HeightBeforeBranchHappens : 0.25,
                b1HeightBeforeBranchHappensRandom : 0,

                b1AngleMax : 5 *Math.PI/180,                            // 0 is holizontal angle.  if b1 branch is corrected if it becomes more than this number.  the number of willow trees should be 90

                b1CenterForce : 0.2,                                       // when it needs more leaves to cover the stem

                b2LengthRate : 0.4,
                b2ReduceRatioType : 2,                                                      // How the first branches change their length based on the stem height 0 - 6    
                b2InitialWidth : 0.25,   
                
                b2Rate : 0.5,
                b2AngleSegmentRate : 0.1,
                b2StraightSegmentRate : 0.1,
                b2AngleSegmentRandom : 0,
                b2StraightSegmentRandom : 0,

                b2ShapeType : 5,                                                        // branch shape type 0-6
                b2ShapeMagnitude : -0.0275,                                                // how strongly the branch changes its shape angle
                b2ShapeMagnitudeRandom : 0.5,
                b2ShapeBeginningPart : 0.02,
                b2ShapeBeginningMagnitude : 28,

                b2RandomAngle : 30    *Math.PI/180,
                b2AngleChangeRate : 0.6,                                              // 1 everytime, 0 non

                b2InitialAngle : 25                  *Math.PI/180,          // branch initial angle when the branch is low
                b2InitialAngleRandom : 0       *Math.PI/180,          // branch initial angle randomize

                b2HeightBeforeBranchHappens : 0,
                b2HeightBeforeBranchHappensRandom : 0,

                b2LowBranchForceUpDown : 0,                       // 0.. normal    if >0, some lower part b2 branch goes up  (if =1, every branch goes up),  if <0, some lower part b2 branch goes down

                branchGeneration : 3,
                
                leavesStartsGeneration : 2,
                
                leavesVolume : 20,
                leavesRandom : 10,
                leavesRate : 1,               // how much leaves
                
                leavesPattern : 0,
                
                leavesSize : 0.22,                 // leaves size
                
                leavesGroupShading : true,                  //  in a small group, it shades accordingly
                leavesHighlight : 0.085,              // if this is >0, some leaves would be brighter than the original color
                leavesShade : 3.75,                // leaves brightness (brighter -> less contrast)  default: 3.75

                leavesDirectionRandomness : 120,     // leaves direction   if 0, leaves don't rotate.  if 360, totally random

                leavesR : 122/256,
                leavesG : 143/256,
                leavesB : 79/256,
                
                makeBranchLeaves : false,            // branches become leaves color

                stemTexShading : true,
                stemTexRandomness : 0.5,
                stemTexStripeRatio : 0.1,               // texture stripe ratio  0.. non,   1.. max    the stripes would be covered by the other texture
                stemTexRatio : 0.25,                         // rough texture on the stem
                stemTexDapples : true,                   // dapples on the texture
                stemTexRoughness : 0.002,            // texture roughness    default should be 0.002
                stemTexPatternLength : 3,               // vertical texture length

                stemHTexRate : 0.4,
                stemHTexRoughness : 0.001,           // holizontal texture roughness    default should be 0.001
                stemHTexOpacity : 0,                   // holizontal texture opacity
                stemHTexSize : 15,                          // 3 - 15
          
                stemR : 100/256,                        // stem color
                stemG : 86/256,
                stemB : 74/256,
                
                baseArrangement : 4,
                
            };
            break;
           
        case 11:                 //Ash Tree
            tree = {
                
                name : "ash",
                stemIntensity : 2,
                leavesIntensity : 2.4,
                
                viewAngle : 5    *Math.PI/180,    //   0... from side      90... from top

                s : 0.65,    // scale      it effects everything
                d : 1.2,    // detail     how much detail

                dimensionRatio : 12/11,       //   y/x

                lightDirection : (180 + 180)      *Math.PI/180,      // light direction

                sLength : 1000,                 // if s = 1, this is pixel
                sLengthStop : 0.65,              // If this is less than 1, it stops growing.  , then 1st branch continues.
                sLengthStopDiverse : 0.2,    // if this is not zero, 3 branches happen.  and it splits based on this number.
                sInitialWidth : 25,                 // stump initial width

                stumpHeight : 50,       // stump part
                stumpShape : 2,          //   stump shape  1-3
                stumpShapeRatio : 4,    //  how much it is thicker on the bottom

                stemSegmentNumber : 400,    // how many segments on the tree?
                stemAngleSegmentRatio : 0.1,  // stem angle segment : stem straight segment

                stemAngleSegmentRandom : 0.5,      // how much change on stemAngleSegment (longer/shorter)
                stemStraightSegmentRandom : 0.5,   // how much change on stemStraightSegment (longer/shorter)

                stemInitialAngleRandom : 0         *Math.PI/180,            //

                stemRandomAngle : 5     *Math.PI/180,
                stemMaxAngle : 2           *Math.PI/180,                     // if stem angle becomes bigger than this, next angle will change to go up
                stemAngleChangeRate : 0.5,                                           // how often the angle changes?   0... no change   1.. always change

                stemHeightBeforeAngleChange : 50,                               // Stem goes straight before this height
                stemHeightBeforeAngleChangeRandom : 100,                     // plus minus this value
                
                stemSurfaceNoiseLevel : 0.5,                                               // less or more bumps

                stemHeightBeforeBranch : 170,
                stemHeightbeforePalmBump : 100000,
                
                stemHeightOfShadow : 0.25,                                          // leaves cast shadow on the stem higher than this height

                
                b1LengthRate : 0.65,                                                          // If this is 0.5, length of the first branch is 50% of sLength
                b1LengthStop : 0.9,                                                     // If this is less than 1, it stops growing. , then 2nd branch continues.
                b1LengthStopDiverse : 0.5,                                          // if this is not zero, 3 second branches happen.  and it splits based on this number.
                b1ReduceRatioType : 3,                                                      // How the first branches change their length based on the stem height 0 - 6
                b1InitialWidth : 0.5,                                                    //  based on the stem width
                
                b1Rate : 1,                                                              // 
                b1AngleSegmentRate : 0.2,
                b1StraightSegmentRate : 0.2,
                b1AngleSegmentRandom : 0.5,
                b1StraightSegmentRandom : 0.5,

                b1ShapeType : 4,                                                        // branch shape type 0-6
                b1ShapeMagnitude : 0.0,                                                // how strongly the branch changes its shape angle
                b1ShapeMagnitudeRandom : 0.0,
                b1ShapeBeginningPart : 0.04,
                b1ShapeBeginningMagnitude : 28,

                b1RandomAngle : 15    *Math.PI/180,
                b1AngleChangeRate : 0.5,                                              // 1 everytime, 0 non

                b1DirectionRandom : 0.02 ,                                            // direction randomness from golden angle

                b1InitialAngle : 10                  *Math.PI/180,          // branch initial angle when the branch is low
                b1InitialAngleChangeRate : 0,                                    // if this is >0, branch angle will change based on its height
                b1InitialAngleRandom : 0       *Math.PI/180,          // branch initial angle randomize
                
                b1HeightBeforeBranchHappens : 0.1,
                b1HeightBeforeBranchHappensRandom : 0,

                b1AngleMax : 5 *Math.PI/180,                            // 0 is holizontal angle.  if b1 branch is corrected if it becomes more than this number.  the number of willow trees should be 90

                b1CenterForce : 0,                                       // when it needs more leaves to cover the stem

                b2LengthRate : 0.35,
                b2ReduceRatioType : 2,                                                      // How the first branches change their length based on the stem height 0 - 6    
                b2InitialWidth : 0.4,   
                
                b2Rate : 0.35,
                b2AngleSegmentRate : 0.05,
                b2StraightSegmentRate : 0.05,
                b2AngleSegmentRandom : 0,
                b2StraightSegmentRandom : 0,

                b2ShapeType : 0,                                                        // branch shape type 0-6
                b2ShapeMagnitude : 1,                                                // how strongly the branch changes its shape angle
                b2ShapeMagnitudeRandom : 0,
                b2ShapeBeginningPart : 0.01,
                b2ShapeBeginningMagnitude : 28,

                b2RandomAngle : 10    *Math.PI/180,
                b2AngleChangeRate : 0.6,                                              // 1 everytime, 0 non

                b2InitialAngle : 45                  *Math.PI/180,          // branch initial angle when the branch is low
                b2InitialAngleRandom : 0       *Math.PI/180,          // branch initial angle randomize

                b2HeightBeforeBranchHappens : 0,
                b2HeightBeforeBranchHappensRandom : 0,

                b2LowBranchForceUpDown : 0.0,                       // 0.. normal    if >0, some lower part b2 branch goes up  (if =1, every branch goes up),  if <0, some lower part b2 branch goes down

                branchGeneration : 3,
                
                leavesStartsGeneration : 2,
                
                leavesVolume : 60,
                leavesRandom : 15,
                leavesRate : 1,               // how much leaves
                
                leavesPattern : 8,
                
                leavesSize : 0.25,                 // leaves size
                
                leavesGroupShading : true,                  //  in a small group, it shades accordingly
                leavesHighlight : 0.075,              // if this is >0, some leaves would be brighter than the original color
                leavesShade : 3.75,                // leaves brightness (brighter -> less contrast)  default: 3.75

                leavesDirectionRandomness : 90,     // leaves direction   if 0, leaves don't rotate.  if 360, totally random

                leavesR : 108/256,
                leavesG : 131/256,
                leavesB : 59/256,
                
                makeBranchLeaves : false,            // branches become leaves color

                stemTexShading : true,
                stemTexRandomness : 0.1,
                stemTexStripeRatio : 0.0,               // texture stripe ratio  0.. non,   1.. max    the stripes would be covered by the other texture
                stemTexRatio : 0.5,                         // rough texture on the stem
                stemTexDapples : true,                   // dapples on the texture
                stemTexRoughness : 0.002,            // texture roughness    default should be 0.002
                stemTexPatternLength : 5,               // vertical texture length

                stemHTexRate : 0.4,
                stemHTexRoughness : 0.001,           // holizontal texture roughness    default should be 0.001
                stemHTexOpacity : 0,                   // holizontal texture opacity
                stemHTexSize : 15,                          // 3 - 15
          
                stemR : 151/256,                        // stem color
                stemG : 146/256,
                stemB : 125/256,
              
                baseArrangement : 8,
              
            };
            break;

        case 12:                 //Young Ash Tree
            tree = {

                name : "youngAsh",
                stemIntensity : 2,
                leavesIntensity : 3.2,
                
                viewAngle : 5    *Math.PI/180,    //   0... from side      90... from top

                s : 0.85,    // scale      it effects everything
                d : 1.3,    // detail     how much detail

                dimensionRatio : 11.8/10,       //   y/x

                lightDirection : (180 + 180)      *Math.PI/180,      // light direction

                sLength : 1000,                 // if s = 1, this is pixel
                sLengthStop : 0.4,              // If this is less than 1, it stops growing.  , then 1st branch continues.
                sLengthStopDiverse : 0.2,    // if this is not zero, 3 branches happen.  and it splits based on this number.
                sInitialWidth : 18,                 // stump initial width

                stumpHeight : 50,       // stump part
                stumpShape : 2,          //   stump shape  1-3
                stumpShapeRatio : 4,    //  how much it is thicker on the bottom

                stemSegmentNumber : 400,    // how many segments on the tree?
                stemAngleSegmentRatio : 0.1,  // stem angle segment : stem straight segment

                stemAngleSegmentRandom : 0.5,      // how much change on stemAngleSegment (longer/shorter)
                stemStraightSegmentRandom : 0.5,   // how much change on stemStraightSegment (longer/shorter)

                stemInitialAngleRandom : 0         *Math.PI/180,            //

                stemRandomAngle : 5     *Math.PI/180,
                stemMaxAngle : 2           *Math.PI/180,                     // if stem angle becomes bigger than this, next angle will change to go up
                stemAngleChangeRate : 0.5,                                           // how often the angle changes?   0... no change   1.. always change

                stemHeightBeforeAngleChange : 50,                               // Stem goes straight before this height
                stemHeightBeforeAngleChangeRandom : 100,                     // plus minus this value
                
                stemSurfaceNoiseLevel : 0.5,                                               // less or more bumps

                stemHeightBeforeBranch : 300,
                stemHeightbeforePalmBump : 100000,
                
                stemHeightOfShadow : 0.85,                                          // leaves cast shadow on the stem higher than this height

                
                b1LengthRate : 0.5,                                                          // If this is 0.5, length of the first branch is 50% of sLength
                b1LengthStop : 0.9,                                                     // If this is less than 1, it stops growing. , then 2nd branch continues.
                b1LengthStopDiverse : 0.5,                                          // if this is not zero, 3 second branches happen.  and it splits based on this number.
                b1ReduceRatioType : 3,                                                      // How the first branches change their length based on the stem height 0 - 6
                b1InitialWidth : 0.5,                                                    //  based on the stem width
                
                b1Rate : 1.3,                                                              // 
                b1AngleSegmentRate : 0.2,
                b1StraightSegmentRate : 0.2,
                b1AngleSegmentRandom : 0.5,
                b1StraightSegmentRandom : 0.5,

                b1ShapeType : 4,                                                        // branch shape type 0-6
                b1ShapeMagnitude : 0.0,                                                // how strongly the branch changes its shape angle
                b1ShapeMagnitudeRandom : 0.0,
                b1ShapeBeginningPart : 0.04,
                b1ShapeBeginningMagnitude : 28,

                b1RandomAngle : 15    *Math.PI/180,
                b1AngleChangeRate : 0.5,                                              // 1 everytime, 0 non

                b1DirectionRandom : 0.02 ,                                            // direction randomness from golden angle

                b1InitialAngle : 10                  *Math.PI/180,          // branch initial angle when the branch is low
                b1InitialAngleChangeRate : 0,                                    // if this is >0, branch angle will change based on its height
                b1InitialAngleRandom : 0       *Math.PI/180,          // branch initial angle randomize
                
                b1HeightBeforeBranchHappens : 0.1,
                b1HeightBeforeBranchHappensRandom : 0,

                b1AngleMax : 5 *Math.PI/180,                            // 0 is holizontal angle.  if b1 branch is corrected if it becomes more than this number.  the number of willow trees should be 90

                b1CenterForce : 0,                                       // when it needs more leaves to cover the stem

                b2LengthRate : 0.32,
                b2ReduceRatioType : 2,                                                      // How the first branches change their length based on the stem height 0 - 6    
                b2InitialWidth : 0.4,   
                
                b2Rate : 0.35,
                b2AngleSegmentRate : 0.05,
                b2StraightSegmentRate : 0.05,
                b2AngleSegmentRandom : 0,
                b2StraightSegmentRandom : 0,

                b2ShapeType : 0,                                                        // branch shape type 0-6
                b2ShapeMagnitude : 1,                                                // how strongly the branch changes its shape angle
                b2ShapeMagnitudeRandom : 0,
                b2ShapeBeginningPart : 0.01,
                b2ShapeBeginningMagnitude : 28,

                b2RandomAngle : 10    *Math.PI/180,
                b2AngleChangeRate : 0.6,                                              // 1 everytime, 0 non

                b2InitialAngle : 45                  *Math.PI/180,          // branch initial angle when the branch is low
                b2InitialAngleRandom : 0       *Math.PI/180,          // branch initial angle randomize

                b2HeightBeforeBranchHappens : 0,
                b2HeightBeforeBranchHappensRandom : 0,

                b2LowBranchForceUpDown : 0.0,                       // 0.. normal    if >0, some lower part b2 branch goes up  (if =1, every branch goes up),  if <0, some lower part b2 branch goes down

                branchGeneration : 3,
                
                leavesStartsGeneration : 2,
                
                leavesVolume : 60,
                leavesRandom : 15,
                leavesRate : 1,               // how much leaves
                
                leavesPattern : 8,
                
                leavesSize : 0.65,                 // leaves size
                
                leavesGroupShading : true,                  //  in a small group, it shades accordingly
                leavesHighlight : 0.075,              // if this is >0, some leaves would be brighter than the original color
                leavesShade : 3.75,                // leaves brightness (brighter -> less contrast)  default: 3.75

                leavesDirectionRandomness : 90,     // leaves direction   if 0, leaves don't rotate.  if 360, totally random

                leavesR : 108/256,
                leavesG : 131/256,
                leavesB : 59/256,
                
                makeBranchLeaves : false,            // branches become leaves color

                stemTexShading : true,
                stemTexRandomness : 0.1,
                stemTexStripeRatio : 0.0,               // texture stripe ratio  0.. non,   1.. max    the stripes would be covered by the other texture
                stemTexRatio : 0.5,                         // rough texture on the stem
                stemTexDapples : false,                   // dapples on the texture
                stemTexRoughness : 0.002,            // texture roughness    default should be 0.002
                stemTexPatternLength : 5,               // vertical texture length

                stemHTexRate : 0.4,
                stemHTexRoughness : 0.001,           // holizontal texture roughness    default should be 0.001
                stemHTexOpacity : 0,                   // holizontal texture opacity
                stemHTexSize : 15,                          // 3 - 15
          
                stemR : 151/256,                        // stem color
                stemG : 146/256,
                stemB : 125/256,
              
                baseArrangement : 5,
              
            };
            break;

        case 13:                 //Willow Tree
            tree = {
                
                name : "willow",
                stemIntensity : 2.2,
                leavesIntensity : 2.4,
                
                viewAngle : 5    *Math.PI/180,    //   0... from side      90... from top

                s : 0.75,    // scale      it effects everything
                d : 1,    // detail     how much detail

                dimensionRatio : 11.5/12.5,       //   y/x

                lightDirection : (180 + 180)      *Math.PI/180,      // light direction

                sLength : 1000,                 // if s = 1, this is pixel
                sLengthStop : 0.88,              // If this is less than 1, it stops growing.  , then 1st branch continues.
                sLengthStopDiverse : 0.5,    // if this is not zero, 3 branches happen.  and it splits based on this number.
                sInitialWidth : 25,                 // stump initial width

                stumpHeight : 50,       // stump part
                stumpShape : 2,          //   stump shape  1-3
                stumpShapeRatio : 4,    //  how much it is thicker on the bottom

                stemSegmentNumber : 400,    // how many segments on the tree?
                stemAngleSegmentRatio : 0.2,  // stem angle segment : stem straight segment

                stemAngleSegmentRandom : 0.15,      // how much change on stemAngleSegment (longer/shorter)
                stemStraightSegmentRandom : 2.25,   // how much change on stemStraightSegment (longer/shorter)

                stemInitialAngleRandom : 0         *Math.PI/180,            //

                stemRandomAngle : 40     *Math.PI/180,
                stemMaxAngle : 15           *Math.PI/180,                     // if stem angle becomes bigger than this, next angle will change to go up
                stemAngleChangeRate : 0.5,                                           // how often the angle changes?   0... no change   1.. always change

                stemHeightBeforeAngleChange : 200,                               // Stem goes straight before this height
                stemHeightBeforeAngleChangeRandom : 5,                     // plus minus this value
                
                stemSurfaceNoiseLevel : 0.5,                                               // less or more bumps

                stemHeightBeforeBranch : 280,
                stemHeightbeforePalmBump : 100000,
                
                stemHeightOfShadow : 0.25,                                          // leaves cast shadow on the stem higher than this height

                
                b1LengthRate : 0.82,                                                          // If this is 0.5, length of the first branch is 50% of sLength
                b1LengthStop : 0.82,                                                     // If this is less than 1, it stops growing. , then 2nd branch continues.
                b1LengthStopDiverse : 0,                                          // if this is not zero, 3 second branches happen.  and it splits based on this number.
                b1ReduceRatioType : 0,                                                      // How the first branches change their length based on the stem height 0 - 6
                b1InitialWidth : 0.5,                                                    //  based on the stem width
                
                b1Rate : 0.62,                                                              // 
                b1AngleSegmentRate : 0.2,
                b1StraightSegmentRate : 1,
                b1AngleSegmentRandom : 0.5,
                b1StraightSegmentRandom : 0.5,

                b1ShapeType : 3,                                                        // branch shape type 0-6
                b1ShapeMagnitude : -0.3,                                                // how strongly the branch changes its shape angle
                b1ShapeMagnitudeRandom : 0.01,
                b1ShapeBeginningPart : 0.14,
                b1ShapeBeginningMagnitude : 28,

                b1RandomAngle : 20    *Math.PI/180,
                b1AngleChangeRate : 5,                                              // 1 everytime, 0 non

                b1DirectionRandom : 0.0002 ,                                            // direction randomness from golden angle

                b1InitialAngle : 8                  *Math.PI/180,          // branch initial angle when the branch is low
                b1InitialAngleChangeRate : 0,                                    // if this is >0, branch angle will change based on its height
                b1InitialAngleRandom : 1       *Math.PI/180,          // branch initial angle randomize
                
                b1HeightBeforeBranchHappens : 0.12,
                b1HeightBeforeBranchHappensRandom : 0,

                b1AngleMax : 5 *Math.PI/180,                            // 0 is holizontal angle.  if b1 branch is corrected if it becomes more than this number.  the number of willow trees should be 90

                b1CenterForce : 0.1,                                       // when it needs more leaves to cover the stem

                b2LengthRate : 0.51,
                b2ReduceRatioType : 2,                                                      // How the first branches change their length based on the stem height 0 - 6    
                b2InitialWidth : 0.25,   
                
                b2Rate : 0.78,
                b2AngleSegmentRate : 0.1,
                b2StraightSegmentRate : 0.5,
                b2AngleSegmentRandom : 0,
                b2StraightSegmentRandom : 0,

                b2ShapeType : 5,                                                        // branch shape type 0-6
                b2ShapeMagnitude : -0.025,                                                // how strongly the branch changes its shape angle
                b2ShapeMagnitudeRandom : 0.5,
                b2ShapeBeginningPart : 0.02,
                b2ShapeBeginningMagnitude : 28,

                b2RandomAngle : 30    *Math.PI/180,
                b2AngleChangeRate : 0.6,                                              // 1 everytime, 0 non

                b2InitialAngle : 25                  *Math.PI/180,          // branch initial angle when the branch is low
                b2InitialAngleRandom : 10       *Math.PI/180,          // branch initial angle randomize

                b2HeightBeforeBranchHappens : 0,
                b2HeightBeforeBranchHappensRandom : 0,

                b2LowBranchForceUpDown : 0,                       // 0.. normal    if >0, some lower part b2 branch goes up  (if =1, every branch goes up),  if <0, some lower part b2 branch goes down

                branchGeneration : 2,
                
                leavesStartsGeneration : 1,
                
                leavesVolume : 0,
                leavesRandom : 0,
                leavesRate : 1,               // how much leaves
                
                leavesPattern : 9,
                
                leavesSize : 0.125,                 // leaves size
                
                leavesGroupShading : true,                  //  in a small group, it shades accordingly
                leavesHighlight : 0.04,              // if this is >0, some leaves would be brighter than the original color
                leavesShade : 3.75,                // leaves brightness (brighter -> less contrast)  default: 3.75

                leavesDirectionRandomness : 0,     // leaves direction   if 0, leaves don't rotate.  if 360, totally random

                leavesR : 150/256,
                leavesG : 163/256,
                leavesB : 90/256,
                
                makeBranchLeaves : false,            // branches become leaves color

                stemTexShading : true,
                stemTexRandomness : 0.5,
                stemTexStripeRatio : 0.05,               // texture stripe ratio  0.. non,   1.. max    the stripes would be covered by the other texture
                stemTexRatio : 0.5,                         // rough texture on the stem
                stemTexDapples : true,                   // dapples on the texture
                stemTexRoughness : 0.002,            // texture roughness    default should be 0.002
                stemTexPatternLength : 3,               // vertical texture length

                stemHTexRate : 0.4,
                stemHTexRoughness : 0.001,           // holizontal texture roughness    default should be 0.001
                stemHTexOpacity : 0,                   // holizontal texture opacity
                stemHTexSize : 15,                          // 3 - 15
          
                stemR : 100/256,                        // stem color
                stemG : 86/256,
                stemB : 74/256,
                
                baseArrangement : 15,
                
            };
            break;

        case 14:                 //Robinia
            tree = {
                
                name : "robinia",
                stemIntensity : 2,
                leavesIntensity : 2.4,
                
                viewAngle : 5    *Math.PI/180,    //   0... from side      90... from top

                s : 0.65,    // scale      it effects everything
                d : 0.9,    // detail     how much detail

                dimensionRatio : 12/11,       //   y/x

                lightDirection : (180 + 180)      *Math.PI/180,      // light direction

                sLength : 1000,                 // if s = 1, this is pixel
                sLengthStop : 0.75,              // If this is less than 1, it stops growing.  , then 1st branch continues.
                sLengthStopDiverse : 0.5,    // if this is not zero, 3 branches happen.  and it splits based on this number.
                sInitialWidth : 22,                 // stump initial width

                stumpHeight : 50,       // stump part
                stumpShape : 2,          //   stump shape  1-3
                stumpShapeRatio : 4,    //  how much it is thicker on the bottom

                stemSegmentNumber : 400,    // how many segments on the tree?
                stemAngleSegmentRatio : 0.05,  // stem angle segment : stem straight segment

                stemAngleSegmentRandom : 0.15,      // how much change on stemAngleSegment (longer/shorter)
                stemStraightSegmentRandom : 2.25,   // how much change on stemStraightSegment (longer/shorter)

                stemInitialAngleRandom : 0         *Math.PI/180,            //

                stemRandomAngle : 10     *Math.PI/180,
                stemMaxAngle : 5           *Math.PI/180,                     // if stem angle becomes bigger than this, next angle will change to go up
                stemAngleChangeRate : 0.8,                                           // how often the angle changes?   0... no change   1.. always change

                stemHeightBeforeAngleChange : 100,                               // Stem goes straight before this height
                stemHeightBeforeAngleChangeRandom : 5,                     // plus minus this value
                
                stemSurfaceNoiseLevel : 0.5,                                               // less or more bumps

                stemHeightBeforeBranch : 140,
                stemHeightbeforePalmBump : 100000,
                
                stemHeightOfShadow : 0.25,                                          // leaves cast shadow on the stem higher than this height

                
                b1LengthRate : 0.8,                                                          // If this is 0.5, length of the first branch is 50% of sLength
                b1LengthStop : 0.7,                                                     // If this is less than 1, it stops growing. , then 2nd branch continues.
                b1LengthStopDiverse : 0,                                          // if this is not zero, 3 second branches happen.  and it splits based on this number.
                b1ReduceRatioType : 2,                                                      // How the first branches change their length based on the stem height 0 - 6
                b1InitialWidth : 0.5,                                                    //  based on the stem width
                
                b1Rate : 0.75,                                                              // 
                b1AngleSegmentRate : 0.2,
                b1StraightSegmentRate : 1,
                b1AngleSegmentRandom : 0.5,
                b1StraightSegmentRandom : 0.5,

                b1ShapeType : 3,                                                        // branch shape type 0-6
                b1ShapeMagnitude : -2,                                                // how strongly the branch changes its shape angle
                b1ShapeMagnitudeRandom : 0.5,
                b1ShapeBeginningPart : 0.14,
                b1ShapeBeginningMagnitude : 28,

                b1RandomAngle : 15    *Math.PI/180,
                b1AngleChangeRate : 1,                                              // 1 everytime, 0 non

                b1DirectionRandom : 0.0002 ,                                            // direction randomness from golden angle

                b1InitialAngle : 30                  *Math.PI/180,          // branch initial angle when the branch is low
                b1InitialAngleChangeRate : 0.25,                                    // if this is >0, branch angle will change based on its height
                b1InitialAngleRandom : 20       *Math.PI/180,          // branch initial angle randomize
                
                b1HeightBeforeBranchHappens : 0.3,
                b1HeightBeforeBranchHappensRandom : 0,

                b1AngleMax : 5 *Math.PI/180,                            // 0 is holizontal angle.  if b1 branch is corrected if it becomes more than this number.  the number of willow trees should be 90

                b1CenterForce : 0.4,                                       // when it needs more leaves to cover the stem

                b2LengthRate : 0.225,
                b2ReduceRatioType : 6,                                                      // How the first branches change their length based on the stem height 0 - 6    
                b2InitialWidth : 0.25,   
                
                b2Rate : 1,
                b2AngleSegmentRate : 0.1,
                b2StraightSegmentRate : 0.1,
                b2AngleSegmentRandom : 0,
                b2StraightSegmentRandom : 0,

                b2ShapeType : 0,                                                        // branch shape type 0-6
                b2ShapeMagnitude : -0.4,                                                // how strongly the branch changes its shape angle
                b2ShapeMagnitudeRandom : 0.5,
                b2ShapeBeginningPart : 0.02,
                b2ShapeBeginningMagnitude : 28,

                b2RandomAngle : 20    *Math.PI/180,
                b2AngleChangeRate : 1,                                              // 1 everytime, 0 non

                b2InitialAngle : 45                  *Math.PI/180,          // branch initial angle when the branch is low
                b2InitialAngleRandom : 20       *Math.PI/180,          // branch initial angle randomize

                b2HeightBeforeBranchHappens : 0,
                b2HeightBeforeBranchHappensRandom : 0,

                b2LowBranchForceUpDown : 0,                       // 0.. normal    if >0, some lower part b2 branch goes up  (if =1, every branch goes up),  if <0, some lower part b2 branch goes down

                branchGeneration : 3,
                
                leavesStartsGeneration : 2,
                
                leavesVolume : 60,
                leavesRandom : 10,
                leavesRate : 1,               // how much leaves
                
                leavesPattern : 10,
                
                leavesSize : 0.155,                 // leaves size
                
                leavesGroupShading : true,                  //  in a small group, it shades accordingly
                leavesHighlight : 0.1,              // if this is >0, some leaves would be brighter than the original color
                leavesShade : 3.75,                // leaves brightness (brighter -> less contrast)  default: 3.75

                leavesDirectionRandomness : 180,     // leaves direction   if 0, leaves don't rotate.  if 360, totally random

                leavesR : 123/256,
                leavesG : 146/256,
                leavesB : 73/256,
                
                makeBranchLeaves : false,            // branches become leaves color

                stemTexShading : true,
                stemTexRandomness : 0.1,
                stemTexStripeRatio : 0.0,               // texture stripe ratio  0.. non,   1.. max    the stripes would be covered by the other texture
                stemTexRatio : 0.4,                         // rough texture on the stem
                stemTexDapples : true,                   // dapples on the texture
                stemTexRoughness : 0.001,            // texture roughness    default should be 0.002
                stemTexPatternLength : 2,               // vertical texture length

                stemHTexRate : 0.4,
                stemHTexRoughness : 0.001,           // holizontal texture roughness    default should be 0.001
                stemHTexOpacity : 0,                   // holizontal texture opacity
                stemHTexSize : 15,                          // 3 - 15
          
                stemR : 106/256,                        // stem color
                stemG : 88/256,
                stemB : 70/256,
                
                baseArrangement : 7,
                
            };
            break;
                      
        case 15:                 // Young Robinia
            tree = {

                name : "youngRobinia",
                stemIntensity : 1.2,
                leavesIntensity : 2,
                
                viewAngle : 5    *Math.PI/180,    //   0... from side      90... from top

                s : 0.67,    // scale      it effects everything
                d : 0.63,    // detail     how much detail

                dimensionRatio : 11/8.5,       //   y/x

                lightDirection : (180 + 180)      *Math.PI/180,      // light direction

                sLength : 1000,                 // if s = 1, this is pixel
                sLengthStop : 0.7,              // If this is less than 1, it stops growing.  , then 1st branch continues.
                sLengthStopDiverse : 0,    // if this is not zero, 3 branches happen.  and it splits based on this number.
                sInitialWidth : 22,                 // stump initial width

                stumpHeight : 50,       // stump part
                stumpShape : 2,          //   stump shape  1-3
                stumpShapeRatio : 4,    //  how much it is thicker on the bottom

                stemSegmentNumber : 400,    // how many segments on the tree?
                stemAngleSegmentRatio : 0.05,  // stem angle segment : stem straight segment

                stemAngleSegmentRandom : 0.15,      // how much change on stemAngleSegment (longer/shorter)
                stemStraightSegmentRandom : 2.25,   // how much change on stemStraightSegment (longer/shorter)

                stemInitialAngleRandom : 0         *Math.PI/180,            //

                stemRandomAngle : 10     *Math.PI/180,
                stemMaxAngle : 5           *Math.PI/180,                     // if stem angle becomes bigger than this, next angle will change to go up
                stemAngleChangeRate : 0.8,                                           // how often the angle changes?   0... no change   1.. always change

                stemHeightBeforeAngleChange : 100,                               // Stem goes straight before this height
                stemHeightBeforeAngleChangeRandom : 5,                     // plus minus this value
                
                stemSurfaceNoiseLevel : 0.5,                                               // less or more bumps

                stemHeightBeforeBranch : 260,
                stemHeightbeforePalmBump : 100000,
                
                stemHeightOfShadow : 0.25,                                          // leaves cast shadow on the stem higher than this height

                
                b1LengthRate : 0.6,                                                          // If this is 0.5, length of the first branch is 50% of sLength
                b1LengthStop : 0.7,                                                     // If this is less than 1, it stops growing. , then 2nd branch continues.
                b1LengthStopDiverse : 0,                                          // if this is not zero, 3 second branches happen.  and it splits based on this number.
                b1ReduceRatioType : 2,                                                      // How the first branches change their length based on the stem height 0 - 6
                b1InitialWidth : 0.5,                                                    //  based on the stem width
                
                b1Rate : 1,                                                              // 
                b1AngleSegmentRate : 0.2,
                b1StraightSegmentRate : 1,
                b1AngleSegmentRandom : 0.5,
                b1StraightSegmentRandom : 0.5,

                b1ShapeType : 3,                                                        // branch shape type 0-6
                b1ShapeMagnitude : -0.5,                                                // how strongly the branch changes its shape angle
                b1ShapeMagnitudeRandom : 0.5,
                b1ShapeBeginningPart : 0.14,
                b1ShapeBeginningMagnitude : 28,

                b1RandomAngle : 20    *Math.PI/180,
                b1AngleChangeRate : 5,                                              // 1 everytime, 0 non

                b1DirectionRandom : 0.0002 ,                                            // direction randomness from golden angle

                b1InitialAngle : 38                  *Math.PI/180,          // branch initial angle when the branch is low
                b1InitialAngleChangeRate : 0,                                    // if this is >0, branch angle will change based on its height
                b1InitialAngleRandom : 2       *Math.PI/180,          // branch initial angle randomize
                
                b1HeightBeforeBranchHappens : 0.25,
                b1HeightBeforeBranchHappensRandom : 0,

                b1AngleMax : 5 *Math.PI/180,                            // 0 is holizontal angle.  if b1 branch is corrected if it becomes more than this number.  the number of willow trees should be 90

                b1CenterForce : 0.4,                                       // when it needs more leaves to cover the stem

                b2LengthRate : 0.3,
                b2ReduceRatioType : 2,                                                      // How the first branches change their length based on the stem height 0 - 6    
                b2InitialWidth : 0.25,   
                
                b2Rate : 0.9,
                b2AngleSegmentRate : 0.1,
                b2StraightSegmentRate : 0.1,
                b2AngleSegmentRandom : 0,
                b2StraightSegmentRandom : 0,

                b2ShapeType : 3,                                                        // branch shape type 0-6
                b2ShapeMagnitude : -0.4,                                                // how strongly the branch changes its shape angle
                b2ShapeMagnitudeRandom : 0.5,
                b2ShapeBeginningPart : 0.02,
                b2ShapeBeginningMagnitude : 28,

                b2RandomAngle : 30    *Math.PI/180,
                b2AngleChangeRate : 0.6,                                              // 1 everytime, 0 non

                b2InitialAngle : 45                  *Math.PI/180,          // branch initial angle when the branch is low
                b2InitialAngleRandom : 0       *Math.PI/180,          // branch initial angle randomize

                b2HeightBeforeBranchHappens : 0.2,
                b2HeightBeforeBranchHappensRandom : 0,

                b2LowBranchForceUpDown : 0,                       // 0.. normal    if >0, some lower part b2 branch goes up  (if =1, every branch goes up),  if <0, some lower part b2 branch goes down

                branchGeneration : 2,
                
                leavesStartsGeneration : 2,
                
                leavesVolume : 30,
                leavesRandom : 10,
                leavesRate : 1,               // how much leaves
                
                leavesPattern : 10,
                
                leavesSize : 0.25,                 // leaves size
                
                leavesGroupShading : true,                  //  in a small group, it shades accordingly
                leavesHighlight : 0.075,              // if this is >0, some leaves would be brighter than the original color
                leavesShade : 3.75,                // leaves brightness (brighter -> less contrast)  default: 3.75

                leavesDirectionRandomness : 180,     // leaves direction   if 0, leaves don't rotate.  if 360, totally random

                leavesR : 165/256,
                leavesG : 199/256,
                leavesB : 94/256,
                
                makeBranchLeaves : false,            // branches become leaves color

                stemTexShading : true,
                stemTexRandomness : 0.1,
                stemTexStripeRatio : 0.0,               // texture stripe ratio  0.. non,   1.. max    the stripes would be covered by the other texture
                stemTexRatio : 0.1,                         // rough texture on the stem
                stemTexDapples : false,                   // dapples on the texture
                stemTexRoughness : 0.0005,            // texture roughness    default should be 0.002
                stemTexPatternLength : 2,               // vertical texture length

                stemHTexRate : 0.4,
                stemHTexRoughness : 0.001,           // holizontal texture roughness    default should be 0.001
                stemHTexOpacity : 0,                   // holizontal texture opacity
                stemHTexSize : 15,                          // 3 - 15
          
                stemR : 168/256,                        // stem color
                stemG : 164/256,
                stemB : 140/256,
                
                baseArrangement : 5,
                
            };
            break;     
            
        case 16:                 //Sakura Tree
            tree = {
                
                name : "sakura",
                stemIntensity : 3,
                leavesIntensity : 2.1,
                
                viewAngle : 5    *Math.PI/180,    //   0... from side      90... from top

                s : 0.9,    // scale      it effects everything
                d : 1.1,    // detail     how much detail

                dimensionRatio : 11/12.5,       //   y/x

                lightDirection : (180 + 180)      *Math.PI/180,      // light direction

                sLength : 1000,                 // if s = 1, this is pixel
                sLengthStop : 0.5,              // If this is less than 1, it stops growing.  , then 1st branch continues.
                sLengthStopDiverse : 0.3,    // if this is not zero, 3 branches happen.  and it splits based on this number.
                sInitialWidth : 18,                 // stump initial width

                stumpHeight : 50,       // stump part
                stumpShape : 2,          //   stump shape  1-3
                stumpShapeRatio : 4,    //  how much it is thicker on the bottom

                stemSegmentNumber : 400,    // how many segments on the tree?
                stemAngleSegmentRatio : 0.5,  // stem angle segment : stem straight segment

                stemAngleSegmentRandom : 0.5,      // how much change on stemAngleSegment (longer/shorter)
                stemStraightSegmentRandom : 0.5,   // how much change on stemStraightSegment (longer/shorter)

                stemInitialAngleRandom : 0         *Math.PI/180,            //

                stemRandomAngle : 12     *Math.PI/180,
                stemMaxAngle : 7.5           *Math.PI/180,                     // if stem angle becomes bigger than this, next angle will change to go up
                stemAngleChangeRate : 0.5,                                           // how often the angle changes?   0... no change   1.. always change

                stemHeightBeforeAngleChange : 50,                               // Stem goes straight before this height
                stemHeightBeforeAngleChangeRandom : 100,                     // plus minus this value
                
                stemSurfaceNoiseLevel : 1.2,                                               // less or more bumps

                stemHeightBeforeBranch : 120,
                stemHeightbeforePalmBump : 100000,
                
                stemHeightOfShadow : 0.25,                                          // leaves cast shadow on the stem higher than this height

                
                b1LengthRate : 0.5,                                                          // If this is 0.5, length of the first branch is 50% of sLength
                b1LengthStop : 0.8,                                                     // If this is less than 1, it stops growing. , then 2nd branch continues.
                b1LengthStopDiverse : 0.5,                                          // if this is not zero, 3 second branches happen.  and it splits based on this number.
                b1ReduceRatioType : 0,                                                      // How the first branches change their length based on the stem height 0 - 6
                b1InitialWidth : 0.5,                                                    //  based on the stem width
                
                b1Rate : 1.3,                                                              // 
                b1AngleSegmentRate : 0.2,
                b1StraightSegmentRate : 0.2,
                b1AngleSegmentRandom : 0.5,
                b1StraightSegmentRandom : 0.5,

                b1ShapeType : 1,                                                        // branch shape type 0-6
                b1ShapeMagnitude : 0.75,                                                // how strongly the branch changes its shape angle
                b1ShapeMagnitudeRandom : 0.0,
                b1ShapeBeginningPart : 0.065,
                b1ShapeBeginningMagnitude : 28,

                b1RandomAngle : 15    *Math.PI/180,
                b1AngleChangeRate : 0.5,                                              // 1 everytime, 0 non

                b1DirectionRandom : 0.02 ,                                            // direction randomness from golden angle

                b1InitialAngle : 10                  *Math.PI/180,          // branch initial angle when the branch is low
                b1InitialAngleChangeRate : 0,                                    // if this is >0, branch angle will change based on its height
                b1InitialAngleRandom : 0       *Math.PI/180,          // branch initial angle randomize
                
                b1HeightBeforeBranchHappens : 0,
                b1HeightBeforeBranchHappensRandom : 0,

                b1AngleMax : 5 *Math.PI/180,                            // 0 is holizontal angle.  if b1 branch is corrected if it becomes more than this number.  the number of willow trees should be 90

                b1CenterForce : 0,                                       // when it needs more leaves to cover the stem

                b2LengthRate : 0.45,
                b2ReduceRatioType : 2,                                                      // How the first branches change their length based on the stem height 0 - 6    
                b2InitialWidth : 0.5,   
                
                b2Rate : 0.425,
                b2AngleSegmentRate : 0.1,
                b2StraightSegmentRate : 0.1,
                b2AngleSegmentRandom : 0,
                b2StraightSegmentRandom : 0,

                b2ShapeType : 4,                                                        // branch shape type 0-6
                b2ShapeMagnitude : 1,                                                // how strongly the branch changes its shape angle
                b2ShapeMagnitudeRandom : 0,
                b2ShapeBeginningPart : 0.01,
                b2ShapeBeginningMagnitude : 28,

                b2RandomAngle : 30    *Math.PI/180,
                b2AngleChangeRate : 0.6,                                              // 1 everytime, 0 non

                b2InitialAngle : 25                  *Math.PI/180,          // branch initial angle when the branch is low
                b2InitialAngleRandom : 0       *Math.PI/180,          // branch initial angle randomize

                b2HeightBeforeBranchHappens : 0,
                b2HeightBeforeBranchHappensRandom : 0,

                b2LowBranchForceUpDown : 0.4,                       // 0.. normal    if >0, some lower part b2 branch goes up  (if =1, every branch goes up),  if <0, some lower part b2 branch goes down

                branchGeneration : 3,
                
                leavesStartsGeneration : 2,
                
                leavesVolume : 75,
                leavesRandom : 5,
                leavesRate : 1,               // how much leaves
                
                leavesPattern : 11,
                
                leavesSize : 0.3,                 // leaves size
                
                leavesGroupShading : true,                  //  in a small group, it shades accordingly
                leavesHighlight : 0.05,              // if this is >0, some leaves would be brighter than the original color
                leavesShade : 7.75,                // leaves brightness (brighter -> less contrast)  default: 3.75

                leavesDirectionRandomness : 360,     // leaves direction   if 0, leaves don't rotate.  if 360, totally random

                leavesR : 255/256,
                leavesG : 175/256,
                leavesB : 224/256,
                
                makeBranchLeaves : false,            // branches become leaves color

                stemTexShading : true,
                stemTexRandomness : 0.75,
                stemTexStripeRatio : 0.25,               // texture stripe ratio  0.. non,   1.. max    the stripes would be covered by the other texture
                stemTexRatio : 0.25,                         // rough texture on the stem
                stemTexDapples : true,                   // dapples on the texture
                stemTexRoughness : 0.003,            // texture roughness    default should be 0.002
                stemTexPatternLength : 2,               // vertical texture length

                stemHTexRate : 0.4,
                stemHTexRoughness : 0.001,           // holizontal texture roughness    default should be 0.001
                stemHTexOpacity : 0,                   // holizontal texture opacity
                stemHTexSize : 15,                          // 3 - 15
          
                stemR : 87/256,                        // stem color
                stemG : 74/256,
                stemB : 54/256,
                
                baseArrangement : 4,
                
            };
            break;

        case 17:                 //Ficus Microcarpa
            tree = {
                
                name : "ficusMicrocarpa",
                stemIntensity : 2,
                leavesIntensity : 2.4,
                
                viewAngle : 5    *Math.PI/180,    //   0... from side      90... from top

                s : 0.85,    // scale      it effects everything
                d : 1.9,    // detail     how much detail

                dimensionRatio : 8.2/12.5,       //   y/x

                lightDirection : (180 + 180)      *Math.PI/180,      // light direction

                sLength : 1000,                 // if s = 1, this is pixel
                sLengthStop : 0.5,              // If this is less than 1, it stops growing.  , then 1st branch continues.
                sLengthStopDiverse : 0.0,    // if this is not zero, 3 branches happen.  and it splits based on this number.
                sInitialWidth : 26,                 // stump initial width

                stumpHeight : 50,       // stump part
                stumpShape : 2,          //   stump shape  1-3
                stumpShapeRatio : 4,    //  how much it is thicker on the bottom

                stemSegmentNumber : 400,    // how many segments on the tree?
                stemAngleSegmentRatio : 0.5,  // stem angle segment : stem straight segment

                stemAngleSegmentRandom : 0.5,      // how much change on stemAngleSegment (longer/shorter)
                stemStraightSegmentRandom : 0.5,   // how much change on stemStraightSegment (longer/shorter)

                stemInitialAngleRandom : 0         *Math.PI/180,            //

                stemRandomAngle : 10     *Math.PI/180,
                stemMaxAngle : 7           *Math.PI/180,                     // if stem angle becomes bigger than this, next angle will change to go up
                stemAngleChangeRate : 0.5,                                           // how often the angle changes?   0... no change   1.. always change

                stemHeightBeforeAngleChange : 50,                               // Stem goes straight before this height
                stemHeightBeforeAngleChangeRandom : 100,                     // plus minus this value
                
                stemSurfaceNoiseLevel : 1,                                               // less or more bumps

                stemHeightBeforeBranch : 200,
                stemHeightbeforePalmBump : 100000,
                
                stemHeightOfShadow : 0.25,                                          // leaves cast shadow on the stem higher than this height

                
                b1LengthRate : 1.75,                                                          // If this is 0.5, length of the first branch is 50% of sLength
                b1LengthStop : 0.7,                                                     // If this is less than 1, it stops growing. , then 2nd branch continues.
                b1LengthStopDiverse : 0.3,                                          // if this is not zero, 3 second branches happen.  and it splits based on this number.
                b1ReduceRatioType : 1,                                                      // How the first branches change their length based on the stem height 0 - 6
                b1InitialWidth : 0.65,                                                    //  based on the stem width
                
                b1Rate : 1,                                                              // 
                b1AngleSegmentRate : 0.2,
                b1StraightSegmentRate : 0.2,
                b1AngleSegmentRandom : 0.5,
                b1StraightSegmentRandom : 0.5,

                b1ShapeType : 4,                                                        // branch shape type 0-6
                b1ShapeMagnitude : 0.2,                                                // how strongly the branch changes its shape angle
                b1ShapeMagnitudeRandom : 0.0,
                b1ShapeBeginningPart : 0.2,
                b1ShapeBeginningMagnitude : 6,

                b1RandomAngle : 8   *Math.PI/180,
                b1AngleChangeRate : 0.5,                                              // 1 everytime, 0 non

                b1DirectionRandom : 0.02 ,                                            // direction randomness from golden angle

                b1InitialAngle : 15                  *Math.PI/180,          // branch initial angle when the branch is low
                b1InitialAngleChangeRate : -1.6,                                    // if this is >0, branch angle will change based on its height
                b1InitialAngleRandom : 0       *Math.PI/180,          // branch initial angle randomize
                
                b1HeightBeforeBranchHappens : 0.2,
                b1HeightBeforeBranchHappensRandom : 0,

                b1AngleMax : 5 *Math.PI/180,                            // 0 is holizontal angle.  if b1 branch is corrected if it becomes more than this number.  the number of willow trees should be 90

                b1CenterForce : 0.1,                                       // when it needs more leaves to cover the stem

                b2LengthRate : 0.3,
                b2ReduceRatioType : 6,                                                      // How the first branches change their length based on the stem height 0 - 6    
                b2InitialWidth : 0.5,   

                b2Rate : 0.3,
                b2AngleSegmentRate : 0.1,
                b2StraightSegmentRate : 0.1,
                b2AngleSegmentRandom : 0,
                b2StraightSegmentRandom : 0,

                b2ShapeType : 1,                                                        // branch shape type 0-6
                b2ShapeMagnitude : 1,                                                // how strongly the branch changes its shape angle
                b2ShapeMagnitudeRandom : 1,
                b2ShapeBeginningPart : 0.01,
                b2ShapeBeginningMagnitude : 28,

                b2RandomAngle : 15    *Math.PI/180,
                b2AngleChangeRate : 0.6,                                              // 1 everytime, 0 non

                b2InitialAngle : 30                  *Math.PI/180,          // branch initial angle when the branch is low
                b2InitialAngleRandom : 0       *Math.PI/180,          // branch initial angle randomize

                b2HeightBeforeBranchHappens : 0,
                b2HeightBeforeBranchHappensRandom : 0,

                b2LowBranchForceUpDown : 0.7,                       // 0.. normal    if >0, some lower part b2 branch goes up  (if =1, every branch goes up),  if <0, some lower part b2 branch goes down

                branchGeneration : 2,
                
                leavesStartsGeneration : 2,
                
                leavesVolume : 50,
                leavesRandom : 5,
                leavesRate : 1,               // how much leaves
                
                leavesPattern : 7,
                
                leavesSize : 0.5,                 // leaves size
                
                leavesGroupShading : true,                  //  in a small group, it shades accordingly
                leavesHighlight : 0.09,              // if this is >0, some leaves would be brighter than the original color
                leavesShade : 3.75,                // leaves brightness (brighter -> less contrast)  default: 3.75

                leavesDirectionRandomness : 360,     // leaves direction   if 0, leaves don't rotate.  if 360, totally random

                leavesR : 108/256,
                leavesG : 131/256,
                leavesB : 59/256,
                
                makeBranchLeaves : false,            // branches become leaves color

                stemTexShading : true,
                stemTexRandomness : 0.5,
                stemTexStripeRatio : 0.5,               // texture stripe ratio  0.. non,   1.. max    the stripes would be covered by the other texture
                stemTexRatio : 0.25,                         // rough texture on the stem
                stemTexDapples : true,                   // dapples on the texture
                stemTexRoughness : 0.002,            // texture roughness    default should be 0.002
                stemTexPatternLength : 3,               // vertical texture length

                stemHTexRate : 0.4,
                stemHTexRoughness : 0.001,           // holizontal texture roughness    default should be 0.001
                stemHTexOpacity : 0,                   // holizontal texture opacity
                stemHTexSize : 15,                          // 3 - 15
          
                stemR : 137/256,                        // stem color
                stemG : 130/256,
                stemB : 101/256,
                
                baseArrangement : 2,
                
            };
            break;

        case 18:                 // Fraxinus Griffithii      (Japanese name : Shimatorineko)
            tree = {
                
                name : "fraxinusGriffithii",
                stemIntensity : 1,
                leavesIntensity : 2.4,
                
                viewAngle : 5    *Math.PI/180,    //   0... from side      90... from top

                s : 0.9,    // scale      it effects everything
                d : 1,    // detail     how much detail

                dimensionRatio : 11.8/9,       //   y/x

                lightDirection : (  180 + 180)      *Math.PI/180,      // light direction

                sLength : 900,       // if s = 1, this is pixel
                sLengthStop : 0.95,        // If this is less than 1, it stops growing.  (not very spercific) , then 1st branch continues on the end.
                sLengthStopDiverse : 0,    // if this is not zero, 3 branches happen.  and it splits based on this number.
                sInitialWidth : 10,     // stump initial width

                stumpHeight : 80,       // stump part
                stumpShape : 1,          //   stump shape  1-3
                stumpShapeRatio : 2,    //  how much it is thicker on the bottom

                stemSegmentNumber : 400,    // how many segments on the tree?
                stemAngleSegmentRatio : 0.5,  // stem angle segment : stem straight segment

                stemAngleSegmentRandom : 0.5,      // how much change on stemAngleSegment (longer/shorter)
                stemStraightSegmentRandom : 0.5,   // how much change on stemStraightSegment (longer/shorter)

                stemInitialAngleRandom : 0         *Math.PI/180,            //

                stemRandomAngle : 5     *Math.PI/180,
                stemMaxAngle : 2           *Math.PI/180,                     // if stem angle becomes bigger than this, next angle will change to go up
                stemAngleChangeRate : 0.5,                                           // how often the angle changes?   0... no change   1.. always change

                stemHeightBeforeAngleChange : 50,                               // Stem goes straight before this height
                stemHeightBeforeAngleChangeRandom : 100,                     // plus minus this value
                
                stemSurfaceNoiseLevel : 0.1,                                               // less or more bumps

                stemHeightBeforeBranch : 75,
                stemHeightbeforePalmBump : 100000,
                
                stemHeightOfShadow : 0.25,                                          // leaves cast shadow on the stem higher than this height

                
                b1LengthRate : 0.8,                                                          // If this is 0.5, length of the first branch is 50% of sLength
                b1LengthStop : 0.8,                                                     // If this is less than 1, it stops growing. , then 2nd branch continues.
                b1LengthStopDiverse : 0.5,                                          // if this is not zero, 3 second branches happen.  and it splits based on this number.
                b1ReduceRatioType : 0,                                                      // How the first branches change their length based on the stem height 0 - 6
                b1InitialWidth : 0.5,                                                    //  based on the stem width
                
                b1Rate : 0.4,                                                              // 
                b1AngleSegmentRate : 0.2,
                b1StraightSegmentRate : 0.2,
                b1AngleSegmentRandom : 1,
                b1StraightSegmentRandom : 1,

                b1ShapeType : 4,                                                        // branch shape type 0-6
                b1ShapeMagnitude : 0.25,                                                // how strongly the branch changes its shape angle
                b1ShapeMagnitudeRandom : 0.0,
                b1ShapeBeginningPart : 0.02,
                b1ShapeBeginningMagnitude : 14,

                b1RandomAngle : 10    *Math.PI/180,
                b1AngleChangeRate : 10,                                              // 1 everytime, 0 non

                b1DirectionRandom : 2 ,                                            // direction randomness from golden angle

                b1InitialAngle : 5                  *Math.PI/180,          // branch initial angle when the branch is low
                b1InitialAngleChangeRate : -0.7,                                    // if this is >0, branch angle will change based on its height
                b1InitialAngleRandom : 5       *Math.PI/180,          // branch initial angle randomize
                
                b1HeightBeforeBranchHappens : 0.25,
                b1HeightBeforeBranchHappensRandom : 0.1,

                b1AngleMax : 5 *Math.PI/180,                            // 0 is holizontal angle.  if b1 branch is corrected if it becomes more than this number.  the number of willow trees should be 90

                b1CenterForce : 0.75,                                       // when it needs more leaves to cover the stem

                b2LengthRate : 0.25,
                b2ReduceRatioType : 2,                                                      // How the first branches change their length based on the stem height 0 - 6    
                b2InitialWidth : 0.5,     
                
                b2Rate : 0.9,
                b2AngleSegmentRate : 0.15,
                b2StraightSegmentRate : 0.15,
                b2AngleSegmentRandom : 0.05,
                b2StraightSegmentRandom : 0.05,

                b2ShapeType : 3,                                                        // branch shape type 0-6
                b2ShapeMagnitude : -2,                                                // how strongly the branch changes its shape angle
                b2ShapeMagnitudeRandom : 0,
                b2ShapeBeginningPart : 0.2,
                b2ShapeBeginningMagnitude : 28,

                b2RandomAngle : 10    *Math.PI/180,
                b2AngleChangeRate : 0.5,                                              // 1 everytime, 0 non

                b2InitialAngle : 45                  *Math.PI/180,          // branch initial angle when the branch is low
                b2InitialAngleRandom : 0       *Math.PI/180,          // branch initial angle randomize

                b2HeightBeforeBranchHappens : 0.25,
                b2HeightBeforeBranchHappensRandom : 0.1,

                b2LowBranchForceUpDown : 0,                       // 0.. normal    if >0, some lower part b2 branch goes up  (if =1, every branch goes up),  if <0, some lower part b2 branch goes down

                branchGeneration : 3,
                
                leavesStartsGeneration : 2,
                
                leavesVolume : 50,
                leavesRandom : 20,
                leavesRate : 0,               // how much leaves
                
                leavesPattern : 7,
                
                leavesSize : 0.5,                 // leaves size 
                
                leavesGroupShading : true,                  //  in a small group, it shades accordingly
                leavesHighlight : 0.1,              // if this is >0, some leaves would be brighter than the original color
                leavesShade : 3.75,                // leaves brightness (brighter -> less contrast)  default: 3.75

                leavesDirectionRandomness : 180,     // leaves direction   if 0, leaves don't rotate.  if 360, totally random

                leavesR : 174/256,
                leavesG : 200/256,
                leavesB : 74/256,
                
                makeBranchLeaves : false,            // branches become leaves color
                
                stemTexShading : true,
                stemTexRandomness : 0.5,
                stemTexStripeRatio : 0.25,               // texture stripe ratio  0.. non,   1.. max    the stripes would be covered by the other texture
                stemTexRatio : 0.5,                         // rough texture on the stem
                stemTexDapples : false,                   // dapples on the texture
                stemTexRoughness : 0.002,            // texture roughness    default should be 0.002
                stemTexPatternLength : 3,               // vertical texture length

                stemHTexRate : 0,
                stemHTexRoughness : 0.001,           // holizontal texture roughness    default should be 0.001
                stemHTexOpacity : 0,                   // holizontal texture opacity
                stemHTexSize : 15,                          // 3 - 15

                stemR : 192/256,                        // stem color
                stemG : 186/256,
                stemB : 175/256,   
                
                baseArrangement : 3,
                
            };
            break;

        case 19:                 // Acer maximowiczianum
            tree = {
                
                name : "acerMaximowiczianum",
                stemIntensity : 1,
                leavesIntensity : 2.4,
                
                viewAngle : 5    *Math.PI/180,    //   0... from side      90... from top

                s : 0.8,    // scale      it effects everything
                d : 1,    // detail     how much detail

                dimensionRatio : 12/12.5,       //   y/x

                lightDirection : (  180 + 180)      *Math.PI/180,      // light direction

                sLength : 900,       // if s = 1, this is pixel
                sLengthStop : 0.5,        // If this is less than 1, it stops growing.  (not very spercific) , then 1st branch continues on the end.
                sLengthStopDiverse : 0,    // if this is not zero, 3 branches happen.  and it splits based on this number.
                sInitialWidth : 16,     // stump initial width

                stumpHeight : 80,       // stump part
                stumpShape : 1,          //   stump shape  1-3
                stumpShapeRatio : 2,    //  how much it is thicker on the bottom

                stemSegmentNumber : 400,    // how many segments on the tree?
                stemAngleSegmentRatio : 0.5,  // stem angle segment : stem straight segment

                stemAngleSegmentRandom : 0.5,      // how much change on stemAngleSegment (longer/shorter)
                stemStraightSegmentRandom : 0.5,   // how much change on stemStraightSegment (longer/shorter)

                stemInitialAngleRandom : 0         *Math.PI/180,            //

                stemRandomAngle : 5     *Math.PI/180,
                stemMaxAngle : 2           *Math.PI/180,                     // if stem angle becomes bigger than this, next angle will change to go up
                stemAngleChangeRate : 0.5,                                           // how often the angle changes?   0... no change   1.. always change

                stemHeightBeforeAngleChange : 50,                               // Stem goes straight before this height
                stemHeightBeforeAngleChangeRandom : 100,                     // plus minus this value
                
                stemSurfaceNoiseLevel : 0.1,                                               // less or more bumps

                stemHeightBeforeBranch : 75,
                stemHeightbeforePalmBump : 100000,
                
                stemHeightOfShadow : 0.25,                                          // leaves cast shadow on the stem higher than this height

                
                b1LengthRate : 1,                                                          // If this is 0.5, length of the first branch is 50% of sLength
                b1LengthStop : 0.8,                                                     // If this is less than 1, it stops growing. , then 2nd branch continues.
                b1LengthStopDiverse : 0.5,                                          // if this is not zero, 3 second branches happen.  and it splits based on this number.
                b1ReduceRatioType : 0,                                                      // How the first branches change their length based on the stem height 0 - 6
                b1InitialWidth : 0.5,                                                    //  based on the stem width
                
                b1Rate : 0.65,                                                              // 
                b1AngleSegmentRate : 0.2,
                b1StraightSegmentRate : 0.2,
                b1AngleSegmentRandom : 1,
                b1StraightSegmentRandom : 1,

                b1ShapeType : 4,                                                        // branch shape type 0-6
                b1ShapeMagnitude : 0.25,                                                // how strongly the branch changes its shape angle
                b1ShapeMagnitudeRandom : 0.0,
                b1ShapeBeginningPart : 0.02,
                b1ShapeBeginningMagnitude : 14,

                b1RandomAngle : 15    *Math.PI/180,
                b1AngleChangeRate : 10,                                              // 1 everytime, 0 non

                b1DirectionRandom : 2 ,                                            // direction randomness from golden angle

                b1InitialAngle : 7                  *Math.PI/180,          // branch initial angle when the branch is low
                b1InitialAngleChangeRate : -0.7,                                    // if this is >0, branch angle will change based on its height
                b1InitialAngleRandom : 2       *Math.PI/180,          // branch initial angle randomize
                
                b1HeightBeforeBranchHappens : 0.4,
                b1HeightBeforeBranchHappensRandom : 0.1,

                b1AngleMax : 5 *Math.PI/180,                            // 0 is holizontal angle.  if b1 branch is corrected if it becomes more than this number.  the number of willow trees should be 90

                b1CenterForce : 0.75,                                       // when it needs more leaves to cover the stem

                b2LengthRate : 0.25,
                b2ReduceRatioType : 2,                                                      // How the first branches change their length based on the stem height 0 - 6    
                b2InitialWidth : 0.5,     
                
                b2Rate : 0.8,
                b2AngleSegmentRate : 0.15,
                b2StraightSegmentRate : 0.15,
                b2AngleSegmentRandom : 0.05,
                b2StraightSegmentRandom : 0.05,

                b2ShapeType : 3,                                                        // branch shape type 0-6
                b2ShapeMagnitude : -2,                                                // how strongly the branch changes its shape angle
                b2ShapeMagnitudeRandom : 0,
                b2ShapeBeginningPart : 0.2,
                b2ShapeBeginningMagnitude : 28,

                b2RandomAngle : 7    *Math.PI/180,
                b2AngleChangeRate : 0.3,                                              // 1 everytime, 0 non

                b2InitialAngle : 45                  *Math.PI/180,          // branch initial angle when the branch is low
                b2InitialAngleRandom : 0       *Math.PI/180,          // branch initial angle randomize

                b2HeightBeforeBranchHappens : 0.25,
                b2HeightBeforeBranchHappensRandom : 0.1,

                b2LowBranchForceUpDown : 0,                       // 0.. normal    if >0, some lower part b2 branch goes up  (if =1, every branch goes up),  if <0, some lower part b2 branch goes down

                branchGeneration : 3,
                
                leavesStartsGeneration : 2,
                
                leavesVolume : 50,
                leavesRandom : 20,
                leavesRate : 0,               // how much leaves
                
                leavesPattern : 7,
                
                leavesSize : 0.5,                 // leaves size 
                
                leavesGroupShading : true,                  //  in a small group, it shades accordingly
                leavesHighlight : 0.1,              // if this is >0, some leaves would be brighter than the original color
                leavesShade : 3.75,                // leaves brightness (brighter -> less contrast)  default: 3.75

                leavesDirectionRandomness : 180,     // leaves direction   if 0, leaves don't rotate.  if 360, totally random

                leavesR : 50/256,
                leavesG : 96/256,
                leavesB : 25/256,
                
                makeBranchLeaves : false,            // branches become leaves color
                
                stemTexShading : true,
                stemTexRandomness : 0.5,
                stemTexStripeRatio : 0.25,               // texture stripe ratio  0.. non,   1.. max    the stripes would be covered by the other texture
                stemTexRatio : 0.5,                         // rough texture on the stem
                stemTexDapples : false,                   // dapples on the texture
                stemTexRoughness : 0.002,            // texture roughness    default should be 0.002
                stemTexPatternLength : 3,               // vertical texture length

                stemHTexRate : 0,
                stemHTexRoughness : 0.001,           // holizontal texture roughness    default should be 0.001
                stemHTexOpacity : 0,                   // holizontal texture opacity
                stemHTexSize : 15,                          // 3 - 15

                stemR : 158/256,                        // stem color
                stemG : 149/256,
                stemB : 121/256,   
                
                baseArrangement : 1,
                
            };
            break;

        case 20:                 //Shrub
            tree = {
                
                name : "shrub",
                stemIntensity : 2,
                leavesIntensity : 2.4,
                
                viewAngle : 5    *Math.PI/180,    //   0... from side      90... from top

                s : 1,    // scale      it effects everything
                d : 2,    // detail     how much detail

                dimensionRatio : 9.5/10,       //   y/x

                lightDirection : (180 + 180)      *Math.PI/180,      // light direction

                sLength : 1000,                 // if s = 1, this is pixel
                sLengthStop : 0.4,              // If this is less than 1, it stops growing.  , then 1st branch continues.
                sLengthStopDiverse : 0.5,    // if this is not zero, 3 branches happen.  and it splits based on this number.
                sInitialWidth : 4,                 // stump initial width

                stumpHeight : 50,       // stump part
                stumpShape : 2,          //   stump shape  1-3
                stumpShapeRatio : 4,    //  how much it is thicker on the bottom

                stemSegmentNumber : 400,    // how many segments on the tree?
                stemAngleSegmentRatio : 0.5,  // stem angle segment : stem straight segment

                stemAngleSegmentRandom : 0.5,      // how much change on stemAngleSegment (longer/shorter)
                stemStraightSegmentRandom : 0.5,   // how much change on stemStraightSegment (longer/shorter)

                stemInitialAngleRandom : 0         *Math.PI/180,            //

                stemRandomAngle : 15     *Math.PI/180,
                stemMaxAngle : 5           *Math.PI/180,                     // if stem angle becomes bigger than this, next angle will change to go up
                stemAngleChangeRate : 0.5,                                           // how often the angle changes?   0... no change   1.. always change

                stemHeightBeforeAngleChange : 50,                               // Stem goes straight before this height
                stemHeightBeforeAngleChangeRandom : 100,                     // plus minus this value
                
                stemSurfaceNoiseLevel : 1,                                               // less or more bumps

                stemHeightBeforeBranch : 10,
                stemHeightbeforePalmBump : 100000,
                
                stemHeightOfShadow : 0.25,                                          // leaves cast shadow on the stem higher than this height

                
                b1LengthRate : 0.4,                                                          // If this is 0.5, length of the first branch is 50% of sLength
                b1LengthStop : 0.8,                                                     // If this is less than 1, it stops growing. , then 2nd branch continues.
                b1LengthStopDiverse : 0.5,                                          // if this is not zero, 3 second branches happen.  and it splits based on this number.
                b1ReduceRatioType : 0,                                                      // How the first branches change their length based on the stem height 0 - 6
                b1InitialWidth : 1,                                                    //  based on the stem width
                
                b1Rate : 1,                                                              // 
                b1AngleSegmentRate : 0.2,
                b1StraightSegmentRate : 0.2,
                b1AngleSegmentRandom : 0.5,
                b1StraightSegmentRandom : 0.5,

                b1ShapeType : 0,                                                        // branch shape type 0-6
                b1ShapeMagnitude : 0.75,                                                // how strongly the branch changes its shape angle
                b1ShapeMagnitudeRandom : 0.0,
                b1ShapeBeginningPart : 0.13,
                b1ShapeBeginningMagnitude : 28,

                b1RandomAngle : 10    *Math.PI/180,
                b1AngleChangeRate : 0.5,                                              // 1 everytime, 0 non

                b1DirectionRandom : 0.02 ,                                            // direction randomness from golden angle

                b1InitialAngle : 50                  *Math.PI/180,          // branch initial angle when the branch is low
                b1InitialAngleChangeRate : 0,                                    // if this is >0, branch angle will change based on its height
                b1InitialAngleRandom : 15       *Math.PI/180,          // branch initial angle randomize
                
                b1HeightBeforeBranchHappens : 0,
                b1HeightBeforeBranchHappensRandom : 0,

                b1AngleMax : 5 *Math.PI/180,                            // 0 is holizontal angle.  if b1 branch is corrected if it becomes more than this number.  the number of willow trees should be 90

                b1CenterForce : 0,                                       // when it needs more leaves to cover the stem

                b2LengthRate : 0.5,
                b2ReduceRatioType : 6,                                                      // How the first branches change their length based on the stem height 0 - 6    
                b2InitialWidth : 0.5,   
                
                b2Rate : 0.225,
                b2AngleSegmentRate : 0.1,
                b2StraightSegmentRate : 0.1,
                b2AngleSegmentRandom : 0,
                b2StraightSegmentRandom : 0,

                b2ShapeType : 4,                                                        // branch shape type 0-6
                b2ShapeMagnitude : 1,                                                // how strongly the branch changes its shape angle
                b2ShapeMagnitudeRandom : 0,
                b2ShapeBeginningPart : 0.01,
                b2ShapeBeginningMagnitude : 28,

                b2RandomAngle : 10    *Math.PI/180,
                b2AngleChangeRate : 0.6,                                              // 1 everytime, 0 non

                b2InitialAngle : 25                  *Math.PI/180,          // branch initial angle when the branch is low
                b2InitialAngleRandom : 25       *Math.PI/180,          // branch initial angle randomize

                b2HeightBeforeBranchHappens : 0,
                b2HeightBeforeBranchHappensRandom : 0,

                b2LowBranchForceUpDown : 0.4,                       // 0.. normal    if >0, some lower part b2 branch goes up  (if =1, every branch goes up),  if <0, some lower part b2 branch goes down

                branchGeneration : 3,
                
                leavesStartsGeneration : 2,
                
                leavesVolume : 70,
                leavesRandom : 5,
                leavesRate : 1,               // how much leaves
                
                leavesPattern : 6,
                
                leavesSize : 0.4,                 // leaves size
                
                leavesGroupShading : true,                  //  in a small group, it shades accordingly
                leavesHighlight : 0.09,              // if this is >0, some leaves would be brighter than the original color
                leavesShade : 3.75,                // leaves brightness (brighter -> less contrast)  default: 3.75

                leavesDirectionRandomness : 360,     // leaves direction   if 0, leaves don't rotate.  if 360, totally random

                leavesR : 137/256,
                leavesG : 156/256,
                leavesB : 95/256,
                
                makeBranchLeaves : false,            // branches become leaves color

                stemTexShading : true,
                stemTexRandomness : 0.5,
                stemTexStripeRatio : 0.5,               // texture stripe ratio  0.. non,   1.. max    the stripes would be covered by the other texture
                stemTexRatio : 0.25,                         // rough texture on the stem
                stemTexDapples : true,                   // dapples on the texture
                stemTexRoughness : 0.002,            // texture roughness    default should be 0.002
                stemTexPatternLength : 3,               // vertical texture length

                stemHTexRate : 0.4,
                stemHTexRoughness : 0.001,           // holizontal texture roughness    default should be 0.001
                stemHTexOpacity : 0,                   // holizontal texture opacity
                stemHTexSize : 15,                          // 3 - 15
          
                stemR : 137/256,                        // stem color
                stemG : 130/256,
                stemB : 101/256,
                
                baseArrangement : 0,
                
            };
            break;

        case 21:                                // palm tree
            tree = {
                
                name : "palm",
                stemIntensity : 1,
                leavesIntensity : 1.7,
                
                viewAngle : 5    *Math.PI/180,    //   0... from side      90... from top

                s : 1.3,    // scale      it effects everything
                d : 6,    // detail     how much detail

                dimensionRatio : 11.5/5.5,       //   y/x

                lightDirection : (  180 + 180)      *Math.PI/180,      // light direction

                sLength : 1000,       // if s = 1, this is pixel
                sLengthStop : 0.5,        // If this is less than 1, it stops growing.  (not very spercific) , then 1st branch continues on the end.
                sLengthStopDiverse : -1,    // if this is bigger than zero, 3 branches with same with happen.  and it splits based on this number.
                                                         // if this is smaller than zero, 3 branches with b1InitialWidth happen.  and it splits based on this number.
                sInitialWidth : 8,     // stump initial width

                stumpHeight : 120,       // stump part
                stumpShape : 1,          //   stump shape  1-3
                stumpShapeRatio : 2,    //  how much it is thicker on the bottom

                stemSegmentNumber : 800,    // how many segments on the tree?
                stemAngleSegmentRatio : 0.5,  // stem angle segment : stem straight segment

                stemAngleSegmentRandom : 0.5,      // how much change on stemAngleSegment (longer/shorter)
                stemStraightSegmentRandom : 0.5,   // how much change on stemStraightSegment (longer/shorter)

                stemInitialAngleRandom : 0         *Math.PI/180,            //

                stemRandomAngle : 1     *Math.PI/180,
                stemMaxAngle : 1           *Math.PI/180,                     // if stem angle becomes bigger than this, next angle will change to go up
                stemAngleChangeRate : 0.5,                                           // how often the angle changes?   0... no change   1.. always change

                stemHeightBeforeAngleChange : 50,                               // Stem goes straight before this height
                stemHeightBeforeAngleChangeRandom : 100,                     // plus minus this value
                
                stemSurfaceNoiseLevel : 0.5,                                               // less or more bumps

                stemHeightBeforeBranch : 480,
                stemHeightbeforePalmBump : 460,
                
                stemHeightOfShadow : 0.95,                                          // leaves cast shadow on the stem higher than this height

                
                b1LengthRate : 0.21,                                                          // If this is 0.5, length of the first branch is 50% of sLength
                b1LengthStop : 1,
                b1LengthStopDiverse : 0.5,                                          // if this is not zero, 3 second branches happen.  and it splits based on this number.
                b1ReduceRatioType : 0,                                                      // How the first branches change their length based on the stem height 0 - 6
                b1InitialWidth : 0.4,                                                    //  based on the stem width
                
                b1Rate : 7,                                                              // 
                b1AngleSegmentRate : 0.2,
                b1StraightSegmentRate : 0.2,
                b1AngleSegmentRandom : 2,
                b1StraightSegmentRandom : 2,

                b1ShapeType : 3,                                                        // branch shape type 0-6
                b1ShapeMagnitude : -4,                                                // how strongly the branch changes its shape angle
                b1ShapeMagnitudeRandom : 2,
                b1ShapeBeginningPart : 0.1,
                b1ShapeBeginningMagnitude : 28,

                b1RandomAngle : 1    *Math.PI/180,
                b1AngleChangeRate : 0.1,                                              // 1 everytime, 0 non

                b1DirectionRandom : 2 ,                                            // direction randomness from golden angle

                b1InitialAngle : 420                  *Math.PI/180,          // branch initial angle when the branch is low
                b1InitialAngleChangeRate : 90,                                    // if this is >0, branch angle will change based on its height
                b1InitialAngleRandom : 10       *Math.PI/180,          // branch initial angle randomize
                
                b1HeightBeforeBranchHappens : 0.2,
                b1HeightBeforeBranchHappensRandom : 0,

                b1AngleMax : 5 *Math.PI/180,                            // 0 is holizontal angle.  if b1 branch is corrected if it becomes more than this number.  the number of willow trees should be 90

                b1CenterForce : 0,                                       // when it needs more leaves to cover the stem
                
                b2LengthRate : 0.45,
                b2ReduceRatioType : 0,                                                      // How the first branches change their length based on the stem height 0 - 6    
                b2InitialWidth : 1.5,   
                
                b2Rate : 1,
                b2AngleSegmentRate : 0.2,
                b2StraightSegmentRate : 0.2,
                b2AngleSegmentRandom : 0,
                b2StraightSegmentRandom : 0,

                b2ShapeType : 3,                                                        // branch shape type 0-6
                b2ShapeMagnitude : -2,                                                // how strongly the branch changes its shape angle
                b2ShapeMagnitudeRandom : 0,
                b2ShapeBeginningPart : 0.01,
                b2ShapeBeginningMagnitude : 28,

                b2RandomAngle : 0    *Math.PI/180,
                b2AngleChangeRate : 1,                                              // 1 everytime, 0 non

                b2InitialAngle : 35                  *Math.PI/180,          // branch initial angle when the branch is low
                b2InitialAngleRandom : 0       *Math.PI/180,          // branch initial angle randomize

                b2HeightBeforeBranchHappens : 0.3,
                b2HeightBeforeBranchHappensRandom : 0,

                b2LowBranchForceUpDown : -0.485,                       // 0.. normal    if >0, some lower part b2 branch goes up  (if =1, every branch goes up),  if <0, some lower part b2 branch goes down

                branchGeneration : 1,
                
                leavesStartsGeneration : 5,
                
                leavesVolume : 50,
                leavesRandom : 20,
                leavesRate : 1,               // how much leaves
                
                leavesPattern : 4,
                
                leavesSize : 2.5,                 // leaves size
                
                leavesGroupShading : true,                  // in a small group, it shades accordingly
                leavesHighlight : 0.2,              // if this is >0, some leaves would be brighter than the original color
                leavesShade : 5,                // leaves brightness (brighter -> less contrast)  default: 3.75

                leavesDirectionRandomness : 360,     // leaves direction   if 0, leaves don't rotate.  if 360, totally random

                leavesR : 143/256,
                leavesG : 183/256,
                leavesB : 85/256,

                makeBranchLeaves : true,            // branches become leaves color

                stemTexShading : true,
                stemTexRandomness : 0.5,
                stemTexStripeRatio : 0.25,               // texture stripe ratio  0.. non,   1.. max    the stripes would be covered by the other texture
                stemTexRatio : 0.5,                         // rough texture on the stem
                stemTexDapples : false,                   // dapples on the texture
                stemTexRoughness : 0.004,            // texture roughness    default should be 0.002
                stemTexPatternLength : 2,               // vertical texture length
                
                stemHTexRate : 1,
                stemHTexRoughness : 0.002,           // holizontal texture roughness    default should be 0.001
                stemHTexOpacity : 0.5,                   // holizontal texture opacity
                stemHTexSize : 15,                          // 3 - 15

                stemR : 203/256,                        // stem color
                stemG : 197/256,
                stemB : 160/256,
                
                baseArrangement : 5,
                
            };
            break;
 
        default :                 //Oak Tree
            tree = {
                
                name : "oak",
                stemIntensity : 2,
                leavesIntensity : 2,

                viewAngle : 5    *Math.PI/180,    //   0... from side      90... from top

                s : 1,    // scale      it effects everything
                d : 2,    // detail     how much detail
                
                lightDirection : (180 + 180)      *Math.PI/180,      // light direction

                sLength : 1000,                 // if s = 1, this is pixel
                sLengthStop : 0.5,              // If this is less than 1, it stops growing.  , then 1st branch continues.
                sLengthStopDiverse : 0.5,    // if this is not zero, 3 branches happen.  and it splits based on this number.
                sInitialWidth : 22,                 // stump initial width

                stumpHeight : 50,       // stump part
                stumpShape : 2,          //   stump shape  1-3
                stumpShapeRatio : 4,    //  how much it is thicker on the bottom

                stemSegmentNumber : 400,    // how many segments on the tree?
                stemAngleSegmentRatio : 0.5,  // stem angle segment : stem straight segment

                stemAngleSegmentRandom : 0.5,      // how much change on stemAngleSegment (longer/shorter)
                stemStraightSegmentRandom : 0.5,   // how much change on stemStraightSegment (longer/shorter)

                stemInitialAngleRandom : 0         *Math.PI/180,            //

                stemRandomAngle : 10     *Math.PI/180,
                stemMaxAngle : 5           *Math.PI/180,                     // if stem angle becomes bigger than this, next angle will change to go up
                stemAngleChangeRate : 0.5,                                           // how often the angle changes?   0... no change   1.. always change

                stemHeightBeforeAngleChange : 50,                               // Stem goes straight before this height
                stemHeightBeforeAngleChangeRandom : 100,                     // plus minus this value
                
                stemSurfaceNoiseLevel : 1,                                               // less or more bumps

                stemHeightBeforeBranch : 120,
                stemHeightbeforePalmBump : 100000,
                
                stemHeightOfShadow : 0.25,                                          // leaves cast shadow on the stem higher than this height
                
                
                b1LengthRate : 0.4,                                                          // If this is 0.5, length of the first branch is 50% of sLength
                b1LengthStop : 0.8,                                                     // If this is less than 1, it stops growing. , then 2nd branch continues.
                b1LengthStopDiverse : 0.5,                                          // if this is not zero, 3 second branches happen.  and it splits based on this number.
                b1ReduceRatioType : 0,                                                      // How the first branches change their length based on the stem height 0 - 6
                b1InitialWidth : 0.5,                                                    //  based on the stem width
                
                b1Rate : 1,                                                              // 
                b1AngleSegmentRate : 0.2,
                b1StraightSegmentRate : 0.2,
                b1AngleSegmentRandom : 0.5,
                b1StraightSegmentRandom : 0.5,

                b1ShapeType : 4,                                                        // branch shape type 0-6
                b1ShapeMagnitude : 0.75,                                                // how strongly the branch changes its shape angle
                b1ShapeMagnitudeRandom : 0.0,
                b1ShapeBeginningPart : 0.13,
                b1ShapeBeginningMagnitude : 28,

                b1RandomAngle : 15    *Math.PI/180,
                b1AngleChangeRate : 0.5,                                              // 1 everytime, 0 non

                b1DirectionRandom : 0.02 ,                                            // direction randomness from golden angle

                b1InitialAngle : 3                  *Math.PI/180,          // branch initial angle when the branch is low
                b1InitialAngleChangeRate : 0,                                    // if this is >0, branch angle will change based on its height
                b1InitialAngleRandom : 0       *Math.PI/180,          // branch initial angle randomize
                
                b1HeightBeforeBranchHappens : 0,
                b1HeightBeforeBranchHappensRandom : 0,

                b1AngleMax : 5 *Math.PI/180,                            // 0 is holizontal angle.  if b1 branch is corrected if it becomes more than this number.  the number of willow trees should be 90

                b1CenterForce : 0,                                       // when it needs more leaves to cover the stem

                b2LengthRate : 0.475,
                b2ReduceRatioType : 2,                                                      // How the first branches change their length based on the stem height 0 - 6    
                b2InitialWidth : 0.5,   
                
                b2Rate : 0.20,
                b2AngleSegmentRate : 0.1,
                b2StraightSegmentRate : 0.1,
                b2AngleSegmentRandom : 0,
                b2StraightSegmentRandom : 0,

                b2ShapeType : 4,                                                        // branch shape type 0-6
                b2ShapeMagnitude : 1,                                                // how strongly the branch changes its shape angle
                b2ShapeMagnitudeRandom : 0,
                b2ShapeBeginningPart : 0.01,
                b2ShapeBeginningMagnitude : 28,

                b2RandomAngle : 30    *Math.PI/180,
                b2AngleChangeRate : 0.6,                                              // 1 everytime, 0 non

                b2InitialAngle : 25                  *Math.PI/180,          // branch initial angle when the branch is low
                b2InitialAngleRandom : 0       *Math.PI/180,          // branch initial angle randomize

                b2HeightBeforeBranchHappens : 0,
                b2HeightBeforeBranchHappensRandom : 0,

                b2LowBranchForceUpDown : 0.4,                       // 0.. normal    if >0, some lower part b2 branch goes up  (if =1, every branch goes up),  if <0, some lower part b2 branch goes down

                branchGeneration : 3,
                
                leavesStartsGeneration : 2,
                
                leavesVolume : 75,
                leavesRandom : 5,
                leavesRate : 1,               // how much leaves
                
                leavesPattern : 7,
                
                leavesSize : 0.4,                 // leaves size
                
                leavesGroupShading : true,                  //  in a small group, it shades accordingly
                leavesHighlight : 0.09,              // if this is >0, some leaves would be brighter than the original color
                leavesShade : 3.75,                // leaves brightness (brighter -> less contrast)  default: 3.75

                leavesDirectionRandomness : 360,     // leaves direction   if 0, leaves don't rotate.  if 360, totally random

                leavesR : 108/256,
                leavesG : 131/256,
                leavesB : 59/256,
                
                makeBranchLeaves : false,            // branches become leaves color

                stemTexShading : true,
                stemTexRandomness : 0.5,
                stemTexStripeRatio : 0.5,               // texture stripe ratio  0.. non,   1.. max    the stripes would be covered by the other texture
                stemTexRatio : 0.25,                         // rough texture on the stem
                stemTexDapples : true,                   // dapples on the texture
                stemTexRoughness : 0.002,            // texture roughness    default should be 0.002
                stemTexPatternLength : 3,               // vertical texture length

                stemHTexRate : 0.4,
                stemHTexRoughness : 0.001,           // holizontal texture roughness    default should be 0.001
                stemHTexOpacity : 0,                   // holizontal texture opacity
                stemHTexSize : 15,                          // 3 - 15
          
                stemR : 137/256,                        // stem color
                stemG : 130/256,
                stemB : 101/256,
                
                baseArrangement : 0,
                
            };
            break;


    }


    if(parameters.randShape)
        Engine.rand(Math.random()*100);    
    else
        Engine.rand((parameters.arrange-1)*10+1 + tree.baseArrangement);

    tree.viewAngle = parameters.tilt*Math.PI/180;
    tree.lightDirection = ( (180-parameters.lightDirection) + 180)*Math.PI/180;

    tree.leavesRate = parameters.leavesAmount/100;
    if (tree.makeBranchLeaves)                                  // palm tree leaves amount
        tree.b1Rate *= tree.leavesRate;
 
    if(parameters.leavesColor){
        tree.leavesR = parameters.leafColor[0];
        tree.leavesG = parameters.leafColor[1];
        tree.leavesB = parameters.leafColor[2];
    }
   tree.stemsColor = parameters.stemsColor;
    if(parameters.stemsColor){        
        tree.stemR = parameters.stemColor[0];
        tree.stemG = parameters.stemColor[1];
        tree.stemB = parameters.stemColor[2];
    }

    tree.leavesGroupShading = !parameters.flatLeaves;
    tree.stemTexShading = !parameters.flatStems;

    tree.noiseLeaves = parameters.noiseLeaves;

    if (parameters == previewParameters)
    {
        var fileIndex = Math.floor((parameters.leavesAmount / 100) * 10 + 0.5) * 2
                
        if (fileIndex != lastFileIndex 
        || previousMTreeTypepreviousMTreeType != parameters.mTreeType
        || previousLightDirection != parameters.lightDirection
        || previousTilt != parameters.tilt
        || previousLeavesColor != parameters.leavesColor
        || previousStemsColor != parameters.stemsColor
        || previousStemColorR != parameters.stemColor[0]
        || previousStemColorG != parameters.stemColor[1]      
        || previousStemColorB != parameters.stemColor[2]
        || previousleavesColorR != parameters.leafColor[0]
        || previousleavesColorG != parameters.leafColor[1]
        || previousleavesColorB != parameters.leafColor[2]
        )
        {
            lastFileIndex = fileIndex;

            image1 = new Image();
            image2 = new Image();

            var treeName = scriptPath + "/Tree/" + tree.name;
            
            image1.load (treeName +"_preview_0.png", treeName + "_alpha_0.png")  // the second image specifies alpha channel for teh first image (we save disk space this way)
             
            var file = treeName +"_preview_" + fileIndex + ".png";
            var file2 = treeName +"_alpha_" + fileIndex + ".png";


            if (fileIndex > 0)
                image2.load (file, file2);   //Engine.getParameter(kScriptalBase) + "/Tree/oak_preview_10.png")  // change file based on leaf density
        
            //alert("rendering image");
            api.pushMatrix();
            api.scale(previewSize, previewSize);
            //api.translate(0,previewSize);
        

            api.Color(kFillColor,  tree.stemR * tree.stemIntensity, tree.stemG * tree.stemIntensity, tree.stemB * tree.stemIntensity);
            image1.render(api);
        
            if (fileIndex > 0)
            {
                api.Color(kFillColor, tree.leavesR * tree.leavesIntensity,  tree.leavesG * tree.leavesIntensity, tree.leavesB * tree.leavesIntensity);
                image2.render(api);
            }
            api.popMatrix();
            
            
            
            // sub preview - icon
            
            var previewMargine = 20;
            
            api.pushMatrix();
            api.scale(1.35,1.35)
            api.translate (previewSize-79-previewMargine, previewSize-79-previewMargine);
            api.Color (kStrokeColor, 0.6,0.6,0.6)
            api.lineWidth (0.01)
            api.Color(kFillColor, 0.6,0.6,0.6);
            api.Polygon(iconFrame);    
            api.popMatrix();

            api.pushMatrix();
            api.scale(1.22,1.22)
            api.translate (previewSize-75-previewMargine, previewSize-75-previewMargine);
            api.Color (kStrokeColor, 0.65,0.65,0.65)
            api.lineWidth (0.01)
            api.Color(kFillColor, 0.65,0.65,0.65);
            api.Polygon(iconFrame);    
            api.popMatrix();

            // sun
            api.pushMatrix();
            api.translate (previewSize-64-previewMargine + parameters.lightDirection*50/180, previewSize-64-previewMargine);
            api.Color (kStrokeColor, 1,1,1)
            api.lineWidth (0.01)
            api.Color(kFillColor, 1,1,1);
            for (var j=0; j<=8; j++)
                api.Polygon(sunIcon[j]);    
            api.popMatrix();

            // stump
            var treeIconStump = new Array();
                treeIconStump.push(new Vector3(2.5,-20,0));
                treeIconStump.push(new Vector3(-2.5,-20,0));
            for(var j=-2.5; j<=2.5; j+= 1)
            {
                    treeIconStump.push(new Vector3(j,Math.sqrt(6.25-j*j) * Math.sin(parameters.tilt*2*3.14/180),0));
            }
            api.pushMatrix();    
            api.translate (previewSize-39-previewMargine, previewSize-6-previewMargine,0);
            api.Color (kStrokeColor,  0.9,0.9,0.9)
            api.lineWidth (2)
            api.Color(kFillColor, 0.7,0.7,0.7);
            api.Polygon(treeIconStump);
            api.popMatrix();
            
            // leaves
            var treeIconLeaves = new Array();
                treeIconLeaves.push(new Vector3(0,-40 *  Math.cos(parameters.tilt*2*3.14/180)*0.8 + Math.sin(parameters.tilt*2*3.14/180)*7,0));
            for(var j=-15; j<=15; j+= 2)
            {
                    treeIconLeaves.push(new Vector3(j,Math.sqrt(225-j*j) * Math.sin(parameters.tilt*2*3.14/180) + Math.sin(parameters.tilt*2*3.14/180)*10,0));
            }
            api.pushMatrix();    
            api.translate (previewSize-39-previewMargine, previewSize-21-previewMargine,0);
            api.Color (kStrokeColor,  0.9,0.9,0.9)
            api.lineWidth (2)
            api.Color(kFillColor, 0.7,0.7,0.7);
            api.Polygon(treeIconLeaves);
            api.popMatrix(); 
            

            if(inputPaths.length > 0){
                // path
                var treeIconStump = new Array();
                    treeIconStump.push(new Vector3(1,-23,0));
                    treeIconStump.push(new Vector3(2,-23,0));
                    treeIconStump.push(new Vector3(2,-25,0));
                    treeIconStump.push(new Vector3(-2,-25,0));
                    treeIconStump.push(new Vector3(-2,-23,0));
                    treeIconStump.push(new Vector3(-1,-23,0));

                    treeIconStump.push(new Vector3(1,-2,0));
                    treeIconStump.push(new Vector3(2,-2,0));
                    treeIconStump.push(new Vector3(2,0,0));
                    treeIconStump.push(new Vector3(-2,0,0));
                    treeIconStump.push(new Vector3(-2,-2,0));
                    treeIconStump.push(new Vector3(-1,-2,0));

                api.pushMatrix();    
                api.translate (previewSize-39-previewMargine, previewSize-6-previewMargine,0);
                api.Color (kStrokeColor,  0.5,0.5,0.5)
                api.lineWidth (2)
                api.Color(kFillColor, 0.9,0.9,0.9);
                api.Polygon(treeIconStump);
                api.popMatrix();
            }



            
            updatePreview = true
            
            previousMTreeTypepreviousMTreeType = parameters.mTreeType;
            previousLightDirection = parameters.lightDirection;
            previousTilt = parameters.tilt;
            previousLeavesColor = parameters.leavesColor;
            previousStemsColor = parameters.stemsColor;
            previousStemColorR = parameters.stemColor[0];
            previousStemColorG = parameters.stemColor[1];   
            previousStemColorB = parameters.stemColor[2];
            previousleavesColorR = parameters.leafColor[0];
            previousleavesColorG = parameters.leafColor[1];
            previousleavesColorB = parameters.leafColor[2];

        } 
        else
            updatePreview = false;
            
    }

    var renderedTree = false;

    if (parameters != previewParameters){

        var paths = inputPaths;
   

        if (paths.length > 0 && paths[0].getValue(kGetGeometryLength) != 0)
        {
                tree.usePath = true;
                
                for (var i=0; i < paths.length; i++)
                {
                    
                    var path = paths[i];
                    var pathlength = path.getValue(kGetGeometryLength);
                    
                    var x1 = path.getValue(kGetPointAlongGeometry,0).x;
                    var y1 = path.getValue(kGetPointAlongGeometry,0).y;      
                    
                    var g1 = 0;
                    for ( var j = 1; j < pathlength-1; j++){
                        var x = path.getValue(kGetPointAlongGeometry,j).x;
                        var y = path.getValue(kGetPointAlongGeometry,j).y;

                        if ((x1-x)*(x1-x)+(y1-y)*(y1-y) >= 7){  // gap on the path

                            var x2 = path.getValue(kGetPointAlongGeometry,j+1).x;
                            var y2 = path.getValue(kGetPointAlongGeometry,j+1).y;    

                            if ((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2) < 7){                                        // sometimes, a point of path (that was made from a selection) value jumps to a previous location.  If only one point jumps, it ignores it.
                                j++;
                                x = x2;
                                y = y2;
                            }
                        }

                        if ((x1-x)*(x1-x)+(y1-y)*(y1-y) >= 7){  // gap on the path

                            var pt0 = path.getValue(kGetPointAlongGeometry, g1);
                            var pt1 = path.getValue(kGetPointAlongGeometry, j-1);
                            var length = Math.sqrt((pt1.x-pt0.x)*(pt1.x-pt0.x)+(pt1.y-pt0.y)*(pt1.y-pt0.y));
                            if (j > length*5)
                                length = j/5;
                            treeSize = (length*1.5)/1000;

                            stumpHeight = tree.stumpHeight*treeSize;
                            if(!renderedTree){                                                                                          // only the first tree will be rendered
                                Engine.addModule(new ModuleStem(api,tree, 0, 0, treeSize, 1, path, g1, j-1-g1, stumpHeight,true, 1,-outputOrigin.x, -outputOrigin.y));
                                renderedTree = true;
                            }
                            g1 = j;

                        }
                        x1 = x;
                        y1 = y;
                    }
                    var pt0 = path.getValue(kGetPointAlongGeometry, g1);
                    var pt1 = path.getValue(kGetPointAlongGeometry, j-1);
                    var length = Math.sqrt((pt1.x-pt0.x)*(pt1.x-pt0.x)+(pt1.y-pt0.y)*(pt1.y-pt0.y));

                    if (pathlength > length*5)
                        length = pathlength/5;

                    treeSize = (length*1.5)/1000;
                    stumpHeight = tree.stumpHeight*treeSize;

                    if(!renderedTree){                                                                                              // only the first tree will be rendered
                        Engine.addModule(new ModuleStem(api,tree, 0, 0, treeSize, 1, path, g1, j-1-g1, stumpHeight,true, 1,-outputOrigin.x, -outputOrigin.y));
                        renderedTree = true;
                    }
                }

        }else{
            tree.usePath = false;
            
            treeSize = (10*tree.s* 1500/1000)/10;


            
            if (treeSize < 0.01)
                treeSize = 0.01;
            
            stumpHeight = tree.stumpHeight * treeSize;
            
            var pp = 1;
            if(visibleSize.x < outputSize.x || visibleSize.y < outputSize.y){
                if (tree.dimensionRatio < (visibleSize.y/visibleSize.x))
                    pp = tree.dimensionRatio/(visibleSize.y/visibleSize.x);
                power = (visibleSize.y/1500) * pp;
                Engine.addModule(new ModuleStem(api,tree, visibleSize.x/(2*power) , 1500/pp -60, treeSize, 1, 0, 0, 0,stumpHeight,false, power, -outputOrigin.x, -outputOrigin.y));
            }else{
                if (tree.dimensionRatio < (outputSize.y/outputSize.x))
                    pp = tree.dimensionRatio/(outputSize.y/outputSize.x);
                power = (outputSize.y/1500) * pp;
                Engine.addModule(new ModuleStem(api,tree, outputSize.x/(2*power) , 1500/pp -60, treeSize, 1, 0, 0, 0,stumpHeight,false, power, -visibleOrigin.x, -visibleOrigin.y));
            }
        }
    }
}


var lastTree = -1
var lastFileIndex = -1
var updatePreview = true




// Setting the bounding box like this will insure that neighboring fills are aligned
Engine.setSceneBBox (0,  outputSize.x,  0,  outputSize.y)

Engine.setParameter (kRunSimulation, 1)
Engine.setParameter (kNumSimulationSteps, 1000)

// Menu start
// If you want to create a menu in which you change some of the script parameters, include the following code:
// For shipped scripts we can include localized strings, prefixed with $$$/

var decoMenu = {    //  an object that defines the menu
   menuTitle : localize("$$$/DecoScripts/Tree/Tree=Tree"),
   menuBackground : [0.99, 0.99, 0.99, 1],
    panels : [
    { panelName : "",
       panelWidth : 500,
       leftColumnWidth : 200,
       panelMenu : [
         { itemName : localize("$$$/DecoScripts/Tree/TreeType=Tree Type:"),  
            itemUnit : '', 
            itemType : 'dropdownlist', itemList :
            [
            { item: localize("$$$/DecoScripts/Tree/Tree1= 1: Oak Tree"),  image : "Tree/t1.png" },
            { item: localize("$$$/DecoScripts/Tree/Tree2= 2: Redwood"),  image : "Tree/t2.png" },
            { item: localize("$$$/DecoScripts/Tree/Tree3= 3: Ginko Tree"),  image : "Tree/t3.png" },
            { item: localize("$$$/DecoScripts/Tree/Tree4= 4: Maple Tree"),  image : "Tree/t4.png" },
            { item: localize("$$$/DecoScripts/Tree/Tree5= 5: Young Maple Tree"),  image : "Tree/t5.png" },
            { item: localize("$$$/DecoScripts/Tree/Tree6= 6: Spruce Tree"),  image : "Tree/t6.png" },
            { item: localize("$$$/DecoScripts/Tree/Tree7= 7: Aspen Tree"),  image : "Tree/t7.png" },
            { item: localize("$$$/DecoScripts/Tree/Tree8= 8: Pine Tree"),  image : "Tree/t8.png" },
            { item: localize("$$$/DecoScripts/Tree/Tree9= 9: Elm Tree"),  image : "Tree/t9.png" },
            { item: localize("$$$/DecoScripts/Tree/Tree10=10: Populus Nigra"),  image : "Tree/t10.png" },
            { item: localize("$$$/DecoScripts/Tree/Tree11=11: Pepper Tree"),  image : "Tree/t11.png" },
            { item: localize("$$$/DecoScripts/Tree/Tree12=12: Ash Tree"),  image : "Tree/t12.png" },
            { item: localize("$$$/DecoScripts/Tree/Tree13=13: Young Ash Tree"),  image : "Tree/t13.png" },
            { item: localize("$$$/DecoScripts/Tree/Tree14=14: Willow Tree"),  image : "Tree/t14.png" },
            { item: localize("$$$/DecoScripts/Tree/Tree15=15: Robinia"),  image : "Tree/t15.png" },
            { item: localize("$$$/DecoScripts/Tree/Tree16=16: Young Robinia"),  image : "Tree/t16.png" },
            { item: localize("$$$/DecoScripts/Tree/Tree17=17: Sakura Cherry blossom"),  image : "Tree/t17.png" },
            { item: localize("$$$/DecoScripts/Tree/Tree18=18: Ficus Microcarpa"),  image : "Tree/t18.png" },
            { item: localize("$$$/DecoScripts/Tree/Tree19=19: Fraxinus Griffithii"),  image : "Tree/t19.png" },
            { item: localize("$$$/DecoScripts/Tree/Tree20=20: Acer maximowiczianum"),  image : "Tree/t20.png" },
            { item: localize("$$$/DecoScripts/Tree/Tree21=21: Shrub"),  image : "Tree/t21.png" },
            { item: localize("$$$/DecoScripts/Tree/Tree22=22: Palm Tree"),  image : "Tree/t22.png" },
            ] , 
            itemValue : modelParameters.treeType, itemMin : 0, itemMax : 20, varName : "mTreeType" },

         { itemName : localize("$$$/DecoScripts/Tree/Light=Light Direction:"),  
             itemUnit : "",  itemType : 'slider', itemValue : modelParameters.lightDirection, itemMin : 0, itemMax : 180, itemStep : 1, varName : 'lightDirection'  }, 

         { itemName : localize("$$$/DecoScripts/Tree/Tilt=Camera Tilt:"),  
             itemUnit : "",  itemType : 'slider', itemValue : modelParameters.tilt, itemMin : 0, itemMax : 12, itemStep : 1, varName : 'tilt'  }, 

         { itemName : localize("$$$/DecoScripts/Tree/LeavesAmount=Leaves Amount:"),  
             itemUnit : "",  itemType : 'slider', itemValue : modelParameters.leavesAmount, itemMin : 0, itemMax : 100, itemStep : 1, varName : 'leavesAmount'  }, 

         { itemName : localize("$$$/DecoScripts/Tree/LeavesColor=Use Custom Color for Leaves:"),
             itemUnit : "",  itemType : 'checkbox', itemValue : modelParameters.leavesColor, itemMin : 0, itemMax : 0, varName : 'leavesColor' ,
             disableItems : [ [false, [5]] ]  }, 

         { itemName : localize("$$$/DecoScripts/Tree/LeafColor=Custom Color for Leaves:"),
             itemUnit : "",  itemType : 'colorpicker', itemValue : modelParameters.leafColor, varName : 'leafColor'  },


         { itemName : localize("$$$/DecoScripts/Tree/StemsColor=Use Custom Color for Branches:"),
             itemUnit : "",  itemType : 'checkbox', itemValue : modelParameters.stemsColor, itemMin : 0, itemMax : 0, varName : 'stemsColor' ,
             disableItems : [ [false, [7]] ]  }, 

         { itemName : localize("$$$/DecoScripts/Tree/StemColor=Custom Color for Branches:"),
             itemUnit : "",  itemType : 'colorpicker', itemValue : modelParameters.stemColor, varName : 'stemColor'  },


         { itemName : localize("$$$/DecoScripts/Tree/FlatLeaves=Flat Shading - Leaves:"),
             itemUnit : "",  itemType : 'checkbox', itemValue : modelParameters.flatLeaves, itemMin : 0, itemMax : 0, varName : 'flatLeaves'  },

         { itemName : localize("$$$/DecoScripts/Tree/NoiseLeaves=Add Noise - Leaves:"),
             itemUnit : "",  itemType : 'checkbox', itemValue : modelParameters.noiseLeaves, itemMin : 0, itemMax : 0, varName : 'noiseLeaves'  },

         { itemName : localize("$$$/DecoScripts/Tree/FlatStems=Flat Shading - Branches:"),
             itemUnit : "",  itemType : 'checkbox', itemValue : modelParameters.flatStems, itemMin : 0, itemMax : 0, varName : 'flatStems'  },

         { itemName : localize("$$$/DecoScripts/Tree/RandShape=Randomize Shapes:"),
             itemUnit : "",  itemType : 'checkbox', itemValue : modelParameters.randShape, itemMin : 0, itemMax : 0, varName : 'randShape',
             disableItems : [ [true, [12]] ] },
         
          { itemName : localize("$$$/DecoScripts/Frame/Arrange=Arrangement:"),  
             itemUnit : "",  itemType : 'slider', itemValue : modelParameters.arrange, itemMin : 1, itemMax : 100, itemStep : 0.1, varName : 'arrange'  }, 



       ] }
   ]  // end of panels
 } // end of menu

// Set  the menu definition into an environment variable 

livePreview = 0 // recommended value is 0
//localizedMenu = RenderAPI.command (kpsLocalizeText, menuScriptText) 

////Engine.message ("script: ", localizedMenu)

//$.setenv("DECO_script_text_from_deco",  localizedMenu);

// Call Photoshop Script that creates the menu
Engine.evalFile ("_Deco Menu.jsx")  //XXX send menuScriptText as a parameter and search for zstrings

// Evaluate potential parameters coming back from the script
//newParams = $.getenv("DECO_script_text_to_deco")
//if (newParams)
//{
//    //Engine.message ("new parameters: ", newParams)
//    eval(newParams) // set the new values
//}


if (typeof skipRun == 'undefined' || !skipRun)  // run unles we exited the preview window without pressing a button
    run(RenderAPI, modelParameters, 1)

// Cleanup
//$.setenv("DECO_script_text_from_deco",  '');
//$.setenv("DECO_script_text_to_deco",  '');

// Menu finished